VM暫存
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
9.0KB

  1. <?php
  2. /*
  3. file: qcar.php 查車系統
  4. */
  5. if (!defined('BASEPATH')) exit('No direct script access allowed');
  6. class Qcar extends CI_Controller
  7. {
  8. var $vars = array(); // 共用變數
  9. function __construct()
  10. {
  11. $client_ip = $this->my_ip();
  12. header('Location: http://192.168.10.201/qcar2.html');
  13. exit;
  14. parent::__construct();
  15. // ----- 程式開發階段log設定 -----
  16. if (@ENVIRONMENT == 'development')
  17. {
  18. ini_set('display_errors', '1');
  19. //error_reporting(E_ALL ^ E_NOTICE);
  20. error_reporting(E_ALL);
  21. }
  22. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  23. /*
  24. // 共用記憶體
  25. $this->vars['mcache'] = new Memcache;
  26. $this->vars['mcache']->connect(MEMCACHE_HOST, MEMCACHE_POST) or die ('Could not connect memcache');
  27. // mqtt subscribe
  28. $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_POST, 'cario');
  29. if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); }
  30. */
  31. // ----- 定義常數(路徑, cache秒數) -----
  32. define('APP_VERSION', '100'); // 版本號
  33. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  34. define('APP_NAME', 'qcar'); // 應用系統名稱
  35. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  36. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
  37. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  38. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  39. define('WEB_LIB', SERVER_URL.'libs/'); // 網頁lib
  40. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  41. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path
  42. $this->load->model('qcar_model');
  43. $this->load->model('payment_ats_model'); // 繳費
  44. $this->load->model('allpay_invoice_model'); // 歐付寶電子發票
  45. define('MAIN_PAGE', "main_page");
  46. define('RESULT_PAGE', "result_page");
  47. // ----- 行動支付, 繳費機告知已付款 -----
  48. define('ALTOB_ATS2PAYED', "http://localhost/carpayment.html/ats2payed");
  49. // ----- 行動支付, 繳費機告知已付款 (end) -----
  50. // ----- 歐付寶金流 -----
  51. define('ALLPAY_PAYMENT_TX_BILL_ATS', SERVER_URL."allpay_payment.html/transfer_money_tx_bill_ats"); // 歐付寶付款系統連結
  52. define('ALLPAY_ClientBackURL', APP_URL."client_back/"); // 您要歐付寶返回按鈕導向的瀏覽器端網址";
  53. define('ALLPAY_OrderResultURL', APP_URL."order_result/"); // 您要收到付款完成通知的瀏覽器端網址(browser) ps. WebATM大部份銀行都回不來;
  54. define('ALLPAY_AltobServiceURL', APP_URL."payment_completed_handler/"); // 付款完成後被通知的位址
  55. // ----- 歐付寶金流 (end) -----
  56. }
  57. // 取得 IP
  58. function my_ip()
  59. {
  60. if (getenv('HTTP_X_FORWARDED_FOR'))
  61. {
  62. $ip = getenv('HTTP_X_FORWARDED_FOR');
  63. }
  64. elseif (getenv('HTTP_X_REAL_IP'))
  65. {
  66. $ip = getenv('HTTP_X_REAL_IP');
  67. }
  68. else {
  69. $ip = $_SERVER['REMOTE_ADDR'];
  70. }
  71. return $ip;
  72. }
  73. // 發生錯誤時集中在此處理
  74. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  75. {
  76. // ex: car_err://message....
  77. //$log_msg = explode('://', $errstr);
  78. /*
  79. if (count($log_msg) > 1)
  80. {
  81. $log_file = LOG_PATH.$log_msg[0];
  82. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  83. }
  84. else
  85. {
  86. $log_file = LOG_PATH.APP_NAME;
  87. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  88. }
  89. */
  90. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  91. //error_log($str, 3, $log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  92. error_log($str, 3, LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  93. }
  94. // 顯示靜態網頁(html檔)
  95. protected function show_page($page_name, $data = null)
  96. {
  97. $page_file = PAGE_PATH.$page_name.'.php';
  98. $last_modified_time = filemtime($page_file);
  99. // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁
  100. header('Cache-Control:max-age='.MAX_AGE); // cache 1個月
  101. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT');
  102. header('Etag: '. APP_VERSION);
  103. header('Cache-Control: public');
  104. if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time)
  105. {
  106. header('HTTP/1.1 304 Not Modified');
  107. }
  108. else
  109. {
  110. $this->load->view(APP_NAME.'/'.$page_name, $data);
  111. }
  112. }
  113. public function index()
  114. {
  115. $this->show_page('main_page');
  116. }
  117. // 顯示logs
  118. public function show_logs()
  119. {
  120. $lines = $this->uri->segment(3); // 顯示行數
  121. if (empty($lines)) $lines = 40; // 無行數參數, 預設為40行
  122. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  123. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  124. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_FILE); // 利用linux指令顯示倒數幾行的logs內容
  125. echo "\n----- " . LOG_FILE . ' -----';
  126. echo '</pre></body></html>';
  127. }
  128. // 付款 - 1.繳月租
  129. public function payment_lpr()
  130. {
  131. $payment_lpr = $this->input->post('payment_lpr', true);
  132. $data = $this->payment_ats_model->create_member_bill($payment_lpr);
  133. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  134. }
  135. // 付款 - 2.確定繳費
  136. public function transfer_money()
  137. {
  138. $order_no = strtoupper($this->uri->segment(3)); // 交易序號
  139. $invoice_receiver = urldecode($this->uri->segment(4)); // 載具編號 (可有可無)
  140. $company_no = urldecode($this->uri->segment(5)); // 公司統編 (可有可無)
  141. $email_base64 = $this->uri->segment(6); // 電子信箱
  142. $mobile = $this->uri->segment(7); // 手機號碼
  143. // decode email
  144. if(strlen($email_base64) > 0){
  145. $email = base64_decode($email_base64.'='); // base64字串尾端的'='還原
  146. }else{
  147. $email = email_base64;
  148. }
  149. $this->payment_ats_model-> // 開始進行繳交帳單
  150. pay_bill($order_no, $invoice_receiver, $company_no, $email, $mobile,
  151. ALLPAY_ClientBackURL,
  152. ALLPAY_OrderResultURL,
  153. ALLPAY_AltobServiceURL,
  154. 52); // 交易種類: 0:未定義, 1:現金, 40:博辰人工模組, 41:博辰自動繳費機, 50:歐付寶轉址刷卡, 51:歐付寶APP, 52:歐付寶轉址WebATM, 60:中國信託刷卡轉址
  155. // 轉址歐付寶付款系統
  156. echo file_get_contents(ALLPAY_PAYMENT_TX_BILL_ATS."/{$order_no}");
  157. }
  158. // L.1 付款完成 (限制存取)
  159. public function payment_completed_handler()
  160. {
  161. $order_no = $this->uri->segment(3); // 交易序號
  162. $data = $this->payment_ats_model->get_tx_bill($order_no);
  163. if (! empty($data))
  164. {
  165. $order_no = $data['order_no'];
  166. $station_no = $data['station_no'];
  167. $lpr = $data['lpr'];
  168. $amt = $data['amt'];
  169. $status = $data['status'];
  170. switch($status){
  171. case 1: // 狀態: 0:剛建立, 1:結帳完成, 2:錢沒對上, 3:發票沒建立, 4:手動調整, 99:訂單逾期作廢, 100:交易進行中, 111:產品已領取
  172. // 開立歐付寶電子發票
  173. $this->allpay_invoice_model->invoice_issue_for_tx_bill_ats($order_no, $amt);
  174. // 記錄為已領取
  175. $this->payment_ats_model->transfer_money_done_and_finished($order_no);
  176. // 繳費機告知已付款
  177. // http://localhost/carpayment.html/ats2payed/車牌/金額/場站編號/序號/MD5
  178. // md5(車牌.金額.場站編號.序號)
  179. $md5 = md5($lpr.$amt.$station_no.$order_no);
  180. file_get_contents(ALTOB_ATS2PAYED."/{$lpr}/{$amt}/{$station_no}/{$order_no}/{$md5}");
  181. default:
  182. // 尚未結帳完成, 或是已領取
  183. trigger_error(APP_NAME.', '.__FUNCTION__.', order_no=>' . $order_no.'<br>'.' status != 1');
  184. }
  185. }
  186. }
  187. // 歐付寶返回按鈕導向的瀏覽器端網址
  188. public function client_back()
  189. {
  190. $this->show_page(MAIN_PAGE);
  191. }
  192. // 收到付款完成通知的瀏覽器端網址(browser) ps. WebATM大部份銀行都回不來
  193. public function order_result()
  194. {
  195. $this->show_page(RESULT_PAGE);
  196. }
  197. // 車位查詢
  198. public function q_pks()
  199. {
  200. $lpr = $this->input->post('lpr', true);
  201. $data = $this->qcar_model->q_pks($lpr);
  202. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  203. }
  204. // 取得進場資訊 (模糊比對)
  205. public function q_fuzzy_pks()
  206. {
  207. $input = $this->input->post('fuzzy_input', true);
  208. $data = $this->qcar_model->q_fuzzy_pks($input);
  209. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  210. }
  211. }