所以几天后,我终于能够在今天早上创建代币、支付方法和支付意向设置。现在我对如何使用Apple Pay实际确认付款感到困惑,在非Apple Pay结账流程中,我有这个功能:
func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPPaymentStatusBlock) {
guard let paymentIntentClientSecret = paymentIntentClientSecret else {
return;
}
// Collect card details
let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClientSecret)
paymentIntentParams.configure(with: paymentResult)
// Submit the payment
let paymentHandler = STPPaymentHandler.shared()
paymentHandler.confirmPayment(paymentIntentParams, with: self) { (status, paymentIntent, error) in
switch (status) {
case .failed:
self.displayFailureAlert(title: "Payment Failed", message: "There was an error trying to complete the payment, please try again later.")
self.paymentInProgress = false
break
case .canceled:
self.displayCancelledAlert(title: "Payment Canceled", message: "The payment has been cancelled.")
self.paymentInProgress = false
break
case .succeeded:
self.displaySuccessAlert(title: "Payment Succeeded", message: "The payment was successful!")
self.paymentInProgress = false
break
@unknown default:
fatalError()
break
}
}
}
这很管用。STPApplePayContext
中的参数与STPPaymentContext
有点不同,我不能为它使用完全相同的功能
func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
guard let paymentIntentClientSecret = paymentIntentClientSecret else {
return;
}
let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClientSecret)
let error = NSError()
completion(paymentIntentClientSecret, error)
}
到目前为止,这就是我所拥有的,我尝试过做STPAPIClient.shared.confirmPaymentIntent(with:)
,但没有任何改变,有什么建议吗?
您将在成功处理程序中获得带有retrievePaymentIntent
的付款响应,并在调用此函数后获得所有与付款相关的信息,还可以使用Strip API获取带有交易id的数据。
func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) {
switch status {
case .success:
// Payment succeeded, show a receipt view
context.apiClient.retrievePaymentIntent(withClientSecret: clientSecret) { (paymentIntent, error) in
print(paymentIntent?.allResponseFields ?? "")
}
break
case .error:
// Payment failed, show the error
break
case .userCancellation:
// User cancelled the payment
break
@unknown default:
fatalError()
}
}
基本上,您需要按照Stripe文档第7步开始的说明进行操作。
具体来说,你需要在服务器上创建一个支付意向,将其客户端机密传递给你的应用程序,然后实现如下委托方法:
extension CheckoutViewController {
func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
let clientSecret = ... // Retrieve the PaymentIntent client secret from your backend (see Server-side step above)
// Call the completion block with the client secret or an error
completion(clientSecret, error);
}
func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) {
switch status {
case .success:
// Payment succeeded, show a receipt view
break
case .error:
// Payment failed, show the error
break
case .userCancellation:
// User cancelled the payment
break
@unknown default:
fatalError()
}
}
}
另请参阅STPApplePayContext的文档,它提供了简化的高级指令:
- 使用描述付款请求(金额、行项目、所需运输信息等(的PKPaymentRequest初始化此类
- 调用presentalApplePayOnViewController:complete:以呈现Apple Pay表单并开始支付过程3(可选(:如果您需要响应用户更改其发货信息/发货方式,请实现可选的委托方法
- 当用户点击"购买"时,此类使用您在applyPayContext:didCreatePaymentMethod:complete:delegate方法中提供的PaymentIntent来完成支付
- 付款完成/出现错误并且表单被驳回后,此类会在applyPayContext:didCompleteWithStatus:delegate方法中通知您