123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace refill\dinghui;
- require_once(BASE_HELPER_RAPI_PATH . '/dinghui/config.php');
- use refill;
- use Log;
- class RefillPhone extends refill\IRefillPhone
- {
- public function __construct($cfgs)
- {
- parent::__construct($cfgs);
- }
- //统一请求
- private function url_request($message, $method)
- {
- $params['msgtype'] = 'request_msg';
- $params['format'] = 'json';
- $params['version'] = '1.0';
- $params['app_id'] = config::APP_ID;
- $params['timestamp'] = $this->getMillisecond();
- $params['method'] = $method;
- $params['channel'] = 'wap';
- $params['request_id'] = "REQ".$params['timestamp'];
- $params['message'] = $message;
- $sign = $this->sign($params);
- $params['sign'] = $sign;
- $url = config::API_URL;
- return http_request($url, $params, 'POST', false, []);
- }
- private function req_params(int $phone, int $card_type, int $amount, string $order_sn)
- {
- $params['goods_sku'] = config::SKU[$card_type][$amount];
- $params['app_order_no'] = $order_sn;
- $params['goods_count'] = 1;
- $charge_params = [];
- $charge_params = (object)$charge_params;
- $charge_params->charge_account_number = "{$phone}";
- $charge_params->charge_type = 0;
- $charge_params->charge_account_type = 'MOBILE';
- $charge_params->charge_account_os_type = '';
- $params['charge_query_info'] = $charge_params;
- $params['notify_url'] = config::NOTIFY_URL;
- return $params;
- }
- public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
- {
- $params = $this->req_params($card_no, $card_type, $amount, $params['order_sn']);
- $message = $this->pub_message($params);
- $req['msgtype'] = 'request_msg';
- $req['format'] = 'json';
- $req['version'] = '1.0';
- $req['app_id'] = config::APP_ID;
- $req['timestamp'] = $this->getMillisecond();
- $req['method'] = 'order.charge.query';
- $req['channel'] = 'wap';
- $req['request_id'] = "REQ".$params['timestamp'];
- $req['message'] = $message;
- $sign = $this->sign($req);
- $req['sign'] = $sign;
- $resp = http_request(config::API_URL, $req, 'POST', false, [], $net_errno);
- if (empty($resp)) {
- return [false, '系统错误', true];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '系统错误', true];
- } elseif ($resp['code'] === '000000') {
- return [true, $resp['result']['trade_no'], false];
- } else {
- return [false, $resp['msg'], false];
- }
- }
- }
- public function query($refill_info)
- {
- $params['app_order_no'] = $refill_info['order_sn'];
- $message = $this->pub_message($params);
- $resp = $this->url_request($message,'order.charge.query');
- if (empty($resp)) {
- return [false, '系统错误'];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '系统错误'];
- }
- elseif ($resp['code'] === '000000')
- {
- $status = $resp['result']['charge_result'];
- if ($status === 'SUCCESS') {
- $order_state = ORDER_STATE_SUCCESS;
- } elseif ($status === 'FAILED') {
- $order_state = ORDER_STATE_CANCEL;
- } elseif ($status === 'WAITTING') {
- $order_state = ORDER_STATE_SEND;
- } else {
- return [false, $resp['msg']];
- }
- return [true, $order_state];
- }
- else {
- return [false, $resp['msg']];
- }
- }
- }
- private function sign($params)
- {
- $content = '';
- ksort($params);
- foreach ($params as $key => $val){
- $content .= "{$key}={$val}&";
- }
- $content = rtrim($content,'&');
- $res = openssl_pkey_get_private(config::PRIVATE_KEY);
- openssl_sign($content, $sign, $res);
- return bin2hex($sign);
- }
- private function getMillisecond() {
- list($t1, $t2) = explode(' ', microtime());
- return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
- }
- private function pub_message($params): string
- {
- $encrypt = json_encode($params, JSON_UNESCAPED_SLASHES);
- return openssl_encrypt($encrypt, 'AES-256-ECB', config::AES_KEY, 0, '');
- }
- }
|