282 lines
7.6 KiB
PHP
282 lines
7.6 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Api\Pay;
|
|
|
|
use App\Cache\Redis\Api\ApiRedisKey;
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Constants\Common\OrderCode;
|
|
use App\Constants\Common\PayCode;
|
|
use App\Exception\ErrException;
|
|
use App\Extend\SystemUtil;
|
|
use App\Model\Order;
|
|
use App\Model\PayOrder;
|
|
use App\Model\UserThird;
|
|
use App\Service\Api\BaseService;
|
|
use App\Service\Common\Pay\Wx\WxJsRechargeOrderService;
|
|
use App\Service\ServiceTrait\Api\CateringTrait;
|
|
use App\Service\ServiceTrait\Api\CouponTrait;
|
|
use App\Service\ServiceTrait\Api\GoodOrderTrait;
|
|
use App\Service\ServiceTrait\Api\RefundOrderTrait;
|
|
use App\Service\ServiceTrait\Common\OrderChangeStatusTrait;
|
|
use Hyperf\DbConnection\Db;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class PlacePayService extends BaseService
|
|
{
|
|
/**
|
|
* @var int
|
|
*/
|
|
private int $orderId;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private int $orderType;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private int $payType;
|
|
|
|
/**
|
|
* @var Order
|
|
*/
|
|
#[Inject]
|
|
protected Order $orderModel;
|
|
|
|
/**
|
|
* @var mixed
|
|
*/
|
|
private Order $orderInfo;
|
|
|
|
/**
|
|
* @var mixed
|
|
*/
|
|
private mixed $payInfo;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private string $lockKey;
|
|
|
|
/**
|
|
* @var RedisCache
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redisCache;
|
|
|
|
/**
|
|
* 构造
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->lockKey = ApiRedisKey::payLock($this->userId);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
if (0 == ($this->redisCache->addLock($this->lockKey,5))) {
|
|
throw new ErrException('请勿重复点击');
|
|
}
|
|
|
|
$this->orderId = (int)$this->request->input('order_id');
|
|
$this->orderType = (int)$this->request->input('order_type');
|
|
$this->payType = (int)$this->request->input('pay_type');
|
|
|
|
$this->orderModel = (new OrderTypeFactory)->getPayOrderModel($this->orderType);
|
|
|
|
$this->checkOrder();
|
|
|
|
//小于等于 0 直接支付
|
|
if ($this->orderInfo->actual_price <= 0) return $this->directPayment();
|
|
|
|
$this->checkPayOrder();
|
|
// 测试环境 0.01
|
|
if (!SystemUtil::checkProEnv() && $this->orderInfo->actual_price > 0) $this->orderInfo->actual_price = '0.01';
|
|
|
|
$this->setPayInfo();
|
|
|
|
$rechargeService = match ($this->payType)
|
|
{
|
|
PayCode::ALIPAY => $this->setAliPayOrder(),
|
|
PayCode::WECHAT_PAY => $this->setWechatPayOrder(),
|
|
};
|
|
|
|
$rechargeService->setConfig();
|
|
$rechargeService->setNotify();
|
|
|
|
$payData = $rechargeService->pay(
|
|
(float)$this->orderInfo->actual_price,
|
|
// $this->request->input('body','订单支付'),
|
|
$this->orderInfo->id,
|
|
$this->orderInfo->order_sno,
|
|
$this->userId
|
|
);
|
|
|
|
$res = [
|
|
'orderId' => $this->orderId,
|
|
'wechat_pay' => [],
|
|
'alipay' => [],
|
|
'status' => $this->orderInfo->status
|
|
];
|
|
|
|
//返回支付数组
|
|
match ($this->payType) {
|
|
PayCode::WECHAT_PAY => $res['wechat_pay'] = $payData,
|
|
PayCode::ALIPAY => throw new ErrException('暂不开放')
|
|
};
|
|
|
|
$this->redisCache->delLock($this->lockKey);
|
|
|
|
return $this->return->success('success', $res);
|
|
}
|
|
|
|
use GoodOrderTrait;
|
|
use CateringTrait;
|
|
use OrderChangeStatusTrait;
|
|
private bool $isCatering;
|
|
|
|
private function directPayment(): array
|
|
{
|
|
switch ($this->orderType) {
|
|
case OrderCode::ORDER_TYPE_GOOD:
|
|
Db::transaction(function (){
|
|
$this->manageOrder();
|
|
|
|
$this->isCatering = $this->manageAddCateringLog();
|
|
});
|
|
|
|
//已经截单 自动退款
|
|
if (!$this->isCatering) $this->directGoodRefund();
|
|
break;
|
|
case OrderCode::ORDER_TYPE_BALANCE: //余额重置是不是不能等于 0 元
|
|
default:
|
|
throw new ErrException('订单类型错误');
|
|
}
|
|
|
|
$res = [
|
|
'orderId' => $this->orderId,
|
|
'wechat_pay' => [],
|
|
'alipay' => [],
|
|
'status' => $this->orderInfo->status
|
|
];
|
|
|
|
$this->redisCache->delLock($this->lockKey);
|
|
|
|
return $this->return->success('success', $res);
|
|
}
|
|
|
|
use RefundOrderTrait;
|
|
use CouponTrait;
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
private function directGoodRefund(): void
|
|
{
|
|
Db::transaction(function (){
|
|
// $this->manageRefundOrder();
|
|
|
|
$this->manageOrderByRefund();
|
|
|
|
$this->manageSubCateringLog();
|
|
|
|
if ($this->orderInfo->coupin_id > 0) $this->rollbackCoupon(OrderCode::ORDER_TYPE_GOOD,$this->orderInfo);
|
|
});
|
|
}
|
|
|
|
private function setAliPayOrder(): null
|
|
{
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @var UserThird $userThirdModel
|
|
*/
|
|
#[Inject]
|
|
protected UserThird $userThirdModel;
|
|
|
|
/**
|
|
* @return WxJsRechargeOrderService
|
|
*/
|
|
private function setWechatPayOrder(): WxJsRechargeOrderService
|
|
{
|
|
$rechargeService = new WxJsRechargeOrderService();
|
|
|
|
if (empty($rechargeService)) {
|
|
throw new ErrException('充值异常');
|
|
}
|
|
|
|
$rechargeService->openId = $this->userThirdModel->getWxThirdInfoByUserId($this->userId);
|
|
if (empty($rechargeService->openId)) throw new ErrException('该账户不可使用微信支付');
|
|
|
|
return $rechargeService;
|
|
}
|
|
|
|
/**
|
|
* @var PayOrder
|
|
*/
|
|
#[Inject]
|
|
protected PayOrder $payOrderModel;
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
private function setPayInfo(): void
|
|
{
|
|
if (!empty($this->payInfo)) return;
|
|
|
|
$this->payInfo = new PayOrder();
|
|
|
|
$this->payInfo->user_id = $this->userId;
|
|
$this->payInfo->order_id = $this->orderId;
|
|
$this->payInfo->order_type = $this->orderType;
|
|
$this->payInfo->order_no = $this->orderInfo->order_sno;
|
|
$this->payInfo->pay_money = $this->orderInfo->actual_price;
|
|
$this->payInfo->recharge_type = $this->payType;
|
|
$this->payInfo->status = PayCode::WAIT_PAY;
|
|
|
|
$this->payInfo->save();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
private function checkOrder(): void
|
|
{
|
|
if (empty($this->orderModel)) throw new ErrException('order type不对');
|
|
|
|
$this->orderInfo = $this->orderModel->where('id',$this->orderId)->first();
|
|
if (empty($this->orderInfo)) throw new ErrException('该订单为空');
|
|
if ($this->orderInfo->user_id != $this->userId) throw new ErrException('该订单不属于你');
|
|
if ($this->orderInfo->status != OrderCode::WAIT_PAY) throw new ErrException('该订单已支付或已取消,请确认后重试');
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
private function checkPayOrder(): void
|
|
{
|
|
$this->payInfo = $this->payOrderModel->getInfoByOrderIdAndTypeAndRType($this->orderId,$this->orderType,$this->payType);
|
|
if (empty($this->payInfo)) return;
|
|
if ($this->payInfo->status == PayCode::FINISH_PAY) throw new ErrException('该订单已支付,请确认后重试');
|
|
if ($this->payInfo->recharge_type != $this->payType) throw new ErrException('该订单调起支付失败');
|
|
if ($this->payInfo->order_type != $this->orderType) throw new ErrException('传值错误');
|
|
}
|
|
} |