制作令牌的代码
- (IBAction)save:(id)sender
{
STPCard *card = [[STPCard alloc] init];
card.number = self.paymentView.card.number;
card.expMonth = self.paymentView.card.expMonth;
card.expYear = self.paymentView.card.expYear;
card.cvc = self.paymentView.card.cvc;
[[STPAPIClient sharedClient] createTokenWithCard:card
completion:^(STPToken *token, NSError *error) {
if (error) {
NSLog(@"%@",error);
} else {
NSString *myVal = [NSString stringWithFormat:@"%@",token];
NSLog(@"%@",token);
[PFCloud callFunctionInBackground:@"chargeMoney"
withParameters:@{@"token":myVal}
block:^(NSString *result, NSError *error) {
if (!error) {
NSLog(@"from Cloud Code: %@",result);
}
}];
};
}];
}
代码收费
Parse.Cloud.define("chargeMoney", function(request, response) {
response.success(request.params.token);
var stripe = require('stripe');
stripe.initialize('sk_test_ldqwdqwdqwdqwdwqdqwd ');
var stripeToken = request.params.token;
var charge = stripe.charges.create({
amount: 1000, // amount in cents, again
currency: "usd",
source: stripeToken,
description: "payinguser@example.com"
}, function(err, charge) {
if (err && err.type === 'StripeCardError') {
// The card has been declined
}
});
});
我收到错误
[Error]: TypeError: Cannot call method 'create' of undefined
at main.js:11:31 (Code: 141, Version: 1.7.1)
我的代码有什么问题。我没有根据文档更改我正在做的任何事情。任何onle请在我的代码中建议我的问题。
答案 0 :(得分:3)
嘿所以我能够复制你的错误。在javascript中,将var charge = stripe.charges.create({
更改为var charge = stripe.Charges.create({
如果您愿意,也可以直接传递令牌,您不需要将其转换为字符串。
答案 1 :(得分:2)
最后我花了一个晚上才发现我的错误,我的代码中有三个错误:
错误1
替换此
NSString *myVal = [NSString stringWithFormat:@"%@",token];
带
NSString *myVal = [NSString stringWithFormat:@"%@",token.tokenId];
错误2
stripe.initialize('sk_test_ldqwdqwdqwdqwdwqdqwd ');
在最后一个单词之后,它们是密钥中的额外空格,即'd'。
错误3
删除此
response.success(request.params.token);
从顶部
最后工作代码是::
创建令牌
- (IBAction)save:(id)sender
{
STPCard *card = [[STPCard alloc] init];
card.number = self.paymentView.card.number;
card.expMonth = self.paymentView.card.expMonth;
card.expYear = self.paymentView.card.expYear;
card.cvc = self.paymentView.card.cvc;
[[STPAPIClient sharedClient] createTokenWithCard:card
completion:^(STPToken *token, NSError *error) {
if (error) {
NSLog(@"%@",error);
} else {
NSString *myVal = token.tokenId;
NSLog(@"%@",token);
[PFCloud callFunctionInBackground:@"hello" withParameters:@{@"token":myVal}
block:^(NSString *result, NSError *error) {
if (!error) {
NSLog(@"from Cloud Code Res: %@",result);
}
else
{
NSLog(@"from Cloud Code: %@",error);
}
}];
};
}];
}
解析云代码(main.js)
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
var stripe = require('stripe');
stripe.initialize('sk_test_lweGasdaqwMKnZRndg5123G');
Parse.Cloud.define("hello", function(request, response) {
var stripeToken = request.params.token;
var charge = stripe.Charges.create({
amount: 1000, // express dollars in cents
currency: 'usd',
card: stripeToken
}).then(null, function(error) {
console.log('Charging with stripe failed. Error: ' + error);
});
});