|
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
|
|
use App\Exception\BasicException;
|
|
|
|
|
use App\Exception\BusinessException;
|
|
|
|
|
use App\Helper\Baofu\Baofu;
|
|
|
|
|
use App\Helper\Efps\Api;
|
|
|
|
@ -13,6 +14,7 @@ use App\Helper\StringHelper;
|
|
|
|
|
use App\Model\App;
|
|
|
|
|
use App\Model\BankCard;
|
|
|
|
|
use App\Model\Order;
|
|
|
|
|
use App\Model\OrderSplitInfo;
|
|
|
|
|
use App\Model\PrePayLog;
|
|
|
|
|
use App\Model\RefundOrder;
|
|
|
|
|
use App\Model\User;
|
|
|
|
@ -25,6 +27,7 @@ use App\Request\ProtocolPayPreRequest;
|
|
|
|
|
use App\Request\RefundQueryRequest;
|
|
|
|
|
use App\Request\RefundRequest;
|
|
|
|
|
use App\Request\UnBindCardRequest;
|
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
|
|
class PaymentService extends AbstractService
|
|
|
|
|
{
|
|
|
|
@ -44,28 +47,127 @@ class PaymentService extends AbstractService
|
|
|
|
|
if ($user) {
|
|
|
|
|
throw new BusinessException('用户已存在');
|
|
|
|
|
}
|
|
|
|
|
$baofu = new Baofu();
|
|
|
|
|
$baofu->paymentSplit([
|
|
|
|
|
'loginNo' => $user->member_id,
|
|
|
|
|
'outOrderNo' => '',
|
|
|
|
|
'amount' => $user->member_id,
|
|
|
|
|
'paidType' => 'CARD',
|
|
|
|
|
'agreementNo' => $reqData[''],
|
|
|
|
|
'pwdPayExpTime' => $reqData[''],
|
|
|
|
|
'notifyUrl' => '',
|
|
|
|
|
], $token);
|
|
|
|
|
$order = Order::where('app_id', $app->app_id)->where('out_order_no', $reqData['outOrderNo'])->first();
|
|
|
|
|
if ($order) {
|
|
|
|
|
throw new BusinessException('订单号重复');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[$order, $orderSplitInfos] = $this->createOrder($app, $reqData, $user);
|
|
|
|
|
|
|
|
|
|
$splitInfoList = [];
|
|
|
|
|
foreach ($orderSplitInfos as $splitInfo) {
|
|
|
|
|
$splitInfoList[] = [
|
|
|
|
|
'subOutOrderNo' => $splitInfo['sub_order_no'],
|
|
|
|
|
'contractNo' => $splitInfo['contract_no'],
|
|
|
|
|
'customerType' => $splitInfo['split_user_type'],
|
|
|
|
|
'splitAmount' => $splitInfo['split_amount'],
|
|
|
|
|
'sellerFlag' => $splitInfo['seller_flag'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$baofu = new Baofu();
|
|
|
|
|
try {
|
|
|
|
|
$url = $baofu->paymentSplit([
|
|
|
|
|
'loginNo' => $user->member_id,
|
|
|
|
|
'outOrderNo' => $order->order_no,
|
|
|
|
|
'amount' => $user->member_id,
|
|
|
|
|
'paidType' => 'CARD',
|
|
|
|
|
'agreementNo' => $order->agreement_no,
|
|
|
|
|
'pwdPayExpTime' => $order->getExpiresIn(),
|
|
|
|
|
'splitInfoList' => $splitInfoList,
|
|
|
|
|
'notifyUrl' => '',
|
|
|
|
|
], $token);
|
|
|
|
|
$order->status = 'APPLY_SUCCESS';
|
|
|
|
|
$order->applied_at = date('Y-m-d H:i:s');
|
|
|
|
|
$order->pay_url = $url;
|
|
|
|
|
return $url;
|
|
|
|
|
} catch (BasicException $e) {
|
|
|
|
|
$order->status = 'APPLY_FAILED';
|
|
|
|
|
$order->applied_at = date('Y-m-d H:i:s');
|
|
|
|
|
$order->error_code = $e->getCode();
|
|
|
|
|
$order->error_message = $e->getMessage();
|
|
|
|
|
$order->save();
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createOrder(App $app, array $params, $user) {
|
|
|
|
|
public function createOrder(App $app, array $params, User $user) {
|
|
|
|
|
$expiresIn = $params['expiresIn'] ?: 15 * 60;
|
|
|
|
|
$order = new Order();
|
|
|
|
|
$order->app_id = $app->app_id;
|
|
|
|
|
$order->user_id = $user->user_id;
|
|
|
|
|
$order->member_id = $user->member_id;
|
|
|
|
|
$order->out_member_id = $params['outMemberId'] ?? '';
|
|
|
|
|
$order->out_order_no = $params['outOrderNo'] ?? '';
|
|
|
|
|
$order->amount = $params['payAmount'] ?? 0;
|
|
|
|
|
$order->notify_url = $params['notifyUrl'] ?? '';
|
|
|
|
|
$order->order_info = json_encode($params['orderInfo'] ?? [], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
$order->order_no = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_PAY);
|
|
|
|
|
$order->out_order_no = $params['outOrderNo'];
|
|
|
|
|
$order->amount = $params['amount'];
|
|
|
|
|
$order->notify_url = $params['notifyUrl'];
|
|
|
|
|
$order->return_url = $params['returnUrl'];
|
|
|
|
|
$order->agreement_no = $params['agreementNo'] ?: '';
|
|
|
|
|
$order->status = 'PREPARE';
|
|
|
|
|
$order->pay_channel = 'BAOFU';
|
|
|
|
|
$order->pay_method = 'paymentSplit';
|
|
|
|
|
$order->pay_type = 'CARD';
|
|
|
|
|
$order->expired_at = date('Y-m-d H:i:s', $expiresIn);
|
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
|
|
$fee = floor($order->amount * 0.007);
|
|
|
|
|
$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'];
|
|
|
|
|
$orderSplitInfos[] = [
|
|
|
|
|
'app_id' => $app->app_id,
|
|
|
|
|
'user_id' => $user->user_id,
|
|
|
|
|
'member_id' => $user->member_id,
|
|
|
|
|
'split_member_id' => $splitUser->member_id,
|
|
|
|
|
'split_user_id' => $splitInfo['userId'],
|
|
|
|
|
'split_user_type' => $splitUser->user_type,
|
|
|
|
|
'order_no' => $order->order_no,
|
|
|
|
|
'out_order_no' => $order->out_order_no,
|
|
|
|
|
'sub_order_no' => StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_PAY_SPLIT),
|
|
|
|
|
'sub_out_order_no' => $splitInfo['subOutOrderNo'],
|
|
|
|
|
'split_amount' => $splitAmount,
|
|
|
|
|
'contract_no' => $splitUser->contract_no,
|
|
|
|
|
'seller_flag' => $splitInfo['sellerFlag'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
if ($fee) {
|
|
|
|
|
$feeSubOrderNo = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_PAY_SPLIT);
|
|
|
|
|
$orderSplitInfos[] = [
|
|
|
|
|
'app_id' => $app->app_id,
|
|
|
|
|
'user_id' => $user->user_id,
|
|
|
|
|
'member_id' => $user->member_id,
|
|
|
|
|
'split_member_id' => $platformAccount->member_id,
|
|
|
|
|
'split_user_id' => $platformAccount->user_id,
|
|
|
|
|
'split_user_type' => $platformAccount->user_type,
|
|
|
|
|
'order_no' => $order->order_no,
|
|
|
|
|
'out_order_no' => $order->out_order_no,
|
|
|
|
|
'sub_order_no' => $feeSubOrderNo,
|
|
|
|
|
'sub_out_order_no' => $feeSubOrderNo,
|
|
|
|
|
'split_amount' => $fee,
|
|
|
|
|
'contract_no' => $platformAccount->contract_no,
|
|
|
|
|
'seller_flag' => 0,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
OrderSplitInfo::insert($orderSplitInfos);
|
|
|
|
|
return [$order, $orderSplitInfos];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function updateOrder($params) {
|
|
|
|
|
$order = Order::where('order_no', $params['order_no'])->where('status', 'APPLY_FAILED')->first();
|
|
|
|
|
if (empty($order)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$order->third_order_no = $params['third_order_no'];
|
|
|
|
|
$order->status = $params['status'];
|
|
|
|
|
$order->finished_at = date('Y-m-d H:i:s', strtotime($params['finished_at']));
|
|
|
|
|
$order->error_message = $params['error_message'];
|
|
|
|
|
$order->transaction_id = $params['transaction_id'];
|
|
|
|
|
$order->out_transaction_id = $params['out_transaction_id'];
|
|
|
|
|
$order->save();
|
|
|
|
|
return $order;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|