You are here

function commerce_paypal_checkout_redirect_form_validate in Commerce PayPal 7.2

Payment method callback: redirect form return validation.

File

modules/checkout/commerce_paypal_checkout.module, line 926
Implements PayPal Checkout in Drupal Commerce checkout.

Code

function commerce_paypal_checkout_redirect_form_validate($order, $payment_method) {
  $payment_method['settings'] += commerce_paypal_checkout_default_settings();

  // Check if the PayPal order ID is known, as well as the "flow".
  if (empty($order->data['commerce_paypal_checkout']['remote_id']) || !isset($order->data['commerce_paypal_checkout']['flow'])) {
    return FALSE;
  }
  $flow = $order->data['commerce_paypal_checkout']['flow'];
  $api_client = commerce_paypal_checkout_api_client($payment_method['settings']);
  if (!$api_client) {
    return FALSE;
  }
  $remote_id = $order->data['commerce_paypal_checkout']['remote_id'];
  try {
    $paypal_order = $api_client
      ->getOrder($remote_id);
  } catch (\Exception $exception) {
    watchdog_exception('commerce_paypal_checkout', $exception);
    return FALSE;
  }
  $order_total = field_get_items('commerce_order', $order, 'commerce_order_total', LANGUAGE_NONE);
  $paypal_amount = $paypal_order['purchase_units'][0]['amount'];
  $paypal_total = commerce_currency_decimal_to_amount($paypal_amount['value'], $paypal_amount['currency_code']);

  // Check the remote status, and that the PayPal total matches the order total.
  if (!in_array($paypal_order['status'], [
    'APPROVED',
    'SAVED',
  ]) || $paypal_total != $order_total[0]['amount'] || $paypal_amount['currency_code'] != $order_total[0]['currency_code']) {
    return FALSE;
  }

  // Store the intent for later reuse, it can't be updated, so no risk in
  // being out of sync.
  $order->data['commerce_paypal_checkout']['intent'] = strtolower($paypal_order['intent']);
  $payer = $paypal_order['payer'];

  // If the user is anonymous, add their PayPal e-mail to the order.
  if (empty($order->mail)) {
    $order->mail = $payer['email_address'];
  }

  // Create a billing information profile for the order with the available info.
  if (!empty($payment_method['settings']['update_billing_profiles'])) {
    commerce_paypal_checkout_customer_profile($order, 'billing', $paypal_order);
  }

  // If the shipping module exists on the site, create a shipping information
  // profile for the order with the available info.
  if (module_exists('commerce_shipping') && !empty($payment_method['settings']['update_shipping_profiles'])) {
    commerce_paypal_checkout_customer_profile($order, 'shipping', $paypal_order);
  }

  // Recalculate the price of products on the order in case taxes have
  // changed or prices have otherwise been affected.
  if ($flow == 'shortcut') {
    commerce_cart_order_refresh($order);
  }

  // Save the changes to the order.
  commerce_order_save($order);
  if ($flow == 'mark') {
    return commerce_paypal_checkout_do_payment($order, $payment_method);
  }
}