Nicholas Ferretti
Nicholas Ferretti

Reputation: 51

Stripe Payments with parse server cloud code

I have this code included in my main.js:

var stripe = require("/cloud/stripe.js")("sk_test_*********");
//create customer
Parse.Cloud.define('createCustomer', function (req, res) {
  stripe.customers.create({
    description: req.params.fullName,
    source: req.params.token 
    //email: req.params.email
  }, function (err, customer) {
    // asynchronously called
    res.error("someting went wrong with creating a customer");
  });
});

After pushing this code to my Heroku server the logs indicate that: Error: Cannot find module '/cloud/stripe.js'

I have also tried var stripe = require("stripe")("sk_test_*********"); but this returns the same error. Whenever I try add this new module to my server the whole server becomes dysfunctional. What workarounds are there to this? Thanks

Upvotes: 1

Views: 548

Answers (2)

thailey01
thailey01

Reputation: 165

I'll tell you what worked for me, I racked my brain on this for a day. Instead of using Cloud Code to make a charge, create a route on index.js. Something like this 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!');
    }
});

I call this using jQuery post but you could also use a form.

Upvotes: 0

korben
korben

Reputation: 1146

Have you added Stripe to the requirements of your package.json file for your node project? If so, you should be able to reference it using the term require('stripe') as opposed to what you're doing.

Upvotes: 1

Related Questions