You are here

function commerce_stripe_refund_form in Commerce Stripe 7.3

Same name and namespace in other branches
  1. 7 includes/commerce_stripe.admin.inc \commerce_stripe_refund_form()

Form callback for processing refunds.

1 string reference to 'commerce_stripe_refund_form'
commerce_stripe_menu in ./commerce_stripe.module
Implements hook_menu().

File

includes/commerce_stripe.admin.inc, line 11
Provides admin forms and functions for commerce_stripe.

Code

function commerce_stripe_refund_form($form, &$form_state, $order, $transaction) {
  $form = array();
  $payment_method = commerce_payment_method_instance_load($transaction->instance_id);
  $form_state['order'] = $order;
  $form_state['transaction'] = $transaction;
  $form_state['payment_method'] = $payment_method;

  // Make sure the library is available.
  if (!commerce_stripe_load_library()) {
    drupal_set_message(t('Cannot load the Stripe PHP library'), 'error');
    return $form;
  }

  // Inject the Stripe Connect public and secret key if appropriate.
  if (module_exists('commerce_stripe_connect') && !empty($payment_method['settings']['use_connected_account']) && $payment_method['settings']['use_connected_account'] == 'site account') {
    $connect_settings = commerce_stripe_connect_get_settings();
    $payment_method['settings']['public_key'] = $connect_settings['connected_public_key'];
    $payment_method['settings']['secret_key'] = $connect_settings['connected_secret_key'];
  }

  // Make sure we can load the original charge object.
  try {
    \Stripe\Stripe::setApiKey(trim($payment_method['settings']['secret_key']));
    $charge = Stripe\Charge::retrieve($transaction->remote_id);
    $form_state['stripe_charge'] = $charge;
  } catch (Exception $e) {
    drupal_set_message(t('The original transaction could not be loaded. The error was: @error', array(
      '@error' => $e
        ->getMessage(),
    )), 'error');
    return $form;
  }

  // Calculate the amount left available for a refund.
  $amount_refunded = !empty($charge->amount_refunded) ? $charge->amount_refunded : 0;
  $remaining = $charge->amount - $amount_refunded;
  $form['amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Refund amount'),
    '#description' => t('Enter any amount to refund up to @txn_amount', array(
      '@txn_amount' => commerce_currency_format($remaining, $transaction->currency_code),
    )),
    '#required' => TRUE,
    '#size' => 8,
  );
  $options = array(
    'requested_by_customer' => t('Requested by customer'),
    'duplicate' => t('Duplicate'),
    'fraudulent' => t('Fraudulent'),
  );
  $form['reason'] = array(
    '#type' => 'select',
    '#title' => t('Refund reason'),
    '#description' => t('Select the most appropriate reason for the refund.'),
    '#options' => $options,
  );
  $form['actions'] = array(
    '#type' => 'container',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Process refund'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'button',
    '#value' => t('Cancel'),
  );
  return $form;
}