PageRenderTime 33ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/system/application/helpers/paypal_payment_helper.php

https://github.com/katzgrau/notes
PHP | 137 lines | 95 code | 21 blank | 21 comment | 20 complexity | 43c861ff59fa42b7e67a499985106372 MD5 | raw file
  1. <?php
  2. if ( ! function_exists('verify_and_retrieve_payment'))
  3. {
  4. // return an array with a boolean result and dictionary of the request response
  5. function verify_and_retrieve_payment()
  6. {
  7. $email = $_GET['ipn_email'];
  8. $header = "";
  9. $emailtext = "";
  10. // Read the post from PayPal and add 'cmd'
  11. $req = 'cmd=_notify-validate';
  12. if(function_exists('get_magic_quotes_gpc'))
  13. {
  14. $get_magic_quotes_exits = true;
  15. }
  16. foreach ($_POST as $key => $value)
  17. // Handle escape characters, which depends on setting of magic quotes
  18. {
  19. if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1)
  20. {
  21. $value = urlencode(stripslashes($value));
  22. }
  23. else
  24. {
  25. $value = urlencode($value);
  26. }
  27. $req .= "&$key=$value";
  28. } // Post back to PayPal to validate
  29. $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
  30. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  31. $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  32. $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
  33. // Process validation from PayPal
  34. if (!$fp)
  35. { // HTTP ERROR
  36. }
  37. else
  38. { // NO HTTP ERROR
  39. fputs ($fp, $header . $req);
  40. while (!feof($fp))
  41. {
  42. $res = fgets ($fp, 1024);
  43. if (strcmp ($res, "VERIFIED") == 0)
  44. {
  45. // TODO: // Check the payment_status is Completed
  46. // Check that txn_id has not been previously processed
  47. // Check that receiver_email is your Primary PayPal email
  48. // Check that payment_amount/payment_currency are correct
  49. // Process payment
  50. // If 'VERIFIED', send an email of IPN variables and values to the
  51. // specified email address
  52. $f_result = array( true, $_POST );
  53. //foreach ($_POST as $key => $value)
  54. //{
  55. //$emailtext .= $key . " = " .$value ."\n\n";
  56. //}
  57. //mail($email, "Live-VERIFIED IPN", $emailtext . "\n\n" . $req);
  58. }
  59. else if (strcmp ($res, "INVALID") == 0)
  60. { // If 'INVALID', send an email. TODO: Log for manual investigation.
  61. //foreach ($_POST as $key => $value)
  62. //{
  63. // $emailtext .= $key . " = " .$value ."\n\n";
  64. //}
  65. //mail($email, "Live-INVALID IPN", $emailtext . "\n\n" . $req);
  66. $f_result = array( false, $_POST );
  67. }
  68. }
  69. fclose ($fp);
  70. }
  71. return $f_result;
  72. }
  73. if ( ! function_exists('generate_purchase_button'))
  74. {
  75. function generate_purchase_button( $price, $site_name, $is_new = true, $item_title = false, $account_id = false)
  76. {
  77. $pay_to_email = config_item('pay_to_email');
  78. $item_number = create_item_number( $site_name, $is_new, $account_id );
  79. if ( ! $item_title ) $item_title = "<Site Name> 1 Year Subscription";
  80. return "
  81. <form action=\"http://www.paypal.com/cgi-bin/webscr\" method=\"post\">
  82. <!-- Identify your business so that you can collect the payments. -->
  83. <input type=\"hidden\" name=\"business\" value=\"$pay_to_email\">
  84. <!-- Specify a Buy Now button. -->
  85. <input type=\"hidden\" name=\"cmd\" value=\"_xclick\">
  86. <!-- Specify details about the item that buyers will purchase. -->
  87. <input type=\"hidden\" name=\"item_name\" value=\"$item_title\">
  88. <input type=\"hidden\" name=\"item_number\" value=\"$item_number\">
  89. <input type=\"hidden\" name=\"amount\" value=\"$price\">
  90. <input type=\"hidden\" name=\"currency_code\" value=\"USD\">
  91. <!-- Display the payment button. -->
  92. <input type=\"image\" name=\"submit\" border=\"0\" src=\"https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif\" alt=\"PayPal - The safer, easier way to pay online\">
  93. <img alt=\"\" border=\"0\" width=\"1\" height=\"1\" src=\"https://www.paypal.com/en_US/i/scr/pixel.gif\" >
  94. </form>";
  95. }
  96. }
  97. if ( ! function_exists( 'create_item_number' ) )
  98. {
  99. function create_item_number( $site_name, $is_new, $account_id = false )
  100. {
  101. if( $is_new )
  102. return "new:00:$site_name";
  103. else
  104. return "renew:$account_id:$site_name";
  105. }
  106. }
  107. if ( ! function_exists( 'info_from_item_number' ) )
  108. {
  109. function info_from_item_number( $item_number )
  110. {
  111. $parts = explode(':', $item_number );
  112. return array( 'type' => $parts[0], 'account_id' => $parts[1], 'site_name' => $parts[2] );
  113. }
  114. }
  115. }
  116. ?>