OriginBank.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. dump($return_info);
  54. if($return_info['errcode'] == 0)
  55. {
  56. $data = json_decode($this->decrypt($return_info['data'],$this->open_key,true),true);
  57. $trade_result = $data['trade_result'];
  58. dd($trade_result);
  59. $pay_info = [
  60. 'appId'=>$trade_result['appid'],
  61. 'timeStamp'=>$trade_result['timeStamp'],
  62. 'nonceStr'=>$trade_result['nonce_str'],
  63. 'signType'=>$trade_result['signType'],
  64. 'package'=>'prepay_id=' . $trade_result['prepay_id'],
  65. 'paySign'=>$trade_result['paySign']
  66. ];
  67. return $pay_info;
  68. }
  69. }catch (\Exception $e)
  70. {
  71. echo $e->getMessage();
  72. return null;
  73. }
  74. }
  75. //签名
  76. public function signs($array){
  77. $signature = array();
  78. foreach($array as $key=>$value){
  79. $signature[$key]=$key.'='.$value;
  80. }
  81. $signature['open_key']='open_key'.'='.$this->open_key;
  82. ksort($signature);
  83. #先sha1加密 在md5加密
  84. $sign_str = md5(sha1(implode('&', $signature)));
  85. return $sign_str;
  86. }
  87. #@todo AES加解密
  88. #加密
  89. private function encrypt($input, $key) {
  90. $size = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  91. dump('size');dump($size);
  92. $input = $this->pkcs5_pad($input, $size);
  93. $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  94. $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  95. @mcrypt_generic_init($td, $key, $iv);
  96. $data = @mcrypt_generic($td, $input);
  97. @mcrypt_generic_deinit($td);
  98. @mcrypt_module_close($td);
  99. $data = strtoupper(bin2hex($data));
  100. return $data;
  101. }
  102. public function encrypt3($key, $iv, $data)
  103. {
  104. /**
  105. * 打开加密
  106. */
  107. $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
  108. /**
  109. * 初始化加密
  110. */
  111. @mcrypt_generic_init($td, $key, $iv);
  112. /**
  113. * 加密
  114. */
  115. $encrypted = @mcrypt_generic($td, $data);
  116. /**
  117. * 清理加密
  118. */
  119. @mcrypt_generic_deinit($td);
  120. /**
  121. * 关闭
  122. */
  123. @mcrypt_module_close($td);
  124. return base64_encode($encrypted);
  125. }
  126. public function encrypt2($input, $key)
  127. {
  128. $data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
  129. $data = base64_encode($data);
  130. return $data;
  131. }
  132. public function decrypt2($sStr, $sKey)
  133. {
  134. $decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA);
  135. return $decrypted;
  136. }
  137. private function pkcs5_pad ($text, $blocksize) {
  138. $pad = $blocksize - (strlen($text) % $blocksize);
  139. return $text . str_repeat(chr($pad), $pad);
  140. }
  141. public function decrypt($sStr, $sKey) {
  142. $sStr=hex2bin($sStr);
  143. $decrypted= @mcrypt_decrypt(
  144. MCRYPT_RIJNDAEL_128,
  145. $sKey,
  146. $sStr,
  147. MCRYPT_MODE_ECB
  148. );
  149. $dec_s = strlen($decrypted);
  150. $padding = ord($decrypted[$dec_s-1]);
  151. $decrypted = substr($decrypted, 0, -$padding);
  152. return $decrypted;
  153. }
  154. }