Recurring Payments Through Paypal Express Check REST API

My goal is to establish recurring subscriptions for 6 and 12 months using "paypal" as a payment method for our SaaS. I use the API for the rest, and one thing I can not find is a working implementation in order to somehow parse and remake my code into a recurring payment using the PayPal API (which I read now is possible). Here where we are:

I have a working advanced integration with the server for payments using the REST API Paypal Express Checkout API.

I created a one-time sale by following the code examples described above and also shown in this example from PayPal. I tried to turn off the two-step process, instead turning on the calls to create and execute a billing agreement, but the light window that opens for users to log in to PayPal is interrupted with the error message "Things don’t appear, work at the moment. Please try again later." Here is my javascript code that almost exactly matches the working example, but with different routes.

paypal.Button.render({ // Set your environment env: 'sandbox', // sandbox | production // Wait for the PayPal button to be clicked payment: function(resolve, reject) { // Make a call to the merchant server to set up the payment console.log("button clicked") return paypal.request.post('/payment/paypal/subscription', {amount: '0.02'}) .then(function(res) { resolve(res.payToken); #--WHERE I'M GOING WRONG }) .catch(function(err) { reject(err); }); }, // Wait for the payment to be authorized by the customer onAuthorize: function(data) { // Make a call to the merchant server to execute the payment return paypal.request.post('/payment/paypal/execute', { data: data, paymentID: data.paymentID, payerID: data.payerID }).then(function (res) { if (res.state == "active") { document.querySelector('#paypal-button-container-server').innerText = 'Payment Complete!'; } else { console.log(res); alert("Payment could not be approved, please try again.") } }); } }, '#paypal-button-container-server'); 

I can say that I am mistaken in the payment function, namely on the line with resolve(res.payToken) . I don’t know how much of the data from the answer v1 / payments / billing agreements I should go to this function, but I tried both profile_id and approval_url (the actual href is "href": " https: //www.sandbox.paypal. com / cgi-bin / webscr? cmd = _express-checkout & token = EC-XXXXXXXXXXXXX ").

Where am I going wrong? If possible, I feel like I am one working example from doing this work, but I would like to know if I am not capable of the way I do it (in this case, may this be required through Payflow?).

Note. I fully understand the billing agreements and billing profiles, and my problems are not related to the REST API. In addition to the current one-time sale, I can make all the necessary profiles / billing agreements (verified through Postman).

Below is the answer from calling the "sandbox" v1/payments/billing-agreements in case someone can specify the necessary part of the data inside it.

 { "name": "Magazine Subscription", "description": "Monthly subscription with a regular monthly payment definition and two-month trial payment definition.", "plan": { "id": "P-XXXXXXXXXXXXXXXXXXX", "state": "ACTIVE", "name": "1 Month Recurring", "description": "A recurring payment plan for customers who sign a 1-month contract", "type": "FIXED", "payment_definitions": [ { "id": "PD-924GIUJ3MWQ32E22348G0018", "name": "Regular Payment Definition", "type": "REGULAR", "frequency": "Month", "amount": { "currency": "USD", "value": "150" }, "cycles": "1", "charge_models": [ { "id": "CHM-940183BIUJ3MWQ5VK14226VH", "type": "TAX", "amount": { "currency": "USD", "value": "10" } } ], "frequency_interval": "1" }, { "id": "PD-5604RIUJ3MWQ4Y4221782C61", "name": "Trial Payment Definition", "type": "TRIAL", "frequency": "Month", "amount": { "currency": "USD", "value": "120" }, "cycles": "1", "charge_models": [ { "id": "CHM-640401IUJ3MWQ64W07759LB2", "type": "TAX", "amount": { "currency": "USD", "value": "10" } } ], "frequency_interval": "1" } ], "merchant_preferences": { "setup_fee": { "currency": "USD", "value": "0" }, "max_fail_attempts": "3", "return_url": "http://localhost:8000/payment/success", "cancel_url": "http://localhost:8000/payment/new", "auto_bill_amount": "NO", "initial_fail_amount_action": "CONTINUE" } }, "links": [ { "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXXX", "rel": "approval_url", "method": "REDIRECT" }, { "href": "https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-XXXXXXXXXXXXX/agreement-execute", "rel": "execute", "method": "POST" } ], "start_date": "2017-12-22T09:13:49Z" } 
+5
source share
1 answer

The REST API still returns the express check address, but to use it with integration before purchasing checkout.js, you need to pass the token found in the approval URL. You can parse this URL and get the token section on the server and return it back or pass it before calling the permission method.

i.e.

 approval_url = { "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXX" } var token = "EC-XXXXXXXXXXXXX"; resolve(token); 
0
source

Source: https://habr.com/ru/post/1266447/


All Articles