KFRequestUtil.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace addons\YunfastPay\common\utils;
  3. /**
  4. * 银典支付
  5. * @package addons\YunfastPay\common\utils
  6. */
  7. class KFRequestUtil
  8. {
  9. /**
  10. * 秘钥类型域,机构秘钥:ORG、商户秘钥:MCHT、终端秘钥:TERM
  11. *
  12. * @var string
  13. */
  14. protected $typeField = 'ORG';
  15. /**
  16. * secretKey是3des加密秘钥,测试时请替换成自己的测试秘钥,
  17. * 生产需要换生产秘钥,如果没有请联系广东快付的技术获取。
  18. * 这里可以使用机构秘钥或者商户秘钥。使用机构秘钥加密类型ORG,
  19. * 使用商户秘钥加密类型MCHT
  20. *
  21. * @var string
  22. */
  23. protected $secretKey;
  24. /**
  25. * orgCd是机构号,测试时请替换成自己的测试机构号,
  26. * 生产需要换生产机构号,如果没有请联系广东快付的技术获取。
  27. *
  28. * @var string
  29. */
  30. protected $orgCd;
  31. /**
  32. * 测试请求地址
  33. *
  34. * @var string
  35. */
  36. protected $testReqUrl = 'http://test.api.route.hangmuxitong.com';
  37. /**
  38. * 生产请求地址
  39. *
  40. * @var string
  41. */
  42. protected $reqUrl = 'https://api.yunfastpay.com';
  43. /**
  44. * @var boolean
  45. */
  46. protected $debug = true;
  47. /**
  48. * 初始化
  49. *
  50. * @param string $orgCd
  51. * @param string $secretKey
  52. * @param string $typeField
  53. * @param boolean $debug
  54. */
  55. public function __construct(
  56. string $orgCd,
  57. string $secretKey,
  58. string $typeField = 'ORG',
  59. bool $debug = false
  60. )
  61. {
  62. $this->orgCd = $orgCd;
  63. $this->secretKey = $secretKey;
  64. $this->typeField = $typeField;
  65. $this->debug = $debug;
  66. }
  67. private function getRequestUrl()
  68. {
  69. return $this->reqUrl;
  70. return $this->debug
  71. ? $this->testReqUrl
  72. : $this->reqUrl;
  73. }
  74. /**
  75. * 发送请求
  76. *
  77. * @param array $reqData
  78. * @return string
  79. */
  80. public function req($reqData)
  81. {
  82. if ($this->debug) {
  83. echo '=====>请求路径:' . $this->getRequestUrl(). '<br/>';
  84. echo '=====>请求参数:' . json_encode($reqData, JSON_UNESCAPED_UNICODE). '<br/>';
  85. }
  86. $encReqData = Des3Utils::encrypt(json_encode($reqData), $this->secretKey);
  87. $data['typeField'] = $this->typeField;
  88. $data['keyField'] = $this->orgCd;
  89. $data['dataField'] = $encReqData;
  90. if ($this->debug) {
  91. echo '=====>请求报文:' . json_encode($data). '<br/>';
  92. }
  93. $encRespStr = $this->send_request($this->getRequestUrl(), json_encode($data));
  94. if ($this->debug) {
  95. echo '=====>返回报文:' . $encRespStr . '<br/>';
  96. }
  97. $respMsg = json_decode($encRespStr, true);
  98. $respStr = Des3Utils::decrypt($respMsg['dataField'], $this->secretKey);
  99. if ($this->debug) {
  100. echo '=====>返回数据:' . $respStr . '<br/>';
  101. }
  102. return $respStr;
  103. }
  104. /**
  105. * CURL发送Request请求,含POST和REQUEST
  106. * @param string $url 请求的链接
  107. * @param mixed $params 传递的参数
  108. * @param string $method 请求的方法
  109. * @param mixed $options CURL的参数
  110. * @return array|string
  111. */
  112. private function send_request($url, $params = [], $method = 'POST', $options = [])
  113. {
  114. $method = strtoupper($method);
  115. $protocol = substr($url, 0, 5);
  116. $query_string = is_array($params) ? http_build_query($params) : $params;
  117. $ch = curl_init();
  118. $defaults = [];
  119. if ('GET' == $method) {
  120. $geturl = $query_string ? $url . (stripos($url, '?') !== false ? '&' : '?') . $query_string : $url;
  121. $defaults[CURLOPT_URL] = $geturl;
  122. } else {
  123. $defaults[CURLOPT_URL] = $url;
  124. if ($method == 'POST') {
  125. $defaults[CURLOPT_POST] = 1;
  126. } else {
  127. $defaults[CURLOPT_CUSTOMREQUEST] = $method;
  128. }
  129. $defaults[CURLOPT_POSTFIELDS] = $query_string;
  130. }
  131. $defaults[CURLOPT_HEADER] = false;
  132. $defaults[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36';
  133. $defaults[CURLOPT_FOLLOWLOCATION] = true;
  134. $defaults[CURLOPT_RETURNTRANSFER] = true;
  135. $defaults[CURLOPT_CONNECTTIMEOUT] = 30;
  136. $defaults[CURLOPT_TIMEOUT] = 30;
  137. // disable 100-continue
  138. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'Content-Type:application/json'));
  139. if ('https' == $protocol) {
  140. $defaults[CURLOPT_SSL_VERIFYPEER] = false;
  141. $defaults[CURLOPT_SSL_VERIFYHOST] = false;
  142. }
  143. curl_setopt_array($ch, (array) $options + $defaults);
  144. $ret = curl_exec($ch);
  145. $err = curl_error($ch);
  146. if (false === $ret || !empty($err)) {
  147. $errno = curl_errno($ch);
  148. $info = curl_getinfo($ch);
  149. curl_close($ch);
  150. return [
  151. 'ret' => false,
  152. 'errno' => $errno,
  153. 'msg' => $err,
  154. 'info' => $info,
  155. ];
  156. }
  157. curl_close($ch);
  158. return $ret;
  159. }
  160. }