Goopago.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Services;
  3. use http\Client\Curl\User;
  4. use function GuzzleHttp\Psr7\build_query;
  5. class Goopago
  6. {
  7. protected $merchantOrder;
  8. protected $thirdUserId;
  9. protected $currency;
  10. protected $amount;
  11. public function getNonceStr( $length = 16 ) {
  12. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  13. $str = "";
  14. for ( $i = 0; $i < $length; $i++ ) {
  15. $str .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 );
  16. }
  17. return $str;
  18. }
  19. /**
  20. * 充值
  21. * 传入数组进行HTTP POST请求
  22. * @param $url
  23. * @param array $post_data
  24. * @param int $timeout
  25. * @param string $header
  26. * @param string $data_type
  27. * @return bool|string
  28. */
  29. public function curlPost($url, $post_data = array(), $timeout = 120)
  30. {
  31. $header = [
  32. 'Content-Type: application/json',
  33. 'tmId:br_auto'
  34. ];
  35. $post_string = json_encode($post_data);
  36. $ch = curl_init(); // 启动一个CURL会话
  37. curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
  38. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 // https请求 不验证证书和hosts
  39. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  40. curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
  41. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的数据包
  42. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
  43. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  44. //curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  46. curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
  47. $result = curl_exec($ch);
  48. curl_close($ch);
  49. return $result;
  50. }
  51. public function sign($data = [])
  52. {
  53. ksort($data); //按照参数名ASCII码从小到大排序
  54. $payConfigService = new PayConfig();
  55. $config = $payConfigService->getConfig('Goopago');
  56. $md5key = $config['SecretKey']; //md5密钥
  57. $str = '';
  58. foreach ( $data as $key => $val ) {
  59. if($val)
  60. $str .= $str ? '&' . $key . '=' . $val : $key . '=' . $val;
  61. }
  62. $str .= '&key=' . $md5key;
  63. return strtoupper(md5($str)); //拼接密钥后,md5加密后转为大写
  64. }
  65. }