| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace crmeb\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'], 'time_expire' => time() + (15 * 60)];
- }
- 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, 'time_expire' => time() + (15 * 60)];
- }
- 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');
- }
- public function payWeixinBarCode()
- {
- $config = WechatService::create()->payWeixinBarCode($this->options['order_sn'], $this->options['pay_price'], $this->options['attach'], $this->options['body'], $this->options['auth_code']);
- return $config;
- }
- public function payAlipayBarCode()
- {
- $config = AlipayService::create($this->affect)->payAlipayBarCode($this->options['order_sn'], $this->options['pay_price'], $this->options['body'], $this->options['auth_code']);
- return $config;
- }
- }
|