Parse.com create stripe card token in cloud code (main.js)

回眸只為那壹抹淺笑 提交于 2020-01-22 02:41:05

问题


I am looking to create stripe token in parse cloud code..

I dont want to create token in client side HTML page. My complete web application is in HTML + Javascript so dont want to expose my Stripe.setPublishableKey('pk_test_xxxxxxx');

Because of this reason interest to define function in cloud code.

Parse.Cloud.define("addCreditCard", function(request, response) {
    var token;
    var group;

    var Stripe = require('https://js.stripe.com/v2/');
    Stripe.setPublishableKey('pk_test_xxxxxxxxx');

    Stripe.card.createToken({
        number : request.params.number,
        cvc : request.params.cvc,
        exp_month : request.params.month,
        exp_year : request.params.year
    }, {
        sucsess: function(result) { response.success("Ok"); },
        error : function(error) { response.error(error); }
    });
});

Here parse cloud unable to call var Stripe = require('https://js.stripe.com/v2/');

If so many place suggested use parse cloud stripe module var Stripe = require('stripe'); var STRIPE_SECRET_KEY = 'sk_test_xxxxxxxxxx';

But here the function Stripe.card.createToken is not define


回答1:


Finally my research is over and I got the solution:

Parse.Cloud.httpRequest({
    method : 'POST',
    url : 'https://api.stripe.com/v1/tokens',
    headers : {
        'Authorization' : 'Bearer sk_test_xxxxxxxxxxxxxx'
    },
    body : {
        "card[number]" : request.params.number,
        "card[exp_month]" : request.params.month,
        "card[exp_year]" : request.params.year,
        "card[cvc]" : request.params.cvc
    },
    success : function(httpResponse) {
        token = httpResponse.data.id; // Its token which required for create payment/charge
    },
    error : function(httpResponse) {
        // Error
    }
})

The above code can be used in any cloud function which are written in main.js



来源:https://stackoverflow.com/questions/27664131/parse-com-create-stripe-card-token-in-cloud-code-main-js

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