|
|
|
@ -13,6 +13,7 @@ use App\Model\App;
|
|
|
|
|
use App\Model\Order;
|
|
|
|
|
use App\Model\OrderSplitInfo;
|
|
|
|
|
use App\Model\Refund;
|
|
|
|
|
use App\Model\RefundSplitInfo;
|
|
|
|
|
use App\Model\User;
|
|
|
|
|
|
|
|
|
|
class PaymentService extends AbstractService
|
|
|
|
@ -104,11 +105,11 @@ class PaymentService extends AbstractService
|
|
|
|
|
$order->org_split_info_list = $params['splitInfoList'];
|
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
|
|
$fee = floor($order->amount * 0.007);
|
|
|
|
|
$platformAccount = User::getPlatformAccount();
|
|
|
|
|
$fee = $platformAccount ? floor($order->amount * 0.01) : 0;
|
|
|
|
|
$orderSplitInfos = [];
|
|
|
|
|
$splitUserIds = array_column($params['splitInfoList'], 'userId');
|
|
|
|
|
$users = User::where('app_id', $app->app_id)->whereIn('user_id', $splitUserIds)->get()->keyBy('user_id');
|
|
|
|
|
$platformAccount = User::getPlatformAccount();
|
|
|
|
|
foreach ($params['splitInfoList'] as $splitInfo) {
|
|
|
|
|
$splitUser = $users[$splitInfo['userId']];
|
|
|
|
|
$splitAmount = $splitInfo['sellerFlag'] == 1 ? ($splitInfo['amount'] - $fee): $splitInfo['amount'];
|
|
|
|
@ -151,7 +152,7 @@ class PaymentService extends AbstractService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function updateOrder($params) {
|
|
|
|
|
$order = Order::where('order_no', $params['order_no'])->where('status', 'APPLY_FAILED')->first();
|
|
|
|
|
$order = Order::where('order_no', $params['order_no'])->where('status', 'APPLY_SUCCESS')->first();
|
|
|
|
|
if (empty($order)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
@ -167,17 +168,17 @@ class PaymentService extends AbstractService
|
|
|
|
|
|
|
|
|
|
public function refundApply(array $data, App $app) {
|
|
|
|
|
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
|
|
|
|
|
if ($user) {
|
|
|
|
|
throw new BusinessException('用户已存在');
|
|
|
|
|
if (!$user) {
|
|
|
|
|
throw new BusinessException('用户不存在');
|
|
|
|
|
}
|
|
|
|
|
$order = Order::where('app_id', $app->app_id)->where('out_order_no', $data['outOrderNo'])->first();
|
|
|
|
|
if (empty($order)) {
|
|
|
|
|
throw new BusinessException('订单号不存在');
|
|
|
|
|
}
|
|
|
|
|
if ($order->status != 'FINISH') {
|
|
|
|
|
if ($order->status != 'SUCCESS') {
|
|
|
|
|
throw new BusinessException('该订单状态不能发起退款');
|
|
|
|
|
}
|
|
|
|
|
$refund = Refund::where('app_id', $app->app_id)->where('refund_no', $data['refundNo'])->first();
|
|
|
|
|
$refund = Refund::where('app_id', $app->app_id)->where('out_refund_no', $data['outRefundNo'])->first();
|
|
|
|
|
if ($refund) {
|
|
|
|
|
throw new BusinessException('该退款单已经存在');
|
|
|
|
|
}
|
|
|
|
@ -189,19 +190,20 @@ class PaymentService extends AbstractService
|
|
|
|
|
$refund->order_no = $order->order_no;
|
|
|
|
|
$refund->out_order_no = $order->out_order_no;
|
|
|
|
|
$refund->refund_no = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_REFUND);
|
|
|
|
|
$refund->out_refund_no = $data['outRefundNo'];
|
|
|
|
|
$refund->refund_amount = $data['refundAmount'];
|
|
|
|
|
$refund->refund_reason = $data['refundReason'];
|
|
|
|
|
$refund->notify_url = $data['notifyUrl'] ?? '';
|
|
|
|
|
$refund->org_refund_split_info_list = $data['refundSplitInfoList'];
|
|
|
|
|
$refund->remark = $data['remark'] ?: '';
|
|
|
|
|
$refund->org_refund_split_info_list = $data['refundSplitInfoList'] ?? [];
|
|
|
|
|
$refund->remark = $data['remark'] ?? '';
|
|
|
|
|
$refund->applied_at = date('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
|
|
$refundSplitInfoList = [];
|
|
|
|
|
$refundSplitInfos = [];
|
|
|
|
|
foreach ($order->order_split_infos as $splitInfo) {
|
|
|
|
|
foreach ($order->orderSplitInfos as $splitInfo) {
|
|
|
|
|
$refundSplitInfoList[] = [
|
|
|
|
|
'orgSubOutOrderNo' => $splitInfo->sub_order_no,
|
|
|
|
|
'refundAmount' => $splitInfo->split_amount,
|
|
|
|
|
'refundAmount' => (string)$splitInfo->split_amount,
|
|
|
|
|
];
|
|
|
|
|
$refundSplitInfos[$splitInfo->sub_order_no] = [
|
|
|
|
|
'app_id' => $splitInfo->user_id,
|
|
|
|
@ -210,12 +212,14 @@ class PaymentService extends AbstractService
|
|
|
|
|
'user_id' => $splitInfo->user_id,
|
|
|
|
|
'split_member_id' => $splitInfo->split_member_id,
|
|
|
|
|
'split_user_id' => $splitInfo->split_user_id,
|
|
|
|
|
'refund_no' => $refund->refund_no,
|
|
|
|
|
'out_refund_no' => $refund->out_refund_no,
|
|
|
|
|
'order_no' => $splitInfo->order_no,
|
|
|
|
|
'out_order_no' => $splitInfo->out_order_no,
|
|
|
|
|
'sub_order_no' => $splitInfo->sub_order_no,
|
|
|
|
|
'sub_out_order_no' => $splitInfo->sub_out_order_no,
|
|
|
|
|
'refund_amount' => $splitInfo->split_amount,
|
|
|
|
|
'status' => 'P',
|
|
|
|
|
'status' => Refund::STATUS_PREPARE,
|
|
|
|
|
'message' => '',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
@ -235,18 +239,69 @@ class PaymentService extends AbstractService
|
|
|
|
|
$refund->real_refund_amount = $result['refundAmount'];
|
|
|
|
|
foreach ($result['refundSplitResultList'] as $refundSplitResult) {
|
|
|
|
|
$subOrderNo = $refundSplitResult['orgSubOutOrderNo'];
|
|
|
|
|
$refundSplitResult[$subOrderNo]['status'] = $refundSplitResult['status'];
|
|
|
|
|
$refundSplitResult[$subOrderNo]['message'] = $refundSplitResult['message'];
|
|
|
|
|
$refundSplitInfos[$subOrderNo]['status'] = Refund::getStatusByShortStatus($refundSplitResult['status']);
|
|
|
|
|
$refundSplitInfos[$subOrderNo]['message'] = $refundSplitResult['message'] ?: '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefundSplitInfo::insert($refundSplitInfos);
|
|
|
|
|
$refund->save();
|
|
|
|
|
|
|
|
|
|
} catch (BasicException $e) {
|
|
|
|
|
$refund->status = 'APPLY_FAILED';
|
|
|
|
|
$refund->error_code = $e->getCode();
|
|
|
|
|
$refund->error_message = $e->getMessage();
|
|
|
|
|
foreach ($refundSplitInfos as $key => $refundSplitInfo) {
|
|
|
|
|
$refundSplitInfos[$key]['status'] = 'APPLY_FAILED';
|
|
|
|
|
$refundSplitInfos[$key]['message'] = $e->getMessage();
|
|
|
|
|
}
|
|
|
|
|
RefundSplitInfo::insert($refundSplitInfos);
|
|
|
|
|
$refund->save();
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$refund->save();
|
|
|
|
|
public function refundConfirm(array $data, App $app, string $token)
|
|
|
|
|
{
|
|
|
|
|
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
|
|
|
|
|
if (!$user) {
|
|
|
|
|
throw new BusinessException('用户不存在');
|
|
|
|
|
}
|
|
|
|
|
$order = Order::where('app_id', $app->app_id)->where('out_order_no', $data['outOrderNo'])->first();
|
|
|
|
|
if (empty($order)) {
|
|
|
|
|
throw new BusinessException('订单号不存在');
|
|
|
|
|
}
|
|
|
|
|
if ($order->status != 'SUCCESS') {
|
|
|
|
|
throw new BusinessException('该订单状态不能发起退款');
|
|
|
|
|
}
|
|
|
|
|
$refund = Refund::where('app_id', $app->app_id)->where('out_refund_no', $data['outRefundNo'])->first();
|
|
|
|
|
if (empty($refund)) {
|
|
|
|
|
throw new BusinessException('退款申请不存在');
|
|
|
|
|
}
|
|
|
|
|
if ($refund->status != Refund::STATUS_APPLY_SUCCESS || $refund->status != Refund::STATUS_CONFIRM_FAILED) {
|
|
|
|
|
throw new BusinessException('该退款单未申请成功');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$baofu = new Baofu();
|
|
|
|
|
$result = $baofu->profitShareRefundConfirm([
|
|
|
|
|
'loginNo' => $user->member_id,
|
|
|
|
|
'refundTradeId' => $refund->refund_no,
|
|
|
|
|
'refundTradeType' => 'REFUND_APPLY',
|
|
|
|
|
'orgTradeId' => $refund->order_no,
|
|
|
|
|
'refundAmount' => $refund->refund_amount,
|
|
|
|
|
'notifyUrl' => $data['notifyUrl'],
|
|
|
|
|
], $token);
|
|
|
|
|
$refund->status = Refund::getStatusByShortStatus($result['refundStatus']);
|
|
|
|
|
$refund->third_refund_no = $result['refundOrderId'];
|
|
|
|
|
$refund->save();
|
|
|
|
|
|
|
|
|
|
} catch (ApiException $e) {
|
|
|
|
|
$refund->status = Refund::STATUS_CONFIRM_FAILED;
|
|
|
|
|
$refund->error_code = $e->getCode();
|
|
|
|
|
$refund->error_message = $e->getMessage();
|
|
|
|
|
$refund->save();
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function updateRefund($params)
|
|
|
|
|