含 AI 助手、抖音 OAuth、流量池推送、支付获客、SuperAdmin 设备与场景管理等本地迭代;以本地为准单向推送,未拉取远程。 Co-authored-by: Cursor <cursoragent@cursor.com>
113 lines
3.8 KiB
PHP
113 lines
3.8 KiB
PHP
<?php
|
||
|
||
namespace app\common\controller;
|
||
|
||
use app\common\service\OpenPlatformService;
|
||
use app\cunkebao\service\PaymentAcquisitionService;
|
||
use library\ResponseHelper;
|
||
use think\Controller;
|
||
use think\facade\Request;
|
||
|
||
/**
|
||
* 开放对接 · 付款码获客(官方微信 V3 / 支付宝 OpenAPI)
|
||
*
|
||
* POST /v1/open/payment/create-order
|
||
* GET /v1/open/payment/query-order
|
||
* GET /v1/open/payment/h5-link
|
||
*/
|
||
class OpenPaymentController extends Controller
|
||
{
|
||
public function createOrder()
|
||
{
|
||
$ctx = OpenPlatformService::resolveContext();
|
||
if (!$ctx) {
|
||
return ResponseHelper::unauthorized('未授权或 Token 已过期');
|
||
}
|
||
|
||
$planId = (int)$ctx['planId'];
|
||
$companyId = (int)$ctx['companyId'];
|
||
$payType = Request::param('payType', PaymentAcquisitionService::PAY_WECHAT_NATIVE);
|
||
$cid = (int)Request::param('cid', 0);
|
||
$amountYuan = Request::param('amountYuan', null);
|
||
$redirectUrl = Request::param('redirectUrl', Request::param('returnUrl', ''));
|
||
$externalOrderNo = Request::param('externalOrderNo', '');
|
||
|
||
try {
|
||
$service = new PaymentAcquisitionService();
|
||
$opts = [
|
||
'payType' => $payType,
|
||
'cid' => $cid,
|
||
'userId' => 0,
|
||
'redirectUrl' => $redirectUrl,
|
||
'externalOrderNo' => $externalOrderNo,
|
||
];
|
||
if ($amountYuan !== null && $amountYuan !== '') {
|
||
$opts['amountYuan'] = (float)$amountYuan;
|
||
}
|
||
$data = $service->createQrOrder($planId, $companyId, $opts);
|
||
return ResponseHelper::success($data, '付款码已生成');
|
||
} catch (\InvalidArgumentException $e) {
|
||
return ResponseHelper::error($e->getMessage(), 400);
|
||
} catch (\Throwable $e) {
|
||
return ResponseHelper::error($e->getMessage(), 500);
|
||
}
|
||
}
|
||
|
||
public function queryOrder()
|
||
{
|
||
$ctx = OpenPlatformService::resolveContext();
|
||
if (!$ctx) {
|
||
return ResponseHelper::unauthorized('未授权或 Token 已过期');
|
||
}
|
||
|
||
$orderNo = Request::param('orderNo', '');
|
||
if ($orderNo === '') {
|
||
return ResponseHelper::error('orderNo 不能为空', 400);
|
||
}
|
||
|
||
$service = new PaymentAcquisitionService();
|
||
$data = $service->queryOrder($orderNo);
|
||
if (!$data) {
|
||
return ResponseHelper::error('订单不存在', 404);
|
||
}
|
||
if ((int)$data['taskId'] !== (int)$ctx['planId']) {
|
||
return ResponseHelper::error('无权查询该订单', 403);
|
||
}
|
||
return ResponseHelper::success($data);
|
||
}
|
||
|
||
/**
|
||
* GET /v1/open/payment/h5-link
|
||
* 第三方嵌入付款页(支付完成后按 redirectUrl 跳转)
|
||
*/
|
||
public function h5Link()
|
||
{
|
||
$ctx = OpenPlatformService::resolveContext();
|
||
if (!$ctx) {
|
||
return ResponseHelper::unauthorized('未授权或 Token 已过期');
|
||
}
|
||
|
||
$planId = (int)$ctx['planId'];
|
||
$cid = (int)Request::param('cid', 0);
|
||
$redirectUrl = Request::param('redirectUrl', Request::param('returnUrl', ''));
|
||
$payType = Request::param('payType', PaymentAcquisitionService::PAY_WECHAT_NATIVE);
|
||
|
||
try {
|
||
$service = new PaymentAcquisitionService();
|
||
$url = $service->buildAcquirePageUrl($planId, array_filter([
|
||
'cid' => $cid > 0 ? $cid : null,
|
||
'redirectUrl' => $redirectUrl ?: null,
|
||
'payType' => $payType ?: null,
|
||
]));
|
||
return ResponseHelper::success([
|
||
'url' => $url,
|
||
'planId' => $planId,
|
||
'cid' => $cid,
|
||
'redirectUrl' => $redirectUrl,
|
||
]);
|
||
} catch (\InvalidArgumentException $e) {
|
||
return ResponseHelper::error($e->getMessage(), 400);
|
||
}
|
||
}
|
||
}
|