如何使用Stripe的Apple Pay

时间:2014-12-24 18:17:17

标签: ios payment stripe-payments

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

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

1 个答案:

答案 0 :(得分:2)

您的问题分为两部分。首先,您似乎已经达到了代码中您拥有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];
}

这将在付款处理后调用。付款授权视图控制器将被解雇。