You are here

function commerce_paypal_ec_paypal_ipn_validate in Commerce PayPal 7.2

Payment method callback: validate an IPN based on receiver e-mail address, price, and other parameters as possible.

File

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

Code

function commerce_paypal_ec_paypal_ipn_validate($order, $payment_method, $ipn) {

  // Prepare a trimmed list of receiver e-mail addresses.
  if (!empty($payment_method['settings']['receiver_emails'])) {
    $receiver_emails = explode(',', $payment_method['settings']['receiver_emails']);
  }
  else {
    $receiver_emails = array();
  }
  foreach ($receiver_emails as $key => &$email) {
    $email = trim(strtolower($email));
  }

  // Return FALSE if the receiver e-mail does not match the one specified by
  // the payment method instance.
  if (!empty($ipn['receiver_email']) && !in_array(trim(strtolower($ipn['receiver_email'])), $receiver_emails)) {
    commerce_payment_redirect_pane_previous_page($order);
    watchdog('commerce_paypal_ec', 'IPN rejected: invalid receiver e-mail specified (@receiver_email); must match a receiver e-mail address configured in the payment method settings.', array(
      '@receiver_email' => $ipn['receiver_email'],
    ), WATCHDOG_NOTICE);
    return FALSE;
  }

  // Prepare the IPN data for inclusion in the watchdog message if enabled.
  $ipn_data = '';
  if (!empty($payment_method['settings']['ipn_logging']) && $payment_method['settings']['ipn_logging'] == 'full_ipn') {
    $ipn_data = '<pre>' . check_plain(print_r($ipn, TRUE)) . '</pre>';
  }

  // Log a message including the PayPal transaction ID if available.
  if (!empty($ipn['txn_id'])) {
    watchdog('commerce_paypal_ec', 'IPN validated for Order @order_number with ID @txn_id.!ipn_data', array(
      '@order_number' => $order->order_number,
      '@txn_id' => $ipn['txn_id'],
      '!ipn_data' => $ipn_data,
    ), WATCHDOG_NOTICE);
  }
  else {
    watchdog('commerce_paypal_ec', 'IPN validated for Order @order_number.!ipn_data', array(
      '@order_number' => $order->order_number,
      '!ipn_data' => $ipn_data,
    ), WATCHDOG_NOTICE);
  }
}