gpt4 book ai didi

ios - 如何使用 Stripe 的 Apple Pay

转载 作者:行者123 更新时间:2023-11-29 10:34:51 30 4
gpt4 key购买 nike

我尝试使用 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,您可以创建一个 token ,而无需向用户询问他们的支付信息。为了实现 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];
}

此代码创建付款,测试付款是否可能被处理,如果是,则显示付款授权 View 。我将此代码放在 View Controller 中的 viewDidAppear 中,该 View Controller 包含收集用户付款信息的功能。如果支付能够被处理,那么支付授权 View Controller 将出现在当前 View Controller 之上,允许用户决定他们是想自己输入支付信息还是使用 apple pay。确保声明当前 View Controller 遵循 PKPaymentAuthorizationViewControllerDelegate。并在 viewDidLoad 中确保设置委托(delegate)。

- (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
}];
}

这将被调用以处理付款。它将创建您处理付款所需的 token 。您的其余代码可以与此 token 一起使用。

- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
[self dismissViewControllerAnimated:YES completion:nil];
}

这将在处理完付款后调用。支付授权 View Controller 将被关闭。

关于ios - 如何使用 Stripe 的 Apple Pay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27640819/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com