我正在使用 stripe 和 parse 来允许我的应用程序的用户输入他们的信用卡并进行购买。到目前为止,用户可以购买一切都很好。但我想让用户输入他们的 CC 信息并保存该信息,这样他们就不必一直重新输入。我真的很难做到这一点,我已经弄明白了第一部分,我只需要得到这个。
更新:
- (IBAction)save:(id)sender {
if (![self.paymentView isValid]) {
return;
}
if (![Stripe defaultPublishableKey]) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Publishable Key"
message:@"Please specify a Stripe Publishable Key in Constants.m"
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
otherButtonTitles:nil];
[message show];
return;
}
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
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;
[Stripe createTokenWithCard:card completion:^(STPToken *token, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
if (error) {
[self hasError:error];
} else {
[self createCustomerFromCard:(NSString *)token completion:(PFIdResultBlock)handler]; //I'm having trouble on this line here.
}
}];
}
- (void)hasError:(NSError *)error {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"Error")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
otherButtonTitles:nil];
[message show];
}
+ (void)createCustomerFromCard:(NSString *)token completion:(PFIdResultBlock)handler
{
[PFCloud callFunctionInBackground:@"createCustomer"
withParameters:@{
@"tokenId":token,
}
block:^(id object, NSError *error) {
//Object is an NSDictionary that contains the stripe customer information, you can use this as is, or create an instance of your own customer class
handler(object,error);
}];
}
最佳答案
因此,您在 iOS 方面所做的一切都是正确的。不同之处在于,在您的后端,您将希望使用此 token 创建一个 Customer
,然后对该 Customer
进行收费。在我们位于 https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later 的文档中有一个与此高度相关的部分。 .
如果我这样做,我会创建 2 个 Parse 函数:一个叫做 createCustomer
,它需要一个 tokenId
,创建一个 Customer
并返回该客户的 ID。您可以在您的 iOS 应用程序中调用它,然后在本地保留客户 ID。 (您也可以将它附加到 Parse 后端的 User
。重要的是您希望以后能够检索它)。当您的应用用户通过输入他们的卡信息创建 token 时,您将调用此函数一次。
然后,任何时候您想要对该信用卡进行另一次收费,您将调用第二个 Parse 函数,将其命名为 chargeCustomer
。这将采用您之前保存的 customerId 和金额(以及可选的货币等)。就是这样!
下面是这些函数的样子(请注意,我还没有测试过这段代码,所以可能会有小错误,但它应该足以表达我的观点):
Parse.Cloud.define("createCustomer", function(request, response) {
Stripe.Customers.create({
card: request.params['tokenId']
}, {
success: function(customer) {
response.success(customer.id);
},
error: function(error) {
response.error("Error:" +error);
}
})
});
Parse.Cloud.define("chargeCustomer", function(request, response) {
Stripe.Charges.create({
amount: request.params['amount'],
currency: "usd",
customer: request.params['customerId']
}, {
success: function(customer) {
response.success(charge.id);
},
error: function(error) {
response.error("Error:" +error);
}
})
});
希望对您有所帮助。如果您需要进一步的帮助,请随时联系 support@stripe.com。
jack
关于ios - Stripe 创建客户 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25901803/