PayService.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. use think\facade\Cache;
  7. class PayService
  8. {
  9. protected $type;
  10. protected $options;
  11. protected $affect;
  12. public function __construct(string $type, array $options, string $affect = 'order')
  13. {
  14. $this->type = $type;
  15. $this->affect = $affect;
  16. $this->options = $options;
  17. }
  18. public function pay(?User $user)
  19. {
  20. $method = 'pay' . ucfirst($this->type);
  21. if (!method_exists($this, $method)) {
  22. throw new ValidateException('不支持该支付方式');
  23. }
  24. return $this->{$method}($user);
  25. }
  26. public function payWeixin(User $user)
  27. {
  28. $wechatUserRepository = app()->make(WechatUserRepository::class);
  29. $openId = $wechatUserRepository->idByOpenId($user['wechat_user_id']);
  30. if (!$openId)
  31. throw new ValidateException('请关联微信公众号!');
  32. $config = WechatService::create()->jsPay($openId, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body']);
  33. return compact('config');
  34. }
  35. public function payWeixinQr(?User $user)
  36. {
  37. $config = WechatService::create()->paymentPrepare('', $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body'], '', 'NATIVE');
  38. return ['config' => $config['code_url']];
  39. }
  40. public function payRoutine(User $user)
  41. {
  42. $wechatUserRepository = app()->make(WechatUserRepository::class);
  43. $openId = $wechatUserRepository->idByRoutineId($user['wechat_user_id']);
  44. if (!$openId)
  45. throw new ValidateException('请关联微信小程序!');
  46. $config = MiniProgramService::create()->jsPay($openId, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body']);
  47. return compact('config');
  48. }
  49. public function payH5(User $user)
  50. {
  51. $config = WechatService::create()->paymentPrepare(null, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body'], '', 'MWEB');
  52. return compact('config');
  53. }
  54. public function payWeixinApp(User $user)
  55. {
  56. $config = WechatService::create()->jsPay(null, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body'], '', 'APP');
  57. return compact('config');
  58. }
  59. public function payAlipay(User $user)
  60. {
  61. $url = AlipayService::create($this->affect)->wapPaymentPrepare($this->options['order_sn'], $this->options['pay_price'], $this->options['body'], $this->options['return_url']);
  62. $pay_key = md5($url);
  63. Cache::store('file')->set('pay_key' . $pay_key, $url, 3600);
  64. return ['config' => $url, 'pay_key' => $pay_key];
  65. }
  66. public function payAlipayQr(? User $user)
  67. {
  68. $url = AlipayService::create($this->affect)->qrPaymentPrepare($this->options['order_sn'], $this->options['pay_price'], $this->options['body']);
  69. return ['config' => $url];
  70. }
  71. public function payAlipayApp(User $user)
  72. {
  73. $config = AlipayService::create($this->affect)->appPaymentPrepare($this->options['order_sn'], $this->options['pay_price'], $this->options['body']);
  74. return compact('config');
  75. }
  76. }