簡體   English   中英

自定義 Stripe UI 集成以保存和獲取用戶卡信息

[英]Customizing Stripe UI integration to save and fetch user card information

我正在使用 Stripe 來允許用戶相互付款。 我遇到了一個錯誤,我很確定這是因為用戶沒有保存他們的付款類型。 我想知道是否可以操縱條紋 STPPaymentOptionsViewController UI 來保存客戶信息? 或者如果我必須自定義我自己的控制器來做到這一點。

可以存儲用戶的信用卡信息。

在您的服務器上,您需要調用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_idstripe_id 我假設您已經熟悉customer_id ,那么我們來談談如何收集用戶的卡信息並獲取代表付款方式的stripe_id

  1. 初始化STPAddCardViewController (相同的用戶界面作為附加卡屏STPPaymentOptionsViewController
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)
  1. 使您的 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 控制台中查看它:

條紋控制台


條紋文檔:

我不認為這是 Stripe 可以支持的用例,因此您可能需要與他們聯系以確認: https : //support.stripe.com/contact/email

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM