0

I have a PayPal Smart Button on my Angular web page that looks like the following:

paypal.Buttons({
      createOrder: (data, actions) => {
        return actions.order.create({
          purchase_units: [
            {
              description: this.product.description,
              amount: {
                currency_code: 'USD',
                value: this.product.price,
              }
            },
          ],

          application_context: {
            shipping_preference: 'NO_SHIPPING'
          }
        });
      },

      onApprove: async (data, actions) => {
        this.dataService.reserveReservation();
        const order = actions.order.capture();
        alert('Transaction Completed!');
      }
    }).render(this.paypalElement.nativeElement);

95% of the time the purchase goes through, but occasionally dataService.reserveReservation gets called without the payment going through. Is there some time of error that actions.order.capture() returns if the payment fails, or why is the onApprove callback being called when the payment isn't going through and doesn't show up anywhere in my PayPal home activity tab? How can I make sure the payment is going through every time?

1 Answer 1

1

Captures can fail; examples of some failure scenarios are listed at https://developer.paypal.com/docs/checkout/integration-features/funding-failure/ -- hence, your code belongs after the capture's resolution.

Specifically, see the sample at https://developer.paypal.com/demo/checkout/#/pattern/client -- your code should be in the .then() promise chain, after actions.order.capture() successfully finalizes the payment.

And make sure you return the result correctly to onApprove's caller.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, that's a lifesaver. I do have one question, why do we return the result of actions.order.capture()? If so what would I really be returning if I use await/async instead of .then
I don't know of a correct implementation other than what's in the sample

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.