host; } public function notify_api() { $msg = Dever::input(); Dever::log($msg, 'yspay'); $order = $msg['order']; if (!$order) { echo 'FAILED';die; } if (strstr($order, 'TX')) { $log = Dever::db('pay/yspay_tixian_log')->one(array('order_num' => $order)); if ($log) { Dever::db('pay/yspay_tixian_log')->update(array('where_id' => $log['id'], 'status' => 2, 'response' => Dever::json_encode($msg))); $account = Dever::db('pay/yspay_merchant')->one($log['merchant_id']); if ($account) { Dever::db('pay/yspay_merchant')->dec(array('where_id' => $account['id'], 'set_yue' => $log['cash'])); } } echo 'SUCCESS';die; } $where['order_num'] = $order; $order = Dever::db('yspay/sell_order')->find($where); $class = new Chinaums(); if ($order && $order['withdraw'] == 1) { $content = $msg['content']; $content = $class->privateDecrypt($content); $log = Dever::db('pay/yspay_tixian_log')->one(array('order_num' => $order, 'shop_id' => $order['shop_id'])); if ($content && isset($content['responseCode']) && ($content['responseCode'] == '000000' || $content['responseCode'] == '000001')) { Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'withdraw' => 2)); if ($log) { Dever::db('pay/yspay_tixian_log')->update(array('where_id' => $log['id'], 'status' => 2, 'response' => Dever::json_encode($content))); $account = Dever::db('shop/yspay_account')->one(array('shop_id' => $log['shop_id'])); if ($account) { Dever::db('pay/yspay_account')->dec(array('where_id' => $account['id'], 'set_cash' => $log['cash'])); } } echo 'SUCCESS';die; } else { Dever::load('shop/lib/cron')->yspayOne($order); } } echo 'FAILED';die; } public function shop_host($type,$content=false) { if ($type == 1) { return $this->host; } elseif($type == 2){ return $this->mch_id; } elseif($type == 3){ return $this->sys_id; } elseif($type == 4 && $content){ return $this->createSignature($content); } elseif($type == 5 && $content){ return $this->privateDecrypt($content); } } # 查询余额 public function query($mid) { $url = $this->host() . 'uisouterfront/qrywithdrawbalance/process'; $mid = $mid ? $mid : $this->mch_id; //整理内容信息 $content = [ 'sysId' => $this->sys_id, 'mchntNo' => $mid, 'timestamp' => date('YmdHis'), ]; //设置签名并加密 $body['content'] = $this->createSignature($content); $result = Dever::curl($url, $body, 'post'); $decryptData = $this->privateDecrypt($result); if (isset($decryptData['tzWithdrawAmtPublic'])) { return $decryptData['tzWithdrawAmtPublic']; } elseif (isset($decryptData['t0WithdrawAmtPublic'])) { return $decryptData['t0WithdrawAmtPublic']; } return 0; } # 查询提现记录 public function record($mid, $start, $end, $page = '1') { $url = $this->host() . 'uisouterfront/withdraw/qryWithDrawLog'; $mid = $mid ? $mid : $this->mch_id; //整理内容信息 $content = [ 'sysId' => $this->sys_id, 'mchntNo' => $mid, 'timestamp' => date('YmdHis'), 'page' => (string) $page, 'beginDate' => $start, 'endDate' => $end, ]; //设置签名并加密 $body['content'] = $this->createSignature($content); $result = Dever::curl($url, $body, 'post'); $result = Dever::json_decode($result); $decryptData = $this->privateDecrypt($result['result']); if (isset($decryptData['recordList']) && $decryptData['recordList']) { $data = Dever::json_decode($decryptData['recordList']); if ($page == 1 && isset($decryptData['totalPage']) && $decryptData['totalPage'] > 1) { for ($i = 2; $i <= $decryptData['totalPage']; $i++) { $temp = $this->record($mid, $start, $end, $i); if ($temp) { $data = array_merge($data, $temp); } } } return $data; } return array(); } # 提现 public function tixian($config, $merchant, $info, $auto = 1) { $url = $this->host() . 'uisouterfront/withdrawback/process'; $mid = $merchant['mid']; $order_num = $info['order_num']; $cash = $info['cash']; $notify = Dever::url('yspay/single.notify?order=' . $info['order_num'], 'pay'); //整理内容信息 $content = [ 'sysId' => $this->sys_id, 'mchntNo' => $mid, 'timestamp' => date('YmdHis'), 'sysOrderId' => $order_num, 'responseUrl' => $notify, 'withdrawType' => 2, 'withdrawAmt' => $cash, ]; Dever::log($content, 'yspay_single_tixian'); $data['merchant_id'] = $merchant['id']; $data['mid'] = $mid; $data['order_num'] = $order_num; $data['type'] = 2; $data['cash'] = $cash; $data['tdate'] = time(); $data['status'] = 1; $data['desc'] = ''; $data['mtype'] = $auto; Dever::db('pay/yspay_tixian_log')->insert($data); //设置签名并加密 $body['content'] = $this->createSignature($content); $result = Dever::curl($url, $body, 'post'); $decryptData = $this->privateDecrypt($result); if (isset($decryptData['responseCode']) && ($decryptData['responseCode'] == '000000' || $decryptData['responseCode'] == '000001')) { return 'ok'; } else { if ($decryptData['responseDesc'] == '订单号已存在' && $info && isset($info['id'])) { Dever::db('pay/yspay_cash')->update(array('where_id' => $info['id'], 'status' => 3)); } return $decryptData['responseDesc']; } } /** * 私钥解密 * @Author Abnermouke * @Originate in Abnermouke's MBP * @Time 2021-08-26 16:36:58 * @param $encryptData * @return string */ public function privateDecrypt($encryptData) { $content = pack('H*', $encryptData); $result = ''; foreach (str_split($content, 256) as $block) { openssl_private_decrypt($block, $dataDecrypt, $this->formatRsaKey($this->private_key_pkcs1), OPENSSL_PKCS1_PADDING); $result .= $dataDecrypt; } $result = Dever::json_decode($result); return $result; } /** * 创建签名 * @Author Abnermouke * @Originate in Abnermouke's MBP * @Time 2021-08-26 16:34:49 * @param $body * @return false|string */ private function createSignature($body) { //重新排序信息 ksort($body); //整理加密字符串 $signature_string = ''; //循环数据 foreach ($body as $key => $value) { //整合字符串 $signature_string .= $key.'='.$value.'&'; } //整理加密字符串 $signature_string = rtrim($signature_string, '&'); //echo $signature_string; //获取加密数组 $array_json = $this->rsaEncrypt($signature_string); //设置签名 $body['sign'] = bin2hex($array_json); //print_r($body); //json序列化 $json_data = json_encode($body); //加密RSA $json_rsa = $this->publicRsa($json_data); //返回结果 return $json_rsa; } /** * 私钥加密 * @Author Abnermouke * @Originate in Abnermouke's MBP * @Time 2021-08-26 16:35:08 * @param string $str * @return false */ private function rsaEncrypt($str=''){ $charSet = mb_detect_encoding($str, ["UTF-8", "GB2312", "GBK"]); $str = mb_convert_encoding($str, "UTF-8", $charSet); $pri_key = $this->private_key; $pri_key = $this->formatRsaKey($pri_key, 'RSA PRIVATE'); $pi_key = openssl_pkey_get_private($pri_key); if(!$pi_key) return false; $algo = "SHA256"; openssl_sign($str, $binary_signature, $pi_key, $algo); return $binary_signature; } /** * 公钥加密 * @Author Abnermouke * @Originate in Abnermouke's MBP * @Time 2021-08-26 16:35:14 * @param string $str * @return false|string */ private function publicRsa($str='') { $charSet = mb_detect_encoding($str, ["UTF-8", "GB2312", "GBK"]); $str = mb_convert_encoding($str, "UTF-8", $charSet); $pub_key = $this->public_key; $pub_key = $this->formatRsaKey($pub_key, 'PUBLIC'); $pu_key = openssl_pkey_get_public($pub_key); if(!$pu_key) return false; $result = ''; foreach (str_split($str,245) as $chunk) { if(openssl_public_encrypt($chunk,$encryptData, $pub_key)){ $result .= $encryptData; } } $result = bin2hex($result); openssl_free_key($pu_key); return $result; } /** * 格式化密钥 * @Author Abnermouke * @Originate in Abnermouke's MBP * @Time 2021-08-26 16:36:36 * @param $key * @param string $alias * @param int $length * @return string */ private function formatRsaKey($key, $alias = 'RSA PRIVATE', $length = 64) { //拆分格式 $key = chunk_split($key, (int)$length, "\n"); //生成pem $pem = "-----BEGIN $alias KEY-----\n".$key."-----END $alias KEY-----"; //返回pem return $pem; } }