Search code examples
node.jsherokustripe-paymentsparse-cloud-codeparse-server

Parse-Server Cloud-Code and Stripe


I'm running a Parse-Server instance for a mobile web-app on Heroku. My problem is Stripe checkout plugin is working fine in my html page, but when the token is created and I call my Cloud Code it seems as if creating the variable initializing stripe does nothing. This is my cloud code.

var stripe = require('stripe')('sk_test_******');
Parse.Cloud.define("pay", function(req, res){
    Parse.Cloud.useMasterKey();
    var token = req.params.token;
    var amount = req.params.amount;
    var email = req.params.email;
    // stripe is null
    res.success(stripe);
});

Upon calling this, the value of stripe is null and I cannot figure out why. I have added stripe: '~4.7.0' in my package.json file and have run npm install to locally create all node modules. I don't know if it makes a difference but in my index.js file I have created a router

app.use('/', express.static(path.join(__dirname, '/public')));

because it's easier to access files in the public directory this way. Otherwise I would have to reference files in my public directory with /public/filename everytime I wanted to include that file. Any help would be much appreciated.


Solution

  • I gave up trying to run this charge in Cloud Code. Instead I created a route '/charge' in my index.js file and called it using a jQuery post. Here is my code in index.js

    var stripe = require('stripe')('sk_test_****');
    var bodyParser = require('body-parser');
    app.use(bodyParser.urlencoded({
        extended: false
    }));
    app.post('/charge', function(req, res){
        var token = req.body.token;
        var amount = req.body.amount;
        stripe.charges.create({
            amount: amount,
            currency: 'usd',
            source: token,
        }, function(err, charge){
            if(err)
                // Error check
            else
                res.send('Payment successful!');
        }
    });
    

    Here is my jQuery request

    var handler = StripeCheckout.configure({
        key: 'pk_test_****',
        locale: 'auto',
        token: function(token){
            $.post('/charge', {
                token: token.id,
                amount: total,
            }, function(data, status){
                alert(data);
            });
        }
    });