HelloCoding HelloCoding - 4 months ago 30
iOS Question

How do I save customer card information with Braintree so that I can then charge it later when they make purchases?

SO in my iOS app (and Java backend) users can purchase things. When they create an account they are required to provide us with their payment card information. I want to be able to save this card information when their account is created in app, and tie it to a UUID with which it will be looked up. Furthermore, whenever the user then wants to buy something, I want to easily be able to get their payment information they provided on sign up, based on their UUID and charge the card stored in it. This seems like an extremely common use case for Braintree but I can't find examples online regarding how to implement it and I am stuck.

So how do I:


  1. Save card information associated with a user based on a UUID?
    and

  2. Look up card information based on a UUID, and then charge it?


Answer

Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.

When creating a customer, you can specify a customer ID and associate a payment method:

CustomerRequest request = new CustomerRequest()
    .id("customer_123")
    .paymentMethodNonce(nonceFromTheClient);

Result<Customer> result = gateway.customer().create(request);

In order to find a customer’s payment method information later using the customer ID, you can call Customer.find() and then retrieve that customer’s payment methods:

Customer customer = gateway.customer().find("customer_123");
customer.getPaymentMethods().get(0).getToken();

Alternatively, you can create a transaction with a customer ID and an amount, which will automatically use the customer's default payment method:

TransactionRequest request = new TransactionRequest()
    .customerId("customer_123")
    .amount(new BigDecimal("10.00"));

Result<Transaction> result = gateway.transaction().sale(request);

Here’s some general documentation about creating transactions and accepting credit cards with Braintree.