You are here

function commerce_paypal_ec_redirect_form_validate in Commerce PayPal 7.2

Payment method callback: redirect form return validation.

File

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

Code

function commerce_paypal_ec_redirect_form_validate($order, $payment_method) {
  $payment_method['settings'] += commerce_paypal_ec_default_settings();
  if (!empty($payment_method['settings']['ipn_logging']) && $payment_method['settings']['ipn_logging'] == 'full_ipn') {
    watchdog('commerce_paypal_ec', 'Customer returned from PayPal with the following POST data:!ipn_data', array(
      '!ipn_data' => '<pre>' . check_plain(print_r($_POST, TRUE)) . '</pre>',
    ), WATCHDOG_NOTICE);
  }

  // This may be an unnecessary step, but if for some reason the user does end
  // up returning at the success URL with a Failed payment, go back.
  if (!empty($_POST['payment_status']) && $_POST['payment_status'] == 'Failed') {
    return FALSE;
  }

  // Don't attempt to verify the Express Checkout details without a valid token.
  if (empty($order->data['commerce_paypal_ec']['token'])) {
    return FALSE;
  }

  // Build a name-value pair array to obtain buyer information from PayPal.
  $nvp = array(
    'METHOD' => 'GetExpressCheckoutDetails',
    'TOKEN' => $order->data['commerce_paypal_ec']['token'],
  );

  // Submit the API request to PayPal.
  $response = commerce_paypal_api_request($payment_method, $nvp, $order);

  // If the request failed, exit now with a failure message.
  if ($response['ACK'] == 'Failure') {
    return FALSE;
  }

  // Set the Payer ID used to finalize payment.
  $order->data['commerce_paypal_ec']['payerid'] = $response['PAYERID'];

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

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

  // 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_ec_customer_profile($order, 'shipping', $response, 'PAYMENTREQUEST_0_');
  }

  // Recalculate the price of products on the order in case taxes have
  // changed or prices have otherwise been affected.
  if ($order->data['commerce_paypal_ec']['flow'] == 'ec') {
    commerce_cart_order_refresh($order);
  }

  // Save the changes to the order.
  commerce_order_save($order);

  // If the customer completed payment using the Mark flow, then we should
  // attempt to process payment now and go back if it fails.
  if ($order->data['commerce_paypal_ec']['flow'] == 'mark') {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $charge = $order_wrapper->commerce_order_total
      ->value();

    // Attempt to process the payment.
    if (!commerce_paypal_ec_do_payment($payment_method, $order, $charge)) {
      return FALSE;
    }
  }
}