1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace ln\services;
- use app\common\model\user\User;
- use app\common\repositories\wechat\WechatUserRepository;
- use think\exception\ValidateException;
- use think\facade\Cache;
- class PayService
- {
- protected $type;
- protected $options;
- protected $affect;
- public function __construct(string $type, array $options, string $affect = 'order')
- {
- $this->type = $type;
- $this->affect = $affect;
- $this->options = $options;
- }
- public function pay(?User $user)
- {
- $method = 'pay' . ucfirst($this->type);
- if (!method_exists($this, $method)) {
- throw new ValidateException('不支持该支付方式');
- }
- return $this->{$method}($user);
- }
- public function payWeixin(User $user)
- {
- $wechatUserRepository = app()->make(WechatUserRepository::class);
- $openId = $wechatUserRepository->idByOpenId($user['wechat_user_id']);
- if (!$openId)
- throw new ValidateException('请关联微信公众号!');
- $config = WechatService::create()->jsPay($openId, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body']);
- return compact('config');
- }
- public function payWeixinQr(?User $user)
- {
- $config = WechatService::create()->paymentPrepare('', $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body'], '', 'NATIVE');
- return ['config' => $config['code_url']];
- }
- public function payRoutine(User $user)
- {
- $wechatUserRepository = app()->make(WechatUserRepository::class);
- $openId = $wechatUserRepository->idByRoutineId($user['wechat_user_id']);
- if (!$openId)
- throw new ValidateException('请关联微信小程序!');
- $config = MiniProgramService::create()->jsPay($openId, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body']);
- return compact('config');
- }
- public function payH5(User $user)
- {
- $config = WechatService::create()->paymentPrepare(null, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body'], '', 'MWEB');
- return compact('config');
- }
- public function payWeixinApp(User $user)
- {
- $config = WechatService::create()->jsPay(null, $this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body'], '', 'APP');
- return compact('config');
- }
- public function payAlipay(User $user)
- {
- $url = AlipayService::create($this->affect)->wapPaymentPrepare($this->options['order_sn'], $this->options['pay_price'], $this->options['body'], $this->options['return_url']);
- $pay_key = md5($url);
- Cache::store('file')->set('pay_key' . $pay_key, $url, 3600);
- return ['config' => $url, 'pay_key' => $pay_key];
- }
- public function payAlipayQr(? User $user)
- {
- $url = AlipayService::create($this->affect)->qrPaymentPrepare($this->options['order_sn'], $this->options['pay_price'], $this->options['body']);
- return ['config' => $url];
- }
- public function payAlipayApp(User $user)
- {
- $config = AlipayService::create($this->affect)->appPaymentPrepare($this->options['order_sn'], $this->options['pay_price'], $this->options['body']);
- return compact('config');
- }
- }
|