簡體   English   中英

如何使用Stripe的Apple Pay

[英]How to use Apple Pay by Stripe

我嘗試使用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];
}

付款處理完畢后,將調用此方法。 付款授權視圖控制器將被關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM