You are here

function commerce_square_capture_form_submit in Commerce Square Connect 7

Submit handler: process the void request.

File

includes/commerce_square.admin.inc, line 50
Provides admin menu callbacks for Commerce Square.

Code

function commerce_square_capture_form_submit($form, &$form_state) {
  $transaction = $form_state['transaction'];
  $client = SquareApi::createFromInstanceId($transaction->instance_id);
  try {
    $client
      ->capture($transaction);
    $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
    drupal_set_message(t('Transaction successfully captured.'));
  } catch (ApiException $e) {

    /** @var \SquareConnect\Model\VoidTransactionResponse $result */
    $result = $e
      ->getResponseObject();
    foreach ($result
      ->getErrors() as $error) {
      drupal_set_message(sprintf('%s %s %s', $error
        ->getCode(), $error
        ->getCategory(), $error
        ->getDetail()), 'error');
    }
  } catch (Exception $e) {
    drupal_set_message(t('There was an error capturing the transaction: @message', array(
      '@message' => $e
        ->getMessage(),
    )), 'error');
  }
  commerce_payment_transaction_save($transaction);
  $form_state['redirect'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
}