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.
189 lines
6.2 KiB
PHP
189 lines
6.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Payment;
|
|
|
|
use App\Exception\BusinessException;
|
|
use App\Helper\Efps\Api;
|
|
use App\Model\Order;
|
|
use App\Model\RefundOrder;
|
|
use App\Request\BindCardConfirmRequest;
|
|
use App\Request\BindCardRequest;
|
|
use App\Request\PaymentQueryRequest;
|
|
use App\Request\ProtocolPayConfirmRequest;
|
|
use App\Request\ProtocolPayPreRequest;
|
|
use App\Request\RefundQueryRequest;
|
|
use App\Request\RefundRequest;
|
|
use App\Request\UnBindCardRequest;
|
|
use Hyperf\HttpServer\Contract\RequestInterface;
|
|
use App\Service\PaymentService;
|
|
|
|
class PayController extends AbstractController
|
|
{
|
|
private PaymentService $paymentService;
|
|
|
|
public function __construct(PaymentService $paymentService)
|
|
{
|
|
$this->paymentService = $paymentService;
|
|
}
|
|
|
|
public function bindCard(RequestInterface $request)
|
|
{
|
|
$req = new BindCardRequest($request->all());
|
|
$result = Api::bindCard($req->getData());
|
|
if (!$result->isSuccess()) {
|
|
throw new BusinessException($result->getMessage());
|
|
}
|
|
return $this->success($result->getData());
|
|
}
|
|
|
|
public function bindCardConfirm(RequestInterface $request)
|
|
{
|
|
$req = new BindCardConfirmRequest($request->all());
|
|
$result = Api::bindCardConfirm($req->getData());
|
|
if (!$result->isSuccess()) {
|
|
throw new BusinessException($result->getMessage());
|
|
}
|
|
return $this->success($result->getData());
|
|
}
|
|
|
|
public function unBindCard(RequestInterface $request)
|
|
{
|
|
$req = new UnBindCardRequest($request->all());
|
|
$result = Api::unBindCard($req->getData());
|
|
if (!$result->isSuccess()) {
|
|
throw new BusinessException($result->getMessage());
|
|
}
|
|
return $this->success($result->getData());
|
|
}
|
|
|
|
public function protocolPayPreRequest(RequestInterface $request)
|
|
{
|
|
$req = new ProtocolPayPreRequest($request->all());
|
|
$app = $req->getApp();
|
|
$data = $req->getData();
|
|
$result = Api::protocolPayPre($data);
|
|
|
|
$order = $this->paymentService->createOrder($app, $data);
|
|
|
|
if (!$result->isSuccess()) {
|
|
$order->status = Order::STATUS_APPLY_FAIL;
|
|
$order->save();
|
|
throw new BusinessException($result->getMessage());
|
|
}
|
|
$order->token = $result->get('token', '');
|
|
$order->protocol = $result->get('protocol', '');
|
|
$order->status = Order::STATUS_WAIT_PAY;
|
|
$order->save();
|
|
return $this->success($result->getData());
|
|
}
|
|
|
|
public function protocolPayConfirm(RequestInterface $request)
|
|
{
|
|
$req = new ProtocolPayConfirmRequest($request->all());
|
|
$app = $req->getApp();
|
|
$data = $req->getData();
|
|
$order = Order::where('app_id', $app->app_id)
|
|
->where('token', $data['token'])
|
|
->where('protocol', $data['protocol'])
|
|
->first();
|
|
if (!$order) {
|
|
throw new BusinessException('订单不存在');
|
|
}
|
|
$result = Api::protocolPayConfirm($req->getData());
|
|
if (!$result->isSuccess()) {
|
|
$order->status = Order::STATUS_FAILED;
|
|
$order->error_code = $result->getCode();
|
|
$order->error_msg = $result->getMessage();
|
|
$order->payed_at = date('Y-m-d H:i:s');
|
|
$order->save();
|
|
throw new BusinessException($result->getMessage());
|
|
}
|
|
|
|
// 00 01 03
|
|
$this->paymentService->handlePayResult($result, $order);
|
|
return $this->success($result->getData());
|
|
}
|
|
|
|
public function refund(RequestInterface $request) {
|
|
$req = new RefundRequest($request->all());
|
|
$app = $req->getApp();
|
|
$data = $req->getData();
|
|
|
|
$refundOrder = new RefundOrder();
|
|
$refundOrder->app_id = $app->app_id;
|
|
$refundOrder->out_order_no = $data['outTradeNo'] ?: '';
|
|
$refundOrder->out_refund_order_no = $data['outRefundNo'] ?: '';
|
|
$refundOrder->order_amount = $data['amount'] ?: '';
|
|
$refundOrder->refund_amount = $data['refundAmount'] ?: '';
|
|
$refundOrder->remark = $data['remark'] ?: '';
|
|
$refundOrder->notify_url = $data['notifyUrl'] ?: '';
|
|
|
|
$result = Api::refund($data);
|
|
if (!$result->isSuccess()) {
|
|
$refundOrder->status = RefundOrder::STATUS_APPLY_FAILED;
|
|
$refundOrder->error_code = $result->getCode();
|
|
$refundOrder->error_msg = $result->getMessage();
|
|
$refundOrder->save();
|
|
throw new BusinessException($result->getMessage());
|
|
}
|
|
$refundOrder->status = RefundOrder::STATUS_APPLY_SUCCESS;
|
|
$refundOrder->save();
|
|
|
|
return $this->success($result->getData());
|
|
}
|
|
|
|
public function refundQuery(RequestInterface $request) {
|
|
$req = new RefundQueryRequest($request->all());
|
|
$data = $req->getData();
|
|
$result = Api::refundQuery($data);
|
|
if (!$result->isSuccess()) {
|
|
throw new BusinessException($result->getMessage());
|
|
}
|
|
|
|
$order = RefundOrder::where('out_refund_order_no', $result->get('outRefundNo'))
|
|
->where('status', RefundOrder::STATUS_APPLY_SUCCESS)
|
|
->first();
|
|
if ($order) {
|
|
$this->paymentService->handleRefundResult($result, $order);
|
|
}
|
|
|
|
return $this->success(
|
|
$result->getData([
|
|
'outRefundNo',
|
|
'transactionNo',
|
|
'amount',
|
|
'refundAmount',
|
|
'refundState'
|
|
])
|
|
);
|
|
}
|
|
|
|
public function paymentQuery(RequestInterface $request) {
|
|
$req = new PaymentQueryRequest($request->all());
|
|
$data = $req->getData();
|
|
$result = Api::paymentQuery($data);
|
|
if (!$result->isSuccess()) {
|
|
throw new BusinessException($result->getMessage());
|
|
}
|
|
|
|
$order = Order::where('out_order_no', $result->get('outTradeNo'))
|
|
->where('status', Order::STATUS_WAIT_PAY)
|
|
->first();
|
|
if ($order) {
|
|
$this->paymentService->handlePayResult($result, $order);
|
|
}
|
|
|
|
return $this->success(
|
|
$result->getData([
|
|
'outTradeNo',
|
|
'transactionNo',
|
|
'payState',
|
|
'procedureFee',
|
|
'amount'
|
|
])
|
|
);
|
|
}
|
|
}
|