How do you check a PayPal subscription with the JavaScript SDK? - paypal

I was able to create a subscription button with the code below:
<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?client-id=client_id&vault=true" data-sdk-integration-source="button-factory"></script>
<script>
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'subscribe'
},
createSubscription: function(data, actions) {
return actions.subscription.create({
'plan_id': 'P-xxxx'
});
},
onApprove: function(data, actions) {
alert('You have successfully created subscription ' + data.subscriptionID);
console.log(data);
}
}).render('#paypal-button-container');
</script>
I was able to test it and it worked fine once I figured out that to test it you have to create a sandbox app at https://developer.paypal.com/developer/applications/ and use that for the client_id and then login with the sandbox facilitator account at https://www.sandbox.paypal.com and create a sandbox paypal subscription button by going to Pay & Get Paid > Subscriptions > Subscription Plans or https://www.paypal.com/billing/plans and "Create Plan" and use that for the plan_id.
After I tested the subscription button by purchasing it with the sandbox buyer account I was then able to check the status of the subscription online at https://www.sandbox.paypal.com/billing/subscriptions using the sandbox facilitator account.
But I do not see a way to check the status of a subscription using the subscription ID and the JavaScript SDK. How do you do that?

The JavaScript SDK is for the web approval, not for things like checking the status.
For this you use the Subscriptions API. Here's the overview documentation: https://developer.paypal.com/docs/subscriptions/
In particular you want to get the details of a subscription: https://developer.paypal.com/docs/subscriptions/full-integration/subscription-management/#show-subscription-details
curl -v -X GET https://api.sandbox.paypal.com/v1/billing/subscriptions/I-######### \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <Access-Token>"
Since you'll be using server code for such a check anyway, you may be interested in a server implementation which is more robust, since you are immediately informed on the server side of when a subscription becomes active.

Related

PayPal subscription payment is not working with sandbox

I have used the following code for PayPal subscribe button, from the sandbox.paypal.com:-
<div id="paypal-button-container-P-86B16126YE351481NMH6WMRY"></div>
<script src="https://www.paypal.com/sdk/js?client-id=AT4fx8CxfLze4ZzMRrp-yUZBlKQvFt97cMDgzUwqXBpbvPSw7w0EmoLBk1zJw7PddEDRD_HJ05y4qjh5&vault=true&intent=subscription" data-sdk-integration-source="button-factory"></script>
<script>
paypal.Buttons({
style: {
shape: 'pill',
color: 'silver',
layout: 'horizontal',
label: 'subscribe'
},
createSubscription: function (data, actions) {
return actions.subscription.create({
/* Creates the subscription */
plan_id: 'P-86B16126YE351481NMH6WMRY'
});
},
onApprove: function (data, actions) {
alert(data.subscriptionID); // You can add optional success message for the subscriber here
}
}).render('#paypal-button-container-P-86B16126YE351481NMH6WMRY'); // Renders the PayPal button
</script>
But, when clicking this button from the website (where I put this button code) and logging into the PayPal sandbox for the payment, it shows the following error message:-
Sorry, we couldn't set up your subscription using the payment method you selected. Please try another payment method.
I have tried all the available payment methods for my sandbox account(where from I am trying to subscribe). But nothing worked. I have been trying it for the last couple of hours, but it is not working.
Review your list of sandbox accounts, log into this button's account via www.sandbox.paypal.com and go to its email settings. Resend a confirmation "email" if necessary--sandbox doesn't send real email messages.
Read the confirmation "email" in the "Notifications" side tab of the developer dashboard, and use the link there to confirm it.
Retry your subscription payment after having confirmed the receiving sandbox account's primary email.

Can't approve PayPal subscription using smart buttons or subscriptions REST API

I'm trying to integrate PayPal subscriptions using Smart Buttons in JavaScript with sandbox app & account.
paypal.Buttons(
{
style: {
layout: 'horizontal',
size: 'small',
color: 'gold',
label: 'paypal',
height: 35,
tagline: 'true'
},
createSubscription: function (data, actions) {
return actions.subscription.create({
'plan_id': 'P-xxxxxxxxxxxxxxxxxxxxxxxx'
});
},
onApprove: function (data, actions) {
alert('You have successfully created subscription ' + data.subscriptionID);
}
}
).render('#paypal-button-container');
After new window opens up I login with my sandbox bussiness account. The spinner appears and after a while I get a message "Something went wrong". I checked webhook and subscription is successfully created, but when PayPal navigates to checkout to approve it, the above happens.
I tried to use Subscriptions API and I made a call using Postman to create subscription. Then in response I recieved url to approve subscription. I opened it, logged in, this time I had to choose PayPal or Credit/Debit Card and again, spinner, some time passes and message appears saying "Something went wrong."
I checked wether encryption settings are disabled, cause I found it might cause issues, but it was disabled by deafult.
I also tried passing the whole subscription object from docs, not just "plan_id", but it changed nothing.
Don't have any other ideas what might went wrong. I hope someone stumbled upon this issue, found the solution and is willing to share it :D

Paypal Smart Payment Buttons and create subscriptions give me a RESOURCE_NOT_FOUND error

I'm using a Sandbox and I created a paypal plan with Paypal PHP SDK and activated it.
I'm following the integration guide to create a subscription to this plan (https://developer.paypal.com/docs/subscriptions/integrate/#4-create-a-subscription).
But when I clic on the yellow button I have a Javascript Error in the console :
Error: Create Subscription Api response error:
{
"name": "RESOURCE_NOT_FOUND",
"message": "The specified resource does not exist.",
"debug_id": "23912dc1195d",
"details": [
{
"issue": "INVALID_RESOURCE_ID",
"description": "Requested resource ID was not found."
}
],
"links": [
{
"href": "https://developer.paypal.com/webapps/developer/docs/api/#INVALID_RESOURCE_ID",
"rel": "information_link",
"method": "GET"
}
]
}
I checked if the plan exists and it's OK with the Paypal PHP SDK.
Here is the Javascript code used :
<script src="https://www.paypal.com/sdk/js?client-id=sandbox_paypal_client_id&vault=true"></script>
<div id="paypal-button-container"></div>
<script>
// paypal.Buttons().render('#paypal-button-container');
paypal.Buttons({
createSubscription: function(data, actions) {
return actions.subscription.create({
'plan_id': 'P-8N6112936N290211KKMN5MZI'
});
},
onApprove: function(data, actions) {
alert('You have successfully created subscription ' + data.subscriptionID);
}
}).render('#paypal-button-container');
</script>
In the paypal developer dashboard I see the API call for this action, but in orange...
Thanks in advance for your help.
I also ran into this problem. My confusion when following the Subscriptions Integration instructions was that I thought the SDK would work with Subscription Plans (API endpoint /billing/plans), but at the time of writing it doesn't (at least the PHP and .NET versions) - the SDK only works with the older Billing Plans (/payments/billing-plans) which are very similarly named (Plans for short).
As #giannisrig says, you have to call the Subscription Plans part of the API directly (/billing/plans). I hope they'll update the SDK soon - this seems like an important missing feature.
In addition to what Harvey Kadyanji said make sure that:
The Plan is in active state
That the plan was created through the Subscriptions API and Catalog Products API
If you are following the steps provided from paypal in this link:
https://developer.paypal.com/docs/subscriptions/integrate/ you will have to use the Subscriptions API to create the Plans and not the Billing Plans and Billing Agreements APIs.
Use the sandbox business account associated with the sandbox client ID you're using and make the paypal smart subscription or plan through there. The planId and clientId need to belong to the same sandbox account.
I had the same problem and the following solved my issues:
Make sure the sandbox_paypal_client_id provided is valid
Make sure the plan_id exists
Add the plan name in the request e.g.
{
'plan_id': 'P-8N6112936N290211KKMN5MZI',
'name': 'Monthly plan'
}
Probably you are loading the button script with "client-id=sb" or no client id specified. In order to work with plans you created via API you need to explicitly set "client-id" to the client-id of your REST SDK app when loading the script. Then the buttons will find your created plan.
I also answered to basically the same problem in the PayPal community forum

How to handle a PayPal Checkout payment when there is a confirmation API error on our server

We are integrating the PayPal client side Checkout Integration for taking payments on our website. This can be found here:
https://developer.paypal.com/docs/checkout/integrate/#6-verify-the-transaction
Once the payment has been made and approved by PayPal, we need to call our server to verify the transaction and store it within our database. This code can be found below, note the part "Call your server to save the transaction".
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '0.01'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name);
// Call your server to save the transaction
return fetch('/paypal-transaction-complete', {
method: 'post',
body: JSON.stringify({
orderID: data.orderID
})
});
});
}
}).render('#paypal-button-container');
</script>
Now, in the above instance, what happens if the call to "/paypal-transaction-complete" fails? session timeout or lost internet connection? For example, in the Stripe integration, the money is "approved" in on the client side and then only confirmed/charged in our API to "/stripe-transaction-complete". If there is an error, we don't actually charge the money.
Within PayPal, the money is charged before the API call, so the is the small possibility we charge the user but they don't receive the paid order in the database. How would we best handle this? one option would be to call the PayPal API and match all the orders with payments and then either auto-refund or auto-complete the order. But I'm not sure if this is recommended.
For both PayPal and similar issues with Stripe Checkout, this can be addressed using WebHooks.
https://developer.paypal.com/docs/integration/direct/webhooks/rest-webhooks/#

Is it possible to create recurring/subscription payment with Express Checkout js 4.0 + Rest API?

I'm currently investigating whether or not it would be possible to use the recent version of Paypal Express Checkout (checkout.js v4) to add it as payment option for a website that has recurring donations.
I'm aware of the old NVP/SOAP version, which is currently flagged as deprecated.
I've checked through the (numerous) documentation pages on the new checkout, starting here and so forth.
I'm also aware of the Billing Plans & Billing Agreement APIs.
I can find plenty of examples using the old NVP but not this combination.
Is it possible to do using these?
Are there any official(ish) examples of this?
You can still use the TOKEN from the NVP api with the javascript integration, by passing it back in the payment() call:
<script src="https://www.paypalobjects.com/api/checkout.js">
</script>
<script>
var CREATE_PAYMENT_URL = 'https://my-store.com/paypal/create-payment';
paypal.Button.render({
env: 'production', // Optional: specify 'sandbox' environment
payment: function() {
return paypal.request.post(CREATE_PAYMENT_URL).then(function(data) {
return data.token;
});
},
onAuthorize: function(data, actions) {
return actions.redirect();
},
onCancel: function(data, actions) {
return actions.redirect();
}
}, '#paymentMethods');
</script>