can i use stripe checkout with destination charges? - laravel

I'm wondering if there is any way to charge some kind of fee on transactions using the new Stripe Checkout system. In this particular instance I am using Laravel 8 with Livewire. In my component I have a function tied to a button called setStripeSession.
\Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => 'Custom Media',
],
'unit_amount' => $this->toPennies($price),
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => 'http://e68ec3e6b8b2.ngrok.io/',
'cancel_url' => 'http://e68ec3e6b8b2.ngrok.io/',
],['stripe_account_id'=>$this->stripe_account_id]);
$this->stripe_session_id = $session->id;
Message::where('id',$message_id)->update(['stripe_session_id'=>$this->stripe_session_id ]);
$this->emit('PayonStripe',['ssid'=>$session->id]);
}
I imagined adding something like the below code but the variables are not recognized by the stripe API during the rquest.
'transfer_data' => [
'destination' => '{{CONNECTED_STRIPE_ACCOUNT_ID}}',
],

Yes! You can set these within the payment_intent_data parameter:
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [...],
'mode' => 'payment',
'success_url' => 'http://example.com/success',
'cancel_url' => 'http://example.com/cancel',
'payment_intent_data' => [
'application_fee_amount' => 100, // optional
'transfer_data' => [
'destination' => 'acct_123',
],
],
]);
A number of parameters are exposed this way in Checkout, such as metadata for the payment intent and similar data for subscription mode under subscription_data.

Related

Laravel Phpunit testing a request that take give output based on the request

I'm still new to laravel and I have a simple app and aSo I have a route that will store data based on the request in my controller.
public funtion store(Request $request, $id){
if ($request->has('work_experiences')) {
WorkExperience::create([
'user_id' => $user->id,
'position' => $request->work_experiences['position'],
'company' => $request->work_experiences['company'],
'start_date' => $request->work_experiences['start_date'],
'end_date' => $request->work_experiences['end_date'],
]);
}
if ($request->has('education')) {
Education::create([
'user_id' => $user->id,
'degree' => $request->education['degree'],
'university' => $request->education['university'],
'start_date' => $request->education['start_date'],
'end_date' => $request->education['end_date'],
]);
}
if ($request->has('job_interests')) {
JobInterest::create([
'user_id' => $user->id,
'job_position' => $request->job_interests['position'],
]);
}}
}
and in my test
public function test_authenticated_user_can_edit_education_profile()
{
$this->withoutExceptionHandling();
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post('/candidate' . '/' . $user->id, [
'user_id' => $user->id,
'position' => 'position',
'company' => 'company',
'start_date' => Carbon::now(),
'end_date' => Carbon::now(),
]);
$this->assertCount(1, WorkExperience::all());
}
when I run the test, the assertCount seems to fail because the response didn't work/insert the data to DB. where do I do wrong?
Well, the test is right.
It should fail because there is no work_experiences key in your request data.
The test request should look like:
$response = $this->post('/candidate' . '/' . $user->id, [
'work_experiences' => [
'user_id' => $user->id,
'position' => 'position',
'company' => 'company',
'start_date' => Carbon::now(),
'end_date' => Carbon::now(),
]
]);
So your data should go under a work_experiences key such that $request->has('work_experiences') returns true and executes the WorkExperience::create() statement.
Currently your endpoint only allows for a single "work experience" to be created. Seeing that you've named it work_experiences I assume you'd want to pass in an array/collection of "work experiences" - but that won't work with the current implementation; you'll have to loop over them instead - something like this:
if ($request->has('work_experiences')) {
foreach ($request->input('work_experiences') as $experience) {
WorkExperience::create([
'user_id' => $request->user()->id,
'position' => $experience['position'],
'company' => $experience['company'],
'start_date' => $experience['start_date'],
'end_date' => $experience['end_date'],
]);
}
}
And then your test should look something like this:
$response = $this->post('/candidate' . '/' . $user->id, [
'work_experiences' => [
[
'user_id' => $user->id,
'position' => 'position',
'company' => 'company',
'start_date' => Carbon::now(),
'end_date' => Carbon::now(),
],
// more "work experiences"
]
]);

How can I create a Laravel Resource relationship for a belongsTo?

I have created a UserResource that is successfully returning all of my user attributes, including the organization it belongs to. It looks something like this:
Resources/User.php
return [
'type' => 'users',
'id' => (string)$this->id,
'attributes' => [
'name' => $this->name,
'email' => $this->email,
...
'relationships' => [
'organization' => $this->organization,
],
];
In my User model, there is a belongsTo relationship for User->Organization.
Instead of returning the actual organization model, I'd like to return the organization resource.
For example, an organization hasMany locations:
Resources/Organization.php
return [
'type' => 'organizations',
'id' => (string)$this->id,
'attributes' => [
'name' => $this->name,
...
'relationships' => [
'locations' => Location::collection($this->locations),
],
];
I can successfully return the collection of locations that belong to the organization. I have not been able to return a belongsTo relationship.
I've tried:
Resources/User.php
'relationships' => [
'organization' => Organization::collection($this->organization),
],
// or this
'relationships' => [
'organization' => Organization::class($this->organization),
],
// or this
use App\Http\Resources\Organization as OrganizationResource;
...
'relationships' => [
'organization' => OrganizationResource($this->organization),
],
How can I return a single model as a related resource? Thank you for any suggestions!
Have you tried it with the new keyword?
'relationships' => [
'organization' => new OrganizationResource($this->organization),
],

How can I redirect to paypal in Laravel?

I have the following in my view:
Pay with PayPal
Defined the route:
Route::get('pay-with-paypal', 'CheckoutController#payWithPaypal')->name('payment.paypal');
My CheckoutController looks like the one below:
use Srmklive\PayPal\Services\ExpressCheckout;
class CheckoutController extends Controller
{
public function payWithPaypal(){
$provider = new ExpressCheckout;
$data = [];
$data['items'] = [
[
'name' => 'Product 1',
'price' => 9.99,
'qty' => 1
],
[
'name' => 'Product 2',
'price' => 4.99,
'qty' => 2
]
];
$data['invoice_id'] = 1;
$data['invoice_description'] = "Order #{$data['invoice_id']} Invoice";
$data['return_url'] = url('/');
$data['cancel_url'] = url('/');
$total = 0;
foreach($data['items'] as $item) {
$total += $item['price']*$item['qty'];
}
$data['total'] = $total;
$response = $provider->setExpressCheckout($data);
// This will redirect user to PayPal
return redirect($response['paypal_link']);
}
}
My config looks like this:
return [
'mode' => 'sandbox', // Can only be 'sandbox' Or 'live'. If empty or invalid, 'live' will be used.
'sandbox' => [
'username' => env('PAYPAL_SANDBOX_API_USERNAME', ''),
'password' => env('PAYPAL_SANDBOX_API_PASSWORD', ''),
'secret' => env('PAYPAL_SANDBOX_API_SECRET', ''),
'certificate' => env('PAYPAL_SANDBOX_API_CERTIFICATE', ''),
'app_id' => 'APP-80W284485P519543T', // Used for testing Adaptive Payments API in sandbox mode
],
'live' => [
'username' => env('PAYPAL_LIVE_API_USERNAME', ''),
'password' => env('PAYPAL_LIVE_API_PASSWORD', ''),
'secret' => env('PAYPAL_LIVE_API_SECRET', ''),
'certificate' => env('PAYPAL_LIVE_API_CERTIFICATE', ''),
'app_id' => '', // Used for Adaptive Payments API
],
'payment_action' => 'Sale', // Can only be 'Sale', 'Authorization' or 'Order'
'currency' => 'USD',
'billing_type' => 'MerchantInitiatedBilling',
'notify_url' => '', // Change this accordingly for your application.
'locale' => '', // force gateway language i.e. it_IT, es_ES, en_US ... (for express checkout only)
'validate_ssl' => true, // Validate SSL when creating api client.
];
My .env is also configured:
PAYPAL_SANDBOX_API_USERNAME=example-facilitator_api1.hotmail.com
PAYPAL_SANDBOX_API_PASSWORD=SLU2YLP4B
PAYPAL_SANDBOX_API_SECRET=TF3K8CtWTEmAcRu40.XWAihLRko
PAYPAL_SANDBOX_API_CERTIFICATE=
When I click on the link "Pay with PayPal", nothing happens. Anything I could have possibly done wrong?
Reference: https://github.com/srmklive/laravel-paypal

Post Multipart and Json together with Guzzle in Laravel

I'm trying to POST multipart and json data with Guzzle to build my apps with Phonegap Build API. I've tried many adjustment but still got error results. Here's the latest function I'm using:
public function testBuild(Request $request)
{
$zip_path = storage_path('zip/testing.zip');
$upload = $this->client->request('POST', 'apps',
['json' =>
['data' => array(
'title' => $request->title,
'create_method' => 'file',
'share' => 'true',
'private' => 'false',
)],
'multipart' =>
['name' => 'file',
'contents' => fopen($zip_path, 'r')
]
]);
$result = $upload->getBody();
return $result;
}
This is my the correct curl format that has success result from the API, but with file I have in my desktop:
curl -F file=#/Users/dedenbangkit/Desktop/testing.zip
-u email#email.com
-F 'data={"title":"API V1 App","version":"0.1.0","create_method":"file"}'
https://build.phonegap.com/api/v1/apps
As mentioned before, you cannot use multipart and json together.
In your curl example it's just a multipart form, so use the same in Guzzle:
$this->client->request('POST', 'apps', [
'multipart' => [
[
'name' => 'file',
'contents' => fopen($zip_path, 'r'),
],
[
'name' => 'data',
'contents' => json_encode(
[
'title' => $request->title,
'create_method' => 'file',
'share' => 'true',
'private' => 'false',
]
),
]
]
]);

laravel wont set header during gopay payment integration

I have a problem with payment integration to my laravel project. It is a GOPAY REST API.
It should by default set request headers with Accept, Content-type and Authorization where the token is stored. Problem is that it doesnt set my request headers. I used the same thing in a normal script which included the SDK and it worked correctly. However in my laravel project it just doesnt work. The SDK uses Curl to set headers and i think there is somewhere the problem.
I also didnt find any similar problem, and i definitely didnt google anyone who integrated GoPay into Laravel.
Pay method in my controller
//minimal configuration
$gopay = GoPay\payments([
'goid' => '8583340073',
'clientId' => '1162442589',
'clientSecret' => 'eDxNQ3ru',
'isProductionMode' => false,
'scope' => GoPay\Definition\TokenScope::ALL,
'language' => GoPay\Definition\Language::CZECH],
['logger' => new GoPay\Http\Log\PrintHttpRequest()]);
$response = $gopay->createPayment([
'payer' => [
'default_payment_instrument' => PaymentInstrument::BANK_ACCOUNT,
'allowed_payment_instruments' => [PaymentInstrument::BANK_ACCOUNT],
'default_swift' => BankSwiftCode::FIO_BANKA,
'allowed_swifts' => [BankSwiftCode::FIO_BANKA, BankSwiftCode::MBANK],
'contact' => [
'first_name' => 'Zbynek',
'last_name' => 'Zak',
'email' => 'test#test.cz',
'phone_number' => '+420777456123',
'city' => 'C.Budejovice',
'street' => 'Plana 67',
'postal_code' => '373 01',
'country_code' => 'CZE',
],
],
'target' => ['type' => 'ACCOUNT', 'goid' => '8583340073'],
'currency' => Currency::CZECH_CROWNS,
'order_number' => '001',
'order_description' => 'pojisteni01',
'items' => [
['name' => 'item01', 'amount' => 50],
['name' => 'item02', 'amount' => 100],
],
'recurrence' => [
'recurrence_cycle' => Recurrence::DAILY,
'recurrence_period' => "7",
'recurrence_date_to' => '2016-12-31'
],
'additional_params' => [
array('name' => 'invoicenumber', 'value' => '2015001003')
],
'callback' => [
'return_url' => 'http://www.hume.cz/public',
'notification_url' => 'http://www.hume.cz/public'
]
]);
I think that somehow laravel changes the headers and doesnt allow the SDK to do it. If you know anything please help me. If you need any extra information, please just ask.
Thank you very much!!
There is a package for handling GoPay payments with Laravel. Install, update config with your credentials and start using GoPay facade to createPayment or another function from official SDK.
I have eshop in production with this my own package and everything works fine.
https://github.com/hazestudio/laravel-gopay-sdk

Resources