PageRenderTime 66ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/modules/sale/payment/paypal/result_rec.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 163 lines | 140 code | 20 blank | 3 comment | 35 complexity | 22f5a52e3d51b4842605094d29aae790 MD5 | raw file
  1. <?if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true) die();?><?
  2. include(GetLangFileName(dirname(__FILE__)."/", "/payment.php"));
  3. if(!isset($GLOBALS["SALE_INPUT_PARAMS"]))
  4. $GLOBALS["SALE_INPUT_PARAMS"] = array();
  5. $req = "";
  6. if(strlen($_REQUEST['tx']) > 0) // PDT
  7. {
  8. $req = 'cmd=_notify-synch';
  9. $tx_token = $_REQUEST['tx'];
  10. $auth_token = CSalePaySystemAction::GetParamValue("IDENTITY_TOKEN");
  11. $req .= "&tx=".$tx_token."&at=".$auth_token;
  12. // post back to PayPal system to validate
  13. $header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
  14. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  15. $header .= "Content-Length: " . strlen($req) . "\r\n";
  16. $header .= "User-Agent: 1C-Bitrix\r\n\r\n";
  17. }
  18. elseif(strlen($_POST['txn_id']) > 0 && $_SERVER["REQUEST_METHOD"] == "POST") // IPN
  19. {
  20. $tx = trim($_POST["txn_id"]);
  21. $req = 'cmd=_notify-validate';
  22. foreach ($_POST as $key => $value)
  23. {
  24. $value = urlencode(stripslashes($value));
  25. $req .= "&$key=$value";
  26. }
  27. // post back to PayPal system to validate
  28. $header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
  29. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  30. $header .= "Content-Length: " . strlen($req) . "\r\n";
  31. $header .= "User-Agent: 1C-Bitrix\r\n\r\n";
  32. }
  33. if(strlen($req) > 0)
  34. {
  35. $domain = "";
  36. if(CSalePaySystemAction::GetParamValue("TEST") == "Y")
  37. $domain = "sandbox.";
  38. if(CSalePaySystemAction::GetParamValue("SSL_ENABLE") == "Y")
  39. $fp = fsockopen ("ssl://www.".$domain."paypal.com", 443, $errno, $errstr, 30);
  40. else
  41. $fp = fsockopen ("www.".$domain."paypal.com", 80, $errno, $errstr, 30);
  42. if($fp)
  43. {
  44. fputs ($fp, $header . $req);
  45. $res = "";
  46. $headerdone = false;
  47. while(!feof($fp))
  48. {
  49. $line = fgets ($fp, 1024);
  50. if(strcmp($line, "\r\n") == 0)
  51. $headerdone = true;
  52. elseif($headerdone)
  53. $res .= $line;
  54. }
  55. // parse the data
  56. $lines = explode("\n", $res);
  57. $keyarray = array();
  58. if(strcmp ($lines[0], "SUCCESS") == 0)
  59. {
  60. for ($i=1, $cnt = count($lines); $i < $cnt; $i++)
  61. {
  62. list($key,$val) = explode("=", $lines[$i]);
  63. $keyarray[urldecode($key)] = urldecode($val);
  64. }
  65. $strPS_STATUS_MESSAGE = "";
  66. $strPS_STATUS_MESSAGE .= "Name: ".$keyarray["first_name"]." ".$keyarray["last_name"]."; ";
  67. $strPS_STATUS_MESSAGE .= "Email: ".$keyarray["payer_email"]."; ";
  68. $strPS_STATUS_MESSAGE .= "Item: ".$keyarray["item_name"]."; ";
  69. $strPS_STATUS_MESSAGE .= "Amount: ".$keyarray["mc_gross"]."; ";
  70. $strPS_STATUS_DESCRIPTION = "";
  71. $strPS_STATUS_DESCRIPTION .= "Payment status - ".$keyarray["payment_status"]."; ";
  72. $strPS_STATUS_DESCRIPTION .= "Payment sate - ".$keyarray["payment_date"]."; ";
  73. $arOrder = CSaleOrder::GetByID($keyarray["custom"]);
  74. $arFields = array(
  75. "PS_STATUS" => "Y",
  76. "PS_STATUS_CODE" => "-",
  77. "PS_STATUS_DESCRIPTION" => $strPS_STATUS_DESCRIPTION,
  78. "PS_STATUS_MESSAGE" => $strPS_STATUS_MESSAGE,
  79. "PS_SUM" => $keyarray["mc_gross"],
  80. "PS_CURRENCY" => $keyarray["mc_currency"],
  81. "PS_RESPONSE_DATE" => Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANG))),
  82. "USER_ID" => $arOrder["USER_ID"],
  83. );
  84. $arFields["PAY_VOUCHER_NUM"] = $tx_token;
  85. $arFields["PAY_VOUCHER_DATE"] = Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANG)));
  86. if (IntVal($arOrder["PRICE"]) == IntVal($keyarray["mc_gross"])
  87. && ToLower($keyarray["receiver_email"]) == ToLower(CSalePaySystemAction::GetParamValue("BUSINESS"))
  88. && $keyarray["payment_status"] == "Completed"
  89. )
  90. CSaleOrder::PayOrder($arOrder["ID"], "Y");
  91. CSaleOrder::Update($arOrder["ID"], $arFields);
  92. $firstname = $keyarray['first_name'];
  93. $lastname = $keyarray['last_name'];
  94. $itemname = $keyarray['item_name'];
  95. $amount = $keyarray['mc_gross'];
  96. echo "<p><h3>".GetMessage("PPL_T1")."</h3></p>";
  97. echo "<b>".GetMessage("PPL_T2")."</b><br>\n";
  98. echo "<li>".GetMessage("PPL_T3").": $firstname $lastname</li>\n";
  99. echo "<li>".GetMessage("PPL_T4").": $itemname</li>\n";
  100. echo "<li>".GetMessage("PPL_T5").": $amount</li>\n";
  101. }
  102. elseif(strcmp ($res, "VERIFIED") == 0)
  103. {
  104. $strPS_STATUS_MESSAGE = "";
  105. $strPS_STATUS_MESSAGE .= GetMessage("PPL_T3").": ".$_POST["first_name"]." ".$_POST["last_name"]."; ";
  106. $strPS_STATUS_MESSAGE .= "Email: ".$_POST["payer_email"]."; ";
  107. $strPS_STATUS_MESSAGE .= GetMessage("PPL_T4").": ".$_POST["item_name"]."; ";
  108. $strPS_STATUS_MESSAGE .= GetMessage("PPL_T5").": ".$_POST["mc_gross"]."; ";
  109. $strPS_STATUS_DESCRIPTION = "";
  110. $strPS_STATUS_DESCRIPTION .= "Payment status - ".$_POST["payment_status"]."; ";
  111. $strPS_STATUS_DESCRIPTION .= "Payment sate - ".$_POST["payment_date"]."; ";
  112. $arOrder = CSaleOrder::GetByID($_POST["custom"]);
  113. $arFields = array(
  114. "PS_STATUS" => "Y",
  115. "PS_STATUS_CODE" => "-",
  116. "PS_STATUS_DESCRIPTION" => $strPS_STATUS_DESCRIPTION,
  117. "PS_STATUS_MESSAGE" => $strPS_STATUS_MESSAGE,
  118. "PS_SUM" => $_POST["mc_gross"],
  119. "PS_CURRENCY" => $_POST["mc_currency"],
  120. "PS_RESPONSE_DATE" => Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANG))),
  121. "USER_ID" => $arOrder["USER_ID"],
  122. );
  123. $arFields["PAY_VOUCHER_NUM"] = $tx;
  124. $arFields["PAY_VOUCHER_DATE"] = Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANG)));
  125. if (IntVal($arOrder["PRICE"]) == IntVal($_POST["mc_gross"])
  126. && ToLower($_POST["receiver_email"]) == ToLower(CSalePaySystemAction::GetParamValue("BUSINESS"))
  127. && $_POST["payment_status"] == "Completed"
  128. && strlen($arOrder["PAY_VOUCHER_NUM"]) <= 0
  129. && $arOrder["PAY_VOUCHER_NUM"] != $tx
  130. )
  131. CSaleOrder::PayOrder($arOrder["ID"], "Y");
  132. if(strlen($arOrder["PAY_VOUCHER_NUM"]) <= 0 || $arOrder["PAY_VOUCHER_NUM"] != $tx)
  133. CSaleOrder::Update($arOrder["ID"], $arFields);
  134. }
  135. else
  136. echo "<p>".GetMessage("PPL_I1")."</p>";
  137. }
  138. else
  139. echo "<p>".GetMessage("PPL_I2")."</p>";
  140. fclose ($fp);
  141. }
  142. ?>
  143. <?=GetMessage("PPL_I3")?><br /><br /><?=GetMessage("PPL_I4")?>