123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848 |
- <?php
- namespace app\common\library;
- use function EasyWeChat\Kernel\data_to_array;
- class Sandpay
- {
-
- protected $mer_no = '';
- protected $mer_key = '';
- protected $domain_url = '';
- public function __construct(){
- $this->mer_no = '6888806122762';
- $this->mer_key = '6mksKr1IsfU0kTD9DNayV67cvfYJVQWLOOLYwJX1rOq5DJVsppgr07JYEMG5g+4hgx/Dlc/pW00=';
- $this->business_private = APP_PATH.'common/library/sandpay/prdnew.pfx';
- $this->sandpay_public = APP_PATH.'common/library/sandpay/prdnew.cer';
- $this->sandpublic = APP_PATH.'common/library/sandpay/sand.cer';
- $this->privateKeyPwd = 'jin860815';
- $this->domain_url = config('domain_url');
- $this->h5_url = config('h5_url');
- $this->md5key = 'kYgEWLWLOUxnxHgcqzdIwvNT9uGPGAAdfe7HyxdHNxa6dfGXO4+DEF+oWkvKRuAcS0h5VeRxfu6SZNbopT9Gz77aAb//j5EciX8mUUtf+8P0ELLrObXih1KbHnhxHKtA6VEMOSJPtypixuz+UKFTiw==';
- }
-
- public function wechat($params=[]){
- $result = [
- 'status' => 1,
- 'msg' => '',
- 'data' => [],
- ];
- try {
-
- $time = time();
- $createTime = date('YmdHis',$time);
-
- $payExtra = ['wx_app_id'=>'wx64e2709ce8095ab6','gh_ori_id'=>'gh_be8042ce502b','path_url'=>'pages/zf/index?','miniProgramType'=>'2'];
- $ip = request()->ip();
- $ipStr = str_replace('.','_',$ip);
- $orderNo = isset($params['order_no']) ? $params['order_no'] : 'P23082914483724021875';
- $goodsName = isset($params['goods_name']) ? $params['goods_name'] : 'test';
- $money = isset($params['money']) ? $params['money'] : "0.1";
- $type = isset($params['type']) ? $params['type'] : 'gold';
- $extend = ['type' => $type];
- $payExtraStr = json_encode($payExtra);
- $payExtraStr = stripslashes($payExtraStr);
-
- $paramsData = [
- 'version' => '10',
- 'mer_no' => $this->mer_no,
- 'mer_order_no' => $orderNo,
- 'create_time' => $createTime,
- 'order_amt' => $money,
- 'notify_url' => 'https://zhiliao.huxiukeji.com/api/sandpay/notify',
- 'create_ip' => $ipStr,
- 'pay_extra' => $payExtraStr,
- 'accsplit_flag' => 'NO',
- 'sign_type' => 'RSA',
- 'store_id' => '000000',
-
-
- 'extend' => json_encode($extend),
-
- ];
- $paramsDataTemp = $this->getSignContent($paramsData);
-
-
- $pubKey = $this->loadX509Cert($this->sandpay_public);
- $priKey = $this->loadPk12Cert($this->business_private, $this->privateKeyPwd);
-
-
-
-
- $sign = $this->signNew($paramsDataTemp);
-
-
-
-
-
-
-
- $expireTime = date('YmdHis',$time + 60 * 30);
- $metaOption = [['s'=>'Android','n'=> '','id'=>'','sc'=>'']];
- $paramsNo = [
- 'return_url' => '',
- 'expire_time' => $expireTime,
- 'goods_name' => $goodsName,
- 'product_code' => '02010005',
- 'clear_cycle' => '3',
- 'sign' => $sign,
- 'jump_scheme' => 'sandcash://scpay',
- 'meta_option' => json_encode($metaOption),
- 'limit_pay' => '',
- 'extend_params' => '',
- ];
- $paramsAll = array_merge($paramsData,$paramsNo);
- ksort($paramsAll);
- $result['data'] = json_encode($paramsAll,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
-
-
-
-
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
-
- public function wechath5($params=[]){
- $result = [
- 'status' => 1,
- 'msg' => '',
- 'data' => [],
- ];
- try {
-
- $time = time();
- $createTime = date('YmdHis',$time);
-
- $payExtra = ['resourceAppid'=>'wxd710096818e8f24a','resourceEnv'=>'wxd710096818e8f24a-5dvn9bbdc0aa5'];
- $ip = request()->ip();
- $ipStr = str_replace('.','_',$ip);
- $orderNo = isset($params['order_no']) ? $params['order_no'] : 'P23091214483724021876';
- $goodsName = isset($params['goods_name']) ? $params['goods_name'] : 'test';
- $money = isset($params['money']) ? $params['money'] : "0.1";
- $type = isset($params['type']) ? $params['type'] : 'gold';
- $extend = json_encode(['type' => $type]);
- $payExtraStr = json_encode($payExtra);
- $url = 'https://sandcash.mixienet.com.cn/pay/h5/applet?';
-
- $dumain = $_SERVER['HTTP_HOST'];
- $notifyUrl = 'https://'.$dumain.'/api/sandpay/notify';
- $paramsData = [
- 'version' => '10',
- 'mer_no' => $this->mer_no,
- 'mer_order_no' => $orderNo,
- 'create_time' => $createTime,
- 'order_amt' => $money,
- 'notify_url' => $notifyUrl,
-
- 'create_ip' => $ipStr,
- 'pay_extra' => $payExtraStr,
- 'accsplit_flag' => 'NO',
- 'sign_type' => 'RSA',
- 'store_id' => '000000',
-
-
- 'extend' => $extend,
-
- ];
- $paramsDataTemp = $this->getSignContent($paramsData);
- $sign = $this->signNew($paramsDataTemp);
-
- $expireTime = date('YmdHis',$time + 60 * 30);
- $metaOption = json_encode([['s'=>'Android','n'=> '','id'=>'','sc'=>''],['s'=>'IOS','n'=> '','id'=>'','sc'=>'']]);
- $paramsNo = [
-
- 'expire_time' => $expireTime,
- 'goods_name' => $goodsName,
- 'product_code' => '02010006',
- 'clear_cycle' => '3',
- 'sign' => $sign,
- 'jump_scheme' => 'sandcash://scpay',
- 'meta_option' => $metaOption,
- 'limit_pay' => '',
- 'extend_params' => '',
- ];
- $paramsAll = array_merge($paramsData,$paramsNo);
- if (!empty($paramsAll)) {
-
- foreach ($paramsAll as $key => &$value) {
- if (in_array($key,['goods_name','notify_url','return_url','pay_extra','meta_option','extend','merch_extend_params','sign'])) {
- $value = urlencode($value);
- }
- }
- }
- $urlStr = $url.$this->getSignContent($paramsAll);
- $result['data'] = $urlStr;
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
-
-
- public function payout($order_no,$money,$remark,$bank,$realname){
- $money = $this->format_money($money);
- $data = [
- 'version' => 10,
- 'productId' => '00000004',
- 'tranTime' => date('YmdHis', time()),
- 'orderCode' => $order_no,
- 'tranAmt' => $money,
- 'currencyCode' => '156',
- 'accAttr' => '0',
- 'accType' => '4',
- 'accNo' => $bank,
- 'accName' => $realname,
- 'remark' => $remark,
- 'extend' => '',
- ];
- $config = [
- 'publicKeyPath' => $this->sandpay_public,
- 'privateKeyPath' => $this->business_private,
- 'privateKeyPwd' => $this->privateKeyPwd,
- 'apiUrl' => 'https://caspay.sandpay.com.cn/agent-main/openapi/agentpay',
- 'variable' =>[
- 'transCode' => 'RTPM',
- 'accessType' => '0',
- 'merId' => $this->mer_no,
- ],
- ];
- $post = $config['variable'];
-
- $datalist = [];
- $datalist['body'] = $data;
-
- $AESKey = $this->aes_generate(16);
- $pubKey = $this->loadX509Cert($config['publicKeyPath']);
- $priKey = $this->loadPk12Cert($config['privateKeyPath'], $config['privateKeyPwd']);
- $encryptKey = $this->RSAEncryptByPub($AESKey, $pubKey);
-
- $encryptData = $this->AESEncrypt($datalist['body'], $AESKey);
-
- $sign = $this->sign($datalist['body'], $priKey);
-
- $post['sign'] = $sign;
- $post['encryptKey'] = $encryptKey;
- $post['encryptData'] = $encryptData;
- $datalist['head'] = $post;
- $url = $config['apiUrl'];
- $ret = $this->http_post_json($url,$post);
- parse_str($ret, $arr);
- try {
-
- $decryptAESKey = $this->RSADecryptByPri($arr['encryptKey'], $priKey);
-
- $decryptPlainText = $this->AESDecrypt($arr['encryptData'], $decryptAESKey);
-
- $this->tixian_verify($decryptPlainText, $arr['sign'], $pubKey);
-
-
-
-
-
- $decryptPlainText = json_decode($decryptPlainText,true);
- if(is_array($decryptPlainText) && isset($decryptPlainText['respCode']) && $decryptPlainText['respCode'] == '0000'){
- return true;
- }
- if(is_array($decryptPlainText) && isset($decryptPlainText['respCode']) && in_array($decryptPlainText['respCode'],['0001','0002'])){
- return 'wait';
- }
- if(is_array($decryptPlainText) && isset($decryptPlainText['respCode']) && isset($decryptPlainText['respDesc'])){
- return $decryptPlainText['respCode'].':'.$decryptPlainText['respDesc'];
- }
- return '提现失败';
-
- } catch (\Exception $e) {
- return json_encode([
- 'json' => $e->getMessage(),
- 'url' => 'https://open.sandpay.com.cn/product/detail/43324/43895/',
- ]);
- echo $e->getMessage();
- exit;
- }
- }
-
- public function onekey_dopayment($order_no,$money,$remark,$user_id){
- $data = [
- 'version' => 10,
- 'mer_no' => $this->mer_no,
- 'mer_key' => $this->mer_key,
- 'mer_order_no' => $order_no,
- 'create_time' => date('YmdHis'),
- 'expire_time' => date('YmdHis', time()+30*60),
- 'order_amt' => ''.$money.'',
- 'notify_url' => $this->domain_url.'/api/sandpay/notify',
- 'return_url' => $this->h5_url . '/#/pages/public/paySuc?type=recharge',
- 'create_ip' => str_replace('.','_',request()->ip()),
- 'goods_name' => $remark,
- 'store_id' => '000000',
- 'product_code' => '05030001',
-
-
- 'clear_cycle' => '3',
- 'pay_extra' => json_encode(['userId'=>'test'.$user_id]),
- 'accsplit_flag' => 'NO',
- 'jump_scheme' => 'sandcash://scpay',
- 'meta_option' => json_encode([['s'=>'Android','n'=>'','id'=>'','sc'=>''],['s'=>'IOS','n'=>'','id'=>'','sc'=>'']]),
- 'sign_type' => 'MD5'
- ];
- $temp = $data;
- unset($temp['goods_name']);
- unset($temp['jump_scheme']);
- unset($temp['expire_time']);
- unset($temp['product_code']);
- unset($temp['clear_cycle']);
- unset($temp['meta_option']);
- $sign = strtoupper(md5($this->getSignContent($temp).'&key='.$this->md5key));
- $data['sign'] = $sign;
- $query = http_build_query($data);
- $payurl = "https://sandcash.mixienet.com.cn/pay/h5/fastpayment?".$query;
- return $payurl;
- }
-
- public function dopayment($order_no,$money,$remark,$banknum = ''){
- $data = [
- 'version' => 10,
- 'mer_no' => $this->mer_no,
- 'mer_key' => $this->mer_key,
- 'mer_order_no' => $order_no,
- 'create_time' => date('YmdHis'),
- 'expire_time' => date('YmdHis', time()+30*60),
- 'order_amt' => ''.$money.'',
- 'notify_url' => $this->domain_url.'/api/sandpay/notify',
- 'return_url' => $this->domain_url.'/index/sandpay/payreturn',
- 'create_ip' => str_replace('.','_',request()->ip()),
- 'goods_name' => $remark,
- 'store_id' => '000000',
- 'product_code' => '06030001',
-
-
- 'clear_cycle' => '3',
- 'pay_extra' => json_encode(['cardNo'=>$banknum]),
- 'accsplit_flag' => 'NO',
- 'jump_scheme' => 'sandcash://scpay',
- 'meta_option' => json_encode([['s'=>'Android','n'=>'','id'=>'','sc'=>''],['s'=>'IOS','n'=>'','id'=>'','sc'=>'']]),
- 'sign_type' => 'MD5'
- ];
- if(!$banknum){
- unset($data['pay_extra']);
- }
- $temp = $data;
- unset($temp['goods_name']);
- unset($temp['jump_scheme']);
- unset($temp['expire_time']);
- unset($temp['product_code']);
- unset($temp['clear_cycle']);
- unset($temp['meta_option']);
- $sign = strtoupper(md5($this->getSignContent($temp).'&key='.$this->md5key));
- $data['sign'] = $sign;
- $query = http_build_query($data);
- $payurl = "https://sandcash.mixienet.com.cn/pay/h5/unionpayh5?".$query;
- return $payurl;
- }
- private function getSignContent($params) {
- ksort($params);
- $stringToBeSigned = "";
- $i = 0;
- foreach ($params as $k => $v) {
- if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
- if ($i == 0) {
- $stringToBeSigned .= "$k" . "=" . "$v";
- } else {
- $stringToBeSigned .= "&" . "$k" . "=" . "$v";
- }
- $i++;
- }
- }
- unset ($k, $v);
- return $stringToBeSigned;
- }
- private function getSignContentNew($params) {
- ksort($params);
- $stringToBeSigned = "";
- $i = 0;
- foreach ($params as $k => $v) {
- if ("@" != substr($v, 0, 1)) {
- if ($i == 0) {
- $stringToBeSigned .= "$k" . "=" . "$v";
- } else {
- $stringToBeSigned .= "&" . "$k" . "=" . "$v";
- }
- $i++;
- }
- }
- unset ($k, $v);
- return $stringToBeSigned;
- }
- private function checkEmpty($value)
- {
- if (!isset($value))
- return true;
- if ($value === null)
- return true;
- if (trim($value) === "")
- return true;
- return false;
- }
-
- public function verify($plainText, $sign)
- {
- $publickey = $this->publicKey();
- if($publickey === false){
- return 0;
- }
- $resource = openssl_pkey_get_public($publickey);
- $result = openssl_verify($plainText, base64_decode($sign), $resource);
- openssl_free_key($resource);
- return $result;
- }
- public function publicKey()
- {
- try {
- $file = file_get_contents($this->sandpublic);
- if (!$file) {
- throw new \Exception('getPublicKey::file_get_contents ERROR');
- }
- $cert = chunk_split(base64_encode($file), 64, "\n");
- $cert = "-----BEGIN CERTIFICATE-----\n" . $cert . "-----END CERTIFICATE-----\n";
- $res = openssl_pkey_get_public($cert);
- $detail = openssl_pkey_get_details($res);
- openssl_free_key($res);
- if (!$detail) {
- throw new \Exception('getPublicKey::openssl_pkey_get_details ERROR');
- }
- return $detail['key'];
- } catch (\Exception $e) {
- throw $e;
- }
- }
-
- function loadX509Cert($path)
- {
- try {
- $file = file_get_contents($path);
- if (!$file) {
- throw new \Exception('loadx509Cert::file_get_contents ERROR');
- }
- $cert = chunk_split(base64_encode($file), 64, "\n");
- $cert = "-----BEGIN CERTIFICATE-----\n" . $cert . "-----END CERTIFICATE-----\n";
- $res = openssl_pkey_get_public($cert);
- $detail = openssl_pkey_get_details($res);
- openssl_free_key($res);
- if (!$detail) {
- throw new \Exception('loadX509Cert::openssl_pkey_get_details ERROR');
- }
- return $detail['key'];
- } catch (\Exception $e) {
- throw $e;
- }
- }
-
- function loadPk12Cert($path, $pwd)
- {
- try {
- $file = file_get_contents($path);
- if (!$file) {
- throw new \Exception('loadPk12Cert::file
- _get_contents');
- }
- if (!openssl_pkcs12_read($file, $cert, $pwd)) {
- throw new \Exception('loadPk12Cert::openssl_pkcs12_read ERROR');
- }
- return $cert['pkey'];
- } catch (\Exception $e) {
- throw $e;
- }
- }
-
- function sign($plainText, $path)
- {
- $plainText = json_encode($plainText);
- try {
- $resource = openssl_pkey_get_private($path);
- $result = openssl_sign($plainText, $sign, $resource);
- openssl_free_key($resource);
- if (!$result) {
- throw new \Exception('签名出错' . $plainText);
- }
- return base64_encode($sign);
- } catch (\Exception $e) {
- throw $e;
- }
- }
- function signNew($str) {
- $file = file_get_contents($this->business_private);
- if (!$file) {
- throw new \Exception('loadPk12Cert::file
- _get_contents');
- }
- if (!openssl_pkcs12_read($file, $cert, $this->privateKeyPwd)) {
- throw new \Exception('loadPk12Cert::openssl_pkcs12_read ERROR');
- }
- $pem = $cert['pkey'];
- openssl_sign($str, $sign, $pem);
- $sign = base64_encode($sign);
- return $sign;
- }
-
- function tixian_verify($plainText, $sign, $path)
- {
- $resource = openssl_pkey_get_public($path);
- $result = openssl_verify($plainText, base64_decode($sign), $resource);
- openssl_free_key($resource);
- if (!$result) {
- throw new \Exception('签名验证未通过,plainText:' . $plainText . '。sign:' . $sign, '02002');
- }
- return $result;
- }
-
- function RSAEncryptByPub($plainText, $puk)
- {
- if (!openssl_public_encrypt($plainText, $cipherText, $puk, OPENSSL_PKCS1_PADDING)) {
- throw new \Exception('AESKey 加密错误');
- }
- return base64_encode($cipherText);
- }
-
- function RSADecryptByPri($cipherText, $prk)
- {
- if (!openssl_private_decrypt(base64_decode($cipherText), $plainText, $prk, OPENSSL_PKCS1_PADDING)) {
- throw new \Exception('AESKey 解密错误');
- }
- return (string)$plainText;
- }
-
- function AESEncrypt($plainText, $key)
- {
- $plainText = json_encode($plainText);
- $result = openssl_encrypt($plainText, 'AES-128-ECB', $key, 1);
- if (!$result) {
- throw new \Exception('报文加密错误');
- }
- return base64_encode($result);
- }
-
- function AESDecrypt($cipherText, $key)
- {
- $result = openssl_decrypt(base64_decode($cipherText), 'AES-128-ECB', $key, 1);
- if (!$result) {
- throw new \Exception('报文解密错误', 2003);
- }
- return $result;
- }
-
- function aes_generate($size)
- {
- $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- $arr = array();
- for ($i = 0; $i < $size; $i++) {
- $arr[] = $str[mt_rand(0, 61)];
- }
- return implode('', $arr);
- }
-
- function http_post_json($url, $param)
- {
- if (empty($url) || empty($param)) {
- return false;
- }
- $param = http_build_query($param);
- try {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $data = curl_exec($ch);
- curl_close($ch);
- if (!$data) {
- throw new \Exception('请求出错');
- }
- return $data;
- } catch (\Exception $e) {
- throw $e;
- }
- }
- public function format_money($money){
- $money = $money * 100;
- $zero_arr = [
- 0 => '000000000000',
- 1 => '00000000000',
- 2 => '0000000000',
- 3 => '000000000',
- 4 => '00000000',
- 5 => '0000000',
- 6 => '000000',
- 7 => '00000',
- 8 => '0000',
- 9 => '000',
- 10 => '00',
- 11 => '0',
- 12 => '',
- ];
- $newmoney = $zero_arr[strlen($money)] . $money;
- return $newmoney;
- }
- protected function parseResult($result)
- {
- $arr = array();
- $response = urldecode($result);
- $arrStr = explode('&', $response);
- foreach ($arrStr as $str) {
- $p = strpos($str, "=");
- $key = substr($str, 0, $p);
- $value = substr($str, $p + 1);
- $arr[$key] = $value;
- }
- return $arr;
- }
- }
|