Skip to content

Instantly share code, notes, and snippets.

@MacoChave
Created January 6, 2022 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MacoChave/3e551c7aaa3e61db0bc54e494025e434 to your computer and use it in GitHub Desktop.
Save MacoChave/3e551c7aaa3e61db0bc54e494025e434 to your computer and use it in GitHub Desktop.
const stripe = require('stripe')('STRIPE_KEY');
exports.handler = async function(req,ctx,callback) {
const stripeToken = req.stripeToken
const cantidad = req.cantidad
const descripcion = req.descripcion
const cantidadInDollar = Math.round(cantidad)
const chargeObject = await stripe.charges.create({
amount: cantidadInDollar,
currency: 'usd',
source: stripeToken,
capture: false,
description: descripcion,
receipt_email: 'ENCARGADO@EMPRESA.COM'
})
// AGREGAR TRANSACCIÓN A BD
try {
await stripe.charges.capture(chargeObject.id)
callback({success: 'True', message: 'Pago realizado con éxito'})
} catch (error) {
await stripe.charges.refunds.create({ charge: chargeObject.id })
callback({success: 'False', message: 'Problemas con el pago, se le devolvió el monto'})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment