/system/application/controllers/api/shop.php

https://github.com/geekbuntu/Microweber · PHP · 170 lines · 110 code · 38 blank · 22 comment · 21 complexity · 8ce741ad7a95df18094bf0180ac96789 MD5 · raw file

  1. <?php
  2. class Shop extends Controller {
  3. function __construct() {
  4. parent::Controller ();
  5. require_once (APPPATH . 'controllers/default_constructor.php');
  6. // require_once (APPPATH . 'controllers/api/default_constructor.php');
  7. }
  8. function ok() {
  9. }
  10. function ipn() {
  11. $email = url_param ( 'ipn_email' );
  12. $header = "";
  13. $emailtext = "";
  14. // Read the post from PayPal and add 'cmd'
  15. $req = 'cmd=_notify-validate';
  16. if (function_exists ( 'get_magic_quotes_gpc' )) {
  17. $get_magic_quotes_exits = true;
  18. }
  19. foreach ( $_POST as $key => $value ) // Handle escape characters, which depends on setting of magic quotes
  20. {
  21. if ($get_magic_quotes_exists == true && get_magic_quotes_gpc () == 1) {
  22. $value = urlencode ( stripslashes ( $value ) );
  23. } else {
  24. $value = urlencode ( $value );
  25. }
  26. $req .= "&$key=$value";
  27. }
  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 ( 'ssl://www.paypal.com', 443, $errno, $errstr, 30 );
  33. // Process validation from PayPal
  34. // TODO: This sample does not test the HTTP response code. All
  35. // HTTP response codes must be handles or you should use an HTTP
  36. // library, such as cUrl
  37. if (! $fp) { // HTTP ERROR
  38. } else {
  39. // NO HTTP ERROR
  40. fputs ( $fp, $header . $req );
  41. while ( ! feof ( $fp ) ) {
  42. $res = fgets ( $fp, 1024 );
  43. if (strcmp ( $res, "VERIFIED" ) == 0) {
  44. // TODO:
  45. // 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. foreach ( $_POST as $key => $value ) {
  53. $emailtext .= $key . " = " . $value . "\n\n";
  54. }
  55. if ($_POST ['item_number']) {
  56. $this->cart_model->orderPaid ( $_POST ['item_number'] );
  57. $data = array ();
  58. $data ['order_id'] = $_POST ['item_number'];
  59. $ord = $this->cart_model->ordersGet ( $data, $limit = false );
  60. $ord = $ord [0];
  61. $user_from_order = explode ( ':', $_POST ['item_number'] );
  62. $user_from_order = $user_from_order [1];
  63. $usr = CI::model ( 'users' )->getUserById ( $user_from_order );
  64. if ($usr ['expires_on'] == false) {
  65. $usr ['expires_on'] = date ( "Y-m-d H:i:s" );
  66. }
  67. $extend_with = $_POST ['period3'];
  68. $extend_with = str_ireplace ( 'M', '', $extend_with );
  69. $extend_with = intval ( $extend_with );
  70. //$newdate = add_date ( $usr ['expires_on'], 0, $extend_with, 0 );
  71. $newdate = strtotime ( "+{$extend_with} months", strtotime ( $usr ['expires_on'] ) );
  72. $newdate = date ( "Y-m-d H:i:s", $newdate );
  73. $to_save = array ();
  74. $to_save ['id'] = $user_from_order;
  75. $to_save ['expires_on'] = $newdate;
  76. $emailtext .= 'Old date:' . $usr ['expires_on'] . "\n\n";
  77. $emailtext .= 'New date:' . $newdate . "\n\n";
  78. $emailtext .= serialize ( $to_save ) . "\n\n";
  79. CI::model ( 'users' )->saveUser ( $to_save );
  80. }
  81. $get_option = array ();
  82. $get_option ['option_key'] = 'mailform_to';
  83. //$get_option ['option_group'] = 'orders';
  84. $get_option1 = CI::model ( 'core' )->optionsGetByKey ( $get_option, true );
  85. $email = $get_option1 ['option_value'];
  86. //$email = 'boksiora@gmail.com';
  87. mail ( $email, "Live-VERIFIED IPN", $emailtext . "\n\n" . $req );
  88. } else if (strcmp ( $res, "INVALID" ) == 0) {
  89. // If 'INVALID', send an email. TODO: Log for manual investigation.
  90. foreach ( $_POST as $key => $value ) {
  91. $emailtext .= $key . " = " . $value . "\n\n";
  92. }
  93. $get_option = array ();
  94. $get_option ['option_key'] = 'mailform_to';
  95. //$get_option ['option_group'] = 'orders';
  96. $get_option1 = CI::model ( 'core' )->optionsGetByKey ( $get_option, true );
  97. $email = $get_option1 ['option_value'];
  98. mail ( $email, "Error: Live-INVALID IPN", $emailtext . "\n\n" . $req );
  99. }
  100. }
  101. fclose ( $fp );
  102. }
  103. }
  104. function place_order() {
  105. $data = $_POST;
  106. $to_table = CI::model ( 'core' )->guessDbTable ();
  107. $to_table_id = CI::model ( 'core' )->guessId ();
  108. $data ['to_table'] = $to_table;
  109. $data ['to_table_id'] = $to_table_id;
  110. $cart = CI::model ( 'cart' )->orderPlace ( $data );
  111. $cart = json_encode ( $cart );
  112. exit ();
  113. }
  114. function promo_code_edit() {
  115. $adm = is_admin ();
  116. if ($adm == true) {
  117. //$this->template ['functionName'] = strtolower ( __FUNCTION__ );
  118. CI::model ( 'cart' )->promoCodeSave ( $_POST );
  119. CI::model ( 'core' )->cacheDelete ( 'cache_group', 'cart' );
  120. exit ();
  121. }
  122. }
  123. function promo_code_delete() {
  124. $adm = is_admin ();
  125. if ($adm == true) {
  126. $id = intval ( $_POST ['id'] );
  127. CI::model ( 'cart' )->promoCodeDeleteById ( $id );
  128. CI::model ( 'core' )->cacheDelete ( 'cache_group', 'cart' );
  129. exit ();
  130. }
  131. }
  132. }