VM暫存
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
7.2KB

  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Altpay_member extends CI_Controller {
  4. public function __construct() {
  5. parent::__construct();
  6. $this->load->library('session');
  7. $this->load->model('api/Altpay_model','Altpay');
  8. // ----- 回傳訊息 -----
  9. define('ALLPA_GO_RESULT_CODE_OK', 0);
  10. define('ALLPA_GO_RESULT_MSG_OK', "成功");
  11. define('ALLPA_GO_RESULT_CODE_CK_ERROR', 10);
  12. define('ALLPA_GO_RESULT_MSG_CK_ERROR', "CK ERROR");
  13. define('ALLPA_GO_RESULT_CODE_USER_NOT_FOUND', 11);
  14. define('ALLPA_GO_RESULT_MSG_USER_NOT_FOUND', "查無歐Pa卡用戶");
  15. define('local_ip', 'http://221.120.36.144');
  16. // ----- 回傳訊息 (END) -----
  17. }
  18. public function index() {
  19. $station_info = array();
  20. $station_info = $this->Altpay->sel_info();
  21. $result = $station_info;
  22. $key = md5("Alt@b80682490".$station_info[0]['station_no']);
  23. $acarps_ip="altapi.altob.com.tw";
  24. $this->get_member_data($acarps_ip,$key);
  25. }
  26. //call總公司API取得歐Pa卡會員資料
  27. public function get_member_data($acarps_ip,$key){
  28. $apijson= "http://".$acarps_ip."/alp_pay_api/Altpay_members_api/index/".$key;
  29. $ch = curl_init();
  30. curl_setopt($ch, CURLOPT_HEADER, 0);
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  32. curl_setopt($ch, CURLOPT_URL, $apijson);
  33. $output = curl_exec($ch);
  34. if (curl_errno($ch)) {
  35. print "Error: " . curl_error($ch);
  36. } else {
  37. curl_close($ch);
  38. $this->save_member_json($output);
  39. print_r(json_encode($output));
  40. }
  41. }
  42. public function save_member_json($data){
  43. $myfile = fopen("/home/data/json/altpay_member.json", "w") or die("Unable to open file!");
  44. fwrite($myfile, $data);
  45. fclose($myfile);
  46. }
  47. public function read_member_json(){
  48. $myfile = fopen("/home/data/json/altpay_member.json", "r") or die("Unable to open file!");
  49. $result = array();
  50. $result = fread($myfile, filesize("/home/data/json/altpay_member.json"));
  51. fclose($myfile);
  52. return $result;
  53. }
  54. // 歐Pa卡 - 判斷有效用戶 (限制存取)
  55. public function get_allpa_valid_user($lpr,$check_mac){
  56. $data = json_decode($this->read_member_json(),true);
  57. $result = array();
  58. $flag = 0;
  59. for($i=0;$i<count($data);$i++){
  60. if($data[$i]["car_id"]==$lpr){
  61. if(empty($check_mac) || (md5($data[$i]["car_id"]) != $check_mac)){
  62. // check mac fail
  63. $result["result_code"] = ALLPA_GO_RESULT_CODE_CK_ERROR;
  64. $result["result_msg"] = ALLPA_GO_RESULT_MSG_CK_ERROR;
  65. //print_r("1|".$data[$i]["car_id"]."|".$check_mac."|".$flag."|".$result["result_msg"]);
  66. return $result; // CK ERROR
  67. } else if(empty($data)){
  68. $result["result_code"] = ALLPA_GO_RESULT_CODE_USER_NOT_FOUND;
  69. $result["result_msg"] = ALLPA_GO_RESULT_MSG_USER_NOT_FOUND;
  70. //print_r("2|".$data[$i]["car_id"]."|".$check_mac."|".$flag."|".$result["result_msg"]);
  71. return $result; // USER NOT FOUND
  72. } else {
  73. $flag = 1;
  74. $result["result_code"] = ALLPA_GO_RESULT_CODE_OK;
  75. $result["result_msg"] = ALLPA_GO_RESULT_MSG_OK;
  76. $result["lpr"] = $data[$i]["car_id"];
  77. //$result["barcode"] = $user["barcode"];
  78. $result["balance"] = $data[$i]["balance"];
  79. //$result["bonus"] = $user["bonus"];
  80. //print_r("3|".$data[$i]["car_id"]."|".$check_mac."|".$flag."|".$result["result_msg"]);
  81. return $result;
  82. }
  83. }
  84. }
  85. if($flag==0){
  86. $result["result_code"] = ALLPA_GO_RESULT_CODE_USER_NOT_FOUND;
  87. $result["result_msg"] = ALLPA_GO_RESULT_MSG_USER_NOT_FOUND;
  88. print_r("4|".$lpr."|".$check_mac."|".$flag."|".$result["result_msg"]);
  89. return $result; // USER NOT FOUND
  90. }
  91. //print_r($lpr."|".$check_mac."|".$flag);
  92. }
  93. // 遠端歐PA卡流程
  94. public function allpa_go_remote($in_time,$lpr,$station_no){
  95. /*
  96. $in_time = $this->uri->segment(3); // 進場時間
  97. $lpr = $this->uri->segment(4); // 車牌號碼
  98. $station_no = $this->uri->segment(5); // 場站編號
  99. $check_mac = $this->uri->segment(6); // 驗証欄位
  100. */
  101. // 驗証欄位
  102. /*
  103. if($check_mac != md5($in_time. $lpr . $station_no)){
  104. echo 'ck_error';
  105. exit;
  106. }
  107. */
  108. // 先檢查本地端是否為歐PA會員
  109. $valid_user_ck = md5($lpr);
  110. $valid_user_result = $this->get_allpa_valid_user($lpr, $valid_user_ck); // check user
  111. if(!isset($valid_user_result['result_code']) || $valid_user_result['result_code'] != 0){
  112. echo json_encode($valid_user_result, JSON_UNESCAPED_UNICODE);
  113. } else {
  114. $res = $this->get_total_fee($lpr);
  115. $tmp = explode('|',$res);
  116. $total_fee = $tmp[0];
  117. $station_no = $tmp[1];
  118. $cario_no = $tmp[2];
  119. $in_time = $tmp[3];
  120. $in_lane = $tmp[4];
  121. $out_lane = $tmp[5];
  122. $pay_time = strtotime(date('Y-m-d H:i:s')); // 結帳時間
  123. $data=array(
  124. "station_no"=>$station_no,
  125. "cario_no"=>$cario_no,
  126. "in_time"=>$in_time,
  127. "out_time"=>'',
  128. "in_lane"=>$in_lane,
  129. "out_lane"=>$out_lane,
  130. "pay_time"=>$pay_time,
  131. "fee"=>$total_fee,
  132. "pre_status_code"=>'01',
  133. "pre_balance"=>$valid_user_result['balance'],
  134. "pre_bonus"=>'',
  135. "status_code"=>'01',
  136. "balance"=>$valid_user_result['balance']-$total_fee,
  137. "bonus"=>'',
  138. "in_pic_name"=>'',
  139. "in_pic_down"=>0,
  140. "out_pic_name"=>'',
  141. "out_pic_down"=>0,
  142. "create_time"=>strtotime(date('Y-m-d H:i:s')),
  143. "update_time"=>strtotime(date('Y-m-d H:i:s'))
  144. );
  145. print_r($data);
  146. }
  147. }
  148. public function get_total_fee($lpr){
  149. $this->load->model('api/Master_db_model','master_db');
  150. $sel_cario=$this->master_db->sel_cario($lpr);
  151. if(count($sel_cario)>0){
  152. $cario_no=$sel_cario[0]['cario_no']; //入場流水號
  153. $station_no=$sel_cario[0]['station_no']; //場站代碼
  154. $member_no=$sel_cario[0]['member_no']; //會員代碼
  155. $obj_id=$sel_cario[0]['obj_id']; //會員代碼
  156. $in_out=$sel_cario[0]['in_out'];
  157. $in_time=$sel_cario[0]['in_time']; //入場時間
  158. $Get_billing_fee= "http://altapi.altob.com.tw/fee_api/Get_billing_fee"; //檢查現場入場時間
  159. $start_time=$in_time;
  160. $end_time=date('Y-m-d H:i:s');
  161. $ch0 = curl_init();
  162. curl_setopt($ch0, CURLOPT_HEADER, 0);
  163. curl_setopt($ch0, CURLOPT_RETURNTRANSFER, 1);
  164. curl_setopt($ch0, CURLOPT_URL, $Get_billing_fee);
  165. curl_setopt($ch0, CURLOPT_POST, true);
  166. curl_setopt($ch0, CURLOPT_POSTFIELDS, http_build_query(array("station_no"=>$station_no, "start_time"=>$start_time, "end_time"=>$end_time)));
  167. $totalfee0 = curl_exec($ch0);
  168. curl_close($ch0);
  169. $totalfee=isset($totalfee0) ? $totalfee0:0 ;
  170. return $totalfee.'|'.$sel_cario[0]['station_no'].'|'.$sel_cario[0]['cario_no'].'|'.$sel_cario[0]['in_time'].'|'.$sel_cario[0]['in_lane'].'|'.$sel_cario[0]['out_lane'];
  171. }
  172. }
  173. public function test(){
  174. //$lpr = "ABC123";
  175. $lpr = "AQB2211";
  176. $ck = md5($lpr);
  177. $this->allpa_go_remote("2018-06-27 21:20:20",$lpr,"40671");exit;
  178. //print_r($lpr."|".$ck."|");
  179. ob_end_clean();
  180. ignore_user_abort();
  181. ob_start();
  182. $data = $this->get_allpa_valid_user($lpr, $ck); // check user
  183. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  184. header('Connection: close');
  185. header('Content-Length: ' . ob_get_length());
  186. ob_end_flush();
  187. flush();
  188. /*
  189. $jdata = file_get_contents("http://localhost/api.html/Altpay_member/get_allpa_valid_user/{$lpr}/{$ck}");
  190. $results = json_decode($jdata, true);
  191. print_r($results);
  192. */
  193. }
  194. }
  195. ?>