You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
payment/app/Service/PaymentService.php

509 lines
21 KiB
PHTML

2 years ago
<?php
declare(strict_types=1);
namespace App\Service;
1 year ago
use App\Exception\ApiException;
1 year ago
use App\Exception\BasicException;
1 year ago
use App\Exception\BusinessException;
1 year ago
use App\Helper\Baofu\Baofu;
1 year ago
use App\Helper\StringHelper;
2 years ago
use App\Model\App;
use App\Model\Order;
1 year ago
use App\Model\OrderSplitInfo;
1 year ago
use App\Model\Refund;
1 year ago
use App\Model\RefundSplitInfo;
1 year ago
use App\Model\User;
2 years ago
class PaymentService extends AbstractService
{
1 year ago
public function generateMemberId($appKey, $cardNo) {
return md5($appKey . '-' . $cardNo);
}
1 year ago
public function generateMemberIdNew($appId, $outMemberId) {
return md5($appId . '-' . $outMemberId);
}
1 year ago
public function payment(array $data, App $app, string $token) {
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
1 year ago
}
1 year ago
$order = Order::where('app_id', $app->app_id)->where('out_order_no', $data['outOrderNo'])->first();
1 year ago
if ($order) {
throw new BusinessException('订单号重复');
}
1 year ago
1 year ago
$data['payType'] = $data['payType'] ?? 'CARD';
1 year ago
$payMethod = $data['payMethod'] ?? 'paymentSplit';
[$order, $orderSplitInfos] = $this->createOrder($app, $data, $user, $payMethod);
1 year ago
$splitInfoList = $this->buildSplitInfoList($orderSplitInfos);
1 year ago
if ($payMethod == 'paymentSplit') {
return $this->paymentSplit($order, $user, $splitInfoList, $token);
} elseif($payMethod == 'unionPay') {
return $this->unionPay($order, $user, $splitInfoList, $token);
} else {
throw new BusinessException('payMethod不支持');
}
1 year ago
}
1 year ago
public function transferPay(array $data, App $app, string $token)
1 year ago
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$order = Order::where('app_id', $app->app_id)->where('out_order_no', $data['outOrderNo'])->first();
if ($order) {
throw new BusinessException('订单号重复');
}
[$order, $orderSplitInfos] = $this->createOrder($app, $data, $user, 'transferSplitPay');
1 year ago
1 year ago
$splitInfoList = $this->buildSplitInfoList($orderSplitInfos);
1 year ago
return $this->transferSplitPay($order, $user, $splitInfoList, $token);
1 year ago
}
private function buildSplitInfoList($orderSplitInfos) {
1 year ago
$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'],
];
}
1 year ago
return $splitInfoList;
}
1 year ago
1 year ago
private function paymentSplit(Order $order, User $user, array $splitInfoList, string $token)
{
1 year ago
try {
1 year ago
$baofu = new Baofu();
1 year ago
$url = $baofu->paymentSplit([
'loginNo' => $user->member_id,
'outOrderNo' => $order->order_no,
1 year ago
'amount' => $order->amount,
1 year ago
'paidType' => $order->pay_type,
1 year ago
'agreementNo' => $order->agreement_no,
1 year ago
'pwdPayExpTime' => $order->expires_in,
1 year ago
'splitInfoList' => $splitInfoList,
], $token);
$order->status = 'APPLY_SUCCESS';
$order->applied_at = date('Y-m-d H:i:s');
$order->pay_url = $url;
1 year ago
$order->save();
1 year ago
return $url;
1 year ago
} catch (ApiException $e) {
1 year ago
$order->status = 'APPLY_FAILED';
$order->applied_at = date('Y-m-d H:i:s');
1 year ago
$order->error_code = $e->getErrorCode();
1 year ago
$order->error_message = $e->getMessage();
$order->save();
throw $e;
}
1 year ago
}
1 year ago
private function transferSplitPay(Order $order, User $user, array $splitInfoList, string $token)
{
try {
$baofu = new Baofu();
$result = $baofu->transferSplitPay([
'loginNo' => $user->member_id,
'outOrderNo' => $order->order_no,
1 year ago
'goodsName' => $order->goods_name,
1 year ago
'amount' => $order->amount,
'agreementNo' => $order->agreement_no,
'expireDate' => date('YmdHis', time() + $order->expires_in),
1 year ago
'validDate' => date('Ymd', strtotime($order->valid_date)),
1 year ago
'splitInfoList' => $splitInfoList,
'marketInfo' => $order->market_info,
], $token);
$order->status = 'APPLY_SUCCESS';
$order->applied_at = date('Y-m-d H:i:s');
$order->acs_no = $result['acsNo'];
$order->save();
return $result['acsNo'];
} catch (ApiException $e) {
$order->status = 'APPLY_FAILED';
$order->applied_at = date('Y-m-d H:i:s');
$order->error_code = $e->getErrorCode();
$order->error_message = $e->getMessage();
$order->save();
throw $e;
}
}
1 year ago
private function unionPay(Order $order, User $user, array $splitInfoList, string $token)
{
// ASSURE_SHARE
// ASSURE_PAYMENT
// ASSURE_PORTFOLIO_PAY
// PAYMENT_SPLIT
try {
$baofu = new Baofu();
$result = $baofu->payment([
'loginNo' => $user->member_id,
'outOrderNo' => $order->order_no,
'goodsName' => $order->goods_name,
'amount' => $order->amount,
'paidType' => $order->pay_type,
'agreementNo' => $order->agreement_no,
'expireDate' => date('YmdHis', time() + $order->expires_in),
'validDate' => date('Ymd', strtotime($order->valid_date)),
'splitInfoList' => $splitInfoList,
'chanalId' => $order->channel_id,
'appId' => $order->channel_app_id,
'callType' => 'PAYMENT_SPLIT',
], $token);
$order->status = 'APPLY_SUCCESS';
$order->applied_at = date('Y-m-d H:i:s');
$order->acs_no = $result['acsNo'];
$order->save();
return $result['acsNo'];
} catch (ApiException $e) {
$order->status = 'APPLY_FAILED';
$order->applied_at = date('Y-m-d H:i:s');
$order->error_code = $e->getErrorCode();
$order->error_message = $e->getMessage();
$order->save();
throw $e;
}
}
1 year ago
public function createOrder(App $app, array $params, User $user, string $payMethod) {
1 year ago
if ($params['amount'] < 10) {
throw new BusinessException('金额不能小于10');
}
1 year ago
$expiresIn = $params['expiresIn'] ?? 10 * 60;
2 years ago
$order = new Order();
$order->app_id = $app->app_id;
1 year ago
$order->user_id = $user->user_id;
1 year ago
$order->member_id = $user->member_id;
1 year ago
$order->order_no = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_PAY);
$order->out_order_no = $params['outOrderNo'];
$order->amount = $params['amount'];
1 year ago
$order->goods_name = $params['goodsName'] ?? '';
1 year ago
$order->notify_url = $params['notifyUrl'];
1 year ago
$order->return_url = $params['returnUrl'] ?? '';
$order->agreement_no = $params['agreementNo'] ?? '';
1 year ago
$order->status = 'PREPARE';
1 year ago
$order->pay_mode = 'BAOFU';
$order->channel_id = $params['channelId'] ?? '';
$order->channel_app_id = $params['appId'] ?? '';
1 year ago
$order->pay_channel = 'BAOFU';
1 year ago
$order->pay_method = $payMethod;
1 year ago
$order->pay_type = $params['payType'] ?? '';
1 year ago
$order->valid_date = $params['validDate'] ?? null;
$order->expires_in = $expiresIn;
1 year ago
$order->expired_at = date('Y-m-d H:i:s', time() + $expiresIn);
$order->market_info = $params['marketInfo'] ?? [];
1 year ago
$order->org_split_info_list = $params['splitInfoList'];
1 year ago
$order->remark = $params['remark'] ?? '';
1 year ago
$order->save();
1 year ago
$platformAccount = User::getPlatformAccount();
1 year ago
$fee = $platformAccount ? floor($order->amount * 0.007) : 0;
1 year ago
if ($fee <= 0) {
$fee = 1;
}
1 year ago
$orderSplitInfos = [];
1 year ago
$splitUserIds = array_column($params['splitInfoList'], 'splitUserId');
1 year ago
$users = User::where('app_id', $app->app_id)->whereIn('user_id', $splitUserIds)->get()->keyBy('user_id');
foreach ($params['splitInfoList'] as $splitInfo) {
1 year ago
$splitUser = $users[$splitInfo['splitUserId']] ?? null;
if (empty($splitUser)) {
throw new BusinessException('分账用户不存在');
}
1 year ago
$splitAmount = $splitInfo['sellerFlag'] == 1 ? ($splitInfo['splitAmount'] - $fee): $splitInfo['splitAmount'];
1 year ago
$orderSplitInfos[] = [
'app_id' => $app->app_id,
'user_id' => $user->user_id,
'member_id' => $user->member_id,
'split_member_id' => $splitUser->member_id,
1 year ago
'split_user_id' => $splitInfo['splitUserId'],
1 year ago
'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];
}
1 year ago
public function updateOrder($params, array $statusList = null) {
1 year ago
$order = Order::where('order_no', $params['order_no'])->first();
1 year ago
if (empty($order)) {
return null;
}
1 year ago
if ($statusList && !in_array($order->status, $statusList)) {
return null;
}
1 year ago
1 year ago
$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']));
1 year ago
$order->error_message = $params['error_message'] ?? '';
$order->transaction_id = $params['transaction_id'] ?? '';
$order->out_transaction_id = $params['out_transaction_id'] ?? '';
1 year ago
$order->save();
2 years ago
return $order;
}
1 year ago
public function refundApply(array $data, App $app) {
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
1 year ago
if (!$user) {
throw new BusinessException('用户不存在');
1 year ago
}
$order = Order::where('app_id', $app->app_id)->where('out_order_no', $data['outOrderNo'])->first();
1 year ago
if (empty($order)) {
throw new BusinessException('订单号不存在');
}
1 year ago
if ($order->status != 'SUCCESS') {
1 year ago
throw new BusinessException('该订单状态不能发起退款');
}
1 year ago
$refund = Refund::where('app_id', $app->app_id)->where('out_refund_no', $data['outRefundNo'])->first();
1 year ago
if ($refund) {
throw new BusinessException('该退款单已经存在');
}
$refund = new Refund();
$refund->app_id = $app->app_id;
$refund->user_id = $user->user_id;
$refund->member_id = $user->member_id;
$refund->order_no = $order->order_no;
$refund->out_order_no = $order->out_order_no;
$refund->refund_no = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_REFUND);
1 year ago
$refund->out_refund_no = $data['outRefundNo'];
1 year ago
$refund->refund_amount = $data['refundAmount'];
$refund->refund_reason = $data['refundReason'];
1 year ago
$refund->notify_url = $data['notifyUrl'] ?? '';
1 year ago
$refund->org_refund_split_info_list = $data['refundSplitInfoList'] ?? [];
$refund->remark = $data['remark'] ?? '';
1 year ago
$refund->applied_at = date('Y-m-d H:i:s');
$refundSplitInfoList = [];
$refundSplitInfos = [];
1 year ago
foreach ($order->orderSplitInfos as $splitInfo) {
1 year ago
$refundSplitInfoList[] = [
'orgSubOutOrderNo' => $splitInfo->sub_order_no,
1 year ago
'refundAmount' => (string)$splitInfo->split_amount,
1 year ago
];
$refundSplitInfos[$splitInfo->sub_order_no] = [
'app_id' => $splitInfo->user_id,
'user_id' => $splitInfo->user_id,
'member_id' => $splitInfo->member_id,
'user_id' => $splitInfo->user_id,
'split_member_id' => $splitInfo->split_member_id,
'split_user_id' => $splitInfo->split_user_id,
1 year ago
'refund_no' => $refund->refund_no,
'out_refund_no' => $refund->out_refund_no,
1 year ago
'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,
1 year ago
'status' => Refund::STATUS_PREPARE,
1 year ago
'message' => '',
];
1 year ago
}
1 year ago
1 year ago
try {
$baofu = new Baofu();
$result = $baofu->profitShareRefundApply([
'loginNo' => $user->member_id,
'refundTradeId' => $refund->refund_no,
'refundTradeType' => 'REFUND_APPLY',
'orgTradeId' => $refund->order_no,
'refundAmount' => $refund->refund_amount,
'refundReason' => $refund->refund_reason,
'refundSplitInfoList' => $refundSplitInfoList,
]);
1 year ago
$refund->status = 'APPLY_SUCCESS';
1 year ago
$refund->real_refund_amount = $result['refundAmount'];
foreach ($result['refundSplitResultList'] as $refundSplitResult) {
$subOrderNo = $refundSplitResult['orgSubOutOrderNo'];
1 year ago
$refundSplitInfos[$subOrderNo]['status'] = Refund::getStatusByShortStatus($refundSplitResult['status']);
$refundSplitInfos[$subOrderNo]['message'] = $refundSplitResult['message'] ?: '';
1 year ago
}
1 year ago
RefundSplitInfo::insert($refundSplitInfos);
$refund->save();
1 year ago
} catch (BasicException $e) {
1 year ago
$refund->status = 'APPLY_FAILED';
1 year ago
$refund->error_code = $e->getCode();
$refund->error_message = $e->getMessage();
1 year ago
foreach ($refundSplitInfos as $key => $refundSplitInfo) {
$refundSplitInfos[$key]['status'] = 'APPLY_FAILED';
$refundSplitInfos[$key]['message'] = $e->getMessage();
}
RefundSplitInfo::insert($refundSplitInfos);
$refund->save();
1 year ago
throw $e;
1 year ago
}
1 year ago
}
1 year ago
1 year ago
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;
}
1 year ago
}
public function updateRefund($params)
{
$refund = Refund::where('refund_no', $params['refund_no'])->where('status', 'APPLY_FAILED')->first();
if (empty($refund)) {
return null;
}
$refund->third_refund_no = $params['third_refund_no'];
$refund->status = Refund::getStatusByShortStatus($params['status']);
$refund->refund_success_at = date('Y-m-d H:i:s', strtotime($params['refund_success_at']));
$refund->error_message = $params['error_message'];
$refund->save();
return $refund;
1 year ago
}
1 year ago
public function confirmPay(array $data, App $app, string $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
1 year ago
$order = Order::where('app_id', $app->app_id)->where('out_order_no', $data['oldOutOrderNo'])->first();
1 year ago
if (empty($order)) {
throw new BusinessException('订单不存在');
}
1 year ago
if ($order->status != Order::STATUS_SUCCESS) {
1 year ago
throw new BusinessException('订单状态异常');
}
$confirmSplitInfoList = [];
foreach ($order->orderSplitInfos as $orderSplitInfo) {
$confirmSplitInfoList[] = [
'oldSubOutOrderNo' => $orderSplitInfo->sub_order_no,
'splitAmount' => $orderSplitInfo->split_amount,
];
}
try {
$baofu = new Baofu();
$result = $baofu->confirmAssurePortfolioPay([
'loginNo' => $user->member_id,
'oldOutOrderNo' => $order->order_no,
'assureConfirmSplitInfoList' => $confirmSplitInfoList,
'outOrderNo' => StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_CONFIRM_PAY),
'amount' => $data['amount'],
], $token);
$order->status = 'CONFIRM_SUCCESS';
$order->save();
return $result;
} catch (ApiException $e) {
$order->status = 'CONFIRM_FAILED';
$order->error_code = $e->getCode();
$order->error_message = $e->getMessage();
$order->save();
throw $e;
}
}
1 year ago
public function queryOrder(array $data, App $app)
{
$order = Order::where('app_id', $app->app_id)->where('out_order_no', $data['outOrderNo'])->first();
if (empty($order)) {
throw new BusinessException('订单不存在');
}
1 year ago
$baofu = new Baofu();
$result = $baofu->queryOrder([
'loginNo' => $order->member_id,
'tradeId' => $order->order_no,
]);
$info = [
'member_id' => $order->member_id,
'order_no' => $order->order_no,
'third_order_no' => $result['requestNo'] ?? '',
'status' => $result['status'],
'amount' => $result['amount'],
'finished_at' => $result['finishDate'],
'error_message' => $result['errorMsg'] ?? '',
'transaction_id' => $result['transactionId'] ?? '',
'out_transaction_id' => $result['outTransactionId'] ?? '',
];
return $this->updateOrder($info);
1 year ago
}
2 years ago
}