phpbraintree

PHP Braintree - create flow with Recurring Billing without using Subscriptions


I'd like not to create subscriptions but still be able to have the Recurring Billing approach in my SaaS software:

  1. Signup for a paid plan
  2. Apply Transaction Sale function
  3. Create braintree customer
  4. Save Payment Method Token in DB
  5. In the next month (whenever I want) use the Payment Method Token to create a new Transaction Sale

All of this without subscription feature. Does Braintree allow it?

Currently I'm having the following problem:

  1. The user signups for a paid plan
  2. The Transaction Sale is executed with the Nonce value
  3. I try to create the customer in Braintree using the same Nonce and I get the error "Cannot use a payment_method_nonce more than once"

The error makes sense, but how would I match the current user signup with the created customer in braintree in order to later use the Payment Method Token to re-use the Transaction Sale?

https://developer.paypal.com/braintree/docs/guides/customers#create-with-payment-method

// After the Transaction Sale is executed with success I try this:
$result = $gateway->customer()->create(
[
    'firstName' => $customer['name'],
    'company' => $customer['company'],
    'email' => $customer['email'],
    'paymentMethodNonce' => $customer['payment_method_nonce']
]);

This would return the Payment Method Token and I would be able to save in DB like this:

$db->braintree_customer_id = $braintreeCustomer->customer->id;
$db->braintree_payment_token = $braintreeCustomer->customer->paymentMethods[0]->token;
$db->save();

And in the next month, my software would be able to execute a payment using:

$result = $gateway->transaction()->sale(
[
    'amount' => $price,
    'paymentMethodToken' => $token, // <--- saved in DB
    'options' =>
    [
        'submitForSettlement' => true
    ]
]);

Solution

  • Solved.

    The answer to this question "All of this without subscription feature. Does Braintree allow it?" is Yes.

    And about the error "Cannot use a payment_method_nonce more than once", I had to create firstly the customer using the nonce and then execute the sale using the token.

    $result = $gateway->customer()->create(
    [
        'firstName' => $customer['name'],
        'company' => $customer['company'],
        'email' => $customer['email'],
        'paymentMethodNonce' => $customer['payment_method_nonce']
    ]);
    
    // We now execute the payment with the token returned
    $braintree->executePayment($result->customer->paymentMethods[0]->token, $total);
    

    Since I save the token in the DB, I'm able to execute the payment again in the next month whenever I want without user interference.