PayService.php 4.6 KB

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