diff --git a/controllers/api.js b/controllers/api.js index a92d655a96..3e63e00e17 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -391,149 +391,37 @@ exports.getSteam = function(req, res, next) { * Stripe API example. */ -exports.getStripe = function(req, res, next) { - //Create a token for the CC - res.render('api/stripe/index', { - title: 'Stripe API' - }); +exports.getStripe = function(req, res) { + res.render('api/stripe', { + title: 'Stripe API' + }); }; /** - * GET /api/onetime - * Stripe One Time Charge API example. + * POST /api/stripe + * @param stipeToken + * @param stripeEmail */ -exports.getStripeOnetime = function(req, res, next) { - //Create a token for the CC - res.render('api/stripe/onetime', { - title: 'Stripe API' - }); -}; -/** - * POST /api/stripe/onetime - * @param ccNumber - * @param expMonth - * @param expYear - * @param ccNumber - * @param expMonth - * @param expYear - * @param customerName - * @param email - * @param chargeAmount - */ -exports.postStripeOnetime = function(req, res, next) { - stripe.tokens.create({ - card: { - "number": req.body.ccNumber, - "exp_month": req.body.expMonth, - "exp_year": req.body.expYear, - "cvc": req.body.cvc - } - }, function(err, token) { - if (err) { - req.flash('errors', { msg: err.message }); - return res.redirect('/api/stripe/onetime'); - } - //Create a new customer - stripe.customers.create({ - card: token.id, - description: req.body.customerName, - email: req.body.email - }).then(function(customer) { - //charge the customer - stripe.charges.create({ - amount: req.body.chargeAmount * 100, // amount in cents - currency: "usd", - customer: customer.id - }, function(err, charge) { - if (err) { - req.flash('errors', { msg: err.message }); - return res.redirect('/api/stripe/onetime'); - }else{ - req.flash('success', { msg: 'Charged Successfully'}); - res.render('api/stripe/onetime', { - title: 'Stipe API', - customer: customer, - charge: charge - }); - } - }); - }); - }); -}; +exports.postStripe = function(req, res, next) { + var stripeToken = req.body.stripeToken; + var stripeEmail = req.body.stripeEmail; -/** - * GET /api/newsubscriber - * Stripe Subscription API example. - */ -exports.getStripeNewSubscriber = function(req, res, next) { - stripe.plans.list(function(err, plans) { - res.render('api/stripe/newsubscriber', { - title: 'Stripe API', - plans: _.pluck(plans.data, 'name') - }); - }); + stripe.charges.create({ + amount: 395, + currency: 'usd', + card: stripeToken, + description: stripeEmail + }, function(err, charge) { + if (err && err.type === 'StripeCardError') { + req.flash('errors', { msg: 'Your card has been declined.'}); + res.redirect('/api/stripe'); + } + req.flash('success', { msg: 'Your card has been charged successfully.'}); + res.redirect('/api/stripe'); + }); }; -/** - * POST /api/stripe/newsubscriber - * @param ccNumber - * @param expMonth - * @param expYear - * @param ccNumber - * @param expMonth - * @param expYear - * @param customerName - * @param email - * @param plantype - */ -exports.postStripeNewSubscriber = function(req, res, next) { - console.log(req.body.plantype); - - stripe.tokens.create({ - card: { - "number": req.body.ccNumber, - "exp_month": req.body.expMonth, - "exp_year": req.body.expYear, - "cvc": req.body.cvc - } - }, function(err, token) { - if (err) { - req.flash('errors', { msg: err.message }); - return res.redirect('/api/stripe/newsubscriber'); - } - //Create a new customer - stripe.customers.create({ - card: token.id, - description: req.body.customerName, - email: req.body.email - }).then(function(customer) { - //charge the customer - stripe.customers.createSubscription( - customer.id, - {plan: req.body.plantype}, - function(err, subscription) { - if (err) { - req.flash('errors', { msg: err.message }); - return res.redirect('/api/stripe/newsubscriber'); - }else{ - stripe.plans.list(function(err, plans) { - req.flash('success', { msg: 'Subscribed Successfully'}); - res.render('api/stripe/newsubscriber', { - title: 'Stipe API', - customer: customer, - subscription: subscription, - plans: _.pluck(plans.data, 'name') - }); - }); - } - } - ); - }); - }); -}; - - /** * GET /api/twilio * Twilio API example. diff --git a/views/api/index.jade b/views/api/index.jade index 68dd8a2879..2a275cac2d 100644 --- a/views/api/index.jade +++ b/views/api/index.jade @@ -4,7 +4,6 @@ block content h2 API Examples hr -<<<<<<< HEAD .row.api-examples .col-sm-4 .panel.panel-default diff --git a/views/api/stripe.jade b/views/api/stripe.jade index 3bdfbdf298..82eba35938 100644 --- a/views/api/stripe.jade +++ b/views/api/stripe.jade @@ -1,10 +1,124 @@ extends ../layout block content - h2 Stripe API + .page-header + h2 Stripe API - ol - li - a(href='/api/stripe/onetime') One Time Charges - li - a(href='/api/stripe/newsubscriber') New Subscriber + .btn-group.btn-group-justified + a.btn.btn-primary(href='https://stripe.com/docs/tutorials/checkout') + i.fa.fa-home + | Integration Checkout + a.btn.btn-primary(href='https://stripe.com/docs/api', target='_blank') + i.fa.fa-code + | API Reference + a.btn.btn-primary(href='https://manage.stripe.com/account/apikeys', target='_blank') + i.fa.fa-gear + | API Keys + + br + + form(method='POST') + input(type='hidden', name='_csrf', value=_csrf) + script( + src='https://checkout.stripe.com/checkout.js', + class='stripe-button', + data-key='pk_test_6pRNASCoBOKtIshFeQd4XMUh', + data-image='http://static.tumblr.com/nljhkjv/z0Jlpk23i/logo', + data-name='Hackathon Starter', + data-description='Caramel Macchiato ($3.95)', + data-amount='395') + + h3 + i.fa.fa-credit-card + | Test Cards + p In test mode, you can use these test cards to simulate a successful transaction: + + table.table.table-striped.table-bordered.table-condensed + thead + tr + th Number + th Card type + tbody + tr + td 4242 4242 4242 4242 + td Visa + tr + td 4012 8888 8888 1881 + td Visa + tr + td 5555 5555 5555 4444 + td MasterCard + tr + td 5105 1051 0510 5100 + td MasterCard + tr + td 3782 822463 10005 + td American Express + tr + td 3714 496353 98431 + td American Express + tr + td 6011 1111 1111 1117 + td Discover + tr + td 6011 0009 9013 9424 + td Discover + tr + td 3056 9309 0259 04 + td Diners Club + tr + td 3852 0000 0232 37 + td Diners Club + tr + td 3530 1113 3330 0000 + td JCB + tr + td 3566 0020 2036 0505 + td JCB + + .panel.panel-primary + .panel-heading Stripe Successful Charge Example + .panel-body + p This is the response you will get when customer's card has been charged successfully. + | You could use some of the data below for logging purposes. + pre. + { id: 'ch_103qzW2eZvKYlo2CiYcKs6Sw', + object: 'charge', + created: 1397510564, + livemode: false, + paid: true, + amount: 395, + currency: 'usd', + refunded: false, + card: + { id: 'card_103qzW2eZvKYlo2CJ2Ss4kwS', + object: 'card', + last4: '4242', + type: 'Visa', + exp_month: 11, + exp_year: 2015, + fingerprint: 'Xt5EWLLDS7FJjR1c', + customer: null, + country: 'US', + name: 'sahat@me.com', + address_line1: null, + address_line2: null, + address_city: null, + address_state: null, + address_zip: null, + address_country: null, + cvc_check: 'pass', + address_line1_check: null, + address_zip_check: null }, + captured: true, + refunds: [], + balance_transaction: 'txn_103qzW2eZvKYlo2CNEcJV8SN', + failure_message: null, + failure_code: null, + amount_refunded: 0, + customer: null, + invoice: null, + description: 'sahat@me.com', + dispute: null, + metadata: {}, + statement_description: null }