0
votes

I am using the PayPal javascript SDK to create a Paypal subscription button for my web page, the problem resides when testing the button using a sandbox account in which after performing all the payment for the subscription the following error appears:

Error: Use intent=capture to use client-side capture ...

Here's my HTML script used to show my pay PayPal button

 <script src="https://www.paypal.com/sdk/js?client-id=MY_CLIENT_ID&vault=true&intent=subscription&components=buttons"></script>
 <div id="paypal-button-container"></div>

Heres my js file which performs the respective handlers depending on the actions and status of the payment:

paypal.Buttons({
    style: {
        shape: 'rect',
        color: 'black',
        layout: 'vertical',
        label: 'subscribe'
    },
    createSubscription: function(data, actions) {
      if (discordUser.value === "") {
        alert("Introduce tu usuario de discord");
        return;
      }

      slotDecremented = true;
      alterSlots();
      
      return actions.subscription.create({
        'plan_id': 'P-ID' // here is my plan id
      });
    },
    onApprove: function(data, actions) { 
      return actions.order.capture().then(function(details) {
        sendEmailToken(details.payer.email_address, discordUser.value, data.subscriptionID);
      });
    },
    onCancel: function(data) {
      alterSlots(true);
      slotDecremented = false;
    },
    onError: function(data) {
      if (!slotDecremented) {
        return;
      }

      alterSlots(true);
      slotDecremented = false;
    }
}).render('#paypal-button-container');

the alterSlot() function makes an API call with AJAX to my specific endpoint, just extra information if neccesary

EDIT:

The sendEmailToken() function makes an AJAX request to an API hosted on my server, such that the server performs the correct action and being secure (just metadata for knowing what's going on in that part)

1

1 Answers

1
votes
    onApprove: function(data, actions) { 
      return actions.order.capture().then(function(details) {
        sendEmailToken(details.payer.email_address, discordUser.value, data.subscriptionID);
      });
    },

actions.order.capture() is for a client-side one-time payment button. It does not belong in a Subscribe button, as there is no order to capture.

See e.g. the PayPal Subscriptions documentation, which only has a basic success alert in its onApprove example: https://developer.paypal.com/docs/subscriptions/integrate/#create-the-subscription


Since it appears your intent is to perform a server-side operation such as sending an email after subscription approval, you should be activating said subscription from your server to ensure subscriptions don't become active without your server being correctly being notified of this fact. Here is some information: https://stackoverflow.com/a/65139331/2069605