parent
6856f5c2cc
commit
88f1002f9c
@ -0,0 +1,326 @@ |
||||
<?php |
||||
|
||||
namespace app\api\service\Server; |
||||
|
||||
use app\api\model\Order as OrderModel; |
||||
use app\api\model\PaymentTrade as PaymentTradeModel; |
||||
use app\api\model\Server\ServerOrder; |
||||
use app\api\model\User as UserModel; |
||||
use app\api\model\user\BalanceLog as BalanceLogModel; |
||||
use app\common\enum\order\PayStatus as PayStatusEnum; |
||||
use app\common\enum\OrderType as OrderTypeEnum; |
||||
use app\common\enum\payment\Method as PaymentMethodEnum; |
||||
use app\common\enum\user\balanceLog\Scene as SceneEnum; |
||||
use app\common\library\Lock; |
||||
use app\common\library\Log; |
||||
use app\common\service\BaseService; |
||||
use cores\exception\BaseException; |
||||
use think\facade\Event; |
||||
|
||||
class PaySuccess extends BaseService |
||||
{ |
||||
// 当前订单信息 |
||||
public ServerOrder $orderInfo; |
||||
|
||||
// 当前用户信息 |
||||
private UserModel $userInfo; |
||||
|
||||
// 当前订单号 |
||||
private string $orderNo; |
||||
|
||||
// 当前订单ID |
||||
private string $orderId; |
||||
|
||||
// 订单支付方式 |
||||
private string $method; |
||||
|
||||
// 第三方交易记录ID |
||||
private ?int $tradeId = null; |
||||
|
||||
// 第三方支付成功返回的数据 |
||||
private array $paymentData = []; |
||||
|
||||
/** |
||||
* 设置支付的订单ID |
||||
* @param int $orderId 订单ID |
||||
*/ |
||||
public function setOrderId(int $orderId): PaySuccess |
||||
{ |
||||
$this->orderId = $orderId; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 设置当前的订单号 |
||||
* @param string $orderNo |
||||
* @return $this |
||||
*/ |
||||
public function setOrderNo(string $orderNo): PaySuccess |
||||
{ |
||||
$this->orderNo = $orderNo; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 设置订单支付方式 |
||||
* @param string $method |
||||
* @return $this |
||||
*/ |
||||
public function setMethod(string $method): PaySuccess |
||||
{ |
||||
$this->method = $method; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 第三方支付交易记录ID |
||||
* @param int|null $tradeId |
||||
* @return $this |
||||
*/ |
||||
public function setTradeId(?int $tradeId = null): PaySuccess |
||||
{ |
||||
$this->tradeId = $tradeId; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 第三方支付成功返回的数据 |
||||
* @param array $paymentData |
||||
* @return $this |
||||
*/ |
||||
public function setPaymentData(array $paymentData): PaySuccess |
||||
{ |
||||
$this->paymentData = $paymentData; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 订单支付成功业务处理 |
||||
* @return bool |
||||
* @throws BaseException |
||||
*/ |
||||
public function handle(): bool |
||||
{ |
||||
// 验证当前参数是否合法 |
||||
$this->verifyParameters(); |
||||
// 当前订单开启并发锁 |
||||
$this->lockUp(); |
||||
// 验证当前订单是否允许支付 |
||||
if ($this->checkOrderStatusOnPay()) { |
||||
// 更新订单状态为已付款 |
||||
$this->updatePayStatus(); |
||||
// 订单支付成功事件 (处理订单来源相关业务) |
||||
Event::trigger('OrderPaySuccess', [ |
||||
'order' => $this->getOrderInfo(), |
||||
'orderType' => OrderTypeEnum::SERVER |
||||
]); |
||||
} |
||||
// 当前订单解除并发锁 |
||||
$this->unLock(); |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* 验证当前参数是否合法 |
||||
* @throws BaseException |
||||
*/ |
||||
private function verifyParameters() |
||||
{ |
||||
if (empty($this->orderNo)) { |
||||
throwError('orderNo not found'); |
||||
} |
||||
if (empty($this->method)) { |
||||
throwError('method not found'); |
||||
} |
||||
if ($this->tradeId) { |
||||
empty($this->paymentData) && throwError('PaymentData not found'); |
||||
!isset($this->paymentData['tradeNo']) && throwError('PaymentData not found'); |
||||
} |
||||
// 记录日志 |
||||
Log::append('PaySuccess', [ |
||||
'orderNo' => $this->orderNo, 'method' => $this->method, |
||||
'tradeId' => $this->tradeId, 'paymentData' => $this->paymentData |
||||
]); |
||||
} |
||||
|
||||
/** |
||||
* 订单模型 |
||||
* @return OrderModel|null |
||||
* @throws BaseException |
||||
*/ |
||||
private function orderModel(): ?OrderModel |
||||
{ |
||||
return $this->getOrderInfo(); |
||||
} |
||||
|
||||
/** |
||||
* 订单已付款事件 |
||||
* @return void |
||||
* @throws BaseException |
||||
*/ |
||||
private function updatePayStatus(): void |
||||
{ |
||||
// 记录日志 |
||||
Log::append('PaySuccess --updatePayStatus', ['title' => '订单已付款事件']); |
||||
// 当前订单信息 |
||||
$orderInfo = $this->getOrderInfo(); |
||||
// 事务处理 |
||||
$this->orderModel()->transaction(function () use ($orderInfo) { |
||||
// 更新订单状态 |
||||
$this->updateOrderStatus(); |
||||
// 累积用户总消费金额 |
||||
UserModel::setIncPayMoney($orderInfo['user_id'], (float)$orderInfo['pay_price']); |
||||
// 记录订单支付信息 |
||||
$this->updatePayInfo(); |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* 记录订单支付的信息 |
||||
* @throws BaseException |
||||
*/ |
||||
private function updatePayInfo() |
||||
{ |
||||
// 当前订单信息 |
||||
$orderInfo = $this->getOrderInfo(); |
||||
// 余额支付 |
||||
if ($this->method == PaymentMethodEnum::BALANCE) { |
||||
// 更新用户余额 |
||||
UserModel::setDecBalance((int)$orderInfo['user_id'], (float)$orderInfo['pay_price']); |
||||
// 新增余额变动记录 |
||||
BalanceLogModel::add(SceneEnum::CONSUME, [ |
||||
'user_id' => (int)$orderInfo['user_id'], |
||||
'money' => -$orderInfo['pay_price'], |
||||
], ['order_no' => $orderInfo['order_no']]); |
||||
} |
||||
// 将第三方交易记录更新为已支付状态 |
||||
if (in_array($this->method, [PaymentMethodEnum::WECHAT, PaymentMethodEnum::ALIPAY])) { |
||||
$this->updateTradeRecord(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 将第三方交易记录更新为已支付状态 |
||||
*/ |
||||
private function updateTradeRecord() |
||||
{ |
||||
if ($this->tradeId && !empty($this->paymentData)) { |
||||
PaymentTradeModel::updateToPaySuccess($this->tradeId, $this->paymentData['tradeNo']); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 更新订单状态 |
||||
* @throws BaseException |
||||
*/ |
||||
private function updateOrderStatus(): void |
||||
{ |
||||
// 更新订单状态 |
||||
$this->orderModel()->save([ |
||||
'pay_method' => $this->method, |
||||
'pay_status' => PayStatusEnum::SUCCESS, |
||||
'pay_time' => time(), |
||||
'trade_id' => $this->tradeId ?: 0, |
||||
]); |
||||
} |
||||
|
||||
/** |
||||
* 获取买家用户信息 |
||||
* @return UserModel|array|null |
||||
* @throws BaseException |
||||
*/ |
||||
private function getUserInfo() |
||||
{ |
||||
if (empty($this->userInfo)) { |
||||
$this->userInfo = UserModel::detail($this->getOrderInfo()['user_id']); |
||||
} |
||||
if (empty($this->userInfo)) { |
||||
throwError('未找到买家用户信息'); |
||||
} |
||||
return $this->userInfo; |
||||
} |
||||
|
||||
/** |
||||
* 验证当前订单是否允许支付 |
||||
* @return bool |
||||
* @throws BaseException |
||||
*/ |
||||
private function checkOrderStatusOnPay(): bool |
||||
{ |
||||
// 当前订单信息 |
||||
$orderInfo = $this->getOrderInfo(); |
||||
// 验证余额支付时用户余额是否满足 |
||||
if ($this->method == PaymentMethodEnum::BALANCE) { |
||||
if ($this->getUserInfo()['balance'] < $orderInfo['pay_price']) { |
||||
throwError('账户余额不足,无法使用余额支付'); |
||||
} |
||||
} |
||||
// 检查订单状态是否为已支付 |
||||
if ($orderInfo['pay_status'] == PayStatusEnum::SUCCESS) { |
||||
$this->onOrderPaid(); |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* 处理订单已支付的情况 |
||||
* @throws BaseException |
||||
*/ |
||||
private function onOrderPaid() |
||||
{ |
||||
// 记录日志 |
||||
Log::append('PaySuccess --onOrderPaid', ['title' => '处理订单已支付的情况']); |
||||
// 当前订单信息 |
||||
$orderInfo = $this->getOrderInfo(); |
||||
// 余额支付直接返回错误信息 |
||||
if ($this->method == PaymentMethodEnum::BALANCE) { |
||||
throwError('当前订单已支付,无需重复支付'); |
||||
} |
||||
// 第三方支付判断是否为重复下单 (因异步回调可能存在网络延迟的原因,在并发的情况下会出现同时付款两次,这里需要容错) |
||||
// 如果订单记录中已存在tradeId并且和当前支付的tradeId不一致, 那么判断为重复的订单, 需进行退款处理 |
||||
if ($this->tradeId > 0 && $orderInfo['trade_id'] != $this->tradeId) { |
||||
// 执行原路退款 |
||||
throwError('当前订单异常'); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 获取当前订单的详情信息 |
||||
* @return OrderModel|null |
||||
* @throws BaseException |
||||
*/ |
||||
private function getOrderInfo(): ?OrderModel |
||||
{ |
||||
// 获取订单详情 (待支付状态) |
||||
if (empty($this->orderInfo)) { |
||||
$this->orderInfo = ServerOrder::getPayDetail($this->orderNo); |
||||
} |
||||
// 判断订单是否存在 |
||||
if (empty($this->orderInfo)) { |
||||
throwError('未找到该订单信息'); |
||||
} |
||||
return $this->orderInfo; |
||||
} |
||||
|
||||
/** |
||||
* 订单锁:防止并发导致重复支付 |
||||
* @throws BaseException |
||||
*/ |
||||
private function lockUp() |
||||
{ |
||||
$orderInfo = $this->getOrderInfo(); |
||||
Lock::lockUp("OrderPaySuccess_{$orderInfo['order_id']}"); |
||||
} |
||||
|
||||
/** |
||||
* 订单锁:防止并发导致重复支付 |
||||
* @throws BaseException |
||||
*/ |
||||
private function unLock() |
||||
{ |
||||
$orderInfo = $this->getOrderInfo(); |
||||
Lock::unLock("OrderPaySuccess_{$orderInfo['order_id']}"); |
||||
} |
||||
} |
@ -0,0 +1,327 @@ |
||||
<?php |
||||
|
||||
namespace app\api\service\Server; |
||||
|
||||
use app\api\model\Payment as PaymentModel; |
||||
use app\api\model\PaymentTrade as PaymentTradeModel; |
||||
use app\api\model\Server\ServerOrder; |
||||
use app\api\service\Order as OrderService; |
||||
use app\api\service\User as UserService; |
||||
use app\common\enum\Client as ClientEnum; |
||||
use app\common\enum\OrderType as OrderTypeEnum; |
||||
use app\common\enum\payment\Method as PaymentMethodEnum; |
||||
use app\common\library\payment\Facade as PaymentFacade; |
||||
use app\common\service\BaseService; |
||||
use cores\exception\BaseException; |
||||
use think\db\exception\DataNotFoundException; |
||||
use think\db\exception\DbException; |
||||
use think\db\exception\ModelNotFoundException; |
||||
|
||||
class ServerPayment extends BaseService |
||||
{ |
||||
// 提示信息 |
||||
private string $message = ''; |
||||
|
||||
// 订单信息 |
||||
private ServerOrder $orderInfo; |
||||
|
||||
// 支付方式 (余额、微信支付、支付宝) |
||||
private string $method = ''; |
||||
|
||||
// 下单的客户端 |
||||
private string $client = ''; |
||||
|
||||
//未支付的订单ID |
||||
private int $orderId; |
||||
|
||||
/** |
||||
* 设置支付的订单ID |
||||
* @param int $orderId 订单ID |
||||
*/ |
||||
public function setOrderId(int $orderId): ServerPayment |
||||
{ |
||||
$this->orderId = $orderId; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 设置当前支付方式 |
||||
* @param string $method 支付方式 |
||||
* @return $this |
||||
*/ |
||||
public function setMethod(string $method): ServerPayment |
||||
{ |
||||
$this->method = $method; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 设置下单的客户端 |
||||
* @param string $client 客户端 |
||||
* @return $this |
||||
*/ |
||||
public function setClient(string $client): ServerPayment |
||||
{ |
||||
$this->client = $client; |
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 返回消息提示 |
||||
* @return string |
||||
*/ |
||||
public function getMessage(): string |
||||
{ |
||||
return $this->message; |
||||
} |
||||
|
||||
/** |
||||
* @notes:获取支付订单的信息 |
||||
* @return array |
||||
* @throws BaseException |
||||
* @throws DataNotFoundException |
||||
* @throws DbException |
||||
* @throws ModelNotFoundException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function orderInfo(): array |
||||
{ |
||||
// 当期用户信息 |
||||
$userInfo = UserService::getCurrentLoginUser(true); |
||||
// 根据指定客户端获取可用的支付方式 |
||||
$PaymentModel = new PaymentModel; |
||||
$methods = $PaymentModel->getMethodsByClient($this->client); |
||||
// 获取结算订单信息 |
||||
$OrderModel = new ServerOrder(); |
||||
$orderInfo = $OrderModel->getUnpaidOrderDetail($this->orderId); |
||||
return [ |
||||
'order' => $orderInfo, |
||||
'personal' => $userInfo, |
||||
'paymentMethods' => $methods |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* @notes:确认订单支付事件 |
||||
* @param array $extra 附加数据 |
||||
* @return array |
||||
* @throws BaseException |
||||
* @throws DataNotFoundException |
||||
* @throws DbException |
||||
* @throws ModelNotFoundException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function orderPay(array $extra = []): array |
||||
{ |
||||
// 获取订单信息 |
||||
$this->orderInfo = ServerOrder::getDetail($this->orderId); |
||||
// 订单支付事件 |
||||
$this->orderPayEvent(); |
||||
// 构建第三方支付请求的参数 |
||||
$payment = $this->unifiedorder($extra); |
||||
// 记录第三方交易信息 |
||||
$this->recordPaymentTrade($payment); |
||||
// 返回结果 |
||||
return compact('payment'); |
||||
} |
||||
|
||||
/** |
||||
* 订单支付事件 |
||||
* @return void |
||||
* @throws BaseException |
||||
* @author: wanghousheng |
||||
*/ |
||||
private function orderPayEvent(): void |
||||
{ |
||||
// 余额支付 |
||||
if ($this->method == PaymentMethodEnum::BALANCE) { |
||||
$this->orderPaySuccess($this->orderInfo['order_no']); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取支付方式的配置信息 |
||||
* @return mixed |
||||
* @throws BaseException |
||||
* @throws DataNotFoundException |
||||
* @throws DbException |
||||
* @throws ModelNotFoundException |
||||
* @author: wanghousheng |
||||
*/ |
||||
private function getPaymentConfig() |
||||
{ |
||||
$PaymentModel = new PaymentModel; |
||||
$templateInfo = $PaymentModel->getPaymentInfo($this->method, $this->client, $this->getStoreId()); |
||||
return $templateInfo['template']['config'][$this->method]; |
||||
} |
||||
|
||||
/** |
||||
* 获取支付宝端的用户buyerId(仅支付宝小程序端) |
||||
* @return null |
||||
* @throws BaseException |
||||
* @author: wanghousheng |
||||
*/ |
||||
private function getAlipayBuyerId() |
||||
{ |
||||
if ($this->client == ClientEnum::MP_ALIPAY) { |
||||
// 当前登录用户信息 |
||||
$useInfo = UserService::getCurrentLoginUser(true); |
||||
if (!$useInfo['currentOauth'] || empty($useInfo['currentOauth']['oauth_id'])) { |
||||
throwError('很抱歉,您当前不存在buyerId 无法发起支付宝支付'); |
||||
} |
||||
// 当前第三方用户标识 |
||||
return $useInfo['currentOauth']['oauth_id']; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 整理下单接口所需的附加数据 |
||||
* @param array $extra |
||||
* @return array |
||||
* @throws BaseException |
||||
* @author: wanghousheng |
||||
*/ |
||||
private function extraAsUnify(array $extra = []): array |
||||
{ |
||||
// 微信支付时需要的附加数据 |
||||
if ($this->method === PaymentMethodEnum::WECHAT) { |
||||
// 微信小程序端和微信公众号端需要openid |
||||
if (in_array($this->client, [ClientEnum::WXOFFICIAL, ClientEnum::MP_WEIXIN])) { |
||||
$extra['openid'] = $this->getWechatOpenid(); |
||||
} |
||||
} |
||||
// 支付宝支付时需要的附加数据 |
||||
if ($this->method === PaymentMethodEnum::ALIPAY) { |
||||
// 支付宝小程序端需要buyerId |
||||
if ($this->client == ClientEnum::MP_ALIPAY) { |
||||
$extra['buyerId'] = $this->getAlipayBuyerId(); |
||||
} |
||||
} |
||||
return $extra; |
||||
} |
||||
|
||||
/** |
||||
* 获取微信端的用户openid(仅微信小程序和微信公众号) |
||||
* @return null |
||||
* @throws BaseException |
||||
* @author: wanghousheng |
||||
*/ |
||||
private function getWechatOpenid() |
||||
{ |
||||
if (in_array($this->client, [ClientEnum::MP_WEIXIN, ClientEnum::WXOFFICIAL])) { |
||||
// 当前登录用户信息 |
||||
$useInfo = UserService::getCurrentLoginUser(true); |
||||
if (!$useInfo['currentOauth'] || empty($useInfo['currentOauth']['oauth_id'])) { |
||||
throwError('很抱歉,您当前不存在openid 无法发起微信支付'); |
||||
} |
||||
// 当前第三方用户标识 |
||||
return $useInfo['currentOauth']['oauth_id']; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 查询订单是否支付成功 (仅限第三方支付订单) |
||||
* @param string $outTradeNo 商户订单号 |
||||
* @return bool |
||||
* @throws BaseException |
||||
* @throws DataNotFoundException |
||||
* @throws DbException |
||||
* @throws ModelNotFoundException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function tradeQuery(string $outTradeNo): bool |
||||
{ |
||||
// 判断支付方式是否合法 |
||||
if (!in_array($this->method, [PaymentMethodEnum::WECHAT, PaymentMethodEnum::ALIPAY])) { |
||||
return false; |
||||
} |
||||
// 获取支付方式的配置信息 |
||||
$options = $this->getPaymentConfig(); |
||||
// 构建支付模块 |
||||
$Payment = PaymentFacade::store($this->method)->setOptions($options, $this->client); |
||||
// 执行第三方支付查询API |
||||
$result = $Payment->tradeQuery($outTradeNo); |
||||
// 订单支付成功事件 |
||||
if (!empty($result) && $result['paySuccess']) { |
||||
// 获取第三方交易记录信息 |
||||
$tradeInfo = PaymentTradeModel::detailByOutTradeNo($outTradeNo); |
||||
// 订单支付成功事件 |
||||
$this->orderPaySuccess($tradeInfo['order_no'], $tradeInfo['trade_id'], $result); |
||||
} |
||||
// 返回订单状态 |
||||
return $result ? $result['paySuccess'] : false; |
||||
} |
||||
|
||||
/** |
||||
* 构建第三方支付请求的参数 |
||||
* @param array $extra 附加数据 |
||||
* @return array |
||||
* @throws BaseException |
||||
* @throws DataNotFoundException |
||||
* @throws DbException |
||||
* @throws ModelNotFoundException |
||||
* @author: wanghousheng |
||||
*/ |
||||
private function unifiedorder(array $extra = []): array |
||||
{ |
||||
// 生成第三方交易订单号 (并非主订单号) |
||||
$outTradeNo = OrderService::createOrderNo(); |
||||
// 获取支付方式的配置信息 |
||||
$options = $this->getPaymentConfig(); |
||||
// 整理下单接口所需的附加数据 |
||||
$extra = $this->extraAsUnify($extra); |
||||
// 构建支付模块 |
||||
$Payment = PaymentFacade::store($this->method)->setOptions($options, $this->client); |
||||
// 执行第三方支付下单API |
||||
if (!$Payment->unify($outTradeNo, (string)$this->orderInfo['pay_price'], $extra)) { |
||||
throwError('第三方支付下单API调用失败'); |
||||
} |
||||
// 返回客户端需要的支付参数 |
||||
return $Payment->getUnifyResult(); |
||||
} |
||||
|
||||
/** |
||||
* 记录第三方交易信息 |
||||
* @param array $payment 第三方支付数据 |
||||
* @throws BaseException |
||||
* @throws DataNotFoundException |
||||
* @throws DbException |
||||
* @throws ModelNotFoundException |
||||
* @author: wanghousheng |
||||
*/ |
||||
private function recordPaymentTrade(array $payment): void |
||||
{ |
||||
if ($this->method != PaymentMethodEnum::BALANCE) { |
||||
PaymentTradeModel::record( |
||||
$this->orderInfo, |
||||
$this->method, |
||||
$this->client, |
||||
OrderTypeEnum::SERVER, |
||||
$payment |
||||
); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 订单支付成功事件 |
||||
* @param string $orderNo 当前订单号 |
||||
* @param int|null $tradeId 第三方交易记录ID |
||||
* @param array $paymentData 第三方支付成功返回的数据 |
||||
* @return void |
||||
* @throws BaseException |
||||
* @author: wanghousheng |
||||
*/ |
||||
private function orderPaySuccess(string $orderNo, ?int $tradeId = null, array $paymentData = []): void |
||||
{ |
||||
// 获取订单详情 |
||||
$service = new PaySuccess(); |
||||
// 订单支付成功业务处理 |
||||
$service->setOrderNo($orderNo)->setMethod($this->method)->setTradeId($tradeId)->setPaymentData($paymentData); |
||||
if (!$service->handle()) { |
||||
throwError($service->getError() ?: '订单支付失败'); |
||||
} |
||||
$this->message = '恭喜您,订单支付成功'; |
||||
} |
||||
} |
Loading…
Reference in new issue