diff --git a/controllers/Shop.php b/controllers/Shop.php new file mode 100644 index 0000000..79752ec --- /dev/null +++ b/controllers/Shop.php @@ -0,0 +1,108 @@ +show_page('main_page'); + } + + // 咖啡包預覽頁 + public function coffee_shop() + { + $product_id = $this->uri->segment(3); // 商品代碼 + $data = $this->app_model()->q_product($product_id); + unset($data['product_plan']); + $this->show_page('main_page', $data); + } + + // 付款 + public function transfer_money() + { + $product_id = $this->input->post('product_id', true); + $product_code = $this->input->post('product_code', true); + $invoice_receiver = $this->input->post('invoice_receiver', true); + $company_no = $this->input->post('company_no', true); + $email = $this->input->post('email', true); + $mobile = $this->input->post('mobile', true); + + // 建立訂單 + $new_bill = $this->app_model()->create_product_bill($product_id, $product_code); + + if(!isset($new_bill['order_no'])) + { + echo 'bill_create_fail'; + exit; + } + + $parms = array( + 'order_no' => $new_bill['order_no'], + 'invoice_receiver' => $invoice_receiver, + 'company_no' => $company_no, + 'email' => $email, + 'mobile' => $mobile + ); + + // 處理產品訂單 + $proceed_bill = $this->app_model()->proceed_product_bill($parms, 50); // 50: 歐付寶刷卡 + + // 開始進行繳交帳單 + if(!isset($proceed_bill['status']) || $proceed_bill['status'] != 100) + { + echo 'bill_proceed_fail'; + exit; + } + + // 串接總公司購物流程 + $proceed_bill['station_no'] = $this->get_station_no(); + $proceed_bill['product_id'] = $product_id; + $proceed_bill['product_code'] = $product_code; + trigger_error(__FUNCTION__ . '..' . print_r($proceed_bill, true)); + + try{ + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, 'https://parks.altob.com.tw/bill_service.html/proceed_bill'); // 金流 + curl_setopt($ch, CURLOPT_HEADER, FALSE); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_POST, TRUE); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,30); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout in seconds + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($proceed_bill)); + $result = curl_exec($ch); + + if(curl_errno($ch)) + { + trigger_error(__FUNCTION__ . ', curl error: '. curl_error($ch)); + } + + curl_close($ch); + + }catch (Exception $e){ + trigger_error(__FUNCTION__ . 'error:'.$e->getMessage()); + } + + echo $result; + } + + // 買 + public function i_do() + { + $product_id = $this->uri->segment(3); // 商品代碼 + $data = $this->app_model()->q_coffee_shop($product_id); + + trigger_error(__FUNCTION__ . '..' . print_r($data, true)); + + + echo 'ok'; + } + +} diff --git a/core/CC_Controller.php b/core/CC_Controller.php index 4859cc5..dddd6ee 100644 --- a/core/CC_Controller.php +++ b/core/CC_Controller.php @@ -168,4 +168,12 @@ class CC_Controller extends CI_Controller $this->$model_name->init($this->vars); return $this->$model_name; } + + // 取得場站編號 + public function get_station_no() + { + $station_setting = $this->data_model()->station_setting_query(); + $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']); + return $station_no_arr[0]; + } } diff --git a/models/Shop_model.php b/models/Shop_model.php new file mode 100644 index 0000000..59a32c4 --- /dev/null +++ b/models/Shop_model.php @@ -0,0 +1,152 @@ +load->database(); + + // product code + define('PRODUCT_CODE_COFFEE_SHOP', 'coffee_shop'); // 咖啡產品包 + define('PRODUCT_CODE_COFFEE', 'coffee'); // 咖啡 + } + + public function init($vars) + { + $this->vars = $vars; + } + + // 取得產品資訊 + public function q_product($product_id=0, $product_code='') + { + $now = date('Y/m/d H:i:s'); + $where_arr = array('start_time <= ' => $now, 'valid_time > ' => $now); + + // 指定產品流水號 + if(!empty($product_id)) + $where_arr['product_id'] = $product_id; + + // 指定產品包 + if(!empty($product_code)) + $where_arr['product_code'] = PRODUCT_CODE_COFFEE_SHOP; // 預設咖啡包 + + $data = array(); + $result = $this->db->select('product_id, product_code, product_name, product_desc, amt, remarks, product_plan') + ->from('products') + ->where($where_arr) + ->limit(1) + ->get() + ->row_array(); + return $result; + } + + // 產生交易序號 + private function gen_trx_no() + { + return time().rand(10000,99999); + } + + // 建立產品訂單 + public function create_product_bill($product_id, $product_code) + { + // 取得商品資訊 + $product_info = $this->q_product($product_id, $product_code); + + if(!isset($product_info['product_plan'])) + { + return 'unknown_product'; // 中斷 + } + + $data = array(); + $data['order_no'] = $this->gen_trx_no(); + $data['product_id'] = $product_info["product_id"]; + $data['product_code'] = $product_info["product_code"]; + $data['product_plan'] = $product_info["product_plan"]; + $data['invoice_remark'] = $product_info["product_name"]; + $data['amt'] = $product_info["amt"]; + $data['valid_time'] = date('Y-m-d H:i:s', strtotime(" + 15 minutes")); // 15 min 內有效 + $this->db->insert('product_bill', $data); + + $affect_rows = $this->db->affected_rows(); + + if ($affect_rows <= 0) + return 'fail'; + + trigger_error(__FUNCTION__ . '..' . print_r($data, true)); + return $data; + } + + // 處理產品訂單 + public function proceed_product_bill($parms, $tx_type=0) + { + $order_no = $parms['order_no']; + $invoice_receiver = $parms['invoice_receiver']; + $company_no = $parms['company_no']; + $email = $parms['email']; + $mobile = $parms['mobile']; + + $product_info = $this->db->select('valid_time, product_plan') + ->from('product_bill') + ->where(array('order_no' => $order_no)) + ->limit(1) + ->get() + ->row_array(); + + if(!isset($product_info['product_plan'])) + { + trigger_error(__FUNCTION__ . "|{$order_no}|unknown_order"); + return 'unknown_order'; // 中斷 + } + + if(!isset($product_info['valid_time'])) + { + trigger_error(__FUNCTION__ . "|{$order_no}|valid_time_not_found"); + return 'valid_time_not_found'; // 中斷 + } + + $data = array(); + $data['tx_type'] = $tx_type; // 交易種類: 0:未定義, 1:現金, 40:博辰人工模組, 41:博辰自動繳費機, 50:歐付寶轉址刷卡, 51:歐付寶APP, 52:歐付寶轉址WebATM, 60:中國信託刷卡轉址 + + if(strlen($company_no) >= 8) + { + $data['company_no'] = $company_no; // 電子發票:公司統編 + $data['company_receiver'] = "公司名稱"; // 電子發票:公司名稱 + $data['company_address'] = "公司地址"; // 電子發票:公司地址 + } + + $data['invoice_receiver'] = (strlen($invoice_receiver) >= 7) ? $invoice_receiver : ''; // 電子發票:載具編號 + $data['email'] = (strlen($email) >= 5) ? $email : ''; // 電子發票:email + $data['mobile'] = (strlen($mobile) >= 10) ? $mobile : ''; // 電子發票:手機 + + // 交易時間 + $tx_time = time(); + $data['tx_time'] = date('Y/m/d H:i:s', $tx_time); + + if(strtotime($product_info['valid_time']) < $tx_time) + { + $data['status'] = 99; //狀態: 99:訂單逾期作廢 + $this->db->update('product_bill', $data, array('order_no' => $order_no)); + trigger_error(__FUNCTION__ . "|{$order_no}| 99 gg"); + return 'gg'; + } + + // 完成 + $data['status'] = 100; // 狀態: 100:交易進行中 + $this->db->update('product_bill', $data, array('order_no' => $order_no)); + + $affect_rows = $this->db->affected_rows(); + + if ($affect_rows <= 0) + return 'fail'; + + $data['order_no'] = $order_no; + trigger_error(__FUNCTION__ . ".." . print_r($data, true)); + return $data; + } +} diff --git a/views/shop/checkout_page.php b/views/shop/checkout_page.php new file mode 100644 index 0000000..32dd879 --- /dev/null +++ b/views/shop/checkout_page.php @@ -0,0 +1,334 @@ +