Maria1995
Maria1995

Reputation: 501

Standard PayPal payment integration does not work

I am trying to implement on a website standard payment with PayPal and I have some issues when paying with the credit card. More exactly, after paying, my transaction does not reach the database and my payment does not complete.

My code is below:

onApprove: function(data, actions) {
            return actions.order.capture().then(function(details) {
                let url = window.location.origin + '/paypal/payment-success';

                let data = {
                    payment_id: details.id,
                    plan_name: details.purchase_units[0].items[0].name,
                    amount: details.purchase_units[0].amount.value,
                    currency_code: details.purchase_units[0].amount.currency_code,
                    status: details.status,
                    payer_email: details.payer.email_address,
                    payer_name: details.payer.name.given_name + ' ' + details.payer.name.surname
                };

                $.ajax({
                    method: "POST",
                    url: url,
                    data: data,
                    dataType: 'JSON',
                    success: function (response) {
                        if(response.url) {
                            window.location.replace(response.url);
                        }
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    }
                })
            });

The problem is that, when debugging, my code does not get to the capture() function. So, I manage to add the card details, but when I am paying, nothing happens. What could be wrong with my code? Can anyone help me with this?

Upvotes: 0

Views: 73

Answers (1)

Preston PHX
Preston PHX

Reputation: 30379

Do not capture on the client side and then send the result to a server. Switch to a proper server-side integration.

Create two routes, one for 'Create Order' and one for 'Capture Order', documented here. These routes should return/output only JSON data (no HTML or text).

Pair those two routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

Upvotes: 1

Related Questions