Search code examples
braintree

braintree not havingt to enter credit card data each time


Asuming that I have a customer registered who already provided his credit card data and I don't want him enter it again on a next purchase, how would I do that? Right now I am using hosted fields and each time when testing I have to enter credit card data.


Solution

  • Ensure the customer's information (including payment method information), is being stored in the Braintree Vault.

    You can create a customer by itself, with a payment method, or with a credit card with a billing address. Example of a customer creation;

    result = braintree.Customer.create({
      "first_name": "Charity",
      "last_name": "Smith",
      "payment_method_nonce": nonce_from_the_client
    })
    
    
    result.is_success
    # True
    
    result.customer.id
    # e.g 160923
    
    result.customer.payment_methods[0].token
    # e.g f28w39
    

    If you intend to create a transaction at the same time as a customer, you may want to use Transaction.sale() with either the options.store_in_vault_on_success or options.store_in_vault options. For example:

    result = braintree.Transaction.sale({
    "amount": "10.00",
    "payment_method_nonce": nonce_from_the_client,
    "customer": {
      "id": "a_customer_id"
    },
    "options": {
    "store_in_vault_on_success": True,
      }
    })
    

    Once their information is stored in the Vault, you can pass their payment method token in the transaction API call, rather than a nonce.