'mobile', 'cash' => 'amount', ); $this->api = 'api/oil_card/get_oilCardCode_notInvoice'; return $this->submit($param, $check); } # 通知处理 主要返回状态 2是成功 3是失败 public function notify($data) { $request = $data; $result = array(); $result['cash'] = 1; if ($data['status'] == 1) { $result['status'] = 2; } elseif ($data['status'] == 2) { $result['status'] = 3; } else { $result['status'] = 4; } $result['yes'] = 'SUCCESS'; $result['data'] = $data; return $result; } # 数据响应格式处理 public function response($data) { $log['type'] = 'response'; $log['data'] = $data; $log['config'] = $this->data; $this->log($log); $array = $this->json_decode($data); if (!$array) { $msg = 'error'; } elseif (isset($array['status']) && $array['status'] == '0') { # 正确 $msg = 'ok'; } else { # 错误 $msg = isset($array['errorMessage']) ? $array['errorMessage'] : 'error'; } return array ( 'msg' => $msg, 'data' => $array, 'array' => $array, ); } # 查询接口 public function query($param) { } # 提交数据 private function submit($param, $check) { $param = $this->param($param, $check); if (is_string($param) || (is_array($param) && $param['status'] == 1)) { return $param; } $request = $param['detail']; $request['pnsn'] = $this->mid; $request['sn'] = $param['order']; $mtimestamp = sprintf("%.3f", microtime(true)); $timestamp = floor($mtimestamp); $milliseconds = round(($mtimestamp - $timestamp) * 1000); $request['dt'] = date("YmdHis", $timestamp) . $milliseconds; $url = $this->host . $this->api; $args = $this->_sign($request, array('dt', 'amount', 'mobile', 'sn', 'pnsn')); $response = $this->curl('get', $url . '?args=' . $args); $response = $this->response($response); $channel_order_id = ''; $this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']); return $response['msg']; } private function _sign($request, $param) { $string = ''; foreach ($param as $k => $v) { $string .= $v . '=' . $request[$v] . '&'; } $public_key = RsaUtil::url_safe_decode($this->token); $public_key = "-----BEGIN PUBLIC KEY-----\n" . $public_key . "\n-----END PUBLIC KEY-----"; $result = RsaUtil::publicEncrypt($string, $public_key); $result = urlencode($result); $result = $this->mid . ',' . $result; return $result; } public function decode($data) { $private_key = RsaUtil::url_safe_decode($this->private_key); $private_key = "-----BEGIN PRIVATE KEY-----\n" . $private_key . "\n-----END PRIVATE KEY-----"; $result = RsaUtil::privateDecrypt($string, $private_key); parse_str($result, $result); return $result; } } class RsaUtil { private static function getPublicKey($publicKey) { return openssl_pkey_get_public($publicKey); } private static function getPrivateKey($privateKey) { return openssl_pkey_get_private($privateKey); } public static function publicEncrypt($data, $publicKey) { if (!is_string($data)) { return null; } return openssl_public_encrypt($data, $encrypted, self::getPublicKey($publicKey)) ? base64_encode($encrypted) : null; } public static function publicDecrypt($encrypted, $publicKey) { if (!is_string($encrypted)) { return null; } return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::getPublicKey($publicKey))) ? $decrypted : null; } public static function privateEncrypt($data, $privateKey) { if(!is_string($data)){ return null; } return openssl_private_encrypt($data, $encrypted, self::getPrivateKey($privateKey)) ? base64_encode($encrypted) : null; } public static function privateDecrypt($encrypted, $privateKey) { if(!is_string($encrypted)){ return null; } return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey($privateKey)))? $decrypted : null; } /** * @param $string * @return string|string[] * url安全解码 */ public static function url_safe_decode($string) { $data = str_replace(['-','_'], ['+','/'], $string); $mod4 = strlen($data) % 4; if ($mod4) { $data .= substr('====', $mod4); } return $data; } /** * @param $string * @return string|string[] * url安全转码 */ public static function url_safe_encode($string) { return str_replace(['+','/','='], ['-','_',''], $string); } }