Search code examples
cordovapaypalbraintree

Braintree Integration - User Credit Card Details only once


We are developing a cordova application which would be similar to Uber. Workflow is simple: User Requests for a service, an associate is assigned, after the work is completed, charge the user for number of hours.

I have the server generating the client token from braintree

    $clientToken = Braintree_ClientToken::generate();

and can ask the user to enter his credit card details only once for the first request, after which I create a Braintree_customer object

$result = Braintree_Customer::create(array(
                            'paymentMethodNonce' => $payment_token
                        ));

and store the token in my DB.

    $payment = new Payment;
    $payment->owner_id = $owner_id;
    $payment->customer_id = $customer_id;
    $payment->last_four = $last_four;
    $payment->card_token = $result->customer->creditCards[0]->token;
    $payment->save();

Can I use this customer id for all subsequent trasactions ?

Braintree_Transaction::sale(array(
                            'amount' => $total,
                            'paymentMethodNonce' => $customer_id
                            ));

In this scenario, we would want the user to enter his card details only once and next time he creates another request even after a month or so, we should be able to bill him without he having to enter his card details.

Can I get a simple workflow as to how we can implement this and examples if any ?


Solution

  • Looks like you'd want to use the paymentMethodToken key for the Transaction.sale, with that token string as the value, to charge a specific customer's stored payment method.

    result = braintree.Transaction.sale({
        'amount' => $total,
        'paymentMethodToken' => 'token_string'
    })
    

    Therefore, for all subsequent transactions, you would want to use the paymentMethodToken and pass in that token string, as opposed to passing in the customer ID.