You are here

function commerce_braintree_express_checkout_button_form in Commerce Braintree 7.3

Builds the Express Checkout form elements.

Form elements are built here are used on the cart and checkout forms.

Parameters

$order:

$payment_method:

$context:

Return value

array

2 calls to commerce_braintree_express_checkout_button_form()
commerce_braintree_express_checkout_form_alter in modules/commerce_braintree_express_checkout/commerce_braintree_express_checkout.module
Implements hook_form_alter().
commerce_braintree_express_checkout_form_views_form_commerce_cart_form_default_alter in modules/commerce_braintree_express_checkout/commerce_braintree_express_checkout.module
Implements hook_form_FORM_ID_alter().

File

modules/commerce_braintree_express_checkout/commerce_braintree_express_checkout.module, line 252
Provides integration PayPal Express Checkout for Braintree.

Code

function commerce_braintree_express_checkout_button_form($order, $payment_method, $context) {
  $form = array();

  // Make sure the payment method is available.
  if (empty($payment_method)) {
    return $form;
  }
  commerce_braintree_initialize($payment_method);
  $form['#attached']['library'][] = array(
    'commerce_braintree_express_checkout',
    'braintree.expresscheckout',
  );
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $amount = $order_wrapper->commerce_order_total
    ->value();

  // Determine the correct submit handler for this form.
  if (!empty($context['form_id'])) {
    if (strpos($context['form_id'], 'commerce_checkout_form') === 0) {
      $submit_selector = '.checkout-continue';
    }
    if ($context['form_id'] == 'views_form_commerce_cart_form_default') {
      $submit_selector = '#edit-checkout';
    }
  }

  // Set up an alterable array of JS settings that will
  // be used in js/commerce_braintree_express_checkout.js.
  $js_settings = array(
    'submitSelector' => !empty($submit_selector) ? $submit_selector : '[name=op]',
    'drupalForm' => !empty($context['form_id']) ? $context['form_id'] : '',
    'environment' => $payment_method['settings']['environment'],
    'orderStatus' => $order->status,
    'buttonId' => 'commerce-braintree-express-checkout-button',
    'nonceSelector' => 'input[name="commerce_payment[payment_details][nonce]"]',
    'payloadInput' => 'commerce_braintree_payload',
    'instanceId' => COMMERCE_BRAINTREE_EXPRESS_CHECKOUT_INSTANCE_ID,
    'buttonStyle' => array(
      'size' => 'small',
      'color' => 'gold',
      'shape' => 'pill',
      'label' => 'checkout',
    ),
    'options' => array(
      'authorization' => Braintree_ClientToken::generate(),
    ),
    'createPaymentOptions' => array(
      'flow' => 'checkout',
      'amount' => commerce_currency_amount_to_decimal($amount['amount'], $amount['currency_code']),
      'currency' => $amount['currency_code'],
      'enableShippingAddress' => TRUE,
    ),
  );

  // If a shipping address exists on the order, pass it to PayPal.
  if (module_exists('commerce_shipping') && !empty($order->commerce_customer_shipping)) {
    try {
      $address = $order_wrapper->commerce_customer_shipping->commerce_customer_address
        ->value();
      $js_settings['createPaymentOptions'] += array(
        'shippingAddressEditable' => FALSE,
        'shippingAddressOverride' => array(
          'recipientName' => $address['name_line'],
          'line1' => $address['thoroughfare'],
          'line2' => $address['premise'],
          'city' => $address['locality'],
          'countryCode' => $address['country'],
          'postalCode' => $address['postal_code'],
          'state' => $address['administrative_area'],
        ),
      );
    } catch (Exception $ex) {
      watchdog('commerce_braintree_express_checkout', 'Unable to access commerce customer shipping address');
    }
  }

  // Allow other modules to alter the JS settings.
  drupal_alter('commerce_braintree_express_checkout_js', $js_settings, $payment_method);

  // Add Express Checkout JS settings object.
  $form['#attached']['js'][] = array(
    'data' => array(
      'commerceBraintreeExpressCheckout' => $js_settings,
    ),
    'type' => 'setting',
  );

  // Create a DOM element for the EC button.
  $form['commerce_braintree_ec_button'] = array(
    '#markup' => '<div id="' . $js_settings['buttonId'] . '"></div>',
    '#weight' => !empty($context['form_id']) && $context['form_id'] == 'views_form_commerce_cart_form_default' ? 10 : -1,
  );
  $form['commerce_braintree_payload'] = array(
    '#type' => 'hidden',
  );
  return $form;
}