Magento 2 Token – How to Capture Braintree Using Customer Token

braintreemagento2token

I enabled vault feature in Braintree.In frontend by default customer should store their credit card(force fully) at checkout.

For example, the customer places an order value with $100.

Now I captured amount by generating an invoice with capture online.

Now I want to capture $15 extra amount using customer Token.
Can you help me how to do programmatically?
Below code using txn_id but I need capture through the token.

Client will share order increment Id, Extra Amount to Capture in CSV file.
So using Order Increment Id I can get order id, order payment id.

1.how to get token associated to payment id?

2.How to charge extra amount using that token?

 \Magento\Braintree\Model\Adapter\BraintreeAdapter $brainTreeAdapter
    $result = $this->brainTreeAdapter->submitForSettlement($txnId, $amount);

Best Answer

Magento provides API to work with payment tokens. The PaymentTokenManagementInterface contains a list of methods to retrieve tokens:

  • getListByCustomerId
  • getByPaymentId
  • getByGatewayToken
  • getByPublicHash

The stored token can be used for to perform payment operations (see Braintree sale):

\Braintree\Transaction::sale([
    'paymentMethodToken' => 'your gateway token',
    'amount' => 'your amount'
]);

Magento already has implemented the adapter to communicate with Braintree and as an example, you can look how to implemented partial invoicing in Magento Braintree payment module. The BraintreeVaultCaptureCommand uses VaultCaptureDataBuilder.php to set payment gateway token for a sale request.

About your second question. You can perform partial invoicing only for a previously authorized amount and according to your example:

  • Customer places the order with $100.
  • You create an invoice in admin panel with $85 amount.
  • You can create another invoice with $15 amount.

You can't capture extra $15 amount if your first invoice was with $100 amount. Braintree does not allow to capture more amount than was authorized. So, if your extra capture more than authorized amount, you need to perform new authorization transaction with saved payment token.

Related Topic