Files
zyt/admin/ORDER_PAYMENT_GATEWAY.md
T
2026-03-11 14:33:49 +08:00

407 lines
8.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 订单系统 - 支付网关集成
## 功能说明
订单支付功能已升级为调用真实的支付网关,而不是直接标记为已支付。
支持的支付方式:
-**支付宝支付** (Alipay)
-**微信支付** (WeChat Pay)
---
## 前端实现
### 支付API
`admin/src/api/order.ts` 中添加了两个支付网关API
```typescript
// 支付宝支付
export function alipayPay(params: any) {
return request.post({ url: '/order.order/alipay', params })
}
// 微信支付
export function wechatPay(params: any) {
return request.post({ url: '/order.order/wechat', params })
}
```
### 支付流程
`confirmPay()` 方法中实现了支付流程:
```typescript
const confirmPay = async () => {
// 1. 验证参数
if (payForm.value.payType === 'supplement' && !payForm.value.supplement_order_no) {
feedback.msgWarning('请输入支付订单号')
return
}
// 2. 准备支付参数
let payParams: any = {}
if (payForm.value.payType === 'normal') {
payParams = {
id: payForm.value.order_id,
order_no: payForm.value.order_no,
amount: payForm.value.amount,
payment_method: payForm.value.payment_method
}
} else {
payParams = {
order_no: payForm.value.supplement_order_no,
payment_method: payForm.value.payment_method,
is_supplement: 1
}
}
// 3. 调用对应的支付网关
let payResult: any
if (payForm.value.payment_method === 'alipay') {
payResult = await alipayPay(payParams)
} else if (payForm.value.payment_method === 'wechat') {
payResult = await wechatPay(payParams)
}
// 4. 处理支付结果
if (payResult && payResult.pay_url) {
if (payForm.value.payment_method === 'alipay') {
// 支付宝:跳转到支付页面
window.location.href = payResult.pay_url
} else if (payForm.value.payment_method === 'wechat') {
// 微信:显示二维码
feedback.msgSuccess('请扫描二维码进行支付')
}
}
}
```
---
## 后端实现
### 控制器
`OrderController` 中添加了两个支付网关端点:
#### 支付宝支付
```php
/**
* @notes 支付宝支付
* @return \think\response\Json
*/
public function alipay()
{
$params = (new OrderValidate())->post()->goCheck('pay');
// 获取订单信息
$order = $this->getOrderByParams($params);
if (!$order) {
return $this->fail('订单不存在');
}
// 调用支付宝支付接口
$payUrl = OrderLogic::alipayPay($order);
if (!$payUrl) {
return $this->fail(OrderLogic::getError());
}
return $this->data(['pay_url' => $payUrl]);
}
```
#### 微信支付
```php
/**
* @notes 微信支付
* @return \think\response\Json
*/
public function wechat()
{
$params = (new OrderValidate())->post()->goCheck('pay');
// 获取订单信息
$order = $this->getOrderByParams($params);
if (!$order) {
return $this->fail('订单不存在');
}
// 调用微信支付接口
$payData = OrderLogic::wechatPay($order);
if (!$payData) {
return $this->fail(OrderLogic::getError());
}
return $this->data($payData);
}
```
### 业务逻辑
`OrderLogic` 中添加了支付网关方法:
#### 支付宝支付
```php
/**
* @notes 支付宝支付
* @param $order
* @return string|false
*/
public static function alipayPay($order)
{
try {
$payUrl = self::generateAlipayUrl($order);
return $payUrl;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 生成支付宝支付URL
* @param $order
* @return string
*/
private static function generateAlipayUrl($order): string
{
// TODO: 集成支付宝SDK
$alipayConfig = [
'app_id' => config('pay.alipay.app_id'),
'merchant_private_key' => config('pay.alipay.merchant_private_key'),
'alipay_public_key' => config('pay.alipay.alipay_public_key'),
];
// 生成支付URL
$payUrl = 'https://openapi.alipay.com/gateway.do?' . http_build_query([
'app_id' => $alipayConfig['app_id'],
'method' => 'alipay.trade.page.pay',
'charset' => 'UTF-8',
'sign_type' => 'RSA2',
'timestamp' => date('Y-m-d H:i:s'),
'version' => '1.0',
'notify_url' => config('app.url') . '/api/order/alipay-notify',
'return_url' => config('app.url') . '/order',
'biz_content' => json_encode([
'out_trade_no' => $order->order_no,
'product_code' => 'FAST_INSTANT_TRADE_PAY',
'total_amount' => $order->amount,
'subject' => '订单支付',
'body' => '订单号:' . $order->order_no,
]),
]);
return $payUrl;
}
```
#### 微信支付
```php
/**
* @notes 微信支付
* @param $order
* @return array|false
*/
public static function wechatPay($order)
{
try {
$payData = self::generateWechatPayData($order);
return $payData;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 生成微信支付数据
* @param $order
* @return array
*/
private static function generateWechatPayData($order): array
{
// TODO: 集成微信SDK
$wechatConfig = [
'app_id' => config('pay.wechat.app_id'),
'mch_id' => config('pay.wechat.mch_id'),
'api_key' => config('pay.wechat.api_key'),
];
// 生成微信支付数据
$payData = [
'appid' => $wechatConfig['app_id'],
'mch_id' => $wechatConfig['mch_id'],
'nonce_str' => uniqid(),
'body' => '订单支付',
'out_trade_no' => $order->order_no,
'total_fee' => (int)($order->amount * 100),
'spbill_create_ip' => request()->ip(),
'notify_url' => config('app.url') . '/api/order/wechat-notify',
'trade_type' => 'NATIVE',
];
return [
'pay_url' => 'https://api.mch.weixin.qq.com/pay/unifiedorder',
'pay_data' => $payData,
'qrcode' => 'https://api.mch.weixin.qq.com/qrcode',
];
}
```
---
## 配置说明
### 支付宝配置
`config/pay.php` 中配置支付宝参数:
```php
'alipay' => [
'app_id' => 'your_alipay_app_id',
'merchant_private_key' => 'your_merchant_private_key',
'alipay_public_key' => 'your_alipay_public_key',
],
```
### 微信配置
`config/pay.php` 中配置微信参数:
```php
'wechat' => [
'app_id' => 'your_wechat_app_id',
'mch_id' => 'your_mch_id',
'api_key' => 'your_api_key',
],
```
---
## API 端点
### 支付宝支付
```bash
POST /order.order/alipay
{
"id": 1,
"payment_method": "alipay"
}
```
**响应**:
```json
{
"code": 0,
"msg": "success",
"data": {
"pay_url": "https://openapi.alipay.com/gateway.do?..."
}
}
```
### 微信支付
```bash
POST /order.order/wechat
{
"id": 1,
"payment_method": "wechat"
}
```
**响应**:
```json
{
"code": 0,
"msg": "success",
"data": {
"pay_url": "https://api.mch.weixin.qq.com/pay/unifiedorder",
"pay_data": {...},
"qrcode": "https://api.mch.weixin.qq.com/qrcode"
}
}
```
---
## 支付流程
### 支付宝支付流程
1. 用户选择支付宝支付
2. 前端调用 `/order.order/alipay` 接口
3. 后端生成支付宝支付URL
4. 前端跳转到支付宝支付页面
5. 用户完成支付
6. 支付宝回调 `/api/order/alipay-notify` 接口
7. 后端更新订单状态为已支付
### 微信支付流程
1. 用户选择微信支付
2. 前端调用 `/order.order/wechat` 接口
3. 后端生成微信支付数据
4. 前端显示二维码
5. 用户扫描二维码完成支付
6. 微信回调 `/api/order/wechat-notify` 接口
7. 后端更新订单状态为已支付
---
## 待实现
以下功能需要根据实际的支付SDK进行实现:
1. **支付宝SDK集成**
- 集成支付宝官方SDK
- 实现签名和验证
- 处理支付回调
2. **微信SDK集成**
- 集成微信官方SDK
- 实现签名和验证
- 处理支付回调
3. **支付回调处理**
- 创建 `/api/order/alipay-notify` 接口
- 创建 `/api/order/wechat-notify` 接口
- 验证回调签名
- 更新订单状态
4. **支付状态查询**
- 创建支付状态查询接口
- 定期检查支付状态
---
## 相关文件
### 前端
- `admin/src/views/order/index.vue` - 订单列表页面
- `admin/src/api/order.ts` - 订单API接口
### 后端
- `server/app/adminapi/controller/order/OrderController.php` - 订单控制器
- `server/app/adminapi/logic/order/OrderLogic.php` - 订单业务逻辑
---
## 版本信息
- **功能版本**: 1.5.0
- **实现日期**: 2026-03-10
- **功能类型**: 支付网关集成
- **状态**: ✅ 框架完成(待SDK集成)
---
**框架实现完成**