Apple Pay + Stripe 不完整集成指南

iOS Nov 4, 2018

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: .black))

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)

效果图是这样的

Tags

Jie Li

🚘 On-road / 📉 US Stock / 💻 Full Stack Developer / 🎓 Grad Student / ®️ ENTJ

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.