Skip to content

Instantly share code, notes, and snippets.

@Abdelkrim
Last active January 3, 2016 03:29
Show Gist options
  • Save Abdelkrim/8402093 to your computer and use it in GitHub Desktop.
Save Abdelkrim/8402093 to your computer and use it in GitHub Desktop.
0. create a customer 1. retrieve selected plan 2. charge a customer ISSUE: 1. Stripe charges twice the user with ±3 seconds delay between the charges 2. The second charge' token isn't not displayed on the web server logs
app.post('/stripeGotoStep02', function (req, res) {
var transaction = req.body;
var newCustomer = {
email: transaction.stripeCustomer.email,
plan: transaction.selectedStripePlan.id,
card: transaction.stripeToken,
description: transaction.stripeCustomer.email,
metadata: {
myTxUniqueID: 'to generate a transaction.myTxUniqueID ',
stripeShippingName: transaction.stripeShippingName,
stripeShippingAddressLine1: transaction.stripeShippingAddressLine1,
stripeShippingAddressZip: transaction.stripeShippingAddressZip,
stripeShippingAddressCity: transaction.stripeShippingAddressCity,
stripeShippingAddressState: transaction.stripeShippingAddressState,
stripeShippingAddressCountry: transaction.stripeShippingAddressCountry
}
};
stripe.customers.create(newCustomer, function (err, customer) {
if (err) {
res.render('stripe-error.html', {
error: 'stripe.customers.create: ' + err,
obj: newCustomer,
objName: 'newCustomer'
});
return;
}
else {
//retrieve the plan details
stripe.plans.retrieve(transaction.selectedStripePlan.id, function (err, plan) {
if (err) {
res.render('stripe-error.html', {
error: 'stripe.plans.retrieve: ' + err,
obj: transaction.selectedStripePlan,
objName: 'transaction.selectedStripePlan'
});
return;
}
else {
// set the charge
var newCharge =
{
amount: transaction.selectedStripePlan.amount * transaction.selectedStripePlanQuantity,
currency: transaction.selectedStripePlan.currency,
customer: customer.id
};
stripe.charges.create(newCharge,
function (err, charge) {
if (err) {
res.render('stripe-error.html', {
error: 'ERROR: stripe.charges.create: ' + err,
obj: newCharge,
objName: 'newCharge'
});
return;
}
else {
res.render('stripe-payment-step99-done.html', {
stripeToken: req.body.stripeToken,
stripeEmail: req.body.stripeEmail,
stripeTransaction: transaction,
stripeCustomer: customer,
selectedStripePlanDetails: plan,
charge: charge
}
);
return;
}
}
);
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment