Created
October 18, 2021 19:02
-
-
Save arvindrajnaidu/8c3f87d95b5a70b075bbf7ad2b86821f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const checkoutNodeJssdk = require('@paypal/checkout-server-sdk'); | |
| function paypalEnv() { | |
| let clientId = process.env.paypal_client_id | |
| let clientSecret = process.env.paypal_client_secret | |
| if (process.env.NODE_ENV === 'production') { | |
| return new checkoutNodeJssdk.core.LiveEnvironment( | |
| clientId, clientSecret | |
| ) | |
| } else { | |
| return new checkoutNodeJssdk.core.SandboxEnvironment( | |
| clientId, clientSecret | |
| ) | |
| } | |
| } | |
| const paypal = new checkoutNodeJssdk.core.PayPalHttpClient(paypalEnv()); | |
| function handler(req, res) { | |
| const orderId = req.body.paymentMethodId; | |
| const captureReq = new checkoutNodeJssdk.orders.OrdersCaptureRequest(orderId); | |
| captureReq.requestBody({}); | |
| try { | |
| const capture = await paypal.execute(captureReq); | |
| const captureId = capture.result.purchase_units[0] | |
| .payments.captures[0].id; | |
| // Save captureId to against order in DB | |
| res.json({message: 'Payment completed'}) | |
| } catch (err) { | |
| console.error(err) | |
| res.status(500).send() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment