CombinePayService.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace ln\services;
  3. use app\common\model\user\User;
  4. use app\common\repositories\wechat\WechatUserRepository;
  5. use think\exception\ValidateException;
  6. class CombinePayService
  7. {
  8. protected $type;
  9. protected $options;
  10. public function __construct(string $type, array $options)
  11. {
  12. $this->type = $type;
  13. $this->options = $options;
  14. }
  15. public function pay(User $user)
  16. {
  17. $method = 'payCombine' . ucfirst($this->type);
  18. if (!method_exists($this, $method)) {
  19. throw new ValidateException('不支持该支付方式');
  20. }
  21. return $this->{$method}($user);
  22. }
  23. public function payCombineWeixin(User $user)
  24. {
  25. $wechatUserRepository = app()->make(WechatUserRepository::class);
  26. $openId = $wechatUserRepository->idByOpenId($user['wechat_user_id']);
  27. if (!$openId)
  28. return new ValidateException('请关联微信公众号!');
  29. $config = WechatService::create()->combinePay()->payJs($openId, $this->options);
  30. return compact('config');
  31. }
  32. public function payCombineWeixinQr(User $user)
  33. {
  34. $config = WechatService::create()->combinePay()->payNative($this->options);
  35. return ['config' => $config['code_url']];
  36. }
  37. public function payCombineRoutine(User $user)
  38. {
  39. $wechatUserRepository = app()->make(WechatUserRepository::class);
  40. $openId = $wechatUserRepository->idByRoutineId($user['wechat_user_id']);
  41. if (!$openId)
  42. throw new ValidateException('请关联微信小程序!');
  43. $config = MiniProgramService::create()->combinePay()->payJs($openId, $this->options);
  44. return compact('config');
  45. }
  46. public function payCombineH5(User $user)
  47. {
  48. $config = WechatService::create()->combinePay()->payH5($this->options, 'Wap');
  49. return ['config' => ['mweb_url' => $config['h5_url']]];
  50. }
  51. public function payCombineWeixinApp(User $user)
  52. {
  53. $config = WechatService::create()->combinePay()->payApp($this->options);
  54. return compact('config');
  55. }
  56. }