123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace rbridge\gy;
- require_once(BASE_HELPER_PATH . '/rbridge/gy/config.php');
- use rbridge\IBridge;
- use rbridge\IBridgeEx;
- use rbridge\BridgeTrait;
- use refill;
- use Log;
- use SimpleXMLElement;
- use member_info;
- class Bridge implements IBridge, IBridgeEx
- {
- use BridgeTrait;
- public function add($params)
- {
- $input_names = ['userid', 'request_id', 'telephone', 'money', 'callbackurl'];
- $out_names = ['order_id', 'request_id', 'telephone', 'money', 'result'];
- $mchid = config::mchid($params);
- if ($mchid === false) {
- $params['result'] = 4; //请求参数不完整或不合法
- return [false, $this->result_format($params, $out_names, '')];
- }
- $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
- if (empty($mchinfo)) {
- $params['result'] = 6; //代理商不存在
- return [false, $this->result_format($params, $out_names, '')];
- }
- $secure_key = $mchinfo['secure_key'];
- [$succ, $result] = $this->check($mchinfo, $params, $input_names);
- if (!$succ) {
- $params['result'] = $result;
- return [false, $this->result_format($params, $out_names, $secure_key)];
- }
- Model('merchant_query')->add_info($mchid, $params['request_id'], json_encode($params));
- $order_time = time();
- $mch_order = $params['request_id'];
- $card_no = $params['telephone'];
- $input = ['mchid' => $mchid,
- 'buyer_id' => intval($mchinfo['admin_id']),
- 'amount' => $params['money'],
- 'card_no' => $card_no,
- 'mch_order' => $mch_order,
- 'notify_url' => config::MCH_NOTIFY_URL,
- 'order_time' => $order_time
- ];
- refill\util::push_queue_order($mchid, $mch_order, ORDER_STATE_QUEUE);
- $detail_id = Model('refill_order')->add_detail($mchid, $mch_order, $order_time, $params, ORDER_STATE_QUEUE);
- $state = refill\util::push_add($input);
- if ($state === true) {
- Log::record("gy::Bridge refill::util::push_add success mchid=$mchid mch_order=$mch_order state=true", Log::DEBUG);
- $params['order_id'] = $detail_id;
- $params['result'] = 2; //提交成功,正在充值(适用于异步接口)
- return [true, $this->result_format($params, $out_names, $secure_key)];
- } else {
- refill\util::del_queue_order($mchid, $mch_order);
- Model('refill_order')->del_detail($mchid, $mch_order);
- Log::record("gy::Bridge refill::util::push_add error mchid=$mchid mch_order=$mch_order state=$state", Log::DEBUG);
- $params['result'] = 12; //未知错误
- return [true, $this->result_format($params, $out_names, $secure_key)];
- }
- }
- private function result_format($input, $names, $secure_key)
- {
- $result = $this->sign($input, $names, $secure_key);
- $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>');
- config::arrayToXml($xml,$result);
- return $xml->asXML();
- }
- private function sign($input, $names, $secure_key)
- {
- $result = [];
- $body = '';
- foreach ($names as $name) {
- $val = $input[$name] ?? '';
- $body .= $val;
- $result[$name] = $val;
- }
- $body .= $secure_key;
- $sign = strtoupper(md5($body));
- $result['sign'] = $sign;
- return $result;
- }
- private function check($mchinfo, $input, $names): array
- {
- if ($mchinfo['merchant_state'] != 1) {
- return [false, 6]; //代理商不存在
- }
- $ips = unserialize($mchinfo['ip_white_list']);
- if(!empty($ips))
- {
- $addr = $_SERVER['REMOTE_ADDR'];
- if(!in_array($addr,$ips)) {
- Log::record("request ip:$addr", Log::DEBUG);
- return [false, 12]; //未知错误
- }
- }
- $usekey = intval($mchinfo['use_key']);
- $key = $mchinfo['secure_key'];
- if ($usekey) {
- $ret = $this->verify($input, $names, $key);
- if (!$ret) {
- return [false, 5]; //签名错误
- }
- }
- return [true, ''];
- }
- private function verify($params, $names, $key)
- {
- $body = '';
- foreach ($names as $name) {
- $val = $params[$name] ?? '';
- $body .= $val;
- }
- $body .= $key;
- $sign = strtoupper(md5($body));
- return $params['sign'] == $sign;
- }
- public function query($params)
- {
- $input_names = ['userid', 'request_id'];
- $out_names = ['order_id', 'request_id', 'telephone', 'money', 'result'];
- $mchid = config::mchid($params);
- $mch_order = $params['request_id'];
- if ($mchid === false or empty($mch_order)) {
- $params['result'] = 4; //请求参数不完整或不合法
- return [false, $this->result_format($params, $out_names, '')];
- }
- $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
- if (empty($mchinfo)) {
- $params['result'] = 6; //代理商不存在
- return [false, $this->result_format($params, $out_names, '')];
- }
- $secure_key = $mchinfo['secure_key'];
- [$succ, $result] = $this->check($mchinfo, $params, $input_names);
- if(!$succ) {
- $params['result'] = $result;
- return [$succ, $this->result_format($params, $out_names, $secure_key)];
- }
- [$detail_id,$info] = $this->detail($mchid, $mch_order);
- [$code, $order] = $this->order_query($mchid, $mch_order);
- if ($code == 200) {
- $info['order_id'] = $detail_id;
- $order_state = $order['order_state'];
- } else {
- $order_state = ORDER_STATE_NOEXIST;
- }
- if (in_array($order_state, [ORDER_STATE_QUEUE, ORDER_STATE_NEW, ORDER_STATE_PAY, ORDER_STATE_SEND])) {
- $info['result'] = 2;
- } elseif ($order_state == ORDER_STATE_SUCCESS) {
- $info['result'] = 0;
- } elseif ($order_state == ORDER_STATE_CANCEL) {
- $info['result'] = 1;
- } else {
- $info['result'] = 3;
- }
- return [true, $this->result_format($info, $out_names, $secure_key)];
- }
- private function detail($mchid, $mch_order)
- {
- $mod_refill = Model('refill_order');
- $detail = $mod_refill->get_detail($mchid,$mch_order);
- if(empty($detail)) {
- return [false, []];
- }
- else {
- return [intval($detail['detail_id']), json_decode($detail['params'], true)];
- }
- }
- public function balance($params)
- {
- $input_names = ['userid'];
- $out_names = ['userid', 'money'];
- $mchid = config::mchid($params);
- if ($mchid === false) {
- $params['result'] = 4; //请求参数不完整或不合法
- return [false, $this->result_format($params, $out_names, '')];
- }
- $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
- if (empty($mchinfo)) {
- $params['result'] = 6; //代理商不存在
- return [false, $this->result_format($params, $out_names, '')];
- }
- $secure_key = $mchinfo['secure_key'];
- [$succ, $result] = $this->check($mchinfo, $params, $input_names);
- if(!$succ) {
- $params['result'] = $result;
- return [$succ, $this->result_format($params, $out_names, $secure_key)];
- }
- $uid = intval($mchinfo['admin_id']);
- $minfo = new member_info($uid);
- $available = $minfo->available_predeposit();
- $money = ncPriceFormat($available - $mchinfo['credit_bonus']);
- $params['money'] = $money;
- return [true, $this->result_format($params, $out_names, $secure_key)];
- }
- public function notify($params)
- {
- $out_names = ['order_id', 'request_id', 'telephone', 'money', 'result'];
- $mchid = $params['mchid'];
- $mch_order = $params['order_sn'];
- [$detail_id,$input] = $this->detail($mchid, $mch_order);
- $mchid = $params['mchid'];
- $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
- $secure_key = $mchinfo['secure_key'];
- $body = $this->notify_body($detail_id,$input,$params['state']);
- $body = $this->sign($body,$out_names,$secure_key);
- $resp = http_request($input['callbackurl'], $body, 'GET');
- if (empty($resp)) {
- Log::record("回调下游,请求超时 mchid = $mchid mch_order = $mch_order", Log::ERR);
- return false;
- } elseif (in_array($resp, ['success'])) {
- return true;
- } else {
- return false;
- }
- }
- private function notify_body($detail_id, $input, $order_state)
- {
- $input['order_id'] = $detail_id;
- if($order_state == 'SUCCESS') {
- $input['result'] = 0;
- } else {
- $input['result'] = 1;
- }
- return $input;
- }
- }
|