You are here

function commerce_paypal_wps_paypal_ipn_validate in Commerce PayPal 7.2

Same name and namespace in other branches
  1. 7 modules/wps/commerce_paypal_wps.module \commerce_paypal_wps_paypal_ipn_validate()

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

File

modules/wps/commerce_paypal_wps.module, line 227
Implements PayPal Website Payments Standard in Drupal Commerce checkout.

Code

function commerce_paypal_wps_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();
  }

  // Add the business e-mail address to the list of addresses.
  $receiver_emails[] = $payment_method['settings']['business'];
  foreach ($receiver_emails as $key => &$email) {
    $email = trim(strtolower($email));
  }

  // Return FALSE if the receiver e-mail does not match one specified by the
  // payment method instance.
  if (!in_array(trim(strtolower($ipn['receiver_email'])), $receiver_emails)) {
    commerce_payment_redirect_pane_previous_page($order);
    watchdog('commerce_paypal_wps', 'IPN rejected: invalid receiver e-mail specified (@receiver_email); must match the primary e-mail address on the PayPal account.', 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_wps', '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_wps', 'IPN validated for Order @order_number.!ipn_data', array(
      '@order_number' => $order->order_number,
      '!ipn_data' => $ipn_data,
    ), WATCHDOG_NOTICE);
  }
}