问题
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