function commerce_braintree_refund_form_submit in Commerce Braintree 7.2
Same name and namespace in other branches
- 7.3 includes/commerce_braintree.admin.inc \commerce_braintree_refund_form_submit()
Submit handler: process a Braintree transaction refund.
File
- includes/
commerce_braintree.admin.inc, line 229 - Provides admin forms for commerce_braintree.
Code
function commerce_braintree_refund_form_submit($form, &$form_state) {
$transaction = $form_state['transaction'];
$amount = number_format($form_state['values']['amount'], 2, '.', '');
$order = $form_state['order'];
$payment_method = $form_state['payment_method'];
commerce_braintree_initialize($payment_method);
try {
$result = Braintree_Transaction::refund($transaction->remote_id, $amount);
} catch (Exception $ex) {
$result = NULL;
}
// Inform the user if there were any issues processing the refund.
if (empty($result) || empty($result->success)) {
$message = !empty($result->message) ? $result->message : t('No error provided from Braintree');
drupal_set_message(t('The payment transaction cannot be refunded at this time. @message', array(
'@message' => $message,
)), 'error');
}
// Inform the user of a successful void and update the payment transaction.
if (!empty($result) && !empty($result->success)) {
$refund_amount = commerce_currency_decimal_to_amount($amount, $transaction->currency_code);
drupal_set_message(t('Refund for @amount issued successfully', array(
'@amount' => commerce_currency_format($refund_amount, $transaction->currency_code),
)));
// Create a new transaction to record the refund.
$refund_transaction = commerce_payment_transaction_new($transaction->payment_method, $order->order_id);
$refund_transaction->instance_id = $payment_method['instance_id'];
$refund_transaction->remote_id = $result->transaction->id;
$refund_transaction->amount = $refund_amount * -1;
$refund_transaction->currency_code = $transaction->currency_code;
$refund_transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
$refund_transaction->remote_status = $result->transaction->status;
$refund_transaction->message = t('Credited to @remote_id.', array(
'@remote_id' => $transaction->remote_id,
));
// Save the refund transaction.
commerce_payment_transaction_save($refund_transaction);
}
$form_state['redirect'] = 'admin/commerce/orders/' . $order->order_id . '/payment';
}