MiniProgramService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use crmeb\services\easywechat\broadcast\Client;
  13. use crmeb\services\easywechat\broadcast\ServiceProvider;
  14. use crmeb\services\easywechat\subscribe\ProgramProvider;
  15. use EasyWeChat\Foundation\Application;
  16. use EasyWeChat\Material\Temporary;
  17. use EasyWeChat\MiniProgram\MiniProgram;
  18. use EasyWeChat\Payment\Order;
  19. use EasyWeChat\Payment\Payment;
  20. use EasyWeChat\Support\Collection;
  21. use Psr\Http\Message\ResponseInterface;
  22. use think\exception\ValidateException;
  23. use think\facade\Log;
  24. use think\facade\Route;
  25. /**
  26. * Class MiniProgramService
  27. * @package crmeb\services
  28. * @author xaboy
  29. * @day 2020-05-11
  30. */
  31. class MiniProgramService
  32. {
  33. /**
  34. * @var MiniProgram
  35. */
  36. protected $service;
  37. protected $config;
  38. /**
  39. * MiniProgramService constructor.
  40. * @param array $config
  41. */
  42. public function __construct(array $config)
  43. {
  44. $this->config = $config;
  45. $this->service = new Application($config);
  46. $this->service->register(new ServiceProvider());
  47. $this->service->register(new ProgramProvider());
  48. }
  49. /**
  50. * @return Client
  51. * @author xaboy
  52. * @day 2020/7/29
  53. */
  54. public function miniBroadcast()
  55. {
  56. return $this->service->miniBroadcast;
  57. }
  58. /**
  59. * @return array[]
  60. * @author xaboy
  61. * @day 2020/6/18
  62. */
  63. public static function getConfig()
  64. {
  65. $wechat = systemConfig(['site_url', 'routine_appId', 'routine_appsecret']);
  66. $payment = systemConfig(['pay_routine_mchid', 'pay_routine_key', 'pay_routine_client_cert', 'pay_routine_client_key', 'pay_weixin_open']);
  67. return [
  68. 'mini_program' => [
  69. 'app_id' => $wechat['routine_appId'],
  70. 'secret' => $wechat['routine_appsecret'],
  71. 'token' => '',
  72. 'aes_key' => '',
  73. ],
  74. 'payment' => [
  75. 'app_id' => $wechat['routine_appId'],
  76. 'merchant_id' => trim($payment['pay_routine_mchid']),
  77. 'key' => trim($payment['pay_routine_key']),
  78. 'cert_path' => (app()->getRootPath() . 'public' . $payment['pay_routine_client_cert']),
  79. 'key_path' => (app()->getRootPath() . 'public' . $payment['pay_routine_client_key']),
  80. 'notify_url' => $wechat['site_url'] . Route::buildUrl('routineNotify')->build(),
  81. 'pay_routine_client_key' => $payment['pay_routine_client_key'],
  82. 'pay_routine_client_cert' => $payment['pay_routine_client_cert'],
  83. ]
  84. ];
  85. }
  86. /**
  87. * @return MiniProgramService
  88. * @author xaboy
  89. * @day 2020/6/2
  90. */
  91. public static function create()
  92. {
  93. return new self(self::getConfig());
  94. }
  95. /**
  96. * 支付
  97. * @return Payment
  98. */
  99. public function paymentService()
  100. {
  101. return $this->service->payment;
  102. }
  103. /**
  104. * 小程序接口
  105. * @return MiniProgram
  106. */
  107. public function miniProgram()
  108. {
  109. return $this->service->mini_program;
  110. }
  111. /**
  112. * @return \EasyWeChat\Material\Material|mixed
  113. * @author xaboy
  114. * @day 2020/7/29
  115. */
  116. public function material()
  117. {
  118. return $this->service->mini_program->material_temporary;
  119. }
  120. /**
  121. * @param $sessionKey
  122. * @param $iv
  123. * @param $encryptData
  124. * @return mixed
  125. * @author xaboy
  126. * @day 2020/6/18
  127. */
  128. public function encryptor($sessionKey, $iv, $encryptData)
  129. {
  130. return $this->miniProgram()->encryptor->decryptData($sessionKey, $iv, $encryptData);
  131. }
  132. /**
  133. * 上传临时素材接口
  134. * @return Temporary
  135. */
  136. public function materialTemporaryService()
  137. {
  138. return $this->miniProgram()->material_temporary;
  139. }
  140. /**
  141. * 客服消息接口
  142. */
  143. public function staffService()
  144. {
  145. return $this->miniProgram()->staff;
  146. }
  147. /**
  148. * @param $code
  149. * @return mixed
  150. * @author xaboy
  151. * @day 2020/6/18
  152. */
  153. public function getUserInfo($code)
  154. {
  155. $userInfo = $this->miniProgram()->sns->getSessionKey($code);
  156. return $userInfo;
  157. }
  158. /**
  159. * @return \EasyWeChat\MiniProgram\QRCode\QRCode
  160. * @author xaboy
  161. * @day 2020/6/18
  162. */
  163. public function qrcodeService()
  164. {
  165. return $this->miniProgram()->qrcode;
  166. }
  167. /**
  168. * 生成支付订单对象
  169. * @param $openid
  170. * @param $out_trade_no
  171. * @param $total_fee
  172. * @param $attach
  173. * @param $body
  174. * @param string $detail
  175. * @param string $trade_type
  176. * @param array $options
  177. * @return Order
  178. */
  179. protected function paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  180. {
  181. $total_fee = bcmul($total_fee, 100, 0);
  182. $order = array_merge(compact('openid', 'out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type'), $options);
  183. if ($order['detail'] == '') unset($order['detail']);
  184. return new Order($order);
  185. }
  186. /**
  187. * 获得下单ID
  188. * @param $openid
  189. * @param $out_trade_no
  190. * @param $total_fee
  191. * @param $attach
  192. * @param $body
  193. * @param string $detail
  194. * @param string $trade_type
  195. * @param array $options
  196. * @return mixed
  197. */
  198. public function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  199. {
  200. $order = $this->paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
  201. $result = $this->paymentService()->prepare($order);
  202. if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
  203. return $result->prepay_id;
  204. } else {
  205. if ($result->return_code == 'FAIL') {
  206. throw new ValidateException('微信支付错误返回:' . $result->return_msg);
  207. } else if (isset($result->err_code)) {
  208. throw new ValidateException('微信支付错误返回:' . $result->err_code_des);
  209. } else {
  210. throw new ValidateException('没有获取微信支付的预支付ID,请重新发起支付!');
  211. }
  212. }
  213. }
  214. /**
  215. * 获得jsSdk支付参数
  216. * @param $openid
  217. * @param $out_trade_no
  218. * @param $total_fee
  219. * @param $attach
  220. * @param $body
  221. * @param string $detail
  222. * @param string $trade_type
  223. * @param array $options
  224. * @return array|string
  225. */
  226. public function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  227. {
  228. return $this->paymentService()->configForJSSDKPayment($this->paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options));
  229. }
  230. /**
  231. * 使用商户订单号退款
  232. * @param $orderNo
  233. * @param $refundNo
  234. * @param $totalFee
  235. * @param null $refundFee
  236. * @param null $opUserId
  237. * @param string $refundReason
  238. * @param string $type
  239. * @param string $refundAccount
  240. * @return Collection|ResponseInterface
  241. */
  242. public function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS')
  243. {
  244. if (empty($this->config['payment']['pay_routine_client_key']) || empty($this->config['payment']['pay_routine_client_cert'])) {
  245. throw new \Exception('请配置微信支付证书');
  246. }
  247. $totalFee = floatval($totalFee);
  248. $refundFee = floatval($refundFee);
  249. return $this->paymentService()->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $type, $refundAccount, $refundReason);
  250. }
  251. /**
  252. * 发送订阅消息
  253. * @param string $touser 接收者(用户)的 openid
  254. * @param string $templateId 所需下发的订阅模板id
  255. * @param array $data 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
  256. * @param string $link 击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  257. * @return \EasyWeChat\Support\Collection|null
  258. * @throws \EasyWeChat\Core\Exceptions\HttpException
  259. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  260. */
  261. public function sendSubscribeTemlate(string $touser, string $templateId, array $data, string $link = '')
  262. {
  263. return $this->miniprogram()->now_notice->to($touser)->template($templateId)->andData($data)->withUrl($link)->send();
  264. }
  265. /**
  266. * @param $orderNo
  267. * @param array $opt
  268. * @return bool
  269. * @author xaboy
  270. * @day 2020/6/18
  271. */
  272. public function payOrderRefund($orderNo, array $opt)
  273. {
  274. if (!isset($opt['pay_price'])) throw new ValidateException('缺少pay_price');
  275. $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
  276. $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
  277. $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
  278. $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
  279. $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
  280. $type = isset($opt['type']) ? $opt['type'] : 'out_trade_no';
  281. $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
  282. try {
  283. $res = ($this->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $refundReason, $type, $refundAccount));
  284. if ($res->return_code == 'FAIL') throw new ValidateException('退款失败:' . $res->return_msg);
  285. if (isset($res->err_code)) throw new ValidateException('退款失败:' . $res->err_code_des);
  286. } catch (\Exception $e) {
  287. throw new ValidateException($e->getMessage());
  288. }
  289. return true;
  290. }
  291. /**
  292. * @return \Symfony\Component\HttpFoundation\Response
  293. * @throws \EasyWeChat\Core\Exceptions\FaultException
  294. * @author xaboy
  295. * @day 2020/6/18
  296. */
  297. public function handleNotify()
  298. {
  299. $this->service->payment = new PaymentService($this->service->merchant);
  300. return $this->service->payment->handleNotify(function ($notify, $successful) {
  301. Log::info('小程序支付回调' . var_export($notify, 1));
  302. if (!$successful) return;
  303. try {
  304. event('pay_success_' . $notify['attach'], ['order_sn' => $notify['out_trade_no'], 'data' => $notify]);
  305. } catch (\Exception $e) {
  306. Log::info('小程序支付回调失败:' . $e->getMessage());
  307. return false;
  308. }
  309. return true;
  310. });
  311. }
  312. }