OriginBank.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $base_data['data'] = $this->encrypt(json_encode($data),$this->open_key);
  33. $base_data['sign'] = $this->signs($data);
  34. dump('base_data');dump($base_data);
  35. $response = $this->PayClient->request('POST','mct1/payorder',['form_params'=>$base_data])->getBody()->getContents();
  36. dd(json_decode($response,1));
  37. // return $this->getPayInfo($response);
  38. }
  39. function getPayInfo($response)
  40. {
  41. try{
  42. $return_info = json_decode($response,1);
  43. //dd($return_info);
  44. if($return_info['errcode'] == 0)
  45. {
  46. $pay_info = [
  47. 'appId'=>$return_info['data']['appId'],
  48. 'timeStamp'=>$return_info['data']['timeStamp'],
  49. 'nonceStr'=>$return_info['data']['nonceStr'],
  50. 'signType'=>$return_info['data']['signType'],
  51. 'package'=>$return_info['data']['package'],
  52. 'paySign'=>$return_info['data']['paySign']
  53. ];
  54. return $pay_info;
  55. }
  56. }catch (\Exception $e)
  57. {
  58. return null;
  59. }
  60. }
  61. public function c($data){
  62. //启动一个CURL会话
  63. $ch = curl_init();
  64. // 设置curl允许执行的最长秒数
  65. curl_setopt($ch, CURLOPT_TIMEOUT, 120);
  66. //忽略证书
  67. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  68. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
  69. // 获取的信息以文件流的形式返回,而不是直接输出。
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  71. curl_setopt($ch, CURLOPT_URL,'https://mixpayuat4.orangebank.com.cn/mct1/payorder');
  72. //发送一个常规的POST请求。
  73. curl_setopt($ch, CURLOPT_POST, 1);
  74. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  75. curl_setopt($ch, CURLOPT_HEADER,0);//是否需要头部信息(否)
  76. // 执行操作
  77. $result = curl_exec($ch);
  78. #返回数据
  79. return $result;
  80. }
  81. //签名
  82. public function signs($array){
  83. $signature = array();
  84. foreach($array as $key=>$value){
  85. $signature[$key]=$key.'='.$value;
  86. }
  87. $signature['open_key']='open_key'.'='.$this->open_key;
  88. ksort($signature);
  89. #先sha1加密 在md5加密
  90. $sign_str = md5(sha1(implode('&', $signature)));
  91. return $sign_str;
  92. }
  93. #@todo AES加解密
  94. #加密
  95. private function encrypt($input, $key) {
  96. $size = @mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  97. $input = $this->pkcs5_pad($input, $size);
  98. $td = @mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  99. $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  100. @mcrypt_generic_init($td, $key, $iv);
  101. $data = @mcrypt_generic($td, $input);
  102. @mcrypt_generic_deinit($td);
  103. @mcrypt_module_close($td);
  104. $data = strtoupper(bin2hex($data));
  105. return $data;
  106. }
  107. private function pkcs5_pad ($text, $blocksize) {
  108. $pad = $blocksize - (strlen($text) % $blocksize);
  109. return $text . str_repeat(chr($pad), $pad);
  110. }
  111. }