【问题标题】:How to use Apple Pay by Stripe如何使用 Apple Pay by Stripe
【发布时间】:2014-12-24 18:17:16
【问题描述】:

我尝试通过 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)

请帮我解决这个问题。 谢谢大家。

【问题讨论】:

    标签: ios payment stripe-payments


    【解决方案1】:

    您的问题分为两部分。首先,您似乎已经在代码中达到了拥有 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 中,该控制器包含收集用户付款信息的功能。如果支付能够被处理,那么支付授权视图控制器将出现在当前视图控制器上,允许用户决定是自己输入支付信息还是使用苹果支付。确保声明当前视图控制器遵循 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];
    }
    

    这将在付款处理完毕后调用。支付授权视图控制器将被关闭。

    【讨论】:

      猜你喜欢
      • 2021-03-13
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 2016-11-11
      • 2016-04-24
      • 1970-01-01
      • 2019-04-05
      • 2021-07-13
      相关资源
      最近更新 更多