MiniProgramService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. $this->service->register(new \crmeb\services\easywechat\certficates\ServiceProvider);
  49. $this->service->register(new \crmeb\services\easywechat\combinePay\ServiceProvider);
  50. }
  51. /**
  52. * @return Client
  53. * @author xaboy
  54. * @day 2020/7/29
  55. */
  56. public function miniBroadcast()
  57. {
  58. return $this->service->miniBroadcast;
  59. }
  60. /**
  61. * @return array[]
  62. * @author xaboy
  63. * @day 2020/6/18
  64. */
  65. public static function getConfig()
  66. {
  67. $wechat = systemConfig(['site_url', 'routine_appId', 'routine_appsecret']);
  68. $payment = systemConfig(['pay_routine_mchid', 'pay_routine_key', 'pay_routine_client_cert', 'pay_routine_client_key', 'pay_weixin_open',
  69. 'wechat_service_merid', 'wechat_service_key', 'wechat_service_v3key', 'wechat_service_client_cert', 'wechat_service_client_key', 'wechat_service_serial_no']);
  70. return [
  71. 'app_id' => $wechat['routine_appId'],
  72. 'secret' => $wechat['routine_appsecret'],
  73. 'mini_program' => [
  74. 'app_id' => $wechat['routine_appId'],
  75. 'secret' => $wechat['routine_appsecret'],
  76. 'token' => '',
  77. 'aes_key' => '',
  78. ],
  79. 'payment' => [
  80. 'app_id' => $wechat['routine_appId'],
  81. 'merchant_id' => trim($payment['pay_routine_mchid']),
  82. 'key' => trim($payment['pay_routine_key']),
  83. 'cert_path' => (app()->getRootPath() . 'public' . $payment['pay_routine_client_cert']),
  84. 'key_path' => (app()->getRootPath() . 'public' . $payment['pay_routine_client_key']),
  85. 'notify_url' => $wechat['site_url'] . Route::buildUrl('routineNotify')->build(),
  86. 'pay_routine_client_key' => $payment['pay_routine_client_key'],
  87. 'pay_routine_client_cert' => $payment['pay_routine_client_cert'],
  88. ],
  89. 'service_payment' => [
  90. 'merchant_id' => trim($payment['wechat_service_merid']),
  91. 'key' => trim($payment['wechat_service_key']),
  92. 'type' => 'routine',
  93. 'cert_path' => (app()->getRootPath() . 'public' . $payment['wechat_service_client_cert']),
  94. 'key_path' => (app()->getRootPath() . 'public' . $payment['wechat_service_client_key']),
  95. 'pay_weixin_client_cert' => $payment['wechat_service_client_cert'],
  96. 'pay_weixin_client_key' => $payment['wechat_service_client_key'],
  97. 'serial_no' => trim($payment['wechat_service_serial_no']),
  98. 'apiv3_key' => trim($payment['wechat_service_v3key']),
  99. ]
  100. ];
  101. }
  102. /**
  103. * @return MiniProgramService
  104. * @author xaboy
  105. * @day 2020/6/2
  106. */
  107. public static function create()
  108. {
  109. return new self(self::getConfig());
  110. }
  111. /**
  112. * 支付
  113. * @return Payment
  114. */
  115. public function paymentService()
  116. {
  117. return $this->service->payment;
  118. }
  119. /**
  120. * 小程序接口
  121. * @return MiniProgram
  122. */
  123. public function miniProgram()
  124. {
  125. return $this->service->mini_program;
  126. }
  127. /**
  128. * @return \EasyWeChat\Material\Material|mixed
  129. * @author xaboy
  130. * @day 2020/7/29
  131. */
  132. public function material()
  133. {
  134. return $this->service->mini_program->material_temporary;
  135. }
  136. /**
  137. * @param $sessionKey
  138. * @param $iv
  139. * @param $encryptData
  140. * @return mixed
  141. * @author xaboy
  142. * @day 2020/6/18
  143. */
  144. public function encryptor($sessionKey, $iv, $encryptData)
  145. {
  146. return $this->miniProgram()->encryptor->decryptData($sessionKey, $iv, $encryptData);
  147. }
  148. /**
  149. * 上传临时素材接口
  150. * @return Temporary
  151. */
  152. public function materialTemporaryService()
  153. {
  154. return $this->miniProgram()->material_temporary;
  155. }
  156. /**
  157. * 客服消息接口
  158. */
  159. public function staffService()
  160. {
  161. return $this->miniProgram()->staff;
  162. }
  163. /**
  164. * @param $code
  165. * @return mixed
  166. * @author xaboy
  167. * @day 2020/6/18
  168. */
  169. public function getUserInfo($code)
  170. {
  171. $userInfo = $this->miniProgram()->sns->getSessionKey($code);
  172. return $userInfo;
  173. }
  174. /**
  175. * @return \EasyWeChat\MiniProgram\QRCode\QRCode
  176. * @author xaboy
  177. * @day 2020/6/18
  178. */
  179. public function qrcodeService()
  180. {
  181. return $this->miniProgram()->qrcode;
  182. }
  183. /**
  184. * 生成支付订单对象
  185. * @param $openid
  186. * @param $out_trade_no
  187. * @param $total_fee
  188. * @param $attach
  189. * @param $body
  190. * @param string $detail
  191. * @param string $trade_type
  192. * @param array $options
  193. * @return Order
  194. */
  195. protected function paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  196. {
  197. $total_fee = bcmul($total_fee, 100, 0);
  198. $order = array_merge(compact('openid', 'out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type'), $options);
  199. if ($order['detail'] == '') unset($order['detail']);
  200. return new Order($order);
  201. }
  202. /**
  203. * 获得下单ID
  204. * @param $openid
  205. * @param $out_trade_no
  206. * @param $total_fee
  207. * @param $attach
  208. * @param $body
  209. * @param string $detail
  210. * @param string $trade_type
  211. * @param array $options
  212. * @return mixed
  213. */
  214. public function paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  215. {
  216. $order = $this->paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
  217. $result = $this->paymentService()->prepare($order);
  218. if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
  219. return $result->prepay_id;
  220. } else {
  221. if ($result->return_code == 'FAIL') {
  222. throw new ValidateException('微信支付错误返回:' . $result->return_msg);
  223. } else if (isset($result->err_code)) {
  224. throw new ValidateException('微信支付错误返回:' . $result->err_code_des);
  225. } else {
  226. throw new ValidateException('没有获取微信支付的预支付ID,请重新发起支付!');
  227. }
  228. }
  229. }
  230. /**
  231. * 获得jsSdk支付参数
  232. * @param $openid
  233. * @param $out_trade_no
  234. * @param $total_fee
  235. * @param $attach
  236. * @param $body
  237. * @param string $detail
  238. * @param string $trade_type
  239. * @param array $options
  240. * @return array|string
  241. */
  242. public function jsPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [])
  243. {
  244. return $this->paymentService()->configForJSSDKPayment($this->paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options));
  245. }
  246. /**
  247. * 使用商户订单号退款
  248. * @param $orderNo
  249. * @param $refundNo
  250. * @param $totalFee
  251. * @param null $refundFee
  252. * @param null $opUserId
  253. * @param string $refundReason
  254. * @param string $type
  255. * @param string $refundAccount
  256. * @return Collection|ResponseInterface
  257. */
  258. public function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS')
  259. {
  260. if (empty($this->config['payment']['pay_routine_client_key']) || empty($this->config['payment']['pay_routine_client_cert'])) {
  261. throw new \Exception('请配置微信支付证书');
  262. }
  263. $totalFee = floatval($totalFee);
  264. $refundFee = floatval($refundFee);
  265. return $this->paymentService()->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $type, $refundAccount, $refundReason);
  266. }
  267. /**
  268. * 发送订阅消息
  269. * @param string $touser 接收者(用户)的 openid
  270. * @param string $templateId 所需下发的订阅模板id
  271. * @param array $data 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
  272. * @param string $link 击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  273. * @return \EasyWeChat\Support\Collection|null
  274. * @throws \EasyWeChat\Core\Exceptions\HttpException
  275. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  276. */
  277. public function sendSubscribeTemlate(string $touser, string $templateId, array $data, string $link = '')
  278. {
  279. return $this->miniprogram()->now_notice->to($touser)->template($templateId)->andData($data)->withUrl($link)->send();
  280. }
  281. /**
  282. * @param $orderNo
  283. * @param array $opt
  284. * @return bool
  285. * @author xaboy
  286. * @day 2020/6/18
  287. */
  288. public function payOrderRefund($orderNo, array $opt)
  289. {
  290. if (!isset($opt['pay_price'])) throw new ValidateException('缺少pay_price');
  291. $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
  292. $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
  293. $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
  294. $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
  295. $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
  296. $type = isset($opt['type']) ? $opt['type'] : 'out_trade_no';
  297. $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
  298. try {
  299. $res = ($this->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, $refundReason, $type, $refundAccount));
  300. if ($res->return_code == 'FAIL') throw new ValidateException('退款失败:' . $res->return_msg);
  301. if (isset($res->err_code)) throw new ValidateException('退款失败:' . $res->err_code_des);
  302. } catch (\Exception $e) {
  303. throw new ValidateException($e->getMessage());
  304. }
  305. return true;
  306. }
  307. /**
  308. * @return \Symfony\Component\HttpFoundation\Response
  309. * @throws \EasyWeChat\Core\Exceptions\FaultException
  310. * @author xaboy
  311. * @day 2020/6/18
  312. */
  313. public function handleNotify()
  314. {
  315. $this->service->payment = new PaymentService($this->service->merchant);
  316. return $this->service->payment->handleNotify(function ($notify, $successful) {
  317. Log::info('小程序支付回调' . var_export($notify, 1));
  318. if (!$successful) return;
  319. try {
  320. event('pay_success_' . $notify['attach'], ['order_sn' => $notify['out_trade_no'], 'data' => $notify]);
  321. } catch (\Exception $e) {
  322. Log::info('小程序支付回调失败:' . $e->getMessage());
  323. return false;
  324. }
  325. return true;
  326. });
  327. }
  328. /**
  329. * @return easywechat\combinePay\Client
  330. */
  331. public function combinePay()
  332. {
  333. return $this->service->combinePay;
  334. }
  335. public function handleCombinePayNotify($type)
  336. {
  337. $this->service->combinePay->handleNotify(function ($notify, $successful) use ($type) {
  338. Log::info('微信支付成功回调' . var_export($notify, 1));
  339. if (!$successful) return false;
  340. try {
  341. event('pay_success_' . $type, ['order_sn' => $notify['combine_out_trade_no'], 'data' => $notify, 'is_combine' => 1]);
  342. } catch (\Exception $e) {
  343. Log::info('微信支付回调失败:' . $e->getMessage());
  344. return false;
  345. }
  346. return true;
  347. });
  348. }
  349. }