|
|
|
<?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)
|
|
|
|
{
|
|
|
|
$data = $this->paymentService->protocolPayPreRequest($request->all());
|
|
|
|
return $this->success($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function protocolPayConfirm(RequestInterface $request)
|
|
|
|
{
|
|
|
|
$data = $this->paymentService->protocolPayConfirm($request->all());
|
|
|
|
return $this->success($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
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'
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|