I am trying to build a #PayPal checkout button in the #backend. The sample code they give uses express.js #NPM. Wix does not support it as they say that #httpfunctions does all that is needed from express.js
However, I have no idea how to convert the code to #httpfunctions. Here is my current code:
export function paypalRedirect() { var express = require('express'); var request = require('request'); // Add your credentials: // Add your client ID and secret var CLIENT = 'myclietID'; var SECRET = 'mySecret'; var PAYPAL_API = 'https://api.sandbox.paypal.com'; express() // Set up the payment: // 1. Set up a URL to handle requests from the PayPal button .post('/my-api/create-payment/', function (req, res) { // 2. Call /v1/payments/payment to set up the payment request.post(PAYPAL_API + '/v1/payments/payment', { auth: { user: CLIENT, pass: SECRET }, body: { intent: 'sale', payer: { payment_method: 'paypal' }, transactions: [{ amount: { total: '5.99', currency: 'USD' } }], redirect_urls: { return_url: 'https://example.com', cancel_url: 'https://example.com' } }, json: true }, function (err, response) { if (err) { console.error(err); return res.sendStatus(500); } // 3. Return the payment ID to the client res.json({ id: response.body.id }); }); }) // Execute the payment: // 1. Set up a URL to handle requests from the PayPal button. .post('/my-api/execute-payment/', function (req, res) { // 2. Get the payment ID and the payer ID from the request body. var paymentID = req.body.paymentID; var payerID = req.body.payerID; // 3. Call /v1/payments/payment/PAY-XXX/execute to finalize the payment. request.post(PAYPAL_API + '/v1/payments/payment/' + paymentID + '/execute', { auth: { user: CLIENT, pass: SECRET }, body: { payer_id: payerID, transactions: [{ amount: { total: '10.99', currency: 'USD' } }] }, json: true }, function (err, response) { if (err) { console.error(err); return res.sendStatus(500); } // 4. Return a success response to the client res.json({ status: 'success' }); }); }).listen(3000, function () { console.log('Server listening at http://localhost:3000/'); }); }
You might want to consider using one of the Paypal NPM libraries available via the Package Manager:
If you really want to "roll your own", then refer to the following documentation:
Corvid: Exposing a Site API with HTTP Functions
wix-http-functions API
Also, check out the MyApi and MyApiClient example to see how to Expose and Access Site APIs.