问题描述
我希望在解析云代码中创建条带令牌..
I am looking to create stripe token in parse cloud code..
我不想在客户端 HTML 页面中创建令牌.我的完整 Web 应用程序是 HTML + Javascript,所以不想公开我的 Stripe.setPublishableKey('pk_test_xxxxxxx');
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); }
});
});
这里解析云无法调用 var Stripe = require('https://js.stripe.com/v2/');
Here parse cloud unable to call var Stripe = require('https://js.stripe.com/v2/');
如果有这么多地方建议使用解析云条模块var Stripe = require('stripe');var STRIPE_SECRET_KEY = 'sk_test_xxxxxxxxxx';
If so many place suggested use parse cloud stripe module var Stripe = require('stripe'); var STRIPE_SECRET_KEY = 'sk_test_xxxxxxxxxx';
但是这里的函数 Stripe.card.createToken
没有定义
But here the function Stripe.card.createToken
is not define
推荐答案
终于我的研究结束了,我得到了解决方案:
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
}
})
以上代码可以在main.js编写的任何云函数中使用
The above code can be used in any cloud function which are written in main.js
这篇关于Parse.com 在云代码中创建条卡令牌 (main.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!