Sunday, 14 May 2017

Need guidance trying to setup a variable amount with stripe using node js

I'm new to node and javascript so please excuse me. This is what I have right now, just copied it from the Stripe docs. I'm confused as to what I'm supposed to do with the commented token function. I was using the simple Stripe configuration and got everything working perfectly but I decided that I want to allow a custom amount to be set by the user via an input field. Any help to guide me in that direction would be awesome.

<script src="https://checkout.stripe.com/checkout.js"></script>

<button id="customButton">Purchase</button>

<script>
var handler = StripeCheckout.configure({
  key: 'pk_test_kwaJPsfz0oRxy4vWuJ5QZcsV',
  image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
  locale: 'auto',
  token: function(token) {
    // You can access the token ID with `token.id`.
    // Get the token ID to your server-side code for use.
  }
});

document.getElementById('customButton').addEventListener('click', function(e) {
  // Open Checkout with further options:
  handler.open({
    name: 'Demo Site',
    description: '2 widgets',
    amount: 2000
  });
  e.preventDefault();
});

// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
  handler.close();
});
</script>

And here's my server-side code (stripe.js):

var express = require('express');
var router = express.Router();
var stripe = require('stripe')('sk_test_7RjcGXzQq0S5OhE5WD9R01wR');

router.post('/charge', function(req, res, next) {
    var token = req.body.stripeToken;
    var chargeAmount = req.body.chargeAmount;
    var charge = stripe.charges.create({
      amount: 2000,
      currency: "usd",
      source: token
    }, function(err, charge) {
      if(err) {
        return console.log(err);
      }
      console.log(req.body);
      res.redirect('/users/dashboard');
    });
});

module.exports = router;



via kolbykskk

No comments:

Post a Comment