Apple 支付与 Stripe 的集成(调试中的 STPTestPaymentAuthorizationViewController)

Apple pay integration with Stripe (STPTestPaymentAuthorizationViewController in debug)

我正在尝试在我的 iOS 应用程序中集成 Apple Pay 和 Stripe。 在 DEBUG 模式下使用 ApplePayStub provided by stripe 查看 apple pay

我正在使用来自 git

latest stripeApplePayStub 代码

尝试在 iPhone 6 模拟器 上 运行,我使用的代码是:

paymentController = [[STPTestPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
((STPTestPaymentAuthorizationViewController*) paymentController).delegate = self;

获取错误:

Error Domain=com.stripe.lib Code=50 "Your payment information is formatted improperly. Please make sure you're correctly using the latest version of our iOS library. For more information see https://stripe.com/docs/mobile/ios ." UserInfo=0xxxxxxxxxxx {com.stripe.lib:ErrorMessageKey=Your payment information is formatted improperly. Please make sure you're correctly using the latest version of our iOS library. For more information see https://stripe.com/docs/mobile/ios ., NSLocalizedDescription=Your payment information is formatted improperly. Please make sure you're correctly using the latest version of our iOS library. For more information see https://stripe.com/docs/mobile/ios .},

非常感谢任何帮助。

尝试使用 PaymentKit 发出请求

@IBAction func payTapped(AnyObject) {
    // Valiate that this device can take payments
    if (PKPaymentAuthorizationViewController.canMakePayments()) {
        // Construct the basic payment request
        var paymentRequest = PKPaymentRequest()
        paymentRequest.merchantIdentifier = "merchant.com.example";
        paymentRequest.supportedNetworks = [PKPaymentNetworkVisa, PKPaymentNetworkMasterCard, PKPaymentNetworkAmex]
        paymentRequest.merchantCapabilities = PKMerchantCapability.Capability3DS | PKMerchantCapability.CapabilityEMV
        paymentRequest.countryCode = "US"
        paymentRequest.currencyCode = "USD"
        paymentRequest.requiredShippingAddressFields = PKAddressField.All;

        // Add a line item
        var totalItem = PKPaymentSummaryItem(label:"Foo", amount:NSDecimalNumber(string:"0.05"))
        paymentRequest.paymentSummaryItems = [totalItem];

        // Show the Apple Pay controller
        var payAuth = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
        payAuth.delegate = self
        self.presentViewController(payAuth, animated:true, completion: nil)
    }
}

@Wizyashas,您对 STPTestPaymentAuthorizationViewController 的设置非常好。当您尝试通过 STPAPIClient 生成令牌时会出现问题。这就是一切变得混乱的地方。

这一点 RnD 帮助了我。深入研究 Stripe 自己提供的 CustomSampleProjectApplePayStubsSTPCard 时工作得很好当委托

时被识别
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                   didAuthorizePayment:(PKPayment *)payment
                            completion:(void (^)(PKPaymentAuthorizationStatus))completion

of PKPaymentAuthorizationViewControllerDelegate 被调用。此处的示例代码检查代码是否在 ApplePayStubs 的调试中为 运行,委托中的 (PKPayment *)payment转换为 STPCard 并启动到 STPAPIClient 以生成 STTPToken。以下是上述代表的正文:

#if DEBUG // This is to handle a test result from ApplePayStubs
if (payment.stp_testCardNumber)
{
    STPCard *card = [STPCard new];
    card.number = payment.stp_testCardNumber;
    card.expMonth = 12;
    card.expYear = 2020;
    card.cvc = @"123";
    [[STPAPIClient sharedClient] createTokenWithCard:card
                                          completion:^(STPToken *token, NSError *error)
    {
        if (error)
        {
            completion(PKPaymentAuthorizationStatusFailure);
            [[[UIAlertView alloc] initWithTitle:@"Error"
                                        message:@"Payment Unsuccessful! \n Please Try Again"
                                       delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
            return;
        }
    /*
     Handle Token here
     */
                                            }];
}
#else
[[STPAPIClient sharedClient] createTokenWithPayment:payment
                                         completion:^(STPToken *token, NSError *error)
{
    if (error)
    {
        completion(PKPaymentAuthorizationStatusFailure);
        [[[UIAlertView alloc] initWithTitle:@"Error"
                                    message:@"Payment Unsuccessful!"
                                   delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
        return;
    }
    /*
     Handle Token here
     */
}];
#endif

这对我有用。使用 ApplePayStubs(在模拟器上)和不使用它们(在设备上)。

希望对您有所帮助:)