1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453:
<?php
if (!defined('SMF'))
die('No direct access...');
class paypal_display
{
public $title = 'PayPal';
public function getGatewaySettings()
{
global $txt;
$setting_data = array(
array(
'email', 'paypal_email',
'subtext' => $txt['paypal_email_desc'],
'size' => 60
),
array(
'email', 'paypal_additional_emails',
'subtext' => $txt['paypal_additional_emails_desc'],
'size' => 60
),
array(
'email', 'paypal_sandbox_email',
'subtext' => $txt['paypal_sandbox_email_desc'],
'size' => 60
),
);
return $setting_data;
}
public function gatewayEnabled()
{
global $modSettings;
return !empty($modSettings['paypal_email']);
}
public function fetchGatewayFields($unique_id, $sub_data, $value, $period, $return_url)
{
global $modSettings, $txt, $boardurl;
$return_data = array(
'form' => 'https://www.' . (!empty($modSettings['paidsubs_test']) ? 'sandbox.' : '') . 'paypal.com/cgi-bin/webscr',
'id' => 'paypal',
'hidden' => array(),
'title' => $txt['paypal'],
'desc' => $txt['paid_confirm_paypal'],
'submit' => $txt['paid_paypal_order'],
'javascript' => '',
);
$return_data['hidden']['business'] = $modSettings['paypal_email'];
$return_data['hidden']['item_name'] = $sub_data['name'] . ' ' . $txt['subscription'];
$return_data['hidden']['item_number'] = $unique_id;
$return_data['hidden']['currency_code'] = strtoupper($modSettings['paid_currency_code']);
$return_data['hidden']['no_shipping'] = 1;
$return_data['hidden']['no_note'] = 1;
$return_data['hidden']['amount'] = $value;
$return_data['hidden']['cmd'] = !$sub_data['repeatable'] ? '_xclick' : '_xclick-subscriptions';
$return_data['hidden']['return'] = $return_url;
$return_data['hidden']['a3'] = $value;
$return_data['hidden']['src'] = 1;
$return_data['hidden']['notify_url'] = $boardurl . '/subscriptions.php';
$return_data['hidden']['lc'] = !empty($txt['lang_paypal']) ? $txt['lang_paypal'] : 'US';
if ($sub_data['flexible'])
{
$return_data['hidden']['p3'] = 1;
$return_data['hidden']['t3'] = strtoupper(substr($period, 0, 1));
}
else
{
preg_match('~(\d*)(\w)~', $sub_data['real_length'], $match);
$unit = $match[1];
$period = $match[2];
$return_data['hidden']['p3'] = $unit;
$return_data['hidden']['t3'] = $period;
}
if (!empty($sub_data['repeatable']))
$return_data['javascript'] = '
document.write(\'<label for="do_paypal_recur"><input type="checkbox" name="do_paypal_recur" id="do_paypal_recur" checked onclick="switchPaypalRecur();">' . $txt['paid_make_recurring'] . '</label><br>\');
function switchPaypalRecur()
{
document.getElementById("paypal_cmd").value = document.getElementById("do_paypal_recur").checked ? "_xclick-subscriptions" : "_xclick";
}';
return $return_data;
}
}
class paypal_payment
{
private $return_data;
public function isValid()
{
global $modSettings;
if ((empty($modSettings['paidsubs_test']) && empty($modSettings['paypal_email'])) || (!empty($modSettings['paidsubs_test']) && empty($modSettings['paypal_sandbox_email'])))
return false;
if ((!isset($_POST['txn_type']) && !isset($_POST['payment_status'])) || (!isset($_POST['business']) && !isset($_POST['receiver_email'])))
return false;
if (!isset($_POST['business']))
$_POST['business'] = $_POST['receiver_email'];
if (empty($modSettings['paidsubs_test']) && strtolower($modSettings['paypal_sandbox_email']) != strtolower($_POST['business']) && (empty($modSettings['paypal_additional_emails']) || !in_array(strtolower($_POST['business']), explode(',', strtolower($modSettings['paypal_additional_emails'])))))
return false;
elseif (strtolower($modSettings['paypal_email']) != strtolower($_POST['business']) && (empty($modSettings['paypal_additional_emails']) || !in_array(strtolower($_POST['business']), explode(',', $modSettings['paypal_additional_emails']))))
return false;
return true;
}
public function precheck()
{
global $modSettings, $txt;
if (!isset($_POST['txn_type']))
$_POST['txn_type'] = '';
$requestString = 'cmd=_notify-validate';
foreach ($_POST as $k => $v)
$requestString .= '&' . $k . '=' . urlencode($v);
if (function_exists('curl_init') && $curl = curl_init((!empty($modSettings['paidsubs_test']) ? 'https://www.sandbox.' : 'https://www.') . 'paypal.com/cgi-bin/webscr'))
{
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDSIZE, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestString);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Host: www.' . (!empty($modSettings['paidsubs_test']) ? 'sandbox.' : '') . 'paypal.com',
'Connection: close'
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$this->return_data = curl_exec($curl);
curl_close($curl);
}
else
{
$header = 'POST /cgi-bin/webscr HTTP/1.1' . "\r\n";
$header .= 'content-type: application/x-www-form-urlencoded' . "\r\n";
$header .= 'Host: www.' . (!empty($modSettings['paidsubs_test']) ? 'sandbox.' : '') . 'paypal.com' . "\r\n";
$header .= 'content-length: ' . strlen($requestString) . "\r\n";
$header .= 'connection: close' . "\r\n\r\n";
if (!empty($modSettings['paidsubs_test']))
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
else
$fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30);
if (!$fp)
generateSubscriptionError($txt['paypal_could_not_connect']);
fputs($fp, $header . $requestString);
while (!feof($fp))
{
$this->return_data = fgets($fp, 1024);
if (strcmp(trim($this->return_data), 'VERIFIED') === 0)
break;
}
fclose($fp);
}
if (strcmp(trim($this->return_data), 'VERIFIED') !== 0)
exit;
if (strtolower($modSettings['paypal_email']) != strtolower($_POST['business']) && (empty($modSettings['paypal_additional_emails']) || !in_array(strtolower($_POST['business']), explode(',', strtolower($modSettings['paypal_additional_emails'])))))
exit;
if (!empty($_POST['item_number']) && strpos($_POST['item_number'], ' ') !== false)
$_POST['item_number'] = str_replace(' ', '+', $_POST['item_number']);
if ($this->isSubscription() && (empty($_POST['item_number']) || strpos($_POST['item_number'], '+') === false))
$this->_findSubscription();
if (strtolower($_POST['mc_currency']) !== strtolower($modSettings['paid_currency_code']))
exit;
if (empty($_POST['item_number']))
exit;
return explode('+', $_POST['item_number']);
}
public function isRefund()
{
if ($_POST['payment_status'] === 'Refunded' || $_POST['payment_status'] === 'Reversed' || $_POST['txn_type'] === 'Refunded' || ($_POST['txn_type'] === 'reversal' && $_POST['payment_status'] === 'Completed'))
return true;
else
return false;
}
public function isSubscription()
{
if (substr($_POST['txn_type'], 0, 14) === 'subscr_payment' && $_POST['payment_status'] === 'Completed')
return true;
else
return false;
}
public function isPayment()
{
if ($_POST['payment_status'] === 'Completed' && $_POST['txn_type'] === 'web_accept')
return true;
else
return false;
}
public function isCancellation()
{
if (substr($_POST['txn_type'], 0, 13) === 'subscr_cancel' || substr($_POST['txn_type'], 0, 10) === 'subscr_eot')
return true;
else
return false;
}
public function performCancel($subscription_id, $member_id, $subscription_info)
{
}
public function getCost()
{
return (isset($_POST['tax']) ? $_POST['tax'] : 0) + $_POST['mc_gross'];
}
public function close()
{
global $smcFunc, $subscription_id;
if ($_POST['txn_type'] == 'subscr_payment' && !empty($_POST['subscr_id']))
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}log_subscribed
SET vendor_ref = {string:vendor_ref}
WHERE id_sublog = {int:current_subscription}',
array(
'current_subscription' => $subscription_id,
'vendor_ref' => $_POST['subscr_id'],
)
);
}
exit();
}
private function _findSubscription()
{
global $smcFunc;
if (empty($_POST['subscr_id']))
return false;
$request = $smcFunc['db_query']('', '
SELECT id_member, id_subscribe
FROM {db_prefix}log_subscribed
WHERE vendor_ref = {string:vendor_ref}
LIMIT 1',
array(
'vendor_ref' => $_POST['subscr_id'],
)
);
if ($smcFunc['db_num_rows']($request) == 0)
{
if (!empty($_POST['payer_email']))
{
$smcFunc['db_free_result']($request);
$request = $smcFunc['db_query']('', '
SELECT ls.id_member, ls.id_subscribe
FROM {db_prefix}log_subscribed AS ls
INNER JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
WHERE mem.email_address = {string:payer_email}
LIMIT 1',
array(
'payer_email' => $_POST['payer_email'],
)
);
if ($smcFunc['db_num_rows']($request) === 0)
return false;
}
else
return false;
}
list ($member_id, $subscription_id) = $smcFunc['db_fetch_row']($request);
$_POST['item_number'] = $member_id . '+' . $subscription_id;
$smcFunc['db_free_result']($request);
}
}
?>