req_params($card_no, $card_type, $amount, $order_sn); $sign = $this->sign($head); $head['chargeSign'] = $sign; $data = [ 'head' => $head, 'body' => [ 'item' => $params ] ]; $xmlData = $this->toXmlData($data); $resp = http_post_data(config::ORDER_URL, $xmlData, config::ExtHeaders, $net_errno); if (empty($resp)) { return [false, '网络错误', true]; } else { Log::record($resp, Log::DEBUG); $resp = refill\util::xmlToArray($resp); if (empty($resp)) { return [false, '网络错误', true]; } elseif ($resp['result'] === '0000') { return [true, '', false]; } else { return [false, $resp['desc'], false]; } } } public function query($refill_info) { if (time() - $refill_info['commit_time'] > 86400*30) { return [false, '时效已过']; } $head['custInteId'] = config::custInteId; $head['echo'] = rand(100000,999999); $head['timestamp'] = date("YmdHis"); $head['version'] = 1; $content = $head['custInteId'] . config::KEY . $head['echo'] . $head['timestamp']; $head['chargeSign'] = base64_encode(md5($content, true)); $params['orderIds'] = $refill_info['order_sn']; $data = [ 'head' => $head, 'body' => [ 'item' => $params ] ]; $xmlData = $this->toXmlData($data); $resp = http_post_data(config::QUERY_URL, $xmlData, config::ExtHeaders); if (empty($resp)) { return [false, '网络错误']; } else { Log::record($resp, Log::DEBUG); $resp = refill\util::xmlToArray($resp); if (empty($resp)) { return [false, '网络错误']; } elseif ($resp['head']['result'] === '0000') { $item = $resp['body']['item']; $status = $item['state']; if ($status === '1') { $order_state = ORDER_STATE_SUCCESS; $updata['official_sn'] = $item['operatorNo']; Model('refill_order')->edit($refill_info['order_id'], $updata); } elseif ($status === '2') { $order_state = ORDER_STATE_CANCEL; } elseif ($status === '0') { $order_state = ORDER_STATE_SEND; } elseif ($status === '-1' && (time() - $refill_info['commit_time'] > 600)) { $order_state = ORDER_STATE_NOEXIST; } else { return [false, $item['desc']]; } return [true, $order_state]; } else { return [false, $resp['head']['desc']]; } } } public function balance() { $head['custInteId'] = config::custInteId; $head['echo'] = rand(100000,999999); $head['timestamp'] = date("YmdHis"); $head['version'] = 1; $content = $head['custInteId'] . config::KEY . $head['echo'] . $head['timestamp']; $head['chargeSign'] = base64_encode(md5($content, true)); $data = [ 'head' => $head, ]; $xmlData = $this->toXmlData($data); $resp = http_post_data(config::BALANCE_URL, $xmlData, config::ExtHeaders); if (empty($resp)) { return [false, '网络错误']; } else { Log::record($resp, Log::DEBUG); $resp = refill\util::xmlToArray($resp); if (empty($resp)) { return [false, '网络错误', true]; } elseif ($resp['result'] === '0000') { return [true, $resp['balance'], false]; } else { return [false, $resp['desc'], false]; } } } private function sign($params) { $content = $params['custInteId'] . $params['orderId'] . config::KEY. $params['echo'] . $params['timestamp']; return base64_encode(md5($content, true)); } private function toXmlData($data) { return $this->xml_encode($data); } private function xml_encode($data, $encoding='utf-8') { $xml = ''; $xml .= ''; $xml .= $this->data_to_xml($data); $xml .= ''; return $xml; } private function data_to_xml($data) { $xml = ''; foreach ($data as $key => $val) { is_numeric($key) && $key = "item id=\"$key\""; $xml .= "<$key>"; $xml .= ( is_array($val) || is_object($val)) ? $this->data_to_xml($val) : $val; list($key, ) = explode(' ', $key); $xml .= ""; } return $xml; } }