I try to implement Paypal subscription service according to: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create
This is my first try.
In sandbox business account I have created two test subscriptions: monthly and yearly and configured application with their id’s.
This is the method:
public function createSubscription($planSlug, $name, $email) {
return $this->makeRequest(
'POST',
'v1/billing/subscriptions',
[],
[
'plan_id' => $this->plans[$planSlug],
'subscriber' => [
'name' => [
'given_name' => $name,
],
'email_address' => $email
],
'application_context'=> [
'brand_name' => config('app.name'),
'shipping_preference' => 'NO_SHIPPING',
'user_action' => 'SUBSCRIBE_NOW',
'return_url' => route('subscribe.approval', ['plan' => $planSlug]),
'cancel_url'=> route('subscribe.cancelled')
],
],
[],
$isJsonRequest = true
);
}
However, when I make a call to API, to create a test subscription, I get weird response that ‘name’ parameter is formed incorrectly:
php artisan tinker
>>> $paypal = resolve(AppServicesPaypalService::class);
=> AppServicesPaypalService {#3413}
>>> $paypal->createSubscription('monthly', 'Test', '[email protected]');
GuzzleHttpExceptionClientException with message 'Client error: `POST https://api-
m.sandbox.paypal.com/v1/billing/subscriptions` resulted in a `400 Bad Request` response:
{"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or
violates schema.","debug_id (truncated...)
This is strange, because in Paypal API doc (see above), the ‘name’ param is described exactly like that!
Do I miss something or it is Paypal API is acting funky?
2
Answers
try this :
I’ve faced this too before and it was not easy for me to figure out how to show an explicit error message.
‘return_url’ => route(‘subscribe.approval’, [‘plan’ => $planSlug]),
‘cancel_url’=> route(‘subscribe.cancelled’)
the problem is in this two url, may be you have changed the APP_URL in the .env
APP_URL=http://127.0.0.1:8000
put app url that and try