Parse-Server Cloud-Code and Stripe

旧巷老猫 提交于 2019-12-11 13:47:22

问题


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.


回答1:


i had issues with this today

I did it by trial and error, here is what i remember

https://www.npmjs.com/package/stripe <- thats where i got the cloud code from

  1. in the root directory of your parse-server through command prompt i executed the following - npm install stripe
  2. then i added the stripe dependancy to package.json (this stack overflow post was the missing key) "stripe": "~4.9.0",
  3. the cloud code is as follows

Parse.Cloud.define("charge", function(request, response) {

var stripe = require('stripe')('sk_test_****');

stripe.customers.create({
  email: theEmailAddress
}).then(function(customer) {
  return stripe.charges.create({
    amount: yourAmount, 
    currency: yourCurrency,
    card: yourToken,
    description: yourDescription
  });
}).then(function(charge) {
  // New charge created on a new customer 
}).catch(function(err) {
  // Deal with an error 
});

});

  1. use that cloud code through your app and see if it works in your stripe dashboard (you must check in the dashboard)

so the two 'breakthroughs' came when i added stripe as a dependancy in package.json and also can you see that var stripe = require is inside the the cloud code function

see the following post where the answer was originally posted

Require modul not working when I migrate from parse to heroku




回答2:


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);
        });
    }
});


来源:https://stackoverflow.com/questions/38299868/parse-server-cloud-code-and-stripe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!