PageRenderTime 77ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/pay.paypal.php

https://github.com/klaus67/revsense-ad-server---version-2.5
PHP | 181 lines | 116 code | 30 blank | 35 comment | 5 complexity | aa0edee2b3addb3309f48f2b6a30fe3c MD5 | raw file
  1. <?
  2. //
  3. // AdRevenue Ad Management
  4. // pay.paypal.php
  5. //
  6. // (C) 2004 W3matter LLC
  7. // This is commercial software!
  8. // Please read the license at:
  9. // http://www.w3matter.com/license
  10. //
  11. // Paypal Payment Module
  12. // Encapsulates IPN and web fowarding
  13. class payment extends main
  14. {
  15. // This sets up the variables for settings
  16. function mod_vars()
  17. {
  18. $s = array();
  19. $s[] = array('name'=>'paypal_email', 'label'=>'Paypal&nbsp;Email', 'length'=>30);
  20. $s[] = array('name'=>'paypal_item', 'label'=>'Paypal&nbsp;Item', 'length'=>10);
  21. return($s);
  22. }
  23. function mod_info()
  24. {
  25. $s = array();
  26. $s[name] = "Paypal";
  27. $s[extern] = TRUE;
  28. $s[extern_description] = "Your Paypal IPN URL";
  29. return($s);
  30. }
  31. // Manages IPN
  32. function _default()
  33. {
  34. }
  35. // Get the payment form
  36. function form()
  37. {
  38. if($this->input->f[amount] >= $this->default[adrevenue][min_payment])
  39. $this->process();
  40. else
  41. $this->output->redirect("Please enter a valid amount", "index.php?section=account", 2);
  42. exit;
  43. }
  44. // Authorize the payment at the gateway
  45. function process()
  46. {
  47. // Secured
  48. $this->output->secure();
  49. // If we have no amount, then go back to the form
  50. if(!$this->input->f[amount])
  51. $this->form();
  52. // Save the payment attempt
  53. $tok = md5(uniqid(rand(), true));
  54. $i = array();
  55. $i[userid] = $_SESSION[user][id];
  56. $i[token] = $tok;
  57. $i[date] = time();
  58. $i[amount] = $this->input->f[amount];
  59. $this->db->insert("adrev_tokens", $i);
  60. // Get the last insert id
  61. $last = $this->db->getsql("SELECT id FROM adrev_tokens WHERE token=?", array($tok));
  62. $invoice = $last[0][id];
  63. // Setup the URL
  64. $url = "https://www.paypal.com/xclick/?";
  65. $i = array();
  66. $i[business] = $this->default[adrevenue][paypal_email];
  67. $i[receiver_email] = $this->default[adrevenue][paypal_email];
  68. $i[item_name] = $this->default[adrevenue][name] . " - " . lib_lang("Advertising");
  69. $i[item_number] = $this->default[adrevenue][paypal_item];
  70. $i[quantity] = 1;
  71. $i[amount] = str_replace(array('$',',',' '), "", $this->input->f[amount]);
  72. $i[page_style] = "PayPal";
  73. $i[no_shipping] = "1";
  74. $i['return'] = $this->default[adrevenue][hostname] . "index.php?section=pay&action=success";
  75. $i['cancel'] = $this->default[adrevenue][hostname] . "index.php?section=pay&action=failure";
  76. $i[no_note] = 1;
  77. $i[custom] = $tok;
  78. $i[currency_code] = $this->default[adrevenue][currency];
  79. $i[invoice] = $invoice;
  80. $i[lc] = $_SESSION[user][country];
  81. $i[notify_url] = $this->default[adrevenue][hostname] . "ipn.php";
  82. $query = array();
  83. while(list($key, $val) = each($i))
  84. {
  85. $query[] = "$key=" . urlencode($val);
  86. }
  87. $url .= implode("&", $query);
  88. // Forward payment to Paypal
  89. header("Location: $url");
  90. exit;
  91. }
  92. // This is an internal confirmation
  93. // Via Paypal
  94. function external()
  95. {
  96. // Our external system will call this
  97. // read the post from PayPal system and add 'cmd'
  98. $req = 'cmd=_notify-validate';
  99. foreach ($_POST as $key => $value)
  100. {
  101. $value = urlencode(stripslashes($value));
  102. $req .= "&$key=$value";
  103. }
  104. // post back to PayPal system to validate
  105. $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
  106. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  107. $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  108. $fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30);
  109. // Paypal Testing Site
  110. #$fp = fsockopen('www.eliteweaver.co.uk', 80, $errno, $errstr, 30);
  111. fputs($fp, $header . $req);
  112. fclose($fp);
  113. // Load the token record first, this prevents duplicate transactions
  114. $token = $_POST['custom'];
  115. $t = $this->db->getsql("SELECT * FROM adrev_tokens WHERE token=?", array($token));
  116. // assign posted variables to local variables
  117. if($_POST['custom'])
  118. {
  119. $i = array();
  120. $i[token] = $_POST['custom'];
  121. $i[txid] = $_POST['txn_id'];
  122. $i[status] = $_POST['payment_status'];
  123. $i[txndate] = time();
  124. $this->db->update("adrev_tokens", "token", $i[token], $i);
  125. }
  126. // Accept the payment only once, just in case IPN flakes out!
  127. if($_POST['payment_status'] == "Completed")
  128. {
  129. // Add the payment
  130. $j = array();
  131. $j[date] = time();
  132. $j[userid] = $t[0][userid];
  133. $j[description] = "Paypal Payment received - $i[status] - $i[txid]";
  134. $j[amount] = $t[0][amount];
  135. $this->db->insert("adrev_payments", $j);
  136. // Compute the balance and update it
  137. $uid = $t[0][userid];
  138. $b = $this->db->getsql("SELECT sum(amount) as spend FROM adrev_traffic WHERE userid=?", array($uid));
  139. $spend = $b[0][spend];
  140. // Grab payment history summary
  141. $h = $this->db->getsql("SELECT sum(amount) as paid FROM adrev_payments WHERE userid=?", array($uid));
  142. $paid = $h[0][paid];
  143. // Update balance
  144. $balance = $paid - $spend;
  145. $ts = time();
  146. $this->db->getsql("UPDATE adrev_users SET balance=?,balance_update=? WHERE id=?", array($balance, $ts, $uid));
  147. }
  148. return( TRUE);
  149. }
  150. }
  151. ?>