0

Is it possible with the Smart Payment Button for Paypal recurring payments to pass additional parameters like an invoice id for example.

paypal.Buttons({

  createSubscription: function(data, actions) {

    return actions.subscription.create({

      'plan_id': 'P-2UF78835G6983425GLSM44AM'

    });

  },


  onApprove: function(data, actions) {

    alert('You have successfully created subscription ' + data.subscriptionID);

  }


}).render('#paypal-button-container');

This is the example code from https://developer.paypal.com/docs/subscriptions/integrate/#4-create-a-subscription and it says:

  • Calls PayPal using actions.subscription.create() to create a subscription for your plan and includes the plan ID, subscriber details, shipping, and other details.

But I can't pass anything besides the plan_id.

Preston PHX
  • 27,642
  • 4
  • 24
  • 44
user2133694
  • 23
  • 1
  • 8

2 Answers2

2

You can add the custom_id field as explained on this page

In that case, your code would look like

return actions.subscription.create({
   'custom_id': 'my-custom-code-for-integration',
   'plan_id': 'P-2UF78835G6983425GLSM44AM'
 });
Fausto R.
  • 1,314
  • 3
  • 16
  • 29
1

The underlying API is /v1/billing/subscriptions

Here is the reference: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create

It does not appear to have a field for an invoice_id -- so, the answer to your question is No, it is not possible.


You will get a unique PayPal subscription profile ID in the resulting response, e.g. "id": "I-BW452GLLEP1G" -- So, what you can do is store that profile ID in your own invoice record.


Example of a createSubscription function that calls a server that can check for pre-existing active duplicates before creating the subscription (or return an error otherwise)

      createSubscription: function(data, actions) {
          return fetch('/path/on/your/server/paypal/subscription/create/', {
              method: 'post'
          }).then(function(res) {
              return res.json();
          }).then(function(serverData) {
              console.log(serverData);
              return serverData.id;
          });
      },
Preston PHX
  • 27,642
  • 4
  • 24
  • 44
  • I am troubleshooting an issue of duplicate payments in Paypal. The solution for the Smart Payment Buttons is to use invoice_id because that prohibits duplicates. Unfortunately custom_id doesn't seem to be unique, and that's all that the Subscription API supports – Dagmar Feb 10 '22 at 15:11
  • That's correct. What you can do is implement a webhook for PAYMENT.SALE.COMPLETED so you are notified every time a payment is made on a subscription, and also BILLING.SUBSCRIPTION.ACTIVATED if desired: https://developer.paypal.com/api/rest/webhooks/event-names/ – Preston PHX Feb 10 '22 at 15:18
  • Then, in `createSubscription`, instead of `return actions.subscription.create({` , do a `return fetch` that gets a subscription created via the REST API on your server. Your server can ensure it's not creating one for an "invoice_id" / user that has already paid. – Preston PHX Feb 10 '22 at 15:19
  • This issue can only be fixed on Paypal's side with a unique identifier that I send to them. I do set the subscription ID as soon as I receive it, but the user is somehow sending duplicate payments a couple seconds apart. This is a very infrequent issue, but it is annoying – Dagmar Feb 10 '22 at 15:27
  • Hm, in that case implementing an auto cancel / refund might work well enough – Preston PHX Feb 10 '22 at 15:49
  • Thanks. I'm going to start with an alert email so at least I find out about it before the customer and then look to automate it if there is no other way. I added a question here: https://stackoverflow.com/questions/71072100/how-to-avoid-duplicate-payments-subscriptions-using-paypal-smart-buttons – Dagmar Feb 10 '22 at 20:52
  • ", in createSubscription, instead of return actions.subscription.create({ , do a return fetch" .. seems this wasn't clear enough, but I explained it again in that other question – Preston PHX Feb 10 '22 at 21:10