123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php namespace Pay\Yspay;
- use Dever;
- /**
- * 银联商务代付
- * Class Chinaums
- */
- class Account
- {
- //测试地址
- protected $test_host = 'https://mobl-test.chinaums.com/';
- //正式地址
- protected $host = 'https://managefree.chinaums.com/';
- //系统编号(sysId)
- protected $sys_id = '10119';
- //商户号
- protected $mch_id = '89810007372106N';
- //外部平台私钥
- protected $private_key = 'MIIEowIBAAKCAQEApzpOtvWGclmdw+/aEzAxwyTgUwnC9qjQEhL+qfMWDBIbFR8N4q6GQ8fRKZ9Zyvm0voFhgpH44Y6sh2U7rF9HQOuw2puywvU8kk5JAUxpxykbH7QqW/ijH1gBG3Ck/Ip/5I4co+q9K/CjQRmId5nSJ8ffnaiAOogNZycrP+AgEEU8vL1eNtnagVxT3EC1LEh0wBwecT9Tkyi0tMD0sMeIUl9POrnh7YADJtUzGf+TTNrqK6DhoT9mE3Plw0pqxKSk4/tHNx1ZIXf4I/UeYSEAreXHWVfLQ/if1C9HRYhEGB57mmqn0JdmsSRD7DX7eU7Oja+RqtWfQ/KH+2fnjq9uSwIDAQABAoIBABACUvrM8nwL/EB/u77OhFUMOLV6X8HBU3oHe33Fmby6FwGkoW2sC4p9nJaBfNUjppv+Qtl/I/Zj9DxaAqZz0Jx0c8/Zy/lCBPR0riSDrjS0yLX2R3/ag7NCd4iq9gB/OdMlj7RZipakSNy4lwnsHeRVDCFJukzgLXhMlaVdzXQBJTdERP3G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PKyhFE6IjdzgiJsqw/Y13DGdFTjQKBgAKjOOgarGX4xKWN0choSZtY95ptpKIIFHfQSSj7VHXR91aNhLlpst+/n6tjrt11pLG9amFHuX2ZQpCpizCouDZ6cyf7TokS2aNu9hWadbmYkYpo1ZAw82MGjRH7zTEQkAYBMVyfg+AVTcf9Fcl9eCX352FDNzoxW8j8U3ZzDTgTAoGBAKne5oH2+kPHLeDrm1gxR4DI8CF79xRoJytZpUJYgl13ckjGexim/l6sqJRC2e2zZvrdf/3OcLP/XPpjF0oT1qKg5ry0/N5nqY9287WwkemKrtNHRpXBl5xBU6IMpRBfwu6RGXAXzXRzVA9BV5j3dcuV78vIaagsGUozPCW6PDQxAoGANlP96CmdAzfHhNH0RGuTSjkY4sc0WI2RJ+TPNncPadEZP7oZGWZvjQu0/MHLn68CAuoGV8QR9OhowZn5WuXYJ48EGhH+P+ryeXOKpncflvymWodJtfznM41Pq0X5hnadEzGkjaBC15xHOijOb6KUQsI1J3pXukDOmXXgpFTZXmU=';
- //银商系统公钥
- protected $public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhTzN8wxdXYr+/+t8iX0mlol8wFy+0pGtxnyB6PjyrIRjGHrZJA7grZPvy7MOK6PqihqTZh6SO7tX9StxVmTQo+PuibAY/p2i08UxkIBUcucHVvHN8gkM3GBFBqD0uJgUc03cerEI7gNeaKYfCVs0/hMo6CKbdXVKqaE987kXYK209uFcte+Y8YofQ2AYamImpzSf7pXomAu+8D98HIOs5bKITHTk30vPdNhrQdpDRO1TuX0x8bwhCykcwAEdB/Z9YGXkLU5S3QJY2qvCZJY08YdNqIpbDqifSRvOg0sqeKFXPfI2M0GsOh6PArTEmh0QHxDht9xjPPt11aVgh8kgkQIDAQAB';
- private function host()
- {
- return $this->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 <abnermouke@gmail.com>
- * @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 <abnermouke@gmail.com>
- * @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 <abnermouke@gmail.com>
- * @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 <abnermouke@gmail.com>
- * @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 <abnermouke@gmail.com>
- * @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;
- }
- }
|