OriginBank.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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['openId'];
  10. $this->open_key = $config['openKey'];
  11. $this->mchId = $config['mchId'];
  12. $this->appkey = $config['appKey'];
  13. $this->subAppid = $config['subAppid'];
  14. $this->PayClient = new client(['base_uri'=>'https://mixpayuat4.orangebank.com.cn/','timeout'=>3]);
  15. }
  16. //生成订单
  17. function send($data)
  18. {
  19. $time = time();
  20. $base_data = [
  21. 'open_id'=>$this->open_id,
  22. 'timestamp'=>$time,
  23. ];
  24. $data = [
  25. 'out_no'=>$data['trade_no'],
  26. 'pmt_tag'=>'Weixin',
  27. 'ord_name'=>'小说充值',
  28. 'original_amount'=>$data['price'],
  29. 'trade_amount'=>$data['price'],
  30. 'notify_url'=>env('ORIGINBANK_NOFITY_URL'),
  31. 'sub_appid'=>$this->appId,
  32. 'sub_openid'=>$data['openid']
  33. ];
  34. $base_data['data'] = $this->encrypt(json_encode($data),$this->open_key);
  35. $base_data['sign'] = $this->signs($data);
  36. dd($base_data);
  37. $response = $this->PayClient->request('POST','mct1/payorder',$base_data)->getBody()->getContents();
  38. dd($response);
  39. return $this->getPayInfo($response);
  40. }
  41. function getPayInfo($response)
  42. {
  43. try{
  44. $return_info = json_decode($response,1);
  45. //dd($return_info);
  46. if($return_info['errcode'] == 0)
  47. {
  48. return json_decode($return_info['result']['pay_info'],1);
  49. }
  50. }catch (\Exception $e)
  51. {
  52. return null;
  53. }
  54. }
  55. //签名
  56. public function signs($array){
  57. $signature = array();
  58. foreach($array as $key=>$value){
  59. $signature[$key]=$key.'='.$value;
  60. }
  61. $signature['open_key']='open_key'.'='.$this->open_key;
  62. ksort($signature);
  63. #先sha1加密 在md5加密
  64. $sign_str = md5(sha1(implode('&', $signature)));
  65. return $sign_str;
  66. }
  67. #@todo AES加解密
  68. #加密
  69. public static function encrypt($input, $key) {
  70. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  71. $input = self::pkcs5_pad($input, $size);
  72. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  73. $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  74. mcrypt_generic_init($td, $key, $iv);
  75. $data = mcrypt_generic($td, $input);
  76. mcrypt_generic_deinit($td);
  77. mcrypt_module_close($td);
  78. $data = strtoupper(bin2hex($data));
  79. return $data;
  80. }
  81. }