我尝试使用Stripe的Apple Pay,但是存在一些问题。这是我的代码:
- (void)hasToken:(STPToken *)token {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSDictionary *chargeParams = @{
@"token": token.tokenId,
@"currency": @"usd",
@"amount": @"1000", // this is in cents (i.e. $10)
};
NSLog(@"Token ID: %@", token.tokenId);
if (!ParseApplicationId || !ParseClientKey) {
UIAlertView *message =
[[UIAlertView alloc] initWithTitle:@"Todo: Submit this token to your backend"
message:[NSString stringWithFormat:@"Good news! Stripe turned your credit card into a token: %@ \nYou can follow the "
@"instructions in the README to set up Parse as an example backend, or use this "
@"token to manually create charges at dashboard.stripe.com .",
token.tokenId]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
otherButtonTitles:nil];
[message show];
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
return;
}
// This passes the token off to our payment backend, which will then actually complete charging the card using your account's
[PFCloud callFunctionInBackground:@"charge"
withParameters:chargeParams
block:^(id object, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
if (error) {
[self hasError:error];
return;
}
[self.presentingViewController dismissViewControllerAnimated:YES
completion:^{
[[[UIAlertView alloc] initWithTitle:@"Payment Succeeded"
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil] show];
}];
}];
}
输入号码卡后,单击“支付”按钮,然后跳转至功能:
// This passes the token off to our payment backend, which will then actually complete charging the card using your account's
[PFCloud callFunctionInBackground:@"charge"
withParameters:chargeParams
block:^(id object, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
if (error) {
[self hasError:error];
return;
}
[self.presentingViewController dismissViewControllerAnimated:YES
completion:^{
[[[UIAlertView alloc] initWithTitle:@"Payment Succeeded"
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil] show];
}];
}];
但这跳到了功能上:
- (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];
}
并显示错误消息:错误:未找到功能(代码:141,版本:1.2.20)
请帮助我解决此问题。谢谢大家
您的问题分为两部分。首先,似乎您的代码中可能已经到达拥有STPtoken的地步,您只需要对其进行收费即可。您的云代码似乎有错误。您将要确保有一个名为“ charge”的函数,否则它将返回错误。您可以执行以下操作(使用javascript)来创建客户并创建费用:
Parse.Cloud.define("charge", function(request, response) {
Stripe.Customers.create({
card: request.params.token,
description: request.params.description
},{
success: function(results) {
response.success(results);
},
error: function(httpResponse) {
response.error(httpResponse);
}
}).then(function(customer){
return Stripe.Charges.create({
amount: request.params.amount, // in cents
currency: request.params.currency,
customer: customer.id
},{
success: function(results) {
response.success(results);
},
error: function(httpResponse) {
response.error(httpResponse);
}
});
});
});
然后,问题的第二部分与使用Apple Pay有关。使用Apple Pay,您可以创建令牌,而无需询问用户其付款信息。为了实施Apple Pay,您需要以下代码:
#import <PassKit/PassKit.h>
#import "Stripe.h" //Necessary as ApplePay category might be ignored if the preprocessor macro conditions are not met
#import "Stripe+ApplePay.h"
PKPaymentRequest *request = [Stripe
paymentRequestWithMerchantIdentifier:APPLE_MERCHANT_ID];
NSString *label = @"Text"; //This text will be displayed in the Apple Pay authentication view after the word "Pay"
NSDecimalNumber *amount = [NSDecimalNumber decimalNumberWithString:@"10.00"]; //Can change to any amount
request.paymentSummaryItems = @[
[PKPaymentSummaryItem summaryItemWithLabel:label
amount:amount]
];
if ([Stripe canSubmitPaymentRequest:request]) {
PKPaymentAuthorizationViewController *auth = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
auth.delegate = self;
[self presentViewController:auth animated:YES completion:nil];
}
此代码创建付款,测试以查看付款是否有可能被处理,如果可以,则显示付款授权视图。我将此代码放在viewDidAppear
包含收集用户付款信息功能的视图控制器中。如果能够处理付款,则付款授权视图控制器将显示在当前视图控制器上方,从而使用户可以决定是自己输入付款信息还是使用Apple Pay。确保声明当前视图控制器遵守PKPaymentAuthorizationViewControllerDelegate。并viewDidLoad
确保设置委托。
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion {
[self handlePaymentAuthorizationWithPayment:payment completion:completion];
}
如果用户决定使用自己的指纹并使用Apple Pay,则将调用此方法
- (void)handlePaymentAuthorizationWithPayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion {
[Stripe createTokenWithPayment:payment
completion:^(STPToken *token, NSError *error) {
if (error) {
completion(PKPaymentAuthorizationStatusFailure);
return;
}
//USE TOKEN HERE
}];
}
这将被称为处理付款。它将创建处理付款所需的令牌。您的其余代码可与此令牌一起使用。
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
[self dismissViewControllerAnimated:YES completion:nil];
}
付款处理完毕后,将调用此方法。付款授权视图控制器将被关闭。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句