You are here

function commerce_braintree_express_checkout_form_alter in Commerce Braintree 7.3

Implements hook_form_alter().

File

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

Code

function commerce_braintree_express_checkout_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'commerce_checkout_form') === 0 && !empty($form['commerce_payment']) && !empty($form['buttons'])) {
    $order = $form_state['order'];

    // Only alter the form if express checkout is enabled and
    // available to this order.
    if (!_commerce_braintree_express_checkout_enabled($order)) {
      return;
    }
    $ec_payment_method = commerce_payment_method_instance_load(COMMERCE_BRAINTREE_EXPRESS_CHECKOUT_INSTANCE_ID);
    if (!empty($order->data['commerce_braintree_express_checkout']['nonce'])) {
      $nonce = $order->data['commerce_braintree_express_checkout']['nonce'];

      // If the order contains a valid nonce alter the payment form
      // to set Express Checkout as the payment option.
      if (commerce_braintree_express_checkout_validate_nonce($nonce)) {

        // Update the payment method label to show the PayPal account.
        $label = t('Pay with your PayPal account (@mail).', array(
          '@mail' => $order->data['commerce_braintree_express_checkout']['details']['email'],
        ));

        // Remove other payment options since the customer has already
        // selected to use Express Checkout payments.
        $form['commerce_payment']['payment_method']['#options'] = array(
          COMMERCE_BRAINTREE_EXPRESS_CHECKOUT_INSTANCE_ID => $label,
        );
      }
    }

    // Add the Express Checkout button to all payment check pages.
    $form['buttons'] += commerce_braintree_express_checkout_button_form($order, $ec_payment_method, array(
      'form_id' => $form_id,
    ));
  }
}