braintreebraintree-sandbox

Class 'Transaction' not found while calling braintree refund


I am integrating braintree payments into my application. I have created transaction successfully and now working on refund functionality, refund() method is in 'Transaction' class so I try to call with 'Transaction::refund()' but it was not getting called instead it is showing the Class 'Transaction' not found error

My Code:

require_once('../assets/plugins/braintree/includes/braintree_init.php');
---------
---------
if(post('refund_mode') == 'braintree'){
  include_once '/var/www/vhosts/my_app_name/assets/plugins/braintree/vendor/braintree/braintree_php/lib/Braintree/Transaction.php'; 
  $result = Transaction::refund($braintree_transaction_id, $refund_amount);
  echo "<pre>"; print_r($result);exit;
}

Is this right way to call the refund() method, if not how to call the refund() method? Can anyone please help me what is my mistake?


Solution

  • Full disclosure, I work at Braintree. If you have any further questions, I recommend contacting support

    The syntax is fairly close to what you've typed, though it does depend on what version of Braintree's PHP SDK library you are using. In more recent versions, you will need to use instance methods, rather than class methods to perform a refund. If you are using a recent version of the SDK, your request may look something like this:

    $result = $gateway->transaction()->refund($braintree_transaction_id, $refund_amount);
    

    However, if you are using an older version of the SDK, your request would look something similar to this:

    $result = Braintree_Transaction::refund($braintree_transaction_id, $refund_amount);
    

    I recommend checking what version of the SDK you are using. If it is older, I've found it best to read about class methods vs instance methods to learn about the differences. Otherwise, referring to Braintree's developer documentation can prove helpful for structuring API requests.