123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace App\Libs\Pay\Merchants;
- use GuzzleHttp\Client;
- use Log;
- class OriginBank
- {
- function __construct($config)
- {
- $this->open_id = $config['open_id'];
- $this->open_key = $config['open_key'];
- $this->sub_appid = $config['sub_appid'];
- $this->PayClient = new client(['base_uri'=>'https://api.orangebank.com.cn/','timeout'=>3]);
- }
- //生成订单
- function send($data)
- {
- $time = time();
- $base_data = [
- 'open_id'=>$this->open_id,
- 'timestamp'=>$time,
- ];
- // $data = [
- // 'out_no'=>$data['trade_no'],
- // 'pmt_tag'=>'Weixin',
- // 'ord_name'=>'小说充值',
- // 'original_amount'=>$data['price'],
- // 'trade_amount'=>$data['price'],
- // //'notify_url'=>env('ORIGINBANK_NOFITY_URL'),
- // 'sub_appid'=>$this->sub_appid,
- // 'sub_openid'=>$data['openid']
- // ];
- $data =[];
- $data['out_no'] = "123456";
- $data['pmt_tag'] = "WeixinBERL";
- $data['original_amount'] = "1";
- $data['trade_amount'] = "1";
- $data['ord_name'] = "购买物品订单名称";
- dump($data);
- $base_data['data'] = $this->encrypt2(json_encode($data),$this->open_key);
- dump($data);
- dump($this->decrypt2($base_data['data'],$this->open_key));
- $base_data['sign'] = $this->signs($data);
- dump('base_data');dd($base_data);
- $response = $this->PayClient->request('POST','mct1/payorder',['form_params'=>$base_data])->getBody()->getContents();
- dd(json_decode($response,1));
- // return $this->getPayInfo($response);
- }
- function getPayInfo($response)
- {
- try{
- $return_info = json_decode($response,1);
- //dd($return_info);
- if($return_info['errcode'] == 0)
- {
- $pay_info = [
- 'appId'=>$return_info['data']['appId'],
- 'timeStamp'=>$return_info['data']['timeStamp'],
- 'nonceStr'=>$return_info['data']['nonceStr'],
- 'signType'=>$return_info['data']['signType'],
- 'package'=>$return_info['data']['package'],
- 'paySign'=>$return_info['data']['paySign']
- ];
- return $pay_info;
- }
- }catch (\Exception $e)
- {
- return null;
- }
- }
- //签名
- public function signs($array){
- $signature = array();
- foreach($array as $key=>$value){
- $signature[$key]=$key.'='.$value;
- }
- $signature['open_key']='open_key'.'='.$this->open_key;
- ksort($signature);
- #先sha1加密 在md5加密
- $sign_str = md5(sha1(implode('&', $signature)));
- return $sign_str;
- }
- #@todo AES加解密
- #加密
- private function encrypt($input, $key) {
- $size = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
- dump('size');dump($size);
- $input = $this->pkcs5_pad($input, $size);
- $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
- $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
- @mcrypt_generic_init($td, $key, $iv);
- $data = @mcrypt_generic($td, $input);
- @mcrypt_generic_deinit($td);
- @mcrypt_module_close($td);
- $data = strtoupper(bin2hex($data));
- return $data;
- }
- public function encrypt3($key, $iv, $data)
- {
- /**
- * 打开加密
- */
- $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
- /**
- * 初始化加密
- */
- @mcrypt_generic_init($td, $key, $iv);
- /**
- * 加密
- */
- $encrypted = @mcrypt_generic($td, $data);
- /**
- * 清理加密
- */
- @mcrypt_generic_deinit($td);
- /**
- * 关闭
- */
- @mcrypt_module_close($td);
- return base64_encode($encrypted);
- }
- public function encrypt2($input, $key)
- {
- $data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
- $data = base64_encode($data);
- return $data;
- }
- public function decrypt2($sStr, $sKey)
- {
- $decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA);
- return $decrypted;
- }
- private function pkcs5_pad ($text, $blocksize) {
- $pad = $blocksize - (strlen($text) % $blocksize);
- return $text . str_repeat(chr($pad), $pad);
- }
- public function decrypt($sStr, $sKey) {
- $sStr=hex2bin($sStr);
- $decrypted= @mcrypt_decrypt(
- MCRYPT_RIJNDAEL_128,
- $sKey,
- $sStr,
- MCRYPT_MODE_ECB
- );
- $dec_s = strlen($decrypted);
- $padding = ord($decrypted[$dec_s-1]);
- $decrypted = substr($decrypted, 0, -$padding);
- return $decrypted;
- }
- }
|