Skip to content

Commit

Permalink
Merge pull request #3 from amorsent/6.x-2.x-custom
Browse files Browse the repository at this point in the history
Backport D7 PayPal IPN fixes #2790197 and #2800003
  • Loading branch information
dsnopek authored Jun 29, 2021
2 parents 077d36c + 1ec9d09 commit a037fbb
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions payment/uc_paypal/uc_paypal.pages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,40 @@ function uc_paypal_ipn() {
return;
}

$req = '';

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= $key .'='. $value .'&';
$post_fields[] = $key . '=' . urlencode(stripslashes($value));
}

$req .= 'cmd=_notify-validate';
$post_fields[] = 'cmd=_notify-validate';

if (variable_get('uc_paypal_wpp_server', '') == 'https://api-3t.paypal.com/nvp') {
$host = 'https://www.paypal.com/cgi-bin/webscr';
$host = 'https://ipnpb.paypal.com/cgi-bin/webscr';
}
else {
$host = variable_get('uc_paypal_wps_server', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
$host = variable_get('uc_paypal_wps_server', 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr');
}

$response = drupal_http_request($host, array(), 'POST', $req);

// TODO: Change this to property_exists when we have a PHP requirement >= 5.1.
if (array_key_exists('error', $response)) {
watchdog('uc_paypal', 'IPN failed with HTTP error @error, code @code.', array('@error' => $response->error, '@code' => $response->code), WATCHDOG_ERROR);
// Setup the cURL request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $post_fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$response = curl_exec($ch);

// Log any errors to the watchdog.
if ($error = curl_error($ch)) {
watchdog('uc_paypal', 'IPN failed with cURL error: @error', array('@error' => $error), WATCHDOG_ERROR);
return;
}

if (strcmp($response->data, 'VERIFIED') == 0) {
curl_close($ch);

if (strcmp($response, 'VERIFIED') == 0) {
watchdog('uc_paypal', 'IPN transaction verified.');

$duplicate = db_result(db_query("SELECT COUNT(*) FROM {uc_payment_paypal_ipn} WHERE txn_id = '%s' AND status <> 'Pending'", $txn_id));
Expand Down Expand Up @@ -158,7 +167,7 @@ function uc_paypal_ipn() {
break;
}
}
elseif (strcmp($response->data, 'INVALID') == 0) {
elseif (strcmp($response, 'INVALID') == 0) {
watchdog('uc_paypal', 'IPN transaction failed verification.', array(), WATCHDOG_ERROR);
uc_order_comment_save($order_id, 0, t('An IPN transaction failed verification for this order.'), 'admin');
}
Expand Down

0 comments on commit a037fbb

Please sign in to comment.