简体   繁体   English

如何使用Stripe的Apple Pay

[英]How to use Apple Pay by Stripe

I try to work Apple Pay by Stripe, but it has some issue. 我尝试使用Stripe的Apple Pay,但是存在一些问题。 This is my code: 这是我的代码:

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

After input number card, then "Pay" button click, and then jump to function: 输入号码卡后,单击“支付”按钮,然后跳转至功能:

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

But it's jump to function: 但这跳到了功能上:

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

And show error message: Error: function not found (Code: 141, Version: 1.2.20) 并显示错误消息:错误:未找到功能(代码:141,版本:1.2.20)

Please help me resolve this issue. 请帮助我解决此问题。 Thanks all. 谢谢大家

There are 2 parts to your question. 您的问题分为两部分。 Firstly, it seems that you might have already reached the point in your code where you have an STPtoken and you just have to be able to run a charge with it. 首先,似乎您可能已经在代码中达到了拥有STPtoken的地步,并且只需要对其进行收费即可。 It looks like there is an error with your cloud code. 您的云代码似乎有错误。 You will want to make sure you have a function called "charge" or else it will return the error. 您将要确保有一个名为“ charge”的函数,否则它将返回错误。 You could do something like the following (in javascript) to create a customer and create the 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);
    }
  });
 });
});

Then the second part of your question has to do with working with Apple Pay. 然后,问题的第二部分与使用Apple Pay有关。 With apple pay, you can create a token without ever asking for the user for their payment info. 使用Apple Pay,您可以创建令牌,而无需询问用户其付款信息。 In order to implement Apple Pay, you need the following code: 为了实施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];
} 

This code creates a payment, tests to see if the payment could potentially be processed, and if so, displays the payment authorization view. 此代码创建付款,测试以查看付款是否有可能被处理,如果可以,则显示付款授权视图。 I place this code in viewDidAppear in the view controller that contains the ability to collect the user's payment info. 我将此代码放在视图控制器中的viewDidAppear中,该代码包含收集用户付款信息的功能。 If the payment is able to be processed, then the payment authorization view controller will appear over the current view controller, allowing the user to decide if they want to enter in the payment info themselves or use apple pay. 如果能够处理付款,则付款授权视图控制器将显示在当前视图控制器上方,从而使用户可以决定自己要输入付款信息还是使用Apple Pay。 Make sure to declare that the current view controller adheres to the PKPaymentAuthorizationViewControllerDelegate. 确保声明当前的视图控制器遵守PKPaymentAuthorizationViewControllerDelegate。 And in viewDidLoad make sure to set the delegate. 并确保在viewDidLoad中设置委托。

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                       didAuthorizePayment:(PKPayment *)payment
                                completion:(void (^)(PKPaymentAuthorizationStatus))completion {
    [self handlePaymentAuthorizationWithPayment:payment completion:completion];
}

This will be called if the user decides to use their fingerprint and use Apple Pay 如果用户决定使用其指纹并使用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
                        }];
}

This will be called to process the payment. 这将被称为处理付款。 It will create the token you require to process the payment. 它将创建处理付款所需的令牌。 The rest of your code can be used with this token. 您的其余代码可与此令牌一起使用。

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

This will be called after the payment has been processed. 付款处理完毕后,将调用此方法。 The payment authorization view controller will be dismissed. 付款授权视图控制器将被关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM