1// 1. Set up your server to make calls to PayPal
2
3// 1a. Import the SDK package
4const paypal = require('checkoutNodeJssdk');
5
6// 1b. Add your client ID and secret
7const PAYPAL_CLIENT = 'PAYPAL_SANDBOX_CLIENT';
8const PAYPAL_SECRET = 'PAYPAL_SANDBOX_SECRET';
9
10// 1c. Set up the SDK client
11const env = new paypal.core.SandboxEnvironment(PAYPAL_CLIENT, PAYPAL_SECRET);
12const client = new paypal.core.PayPalHttpClient(env);
13
14// 2. Set up your server to receive a call from the client
15module.exports = async function handleRequest(req, res) {
16
17 // 3. Call PayPal to set up a transaction with payee
18 const request = new sdk.orders.OrdersCreateRequest();
19 request.prefer("return=representation");
20 request.requestBody({
21 intent: 'CAPTURE',
22 purchase_units: [{
23 amount: {
24 currency_code: 'USD',
25 value: '220.00'
26 },
27 payee: {
28 email_address: 'payee@email.com'
29 }
30 }]
31 });
32
33 let order;
34 try {
35 order = await payPalClient.client().execute(request);
36 } catch (err) {
37
38 // 4. Handle any errors from the call
39 console.error(err);
40 return res.send(500);
41 }
42
43 // 5. Return a successful response to the client with the order ID
44 res.status(200).json{
45 orderID: order.result.id
46 });
47}
48