123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace app\services\pay;
- use app\services\order\StoreOrderCartInfoServices;
- use app\services\wechat\WechatUserServices;
- use think\exception\ValidateException;
- class OrderPayServices
- {
-
- protected $payServices;
- public function __construct(PayServices $services)
- {
- $this->payServices = $services;
- }
-
- public function orderPay(array $orderInfo, string $payType)
- {
- if ($orderInfo['paid']) {
- throw new ValidateException('订单已支付!');
- }
- if ($orderInfo['pay_price'] <= 0) {
- throw new ValidateException('该支付无需支付!');
- }
- $openid = '';
- if (!in_array($payType, ['weixinh5', 'pc', 'store']) && !request()->isApp()) {
- if ($payType === 'weixin') {
- $userType = 'wechat';
- } else {
- $userType = $payType;
- }
-
- $services = app()->make(WechatUserServices::class);
- $openid = $services->uidToOpenid($orderInfo['uid'], $userType);
- if (!$openid) {
- throw new ValidateException('获取用户openid失败,无法支付');
- }
- }
- $site_name = sys_config('site_name');
- if (isset($orderInfo['member_type'])) {
- $body = substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
- $successAction = "member";
- } else {
-
- $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
- $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
- $body = substrUTf8($site_name . '--' . $body, 30);
- $successAction = "product";
- }
- if (!$body) {
- throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
- }
- return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
- }
-
- public function alipayOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
- {
- if ($orderInfo['paid']) {
- throw new ValidateException('订单已支付!');
- }
- if ($orderInfo['pay_price'] <= 0) {
- throw new ValidateException('该支付无需支付!');
- }
- $site_name = sys_config('site_name');
- if (isset($orderInfo['member_type'])) {
- $body = substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
- $successAction = "member";
- } else {
-
- $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
- $body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
- $body = substrUTf8($site_name . '--' . $body, 30);
- $successAction = "product";
- }
- if (!$body) {
- throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
- }
- return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $isCode);
- }
-
- public function orderIntegralPay(array $orderInfo, string $payType)
- {
- if ($orderInfo['paid']) {
- throw new ValidateException('订单已支付!');
- }
- if ($orderInfo['total_price'] <= 0) {
- throw new ValidateException('该支付无需支付!');
- }
- $openid = '';
- if (!in_array($payType, ['weixinh5', 'pc', 'store']) && !request()->isApp()) {
- if ($payType === 'weixin') {
- $userType = 'wechat';
- } else {
- $userType = $payType;
- }
-
- $services = app()->make(WechatUserServices::class);
- $openid = $services->uidToOpenid($orderInfo['uid'], $userType);
- if (!$openid) {
- throw new ValidateException('获取用户openid失败,无法支付');
- }
- }
- $site_name = sys_config('site_name');
- $body = $orderInfo['store_name'];
- $body = substrUTf8($site_name . '--' . $body, 30);
- $successAction = "integral";
- if (!$body) {
- throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
- }
- return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['total_price'], $successAction, $body);
- }
-
- public function alipayIntegralOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
- {
- if ($orderInfo['paid']) {
- throw new ValidateException('订单已支付!');
- }
- if ($orderInfo['total_price'] <= 0) {
- throw new ValidateException('该支付无需支付!');
- }
- $site_name = sys_config('site_name');
- $body = $orderInfo['store_name'];
- $body = substrUTf8($site_name . '--' . $body, 30);
- $successAction = "integral";
- if (!$body) {
- throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
- }
- return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['total_price'], $successAction, $body, $isCode);
- }
-
- public function otherRecharge(array $orderInfo, string $payType, string $authCode = '')
- {
- if (!$orderInfo) {
- throw new ValidateException('订单失效或者不存在');
- }
- if ($orderInfo['paid'] == 1) {
- throw new ValidateException('订单已支付');
- }
- $openid = '';
-
- if (!$authCode && !in_array($payType, ['weixinh5', 'store', 'pc', 'alipay']) && !request()->isApp()) {
- $userType = '';
- switch ($payType) {
- case 'weixin':
- case 'weixinh5':
- $userType = 'wechat';
- break;
- case 'routine':
- $userType = 'routine';
- break;
- }
- if (!$userType) {
- throw new ValidateException('不支持该类型方式');
- }
-
- $wechatUser = app()->make(WechatUserServices::class);
- $openid = $wechatUser->uidToOpenid((int)$orderInfo['uid'], $userType);
- if (!$openid) {
- throw new ValidateException('获取用户openid失败,无法支付');
- }
- }
- $site_name = sys_config('site_name');
- if (isset($orderInfo['member_type'])) {
- $body = substrUTf8($site_name . '--' . $orderInfo['member_type'], 30);
- $successAction = "member_recharge";
- }
- if (!$body) {
- throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
- }
- return $this->payServices->setAuthCode($authCode)->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
- }
- }
|