OriginBank.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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'=>$data['body'],
  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. 'JSAPI'=>1
  32. ];
  33. $base_data['data'] = $this->encrypt(json_encode($data),$this->open_key);
  34. $base_data['sign'] = $this->signs($base_data);
  35. $response = $this->PayClient->request('POST','/mct1/payorder',['form_params'=>$base_data])->getBody()->getContents();
  36. return $this->getPayInfo($response);
  37. }
  38. function getPayInfo($response)
  39. {
  40. try{
  41. $return_info = json_decode($response,1);
  42. if($return_info['errcode'] == 0)
  43. {
  44. $data = json_decode($this->decrypt($return_info['data'],$this->open_key,true),true);
  45. $trade_result = json_decode($data['trade_result'],true);
  46. $wc_pay_data = json_decode($trade_result['wc_pay_data'],true);
  47. $pay_info = [
  48. 'appId'=>$wc_pay_data['appId'],
  49. 'timeStamp'=>$wc_pay_data['timeStamp'],
  50. 'nonceStr'=>$wc_pay_data['nonceStr'],
  51. 'signType'=>$wc_pay_data['signType'],
  52. 'package'=>$wc_pay_data['package'],
  53. 'paySign'=>$wc_pay_data['paySign']
  54. ];
  55. return $pay_info;
  56. }
  57. }catch (\Exception $e)
  58. {
  59. echo $e->getMessage();
  60. Log::error($response);
  61. return null;
  62. }
  63. }
  64. //签名
  65. public function signs($array){
  66. $signature = array();
  67. foreach($array as $key=>$value){
  68. $signature[$key]=$key.'='.$value;
  69. }
  70. $signature['open_key']='open_key'.'='.$this->open_key;
  71. ksort($signature);
  72. #先sha1加密 在md5加密
  73. $sign_str = md5(sha1(implode('&', $signature)));
  74. return $sign_str;
  75. }
  76. #@todo AES加解密
  77. #加密
  78. private function encrypt($input, $key) {
  79. $size = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  80. $input = $this->pkcs5_pad($input, $size);
  81. $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  82. $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  83. @mcrypt_generic_init($td, $key, $iv);
  84. $data = @mcrypt_generic($td, $input);
  85. @mcrypt_generic_deinit($td);
  86. @mcrypt_module_close($td);
  87. $data = strtoupper(bin2hex($data));
  88. return $data;
  89. }
  90. private function pkcs5_pad ($text, $blocksize) {
  91. $pad = $blocksize - (strlen($text) % $blocksize);
  92. return $text . str_repeat(chr($pad), $pad);
  93. }
  94. public function decrypt($sStr, $sKey) {
  95. $sStr=hex2bin($sStr);
  96. $decrypted= @mcrypt_decrypt(
  97. MCRYPT_RIJNDAEL_128,
  98. $sKey,
  99. $sStr,
  100. MCRYPT_MODE_ECB
  101. );
  102. $dec_s = strlen($decrypted);
  103. $padding = ord($decrypted[$dec_s-1]);
  104. $decrypted = substr($decrypted, 0, -$padding);
  105. return $decrypted;
  106. }
  107. }