Apple Pay + Stripe 不完整整合指南
Apple Pay 整合本身沒幾行程式碼,基本步驟就是 Controller 遵循 PKPaymentAuthorizationViewControllerDelegate
,構造一個 PKPaymentRequest
,設定一個 PKPaymentSummaryItem
array。
整合過程遇到幾個坑,首先是扣款總是不成功,錢包已經顯示扣款,但是銀行賬單看不到,解決方案是後端需要多呼叫一個 API,用 Apple Pay 付款成功得到的 tokenId,發起一個扣款,把結果返回給 iOS,作為顯示用。
第二個坑是稽核,蘋果要求必須用自帶的按鈕樣式,我的 app 圖省事,用了一個 UIAlertController,讓使用者選支付方式,預設的 UIAlertAction 只能設定標題。解決方案如下
let applePayAction = UIAlertAction(title: "", style: .default, handler: { (action) in self.popUpApplePayWindow() }) let img = UIImage.fromView(PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .white)) let left = -alert.view.frame.size.width / 2 + img.size.width - 7 let centeredImg = img.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: left, bottom: 0, right: 0)).withRenderingMode(.alwaysOriginal) applePayAction.setValue(centeredImg, forKey: "image") alert.addAction(applePayAction)
效果圖是這樣的
