【发布时间】:2015-01-19 16:19:32
【问题描述】:
我正在尝试在 Parse 后端为我的应用程序用户保存一个 customerId。
以下代码可能有什么问题:
var Stripe = require("stripe");
Stripe.initialize('sk_test_----------');
Parse.Cloud.define("saveStripeCustomerId", function (request, response) {
Stripe.Customers.create(
{ card: request.params.token
}, {
success: function(httpResponse) {
response.success("Purchase made!");
var Usr = Parse.User.current();
Usr.set("StripeCustomerId",request.params.objectId );
Usr.save(null, {
success: function(newUsr) {
// Execute any logic that should take place after the object is saved.
alert('New object created with objectId: ' + newUsr.id);
},
error: function(newUsr, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
alert('Failed to create new customer , with error code: ' + error.message);
}
});
},
error: function(httpResponse) {
response.error("Error ...oh no");
}
});
});
IOS代码:
- (IBAction)save:(id)sender
{
PTKCard* card = self.paymentView.card;
NSLog(@"Card last4: %@", card.last4);
NSLog(@"Card expiry: %lu/%lu", (unsigned long)card.expMonth, (unsigned long)card.expYear);
NSLog(@"Card cvc: %@", card.cvc);
[[NSUserDefaults standardUserDefaults] setValue:card.last4 forKey:@"card.last4"];
[self.navigationController popViewControllerAnimated:YES];
STPCard* stpcard = [[STPCard alloc] init];
stpcard.number = card.number;
stpcard.expMonth = card.expMonth;
stpcard.expYear = card.expYear;
stpcard.cvc = card.cvc;
[Stripe createTokenWithCard:stpcard completion:^(STPToken *token, NSError *error) {
if (error) {
//[self handleError:error];
} else {
//[self createBackendChargeWithToken:token];
[PFCloud callFunctionInBackground:@"saveStripeCustomerId"
withParameters:[NSDictionary dictionaryWithObjectsAndKeys:token.tokenId, @"token", nil]
block:^(id object, NSError *error) {
if(error == nil)
{
[[[UIAlertView alloc] initWithTitle:@"Stripe Customer Id saved!"
message:@"Your stripe cust id has been saved!"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil] show];
}
}];
}
}];
}
@end
使用此代码,我可以在 Stripe 中创建客户。但是,它不会将其保存在 Parse User Table 中。
Parse Cloud Log:
I2014-11-21T15:55:10.887Z] v46: Ran cloud function saveStripeCustomerId for user GBkeqCcOcU with:
Input: {"token":"tok_-----------"}
Result: Purchase made!
可能出了什么问题?我将不胜感激,谢谢!
【问题讨论】:
-
如果您可以在 Stripe.Customers.create 的错误回调中记录 httpResponse 以查看响应是什么会很有帮助,很可能它包含有用的提示。
-
谢谢@Björn,有机会我会补充的
-
这会不会导致安全漏洞???
标签: ios parse-platform stripe-payments