You are here

function commerce_square_payment_method_submit_form in Commerce Square Connect 7

Square payment checkout pane form callback.

File

./commerce_square.module, line 383
Module file for Commerce Square.

Code

function commerce_square_payment_method_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
  $settings = variable_get('commerce_square_settings', commerce_square_default_settings()) + commerce_square_default_settings();
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $postal_code = NULL;
  if (!empty($order->commerce_customer_billing) && !empty($order_wrapper->commerce_customer_billing
    ->value()->commerce_customer_address)) {
    $address_value = $order_wrapper->commerce_customer_billing->commerce_customer_address
      ->value();
    if (!empty($address_value['postal_code'])) {
      $postal_code = $address_value['postal_code'];
    }
  }
  $mode = $payment_method['settings']['mode'];
  $element['#attached'] = array(
    'js' => array(
      array(
        'data' => array(
          'commerceSquare' => array(
            'applicationId' => $settings[$mode . '_app_id'],
            'postalCode' => $postal_code,
          ),
        ),
        'type' => 'setting',
      ),
    ),
  );
  _commerce_square_ensure_assets_attached($element, $mode);
  $element['#type'] = 'container';
  $element['#attributes']['class'][] = 'square-form';

  // Populated by the JS library.
  $element['payment_method_nonce'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => array(
        'square-nonce',
      ),
    ),
  );
  $element['card_type'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => array(
        'square-card-type',
      ),
    ),
  );
  $element['last4'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => array(
        'square-last4',
      ),
    ),
  );
  $element['exp_month'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => array(
        'square-exp-month',
      ),
    ),
  );
  $element['exp_year'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => array(
        'square-exp-year',
      ),
    ),
  );
  $element['number'] = array(
    '#type' => 'item',
    '#title' => t('Card number'),
    '#markup' => '<div id="square-card-number"></div>',
  );
  $element['details'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'credit-card-form__expiration',
      ),
    ),
  );
  $element['details']['expiration'] = array(
    '#type' => 'item',
    '#title' => t('Expiration'),
    '#markup' => '<div id="square-expiration-date"></div>',
  );
  $element['details']['cvv'] = array(
    '#type' => 'item',
    '#title' => t('CVV'),
    '#markup' => '<div id="square-cvv"></div>',
  );
  $element['details']['postal-code'] = array(
    '#type' => 'item',
    '#title' => t('Postal code'),
    '#markup' => '<div id="square-postal-code"></div>',
  );
  return $element;
}