Skip to content

Instantly share code, notes, and snippets.

@arvindrajnaidu
Created October 18, 2021 19:02
Show Gist options
  • Select an option

  • Save arvindrajnaidu/8c3f87d95b5a70b075bbf7ad2b86821f to your computer and use it in GitHub Desktop.

Select an option

Save arvindrajnaidu/8c3f87d95b5a70b075bbf7ad2b86821f to your computer and use it in GitHub Desktop.
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