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