OriginBank.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Libs\Pay\Merchants;
  3. use GuzzleHttp\Client;
  4. use Log;
  5. class OriginBank
  6. {
  7. function __construct($config)
  8. {
  9. $this->open_id = $config['open_id'];
  10. $this->open_key = $config['open_key'];
  11. $this->sub_appid = $config['sub_appid'];
  12. $this->PayClient = new client(['base_uri'=>'https://api.orangebank.com.cn/','timeout'=>3]);
  13. }
  14. //生成订单
  15. function send($data)
  16. {
  17. $time = time();
  18. $base_data = [
  19. 'open_id'=>$this->open_id,
  20. 'timestamp'=>$time,
  21. ];
  22. $data = [
  23. 'out_no'=>$data['trade_no'],
  24. 'pmt_tag'=>'WeixinOL',
  25. 'ord_name'=>'小说充值',
  26. 'original_amount'=>$data['price'],
  27. 'trade_amount'=>$data['price'],
  28. //'notify_url'=>env('ORIGINBANK_NOFITY_URL'),
  29. 'sub_appid'=>$this->sub_appid,
  30. 'sub_openid'=>$data['openid']
  31. ];
  32. // $data =[];
  33. // $data['out_no'] = "123456";
  34. // $data['pmt_tag'] = "WeixinBERL";
  35. // $data['original_amount'] = "1";
  36. // $data['trade_amount'] = "1";
  37. // $data['ord_name'] = "购买物品订单名称";
  38. dump($data);
  39. $base_data['data'] = $this->encrypt(json_encode($data),$this->open_key);
  40. dump($data);
  41. dump($this->decrypt($base_data['data'],$this->open_key));
  42. $base_data['sign'] = $this->signs($base_data);
  43. dump('base_data');dump($base_data);
  44. $response = $this->PayClient->request('POST','/mct1/payorder',['form_params'=>$base_data])->getBody()->getContents();
  45. dump($this->decrypt(json_decode($response,1)['data'],$this->open_key));
  46. dd($this->getPayInfo($response));
  47. return $this->getPayInfo($response);
  48. }
  49. function getPayInfo($response)
  50. {
  51. try{
  52. $return_info = json_decode($response,1);
  53. dd($return_info);
  54. if($return_info['errcode'] == 0)
  55. {
  56. $data = $this->decrypt($return_info['data'],$this->open_key,true);
  57. $trade_result = $data['trade_result'];
  58. $pay_info = [
  59. 'appId'=>$trade_result['appid'],
  60. 'timeStamp'=>$trade_result['timeStamp'],
  61. 'nonceStr'=>$trade_result['nonce_str'],
  62. 'signType'=>$trade_result['signType'],
  63. 'package'=>'prepay_id=' . $trade_result['prepay_id'],
  64. 'paySign'=>$trade_result['paySign']
  65. ];
  66. return $pay_info;
  67. }
  68. }catch (\Exception $e)
  69. {
  70. return null;
  71. }
  72. }
  73. //签名
  74. public function signs($array){
  75. $signature = array();
  76. foreach($array as $key=>$value){
  77. $signature[$key]=$key.'='.$value;
  78. }
  79. $signature['open_key']='open_key'.'='.$this->open_key;
  80. ksort($signature);
  81. #先sha1加密 在md5加密
  82. $sign_str = md5(sha1(implode('&', $signature)));
  83. return $sign_str;
  84. }
  85. #@todo AES加解密
  86. #加密
  87. private function encrypt($input, $key) {
  88. $size = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  89. dump('size');dump($size);
  90. $input = $this->pkcs5_pad($input, $size);
  91. $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  92. $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  93. @mcrypt_generic_init($td, $key, $iv);
  94. $data = @mcrypt_generic($td, $input);
  95. @mcrypt_generic_deinit($td);
  96. @mcrypt_module_close($td);
  97. $data = strtoupper(bin2hex($data));
  98. return $data;
  99. }
  100. public function encrypt3($key, $iv, $data)
  101. {
  102. /**
  103. * 打开加密
  104. */
  105. $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
  106. /**
  107. * 初始化加密
  108. */
  109. @mcrypt_generic_init($td, $key, $iv);
  110. /**
  111. * 加密
  112. */
  113. $encrypted = @mcrypt_generic($td, $data);
  114. /**
  115. * 清理加密
  116. */
  117. @mcrypt_generic_deinit($td);
  118. /**
  119. * 关闭
  120. */
  121. @mcrypt_module_close($td);
  122. return base64_encode($encrypted);
  123. }
  124. public function encrypt2($input, $key)
  125. {
  126. $data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
  127. $data = base64_encode($data);
  128. return $data;
  129. }
  130. public function decrypt2($sStr, $sKey)
  131. {
  132. $decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA);
  133. return $decrypted;
  134. }
  135. private function pkcs5_pad ($text, $blocksize) {
  136. $pad = $blocksize - (strlen($text) % $blocksize);
  137. return $text . str_repeat(chr($pad), $pad);
  138. }
  139. public function decrypt($sStr, $sKey) {
  140. $sStr=hex2bin($sStr);
  141. $decrypted= @mcrypt_decrypt(
  142. MCRYPT_RIJNDAEL_128,
  143. $sKey,
  144. $sStr,
  145. MCRYPT_MODE_ECB
  146. );
  147. $dec_s = strlen($decrypted);
  148. $padding = ord($decrypted[$dec_s-1]);
  149. $decrypted = substr($decrypted, 0, -$padding);
  150. return $decrypted;
  151. }
  152. }