145 lines
4.1 KiB
PHP
145 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Exceptions\BizException;
|
|
use App\Exceptions\InvalidPaySerialNumberException;
|
|
use App\Models\DistributionPreIncomeJob;
|
|
use App\Models\Order;
|
|
use App\Models\PayLog;
|
|
|
|
class PayService
|
|
{
|
|
/**
|
|
* 根据支付流水号处理支付成功业务
|
|
*
|
|
* @param string $sn
|
|
* @param array $params
|
|
* @return \App\Models\PayLog
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
* @throws \App\Exceptions\InvalidPaySerialNumberException
|
|
*/
|
|
public function handleSuccessByPaySerialNumber(string $paySerialNumber, array $params = []): PayLog
|
|
{
|
|
$payLog = PayLog::where('pay_sn', $paySerialNumber)->lockForUpdate()->first();
|
|
|
|
if ($payLog === null) {
|
|
throw new InvalidPaySerialNumberException();
|
|
}
|
|
|
|
return $this->handleSuccess($payLog, $params);
|
|
}
|
|
|
|
/**
|
|
* 处理支付成功业务
|
|
*
|
|
* @param \App\Models\PayLog $payLog
|
|
* @param array $params
|
|
* @return \App\Models\PayLog
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
* @throws \App\Exceptions\InvalidPaySerialNumberException
|
|
*/
|
|
public function handleSuccess(PayLog $payLog, array $params = []): PayLog
|
|
{
|
|
if (! $payLog->isPending()) {
|
|
throw new InvalidPaySerialNumberException();
|
|
}
|
|
|
|
$payLog->update([
|
|
'pay_at' => $params['pay_at'] ?? now(),
|
|
'out_trade_no' => $params['out_trade_no'] ?? null,
|
|
'status' => PayLog::STATUS_SUCCESS,
|
|
]);
|
|
|
|
$payable = $payLog->payable;
|
|
|
|
if ($payable instanceof Order) {
|
|
if ($payable->isPaid()) {
|
|
throw new BizException('订单已支付');
|
|
}
|
|
|
|
if ($payable->isCompleted()) {
|
|
throw new BizException('订单已完成');
|
|
}
|
|
|
|
// 支付方式
|
|
$payWay = $payLog->pay_way;
|
|
|
|
if ($payLog->isWxpay()) {
|
|
// 微信支付
|
|
$payWay = Order::PAY_WAY_WXPAY;
|
|
} elseif ($payLog->isWallet()) {
|
|
// 可提支付
|
|
$payWay = Order::PAY_WAY_WALLET;
|
|
} elseif ($payLog->isBalance()) {
|
|
// 余额支付
|
|
$payWay = Order::PAY_WAY_BALANCE;
|
|
}
|
|
|
|
$payable->update([
|
|
'pay_sn' => $payLog->pay_sn,
|
|
'pay_way' => $payWay,
|
|
'pay_at' => $payLog->pay_at,
|
|
'out_trade_no' => $payLog->out_trade_no,
|
|
'status' => Order::STATUS_PAID,
|
|
]);
|
|
|
|
DistributionPreIncomeJob::create([
|
|
'jobable_id' => $payable->id,
|
|
'jobable_type' => $payable->getMorphClass(),
|
|
'remarks' => '支付订单',
|
|
]);
|
|
}
|
|
|
|
return $payLog;
|
|
}
|
|
|
|
/**
|
|
* 根据支付流水号处理支付失败业务
|
|
*
|
|
* @param string $sn
|
|
* @param array $params
|
|
* @return \App\Models\PayLog
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
* @throws \App\Exceptions\InvalidPaySerialNumberException
|
|
*/
|
|
public function handleFailedByPaySerialNumber(string $paySerialNumber, array $params = []): PayLog
|
|
{
|
|
$payLog = PayLog::where('pay_sn', $paySerialNumber)->lockForUpdate()->first();
|
|
|
|
if ($payLog === null) {
|
|
throw new InvalidPaySerialNumberException();
|
|
}
|
|
|
|
return $this->handleFailed($payLog, $params);
|
|
}
|
|
|
|
/**
|
|
* 处理支付失败业务
|
|
*
|
|
* @param \App\Models\PayLog $payLog
|
|
* @param array $params
|
|
* @return \App\Models\PayLog
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
* @throws \App\Exceptions\InvalidPaySerialNumberException
|
|
*/
|
|
public function handleFailed(PayLog $payLog, array $params = []): PayLog
|
|
{
|
|
if (! $payLog->isPending()) {
|
|
throw new InvalidPaySerialNumberException();
|
|
}
|
|
|
|
$payLog->update([
|
|
'out_trade_no' => $params['out_trade_no'] ?? null,
|
|
'status' => PayLog::STATUS_FAILED,
|
|
'failed_reason' => $params['failed_reason'] ?? null,
|
|
]);
|
|
|
|
return $payLog;
|
|
}
|
|
}
|