OriginBank.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Log::error($return_info);
  58. }catch (\Exception $e)
  59. {
  60. echo $e->getMessage();
  61. Log::error($response);
  62. return null;
  63. }
  64. }
  65. //签名
  66. public function signs($array){
  67. $signature = array();
  68. foreach($array as $key=>$value){
  69. $signature[$key]=$key.'='.$value;
  70. }
  71. $signature['open_key']='open_key'.'='.$this->open_key;
  72. ksort($signature);
  73. #先sha1加密 在md5加密
  74. $sign_str = md5(sha1(implode('&', $signature)));
  75. return $sign_str;
  76. }
  77. #@todo AES加解密
  78. #加密
  79. private function encrypt($input, $key) {
  80. $size = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  81. $input = $this->pkcs5_pad($input, $size);
  82. $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  83. $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  84. @mcrypt_generic_init($td, $key, $iv);
  85. $data = @mcrypt_generic($td, $input);
  86. @mcrypt_generic_deinit($td);
  87. @mcrypt_module_close($td);
  88. $data = strtoupper(bin2hex($data));
  89. return $data;
  90. }
  91. private function pkcs5_pad ($text, $blocksize) {
  92. $pad = $blocksize - (strlen($text) % $blocksize);
  93. return $text . str_repeat(chr($pad), $pad);
  94. }
  95. public function decrypt($sStr, $sKey) {
  96. $sStr=hex2bin($sStr);
  97. $decrypted= @mcrypt_decrypt(
  98. MCRYPT_RIJNDAEL_128,
  99. $sKey,
  100. $sStr,
  101. MCRYPT_MODE_ECB
  102. );
  103. $dec_s = strlen($decrypted);
  104. $padding = ord($decrypted[$dec_s-1]);
  105. $decrypted = substr($decrypted, 0, -$padding);
  106. return $decrypted;
  107. }
  108. }