OriginBank.php 4.9 KB

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