简体   繁体   中英

Customizing Stripe UI integration to save and fetch user card information

I am using stripe to allow users to send payments to each other. I have run into an error and I am pretty sure it is because the user doesn't have their payment types saved. I was wondering if it is possible to manipulate stripes STPPaymentOptionsViewController UI to save the customer info? Or if I would have to customize my own controller to do this.

It is possible to store users' credit card information.

On your server, you need to call stripe.paymentMethods.attach() method to save a payment method (represented by a stripeID , we'll talk about that later).

Example:

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()
    });
});

There are two parameters needed for the above code, customer_id and stripe_id . I assume you are already familiar with customer_id , so let's talk about how to gather users' card information and get the stripe_id that represents the payment method.

  1. Initialize a STPAddCardViewController (same UI as the add card screen in 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. Make your ViewController comply to the 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)
}

Now the card info is saved, and you can view it in your Stripe console:

条纹控制台


Stripe documentation:

我不认为这是 Stripe 可以支持的用例,因此您可能需要与他们联系以确认: https : //support.stripe.com/contact/email

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM