可以存储用户的信用卡信息。
在您的服务器上,您需要调用stripe.paymentMethods.attach() 方法来保存付款方式(以stripeID 表示,我们稍后会谈到)。
例子:
app.post('/attach_card', (req, res) => {
const customerID = req.body.customer_id;
const cardStripeID = req.body.stripe_id;
stripe.paymentMethods.attach(
cardStripeID,
{ customer: customerID }
).then(value => {
res.status(200).send(value);
}).catch(err => {
console.log(err);
res.status(500).end()
});
});
上面的代码需要两个参数customer_id和stripe_id。假设你已经对customer_id很熟悉了,那么我们来谈谈如何收集用户的卡片信息并获取代表付款方式的stripe_id。
- 初始化
STPAddCardViewController(与STPPaymentOptionsViewController中的添加卡片屏幕相同的UI)
let config = STPPaymentConfiguration()
config.requiredBillingAddressFields = .full
let viewController = STPAddCardViewController(configuration: config, theme: STPTheme.default())
viewController.delegate = self
let navigationController = UINavigationController(rootViewController: viewController)
present(navigationController, animated: true, completion: nil)
- 使您的 ViewController 符合
STPAddCardViewControllerDelegate
func addCardViewControllerDidCancel(_ addCardViewController: STPAddCardViewController) {
// Handle cancel action if needed
}
func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreatePaymentMethod paymentMethod: STPPaymentMethod, completion: @escaping STPErrorBlock) {
completion(nil)
print("--- created payment method with stripe ID: \(paymentMethod.stripeId)")
// Call the previous server code to store card info with Alamofire
let url = YOUR_SERVER_BASE_URL.appendingPathComponent("attach_card")
AF.request(url, method: .post, parameters: [
"customer_id": YOUR_CUSTOMER_ID,
"stripe_id": paymentMethod.stripeId
])
// Dismiss the modally presented VC
dismiss(animated: true, completion: nil)
}
现在卡片信息已保存,您可以在 Stripe 控制台中查看它:
条带化文档: