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/Controller/Payment/PayController.php

153 lines
4.9 KiB
PHTML

2 years ago
<?php
declare(strict_types=1);
namespace App\Controller\Payment;
2 years ago
use App\Exception\BusinessException;
use App\Helper\Efps\Api;
1 year ago
use App\Model\Order;
use App\Model\RefundOrder;
2 years ago
use App\Request\BindCardConfirmRequest;
use App\Request\BindCardRequest;
1 year ago
use App\Request\PaymentQueryRequest;
2 years ago
use App\Request\ProtocolPayConfirmRequest;
use App\Request\ProtocolPayPreRequest;
1 year ago
use App\Request\RefundQueryRequest;
use App\Request\RefundRequest;
2 years ago
use App\Request\UnBindCardRequest;
2 years ago
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Service\PaymentService;
class PayController extends AbstractController
{
private PaymentService $paymentService;
public function __construct(PaymentService $paymentService)
{
$this->paymentService = $paymentService;
}
2 years ago
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)
{
1 year ago
$data = $this->paymentService->protocolPayPreRequest($request->all());
return $this->success($data);
2 years ago
}
public function protocolPayConfirm(RequestInterface $request)
{
1 year ago
$data = $this->paymentService->protocolPayConfirm($request->all());
return $this->success($data);
1 year ago
}
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();
2 years ago
throw new BusinessException($result->getMessage());
}
1 year ago
$refundOrder->status = RefundOrder::STATUS_APPLY_SUCCESS;
$refundOrder->save();
2 years ago
return $this->success($result->getData());
2 years ago
}
1 year ago
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());
}
1 year ago
$order = RefundOrder::where('out_refund_order_no', $result->get('outRefundNo'))
->where('status', RefundOrder::STATUS_APPLY_SUCCESS)
->first();
if ($order) {
$this->paymentService->handleRefundResult($result, $order);
}
1 year ago
return $this->success(
$result->getData([
'outRefundNo',
'transactionNo',
'amount',
'refundAmount',
'refundState'
])
);
}
1 year ago
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'
])
);
}
2 years ago
}