EasyWeChatService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\common\service;
  3. namespace app\common\service;
  4. use EasyWeChat\Factory;
  5. use think\Log;
  6. class EasyWeChatService
  7. {
  8. public $config;
  9. public $app;
  10. public $payApp;
  11. public function __construct()
  12. {
  13. $this->config = config('common.wechat');
  14. $this->app = Factory::miniProgram($this->config);
  15. $this->payApp = Factory::payment($this->config);
  16. }
  17. public function login($code)
  18. {
  19. $res = $this->app->auth->session($code);
  20. Log::info('login-res' . json_encode([$res]));
  21. return $res;
  22. }
  23. /**
  24. * @param $session
  25. * @param $iv
  26. * @param $encryptedData
  27. * @return array
  28. */
  29. public function decryData($session, $iv, $encryptedData)
  30. {
  31. $decryptedData = $this->app->encryptor->decryptData($session, $iv, $encryptedData);
  32. return $decryptedData;
  33. }
  34. /**
  35. * 统一下单接口
  36. * @param $body
  37. * @param $out_trade_no
  38. * @param $total_fee float 单位元
  39. * @param $openid
  40. * @param string $notify_url
  41. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  42. */
  43. public function unify($body, $out_trade_no, $total_fee, $openid, $notify_url = '')
  44. {
  45. $params = [
  46. 'body' => $body,
  47. 'out_trade_no' => $out_trade_no,
  48. 'total_fee' => $total_fee * 100, //记得转为分
  49. 'spbill_create_ip' => '', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
  50. 'notify_url' => $notify_url ?: null, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  51. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  52. 'openid' => $openid,
  53. ];
  54. Log::info('unify-params' . json_encode($params));
  55. $result = $this->payApp->order->unify($params);
  56. Log::info('unify-res' . json_encode($result));
  57. return $result;
  58. }
  59. /**
  60. * @param $prepayId
  61. * @return array|string
  62. */
  63. public function sdkConfig($prepayId)
  64. {
  65. $jssdk = $this->payApp->jssdk;
  66. $config = $jssdk->bridgeConfig($prepayId, false); // 返回数组
  67. Log::info('bridgeConfig-res' . json_encode($config));
  68. return $config;
  69. }
  70. /**
  71. * 发送数据消息
  72. * @param $order_id
  73. * @param $user_id
  74. * @param $template_id
  75. * @param array $msgData
  76. */
  77. public function sendMsg($user_id, $template_id, array $msgData = [])
  78. {
  79. $data = SubscribeMessageService::packMsg($user_id, $template_id, $msgData);
  80. $res = $this->app->subscribe_message->send($data);
  81. Log::info('发送订阅消息-res' . json_encode(['data' => $data, 'res' => $res], JSON_UNESCAPED_UNICODE));
  82. }
  83. /**
  84. * @param $order_sn
  85. * @param $openid
  86. * @param $name
  87. * @param $amount
  88. * @param $desc
  89. * @return array|mixed
  90. * @throws \Exception
  91. */
  92. public function mchPay($order_sn, $openid, $name, $amount, $desc = '用户提现')
  93. {
  94. $data = [
  95. 'partner_trade_no' => $order_sn, // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  96. 'openid' => $openid,
  97. 'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  98. 're_user_name' => $name, // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
  99. 'amount' => $amount * 100, // 企业付款金额,单位为分
  100. 'desc' => $desc, // 企业付款操作说明信息。必填
  101. ];
  102. $res = $this->payApp->transfer->toBalance($data);
  103. Log::info('企业付款-res' . json_encode(['data' => $data, 'res' => $res], JSON_UNESCAPED_UNICODE));
  104. return $res;
  105. }
  106. /**
  107. * @param $apply_sn
  108. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  109. */
  110. public function queryBalance($apply_sn)
  111. {
  112. $res = $this->payApp->transfer->queryBalanceOrder($apply_sn);
  113. Log::info('企业付款查询-res' . json_encode(['apply_sn' => $apply_sn, 'res' => $res]));
  114. return $res;
  115. }
  116. /**
  117. * @param $order_sn
  118. * @param $refund_no
  119. * @param $actual_amount
  120. * @param $refund_amount
  121. * @param $desc
  122. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  123. */
  124. public function refund($order_sn, $refund_no, $actual_amount, $refund_amount, $desc = '出票失败退款')
  125. {
  126. //商户订单号、商户退款单号、订单金额、退款金额、其他参数
  127. $res = $this->payApp->refund->byOutTradeNumber($order_sn, $refund_no, bcmul($actual_amount, 100), bcmul($refund_amount, 100), [
  128. // 可在此处传入其他参数,详细参数见微信支付文档
  129. 'refund_desc' => $desc,
  130. ]);
  131. Log::info('微信订单退款-res' . json_encode(['param' => func_get_args(), 'res' => $res, [bcmul($actual_amount, 100), bcmul($refund_amount, 100)]]));
  132. return $res;
  133. }
  134. /**
  135. * @param $refund_no
  136. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  137. */
  138. public function refundQuery($refund_no)
  139. {
  140. //商户订单号、商户退款单号、订单金额、退款金额、其他参数
  141. $res = $this->payApp->refund->queryByOutRefundNumber($refund_no);
  142. Log::info('微信订单退款查询-res' . json_encode(['param' => func_get_args(), 'res' => $res]));
  143. return $res;
  144. }
  145. }