You are here

function commerce_paypal_ec_order_form_submit in Commerce PayPal 7.2

Submit handler: redirect to PayPal Express Checkout.

1 string reference to 'commerce_paypal_ec_order_form_submit'
commerce_paypal_bml_order_form in modules/ec/commerce_paypal_ec.module
Displays a PayPal Credit button as a form that redirects to PayPal.

File

modules/ec/commerce_paypal_ec.module, line 597
Implements PayPal Express Checkout in Drupal Commerce checkout.

Code

function commerce_paypal_ec_order_form_submit($form, &$form_state) {

  // Determine the payment flow to use: Credit if that button was used or default
  // to the EC flow.
  if (isset($form_state['values']['paypal_bml'])) {
    $flow = 'bml';
  }
  else {
    $flow = 'ec';
  }

  // Update the order to reference the PayPal Express Checkout payment method.
  $payment_method = $form_state['payment_method'];
  $order = $form_state['order'];
  $order->data['payment_method'] = $payment_method['instance_id'];

  // Generate a payment redirect key.
  $order->data['payment_redirect_key'] = drupal_hash_base64(time());

  // Request a token from Express Checkout.
  $token = commerce_paypal_ec_set_express_checkout($payment_method, $order, $flow);

  // If we got one back...
  if (!empty($token)) {

    // Set the Express Checkout data array.
    $order->data['commerce_paypal_ec'] = array(
      'flow' => 'ec',
      'token' => $token,
      'payerid' => FALSE,
    );

    // Set the redirect to PayPal.
    $form_state['redirect'] = commerce_paypal_ec_checkout_url($payment_method['settings']['server'], $order->data['commerce_paypal_ec']['token']);

    // Update the order status to the payment redirect page.
    commerce_order_status_update($order, 'checkout_payment', FALSE, NULL, t('Customer clicked the Express Checkout button on the cart page.'));

    // Save the changes to the order data array.
    commerce_order_save($order);
  }
  else {

    // Otherwise show an error message and remain on the cart page.
    drupal_set_message(t('Redirect to PayPal Express Checkout failed. Please try again or contact an administrator to resolve the issue.'), 'error');
    $form_state['redirect'] = 'cart';
  }
}