You are here

function commerce_stripe_capture_form_submit in Commerce Stripe 7.3

Submit handler: process a prior authorization capture via AIM.

File

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

Code

function commerce_stripe_capture_form_submit($form, &$form_state) {
  $transaction = $form_state['transaction'];
  $payment_method = $form_state['payment_method'];
  $dec_amount = number_format($form_state['values']['amount'], 2, '.', '');
  if (!commerce_stripe_load_library()) {
    drupal_set_message(t('Error capturing payment. Please contact shop admin to proceed.'), 'error');
    drupal_goto('admin/commerce/orders/' . $form_state['order']->order_id . '/payment');
  }

  // Set the amount that is to be captured. This amount has already been
  // validated, but needs to be converted to cents for Stripe.
  $capture_amount = commerce_currency_decimal_to_amount($dec_amount, $transaction->currency_code);
  $cparams = array(
    'amount' => $capture_amount,
  );
  try {
    $charge = Stripe\Charge::retrieve($transaction->remote_id);
    $response = $charge
      ->capture($cparams);
    $transaction->payload[REQUEST_TIME] = $response
      ->__toJSON();
    $transaction->remote_status = commerce_stripe_get_remote_status(NULL, $transaction, 'capture');
    $transaction->message .= '<br />' . t('Captured: @date', array(
      '@date' => format_date(REQUEST_TIME, 'short'),
    ));
    $transaction->message .= '<br />' . t('Captured Amount: @amount', array(
      '@amount' => $dec_amount,
    ));
    $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
    $transaction->amount = $capture_amount;
    commerce_payment_transaction_save($transaction);
  } catch (Exception $e) {
    drupal_set_message(t('We received the following error when trying to capture the transaction.'), 'error');
    drupal_set_message(check_plain($e
      ->getMessage()), 'error');
    watchdog('commerce_stripe', 'Following error received when processing card for capture @stripe_error', array(
      '@stripe_error' => $e
        ->getMessage(),
    ), WATCHDOG_NOTICE);
    $transaction->payload[REQUEST_TIME] = $e->json_body;
    $transaction->message = t('Capture processing error: @stripe_error', array(
      '@stripe_error' => $e
        ->getMessage(),
    ));
    $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
    $transaction->remote_status = 'FAILED';
    commerce_payment_transaction_save($transaction);
  }
  $form_state['redirect'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
}