WxPayHelper.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\libs\wechat\wxpay;
  3. use App\libs\helpers\Helper;
  4. use lc\wechat\wxpay\base\WxPayException;
  5. use lc\wechat\wxpay\database\RedPack;
  6. use lc\wechat\wxpay\database\Results;
  7. use lc\wechat\wxpay\database\Transfer;
  8. use lc\wechat\wxpay\database\UnifyOrder;
  9. use Illuminate\Support\Facades\Config;
  10. use Illuminate\Support\Facades\DB;
  11. class WxPayHelper
  12. {
  13. /**
  14. * 红包日志.
  15. *
  16. * @param Transfer|RedPack $database 红包类|企业支付零钱类
  17. */
  18. public static function writeRedPackLogs($database, array $output, int $type)
  19. {
  20. if (1 == $type) {
  21. $paymentNo = !empty($output['payment_no']) ? $output['payment_no'] : null;
  22. $openid = $database->getAttributes('openid');
  23. $orderId = $database->getAttributes('partner_trade_no');
  24. $amount = $database->getAttributes('amount');
  25. } else {
  26. $paymentNo = !empty($output['send_listid']) ? $output['send_listid'] : null;
  27. $openid = $database->getAttributes('re_openid');
  28. $orderId = $database->getAttributes('mch_billno');
  29. $amount = $database->getAttributes('total_amount');
  30. }
  31. Db::connect('main')
  32. ->table('lcapp_v4_hblogs')
  33. ->insert([
  34. 'appid' => Config::get('lc.wxpay.id'),
  35. 'proj_id' => env('id', Config::get('lc.pid')),
  36. 'openid' => $openid,
  37. 'order_id' => $orderId,
  38. 'amount' => $amount,
  39. 'payment_no' => $paymentNo,
  40. 'input' => json_encode($database->getValues(), JSON_UNESCAPED_UNICODE),
  41. 'output' => json_encode($output, JSON_UNESCAPED_UNICODE),
  42. 'result_code' => isset($output['result_code']) ? $output['result_code'] : $output['return_code'],
  43. 'err_code' => isset($output['err_code']) ? $output['err_code'] : null,
  44. 'created_date' => date('YmdHis'),
  45. 'created_time' => time(),
  46. ]);
  47. }
  48. /**
  49. * 记录调用日志.
  50. *
  51. * @param UnifyOrder $unify 统一下单类
  52. * @param array $output 接口返回数据
  53. * @param string $reqName 请求名
  54. */
  55. public static function writeWxOrderLogs($unify, array $output, string $reqName)
  56. {
  57. Db::table('lcapp_v4_wxorder_logs')
  58. ->insert([
  59. 'req_name' => $reqName,
  60. 'appid' => Config::get('lc.wxpay.id'),
  61. 'proj_id' => env('id', Config::get('lc.pid')),
  62. 'openid' => $unify->getAttributes('openid'),
  63. 'order_id' => $unify->getAttributes('out_trade_no'),
  64. 'amount' => $unify->getAttributes('total_fee') ? $unify->getAttributes('total_fee') : 0,
  65. 'input' => json_encode($unify->getValues(), JSON_UNESCAPED_UNICODE),
  66. 'output' => json_encode($output, JSON_UNESCAPED_UNICODE),
  67. 'result_code' => isset($output['result_code']) ? $output['result_code'] : $output['return_code'],
  68. 'err_code' => isset($output['err_code']) ? $output['err_code'] : null,
  69. 'created_date' => date('YmdHis'),
  70. 'created_time' => time(),
  71. ]);
  72. }
  73. /*
  74. * 发送红包请求
  75. *
  76. * @param int $type 1企业支付到零钱,2现金红包
  77. * @param string $url 请求地址
  78. * @param array $params 请求参数
  79. * @return array
  80. */
  81. public static function sendRedPackRequest(int $type, string $url, array $params)
  82. {
  83. $class = (1 == $type) ? new Transfer() : new RedPack();
  84. foreach ($params as $key => $value) {
  85. $class->setAttributes($key, $value);
  86. }
  87. $class->setSign();
  88. $xml = Helper::arrayToXml($class->getValues());
  89. $response = self::postXmlCurl($xml, $url, true, 6);
  90. $result = Results::init($response, false);
  91. self::writeRedPackLogs($class, $result, $type);
  92. return $result;
  93. }
  94. /*
  95. * 查询红包请求
  96. *
  97. * @param int $type 1企业支付到零钱,2现金红包
  98. * @param string $url 请求地址
  99. * @param array $params 请求参数
  100. * @return array
  101. */
  102. public static function queryRequest(int $type, string $url, array $params)
  103. {
  104. $class = (1 == $type) ? new Transfer() : new RedPack();
  105. $apiName = (1 == $type) ? 'gettransferinfo' : 'gethbinfo';
  106. foreach ($params as $key => $value) {
  107. $class->setAttributes($key, $value);
  108. }
  109. $class->setSign();
  110. $xml = Helper::arrayToXml($class->getValues());
  111. $response = self::postXmlCurl($xml, $url, true, 6);
  112. $result = Results::init($response, false);
  113. self::writeWxOrderLogs($class, $result, $apiName);
  114. return $result;
  115. }
  116. /*
  117. * 发送统一下单请求
  118. *
  119. * @param string $url 请求地址
  120. * @param array $params 参数
  121. * @param string $apiName 接口名
  122. * @return array
  123. */
  124. public static function sendUnifyRequest(string $url, array $params, string $apiName)
  125. {
  126. $unify = new UnifyOrder();
  127. foreach ($params as $key => $value) {
  128. $unify->setAttributes($key, $value);
  129. }
  130. $unify->setSign();
  131. $xml = Helper::arrayToXml($unify->getValues());
  132. $response = self::postXmlCurl($xml, $url, in_array($apiName, ['refund', 'send_coupon']) ? true : false, 6);
  133. $result = Results::init($response, false);
  134. self::writeWxOrderLogs($unify, $result, $apiName);
  135. return $result;
  136. }
  137. /**
  138. * 以post方式提交xml到对应的接口url.
  139. *
  140. * @param string $xml 需要post的xml数据
  141. * @param string $url url
  142. * @param bool $useCert 是否需要证书,默认不需要
  143. * @param int $second url执行超时时间,默认30s
  144. *
  145. * @throws WxPayException
  146. */
  147. public static function postXmlCurl($xml, $url, $useCert = false, $second = 30)
  148. {
  149. $ch = curl_init();
  150. // 设置超时
  151. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  152. // 如果有配置代理这里就设置代理
  153. if ('0.0.0.0' != WxPayConfig::$curlProxyPort && 0 != WxPayConfig::$curlProxyPort) {
  154. curl_setopt($ch, CURLOPT_PROXY, WxPayConfig::$curlProxyHost);
  155. curl_setopt($ch, CURLOPT_PROXYPORT, WxPayConfig::$curlProxyPort);
  156. }
  157. curl_setopt($ch, CURLOPT_URL, $url);
  158. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  159. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 严格校验
  160. // 设置header
  161. curl_setopt($ch, CURLOPT_HEADER, false);
  162. // 要求结果为字符串且输出到屏幕上
  163. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  164. if (true == $useCert) {
  165. // 设置证书
  166. // 使用证书:cert 与 key 分别属于两个.pem文件
  167. curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
  168. curl_setopt($ch, CURLOPT_SSLCERT, WxPayConfig::$sslCertPath);
  169. curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
  170. curl_setopt($ch, CURLOPT_SSLKEY, WxPayConfig::$sslKeyPath);
  171. }
  172. // post提交方式
  173. curl_setopt($ch, CURLOPT_POST, true);
  174. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  175. // 运行curl
  176. $data = curl_exec($ch);
  177. // 返回结果
  178. if ($data) {
  179. curl_close($ch);
  180. return $data;
  181. }
  182. $errorNo = curl_errno($ch);
  183. $error = curl_error($ch);
  184. curl_close($ch);
  185. throw new WxPayException('curl出错,错误码: ' . $errorNo . ',错误信息:' . $error);
  186. }
  187. /**
  188. * 获取 JSAPI 支付,前端所需参数数据.
  189. *
  190. * @param string $prepayId 统一下单成功返回的id
  191. *
  192. * @return array
  193. */
  194. public static function getJsApiParameters($prepayId)
  195. {
  196. new WxPayConfig();
  197. $data = [
  198. 'appId' => WxPayConfig::$appId,
  199. 'timeStamp' => (string) time(),
  200. 'nonceStr' => Helper::generateNonceStr(),
  201. 'package' => 'prepay_id=' . $prepayId,
  202. 'signType' => 'MD5',
  203. ];
  204. $data['paySign'] = Helper::makeSign($data, WxPayConfig::$key);
  205. return $data;
  206. }
  207. }