You are here

function commerce_stripe_refund_form_submit in Commerce Stripe 7.3

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

Submit callback for submitting refunds to Stripe.

File

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

Code

function commerce_stripe_refund_form_submit($form, &$form_state) {

  // Don't rely on form_state objects to be fresh.
  $order = commerce_order_load($form_state['order']->order_id);
  $transaction = commerce_payment_transaction_load($form_state['transaction']->transaction_id);
  $payment_method = $form_state['payment_method'];
  global $user;

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

  // Create the refund object.
  $data = array(
    'charge' => $transaction->remote_id,
    'amount' => commerce_currency_decimal_to_amount($form_state['values']['amount'], $transaction->currency_code),
    'reason' => $form_state['values']['reason'],
  );

  // 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'];
  }

  // Let modules alter the refund object to add attributes.
  drupal_alter('commerce_stripe_order_refund', $data, $form_state);
  try {
    Stripe\Stripe::setApiKey(trim($payment_method['settings']['secret_key']));
    $refund = Stripe\Refund::create($data);
    if (is_object($refund)) {

      // Copy the refund object into our own payload so we don't save API keys
      // included in the response object.
      $payload = array(
        'id' => $refund->id,
        'amount' => $refund->amount,
        'currency' => $refund->currency,
        'created' => $refund->created,
        'object' => $refund->object,
        'balance_transaction' => $refund->balance_transaction,
        'charge' => $refund->charge,
        'receipt_number' => $refund->receipt_number,
        'reason' => $refund->reason,
      );

      // Create the new commerce payment transation and set appropriate values.
      $refund_transaction = commerce_payment_transaction_new($transaction->payment_method, $order->order_id);
      $refund_transaction->instance_id = $payment_method['instance_id'];
      $refund_transaction->payload[REQUEST_TIME] = print_r($payload, TRUE);
      $refund_transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
      $refund_transaction->remote_id = $refund->id;
      $refund_transaction->message = t('Refund issued.');

      // Save data on the initial charge and flag this transaction as a refund.
      $refund_transaction->data = array(
        'stripe' => array(
          'stripe_charge' => array(
            'id' => $refund->id,
            'balance_transaction' => $refund->balance_transaction,
            'amount_refunded' => $refund->amount,
          ),
          'stripe_refund' => TRUE,
        ),
      );

      // Save the amount as a negative integer.
      $refund_transaction->amount = $refund->amount * -1;
      $refund_transaction->currency_code = strtoupper($refund->currency);
      commerce_payment_transaction_save($refund_transaction);

      // Inform the user of the success and redirect them back to payments.
      drupal_set_message(t('Refund processed successfully'));
      $form_state['redirect'] = 'admin/commerce/orders/' . $order->order_id . '/payment';
    }
  } catch (Exception $e) {
    drupal_set_message(t('The transaction could not be refunded. The error was: @error', array(
      '@error' => $e
        ->getMessage(),
    )), 'error');
  }
}