Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
1,492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.09 KB | None | 0 0
  1. <?php
  2.  
  3. $loggedUser = $_SESSION["id"];
  4.  
  5. $date = getdate();
  6.  
  7. $day = $date["mday"];
  8. $month = $date["mon"];
  9. $year = $date["year"];
  10.  
  11. //PayPal variables
  12. $paypalURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
  13. $paypalID = 'facilitator@hotmail.com';
  14. $successURL = 'http://prueba.com/success';
  15. $cancelURL = 'http://prueba.com/cancel';
  16. $notifyURL = 'http://prueba.com/paypal_ipn';
  17.  
  18. $itemName = 'Suscripcion 1!';
  19. $itemName2 = 'Suscripcion 2!';
  20. $itemName3 = 'Suscripcion 3!';
  21. $itemNumber = 'MS/'.$day.'/'.$month.'/'.$year.'/'.$loggedUser;
  22.  
  23. //subscription price for one month
  24. $itemPrice = 0.02;
  25.  
  26. <form action="<?php echo $paypalURL; ?>" method="post">
  27. <!-- identify your business so that you can collect the payments -->
  28. <input type="hidden" name="business" value="<?php echo $paypalID; ?>">
  29. <!-- specify a subscriptions button. -->
  30. <input type="hidden" name="cmd" value="_xclick-subscriptions">
  31. <!-- specify details about the subscription that buyers will purchase -->
  32. <input type="hidden" name="item_name" value="<?php echo $itemName; ?>">
  33. <input type="hidden" name="item_number" value="<?php echo $itemNumber; ?>">
  34. <input type="hidden" name="currency_code" value="MXN">
  35. <input type="hidden" name="a3" id="paypalAmt" value="<?php echo $itemPrice; ?>">
  36. <input type="hidden" name="p3" id="paypalValid" value="1">
  37. <input type="hidden" name="t3" value="M">
  38. <!-- custom variable user ID -->
  39. <input type="hidden" name="custom" value="<?php echo $loggedUser; ?>">
  40. <!-- specify urls -->
  41. <input type="hidden" name="cancel_return" value="<?php echo $cancelURL; ?>">
  42. <input type="hidden" name="return" value="<?php echo $successURL; ?>">
  43. <input type="hidden" name="notify_url" value="<?php echo $notifyURL; ?>">
  44. <!-- display the payment button -->
  45. <input class="paypal_button" type="submit" value="Comprar Suscripción">
  46. </form>
  47.  
  48. http://exampledomain.com/success?auth=AwayQdhjzADAexJ4X-hVqtlSrIzEW1KUFaLNIMlJg8Qhd6LkVXizKvsoahKvZkQN4xe1MjohmDqn3NaFS-lTsBQ&form_charset=UTF-8
  49.  
  50. <?php
  51.  
  52. /*
  53. * Read POST data
  54. * reading posted data directly from $_POST causes serialization
  55. * issues with array data in POST.
  56. * Reading raw POST data from input stream instead.
  57. */
  58. $raw_post_data = file_get_contents('php://input');
  59. $raw_post_array = explode('&', $raw_post_data);
  60. $myPost = array();
  61. foreach ($raw_post_array as $keyval) {
  62. $keyval = explode ('=', $keyval);
  63. if (count($keyval) == 2)
  64. $myPost[$keyval[0]] = urldecode($keyval[1]);
  65. }
  66.  
  67. // Read the post from PayPal system and add 'cmd'
  68. $req = 'cmd=_notify-validate';
  69. if(function_exists('get_magic_quotes_gpc')) {
  70. $get_magic_quotes_exists = true;
  71. }
  72. foreach ($myPost as $key => $value) {
  73. if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
  74. $value = urlencode(stripslashes($value));
  75. } else {
  76. $value = urlencode($value);
  77. }
  78. $req .= "&$key=$value";
  79. }
  80.  
  81. /*
  82. * Post IPN data back to PayPal to validate the IPN data is genuine
  83. * Without this step anyone can fake IPN data
  84. */
  85. $paypalURL = "https://ipnpb.sandbox.paypal.com/cgi-bin/webscr";
  86. $ch = curl_init($paypalURL);
  87. if ($ch == FALSE) {
  88. return FALSE;
  89. }
  90. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  91. curl_setopt($ch, CURLOPT_POST, 1);
  92. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  93. curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
  94. curl_setopt($ch, CURLOPT_SSLVERSION, 6);
  95. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  96. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  97. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  98.  
  99. // Set TCP timeout to 30 seconds
  100. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  101. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close', 'User-Agent: company-name'));
  102. $res = curl_exec($ch);
  103.  
  104. /*
  105. * Inspect IPN validation result and act accordingly
  106. * Split response headers and payload, a better way for strcmp
  107. */
  108. $tokens = explode("rnrn", trim($res));
  109. $res = trim(end($tokens));
  110. if (strcmp($res, "VERIFIED") == 0 || strcasecmp($res, "VERIFIED") == 0) {
  111. //Include DB configuration file
  112. //Database credentials
  113. $dbHost = 'localhost';
  114. $dbUsername = 'User';
  115. $dbPassword = 'Pass';
  116. $dbName = 'Name';
  117.  
  118. //Connect with the database
  119. $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
  120.  
  121. //Display error if failed to connect
  122. if ($db->connect_errno) {
  123. printf("Connect failed: %sn", $db->connect_error);
  124. exit();
  125. }
  126.  
  127. $unitPrice = 25;
  128.  
  129. //Payment data
  130. $subscrid = $_POST['subscr_id'];
  131. $payerwemail = $_POST['payer_email'];
  132. $itemnumber = $_POST['item_number'];
  133. $txnid = $_POST['txn_id'];
  134. $paymentgross = $_POST['mc_gross'];
  135. $currencycode = $_POST['mc_currency'];
  136. $paymentstatus = $_POST['payment_status'];
  137. $custom = $_POST['custom'];
  138. $subscrmonth = ($payment_gross/$unitPrice);
  139. $subscrdays = ($subscr_month*30);
  140. $subscrdate_from = date("Y-m-d H:i:s");
  141. $subscrdateto = date("Y-m-d H:i:s", strtotime($subscrdatefrom. ' + '.$subscrdays.' days'));
  142.  
  143. if(!empty($txn_id)){
  144. //Check if subscription data exists with the same TXN ID.
  145. $prevPayment = $db->query("SELECT id FROM usersubscriptions WHERE txnid = '".$txnid."'");
  146. if($prevPayment->num_rows > 0){
  147. exit();
  148. }else{
  149. //Insert tansaction data into the database
  150. $insert = $db->query("INSERT INTO usersubscriptions(userid,validity,validfrom,validto,itemnumber,txnid,paymentgross,currencycode,subscrid,paymentstatus,payerwemail) VALUES('".$custom."','".$subscrmonth."','".$subscrdatefrom."','".$subscrdateto."','".$itemnumber."','".$txnid."','".$paymentgross."','".$currencycode."','".$subscrid."','".$paymentstatus."','".$payerwemail."')");
  151.  
  152. //Update subscription id in users table
  153. if($insert){
  154. $subscriptionid = $db->insertid;
  155. $update = $db->query("UPDATE users SET subscriptionid = {$subscriptionid} WHERE id = {$custom}");
  156. }
  157. }
  158. }
  159. }
  160. die;
  161.  
  162. <?php
  163. //Include DB configuration file
  164. include 'dbconnect.php';
  165.  
  166. if(!empty($_GET['item_number']) && !empty($_GET['tx']) && !empty($_GET['amt']) && $_GET['st'] == 'Completed'){
  167. //get transaction information from query string
  168. $item_number = $_GET['item_number'];
  169. $txn_id = $_GET['tx'];
  170. $payment_gross = $_GET['amt'];
  171. $currency_code = $_GET['cc'];
  172. $payment_status = $_GET['st'];
  173. $custom = $_GET['cm'];
  174.  
  175. //Check if subscription data exists with the TXN ID
  176. $prevPaymentResult = $db->query("SELECT * FROM usersubscriptions WHERE txnid = '".$txn_id."'");
  177.  
  178. if($prevPaymentResult->num_rows > 0){
  179. //get subscription info from database
  180. $paymentRow = $prevPaymentResult->fetch_assoc();
  181.  
  182. //prepare subscription html to display
  183. $phtml = '<h5 class="success">Thanks for payment, your payment was successful. Payment details are given below.</h5>';
  184. $phtml .= '<div class="paymentInfo">';
  185. $phtml .= '<p>Payment Reference Number: <span>MS'.$paymentRow['id'].'</span></p>';
  186. $phtml .= '<p>Transaction ID: <span>'.$paymentRow['txn_id'].'</span></p>';
  187. $phtml .= '<p>Paid Amount: <span>'.$paymentRow['payment_gross'].' '.$paymentRow['currency_code'].'</span></p>';
  188. $phtml .= '<p>Validity: <span>'.$paymentRow['valid_from'].' to '.$paymentRow['valid_to'].'</span></p>';
  189. $phtml .= '</div>';
  190. }else{
  191. $phtml = '<h5 class="error">Your payment was unsuccessful, please try again.</h5>';
  192. }
  193. }elseif(!empty($_GET['item_number']) && !empty($_GET['tx']) && !empty($_GET['amt']) && $_GET['st'] != 'Completed'){
  194. $phtml = '<h5 class="error">Your payment was unsuccessful, please try again.</h5>';
  195. }
  196. ?>
  197. <!DOCTYPE html>
  198. <html>
  199. <head>
  200. <title>PayPal Subscriptions Payment Payment Status</title>
  201. <meta charset="utf-8">
  202. </head>
  203. <body>
  204. <div class="container">
  205. <h1>PayPal Subscriptions Payment Status</h1>
  206. <!-- render subscription details -->
  207. <?php echo !empty($phtml)?$phtml:''; ?>
  208. </body>
  209. </html>
  210.  
  211. <?php
  212.  
  213. $nombre = $_SESSION["name"];
  214. $apellido = $_SESSION["lastname"];
  215. $nombreCompleto = $nombre.' '.$apellido;
  216.  
  217. $subscr_date_from = $this->issetCheck($post,'subscr_date');
  218. $subscr_days = 30;
  219. $subscr_date_to = date("Y-m-d H:i:s", strtotime($subscr_date_from. ' + '.$subscr_days.' days'));
  220.  
  221. class PayPal_IPN{
  222. function infotuts_ipn($im_debut_ipn) {
  223.  
  224. define('SSL_P_URL', 'https://www.paypal.com/cgi-bin/webscr');
  225. define('SSL_SAND_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
  226. $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
  227. if (!preg_match('/paypal.com$/', $hostname)) {
  228. $ipn_status = 'Validation post isn't from PayPal';
  229. if ($im_debut_ipn == true) {
  230. // mail test
  231. }
  232.  
  233. return false;
  234. }
  235.  
  236. // parse the paypal URL
  237. $paypal_url = ($_REQUEST['test_ipn'] == 1) ? SSL_SAND_URL : SSL_P_URL;
  238. $url_parsed = parse_url($paypal_url);
  239.  
  240. $post_string = '';
  241. foreach ($_REQUEST as $field => $value) {
  242. $post_string .= $field . '=' . urlencode(stripslashes($value)) . '&';
  243. }
  244. $post_string.="cmd=_notify-validate"; // append ipn command
  245. // get the correct paypal url to post request to
  246. $paypal_mode_status = $im_debut_ipn; //get_option('im_sabdbox_mode');
  247. if ($paypal_mode_status == true){
  248. $fp = fsockopen('ssl://www.sandbox.paypal.com', "443", $err_num, $err_str, 60);
  249. } else{
  250. $fp = fsockopen('ssl://www.paypal.com', "443", $err_num, $err_str, 60);
  251. }
  252.  
  253. $ipn_response = '';
  254.  
  255. if (!$fp) {
  256. // could not open the connection. If loggin is on, the error message
  257. // will be in the log.
  258. $ipn_status = "fsockopen error no. $err_num: $err_str";
  259. if ($im_debut_ipn == true) {
  260. echo 'fsockopen fail';
  261. }
  262. return false;
  263. } else {
  264. // Post the data back to paypal
  265. fputs($fp, "POST $url_parsed[path] HTTP/1.1rn");
  266. fputs($fp, "Host: $url_parsed[host]rn");
  267. fputs($fp, "Content-type: application/x-www-form-urlencodedrn");
  268. fputs($fp, "Content-length: " . strlen($post_string) . "rn");
  269. fputs($fp, "Connection: closernrn");
  270. fputs($fp, $post_string . "rnrn");
  271.  
  272. // loop through the response from the server and append to variable
  273. while (!feof($fp)) {
  274. $ipn_response .= fgets($fp, 1024);
  275. }
  276. fclose($fp); // close connection
  277. }
  278.  
  279. // Invalid IPN transaction. Check the $ipn_status and log for details.
  280. if (!preg_match("/VERIFIED/s", $ipn_response)) {
  281. $ipn_status = 'IPN Validation Failed';
  282.  
  283. if ($im_debut_ipn == true) {
  284. echo 'Validation fail';
  285. print_r($_REQUEST);
  286. }
  287. return false;
  288. } else {
  289. $ipn_status = "IPN VERIFIED";
  290. if ($im_debut_ipn == true) {
  291. echo 'SUCCESS';
  292.  
  293. }
  294.  
  295. return true;
  296. }
  297. }
  298.  
  299. function ipn_response($request){
  300. mail("clientes@amoriadate.com","My subject",print_r($request,true));
  301. $im_debut_ipn=true;
  302. if ($this->infotuts_ipn($im_debut_ipn)) {
  303.  
  304. // if paypal sends a response code back let's handle it
  305. if ($im_debut_ipn == true) {
  306. $sub = 'PayPal IPN Debug Email Main';
  307. $msg = print_r($request, true);
  308. $aname = 'infotuts';
  309. //mail send
  310. }
  311.  
  312. // process the membership since paypal gave us a valid +
  313. $this->insert_data($request);
  314. }
  315. }
  316.  
  317. function issetCheck($post,$key){
  318. if(isset($post[$key])){
  319. $return=$post[$key];
  320. } else {
  321. $return='';
  322. }
  323. return $return;
  324. }
  325.  
  326. function insert_data($request){
  327. require_once('dbconnect.php');
  328. $post=$request;
  329.  
  330. $datos = array(
  331. $idUser = $_SESSION["id"],
  332. $suscriptorId = $this->issetCheck($post,'subscr_id'),
  333. $name = $nombreCompleto,
  334. $nombrePlan = $this->issetCheck($post,'item_name'),
  335. $payerStatus = $this->issetCheck($post,'payer_status'),
  336. $payer_email = $this->issetCheck($post,'payer_email'),
  337. $payerId = $this->issetCheck($post,'payer_id'),
  338. $cantidad = $this->issetCheck($post,'mc_amount3'),
  339. $periodo = $this->issetCheck($post,'period3'),
  340. $diaSuscripcion = $this->issetCheck($post,'subscr_date'),
  341. $finalizaEn = $subscr_date_to,
  342. $autorizacion = $this->issetCheck($post,'auth')
  343. );
  344.  
  345. $respuestaPago = ControladorPago::ctrCrearPago($datos);
  346.  
  347. if ($respuestaPago = "ok") {
  348. $phtml = '<h5 class="error">Tu pago se ha generado correctamente.</h5>';
  349. } else {
  350. echo "eror";
  351. }
  352. }
  353.  
  354. $obj = New PayPal_IPN();
  355. $obj -> ipn_response($_REQUEST);
  356. }
  357.  
  358. ?>
  359.  
  360. [11-Jul-2018 20:32:12 UTC] PHP Parse error: syntax error, unexpected '$obj'
  361. (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) in
  362. /home/ntkoeqcqx3ss/prueba/vista/modulos/ipn.php on line 144
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement