CombinePayService.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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 app\common\model\user\User;
  13. use app\common\repositories\wechat\WechatUserRepository;
  14. use think\exception\ValidateException;
  15. class CombinePayService
  16. {
  17. protected $type;
  18. protected $options;
  19. public function __construct(string $type, array $options)
  20. {
  21. $this->type = $type;
  22. $this->options = $options;
  23. }
  24. public function pay(User $user)
  25. {
  26. $method = 'payCombine' . ucfirst($this->type);
  27. if (!method_exists($this, $method)) {
  28. throw new ValidateException('不支持该支付方式');
  29. }
  30. return $this->{$method}($user);
  31. }
  32. public function payCombineWeixin(User $user)
  33. {
  34. $wechatUserRepository = app()->make(WechatUserRepository::class);
  35. $openId = $wechatUserRepository->idByOpenId($user['wechat_user_id']);
  36. if (!$openId)
  37. throw new ValidateException('请关联微信公众号!');
  38. $config = WechatService::create()->combinePay()->payJs($openId, $this->options);
  39. return compact('config');
  40. }
  41. public function payCombineWeixinQr(User $user)
  42. {
  43. $config = WechatService::create()->combinePay()->payNative($this->options);
  44. return ['config' => $config['code_url']];
  45. }
  46. public function payCombineRoutine(User $user)
  47. {
  48. $wechatUserRepository = app()->make(WechatUserRepository::class);
  49. $openId = $wechatUserRepository->idByRoutineId($user['wechat_user_id']);
  50. if (!$openId)
  51. throw new ValidateException('请关联微信小程序!');
  52. $config = MiniProgramService::create()->combinePay()->payJs($openId, $this->options);
  53. return compact('config');
  54. }
  55. public function payCombineH5(User $user)
  56. {
  57. $config = WechatService::create()->combinePay()->payH5($this->options, 'Wap');
  58. return ['config' => ['mweb_url' => $config['h5_url']]];
  59. }
  60. public function payCombineWeixinApp(User $user)
  61. {
  62. $config = WechatService::create()->combinePay()->payApp($this->options);
  63. return compact('config');
  64. }
  65. }