feat: 收银台页面调整展示内容,付款模板页面调整展示内容,接口错误提示修改

This commit is contained in:
wolter 2024-12-24 14:57:33 +08:00
parent 2a21f854e1
commit 9a2c47ec0d
8 changed files with 237 additions and 177 deletions

View File

@ -56,6 +56,18 @@ const (
THIRD_ORDER_LOG_STATUS_SUCCESS = 2 // 第三方日志状态 成功
)
var PayChannelName = map[int]string{
PAY_CHANNEL_WECHAT_JSAPI: "微信支付",
PAY_CHANNEL_WECHAT_H5: "微信支付",
PAY_CHANNEL_WECHAT_APP: "微信支付",
PAY_CHANNEL_WECHAT_NATIVE: "微信支付",
PAY_CHANNEL_WECHAT_MINI: "微信支付",
PAY_CHANNEL_ALIPAY_WEB: "支付宝支付",
PAY_CHANNEL_ALIPAY_MINI: "支付宝支付",
PAY_CHANNEL_ALIPAY_JSAPI: "支付宝支付",
PAY_CHANNEL_ALIPAY_PC: "支付宝支付",
}
var PayChannelList = map[int]string{
PAY_CHANNEL_WECHAT_JSAPI: "微信JSAPI",
PAY_CHANNEL_WECHAT_H5: "微信H5",

View File

@ -1,6 +1,7 @@
package front
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/controllers"
"PaymentCenter/app/http/entities/front"
@ -13,10 +14,10 @@ import (
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"xorm.io/builder"
"strings"
)
// 预支付接口V2, 返回收银台页面
// 预支付接口V2, 返回收银台页面地址
func PayUrlV2(c *gin.Context) {
var res front.ApiResponse
req := controllers.GetRequest(c).(*front.PayReqsV2)
@ -41,21 +42,23 @@ func PayUrlV2(c *gin.Context) {
// 收银台页面
func PayPage(c *gin.Context) {
var (
code int
message string
amount string
code int
message string
amount string
orderInfo ordersmodel.Orders
desc string
)
orderId := c.Query("no")
if orderId != "" {
orderInfo := &ordersmodel.Orders{}
orderInfo, code = services.OrderFindOne(&ordersmodel.Orders{}, builder.Eq{"id": orderId})
if code == errorcode.Success {
amount = fmt.Sprintf("%.2f", float64(orderInfo.Amount)/100)
} else {
message = errorcode.GetMsg(code, "")
}
} else {
orderId := strings.TrimSpace(c.Query("no"))
if orderId == "" {
message = "页面不存在"
} else {
orderInfo, code = thirdpay.PayPageCheckOrder(orderId)
if code != errorcode.Success {
message = errorcode.GetMsg(code, "")
} else {
amount = fmt.Sprintf("%.2f", float64(orderInfo.Amount)/100)
desc = orderInfo.Desc
}
}
c.HTML(http.StatusOK, "payPage.html", gin.H{
@ -63,10 +66,11 @@ func PayPage(c *gin.Context) {
"message": message,
"id": orderId,
"amount": amount,
"desc": desc,
})
}
// 收银台获取支付渠道列表
// 收银台获取支付渠道列表接口
func PayChannelList(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*front.PayChannelListRequest)
req.UserAgent = c.Request.UserAgent()
@ -76,7 +80,7 @@ func PayChannelList(c *gin.Context) {
linq.From(data).SelectT(func(payChannel paychannelmodel.PayChannel) front.PayChannelListResponse {
return front.PayChannelListResponse{
ChannelType: payChannel.ChannelType,
PayName: payChannel.PayName,
PayName: common.PayChannelName[payChannel.ChannelType],
PayChannelId: strconv.Itoa(int(payChannel.Id)),
}
}).ToSlice(&result)
@ -84,16 +88,17 @@ func PayChannelList(c *gin.Context) {
controllers.HandCodeRes(c, result, code)
}
// 获取付款链接
// 获取付款链接,返回付款模板
func GetPayLink(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*front.GetPayLinkRequest)
result, message, code := thirdpay.GetPayLinkService(*req)
if code == errorcode.Success {
c.HTML(http.StatusOK, "payTemplate.html", gin.H{
"payUrl": result,
})
} else {
c.String(http.StatusOK, errorcode.GetMsg(code, "")+message)
if message == "" {
message = errorcode.GetMsg(code, "")
}
c.HTML(http.StatusOK, "payTemplateDefault.html", gin.H{
"code": code,
"payUrl": result,
"message": message,
})
}

View File

@ -11,12 +11,11 @@ import (
"PaymentCenter/app/http/trace"
"PaymentCenter/app/utils/metric"
"PaymentCenter/config"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
"github.com/gin-gonic/gin"
"github.com/qit-team/snow-core/http/middleware"
"github.com/qit-team/snow-core/log/logger"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
)
// api路由配置
@ -94,7 +93,7 @@ func RegisterRoute(router *gin.Engine) {
payPage := router.Group(common.PayPageAddress, middlewares.ValidateRequest())
// 收银台 支付渠道列表
payPage.POST("/list", front.PayChannelList)
// 获取付款链接
// 付款
payPage.GET("/submit", front.GetPayLink)
//router.GET(common.PayPageAddress, middlewares.ValidateRequest(), front.PayPage)

View File

@ -85,7 +85,7 @@ func ClientEnvCheck(ua string) int {
return common.OpenInUnknown
}
// 订单是否是支付状态
// 订单状态检查是否是支付状态
func OrderStatusCheck(order ordersmodel.Orders) (code int) {
switch order.Status {
case common.ORDER_STATUS_CLOSE:

View File

@ -24,6 +24,12 @@ import (
// 收银台获取付款链接
func GetPayLinkService(req front.GetPayLinkRequest) (result, message string, code int) {
defer func() {
if err := recover(); err != nil {
utils.Log(nil, "", "GetPayLinkService,获取支付链接失败", fmt.Sprintf("错误原因:%s", err))
code = errorcode.SystemError
}
}()
var (
payChannel = &paychannelmodel.PayChannel{}
order = &ordersmodel.Orders{}
@ -48,7 +54,7 @@ func GetPayLinkService(req front.GetPayLinkRequest) (result, message string, cod
if code != errorcode.Success {
return
}
// todo 订单的支付方式是否一致
// 订单的支付方式是否一致, 不一致则更新
if order.PayChannelId != payChannel.Id {
order.PayChannelId = payChannel.Id
code = services.OrderUpdate(order)
@ -210,3 +216,14 @@ func (this *payUrl) PayUrlV2Service() (result front.PayReqsV2Response, code int)
return this.result, code
}
func PayPageCheckOrder(orderId string) (order ordersmodel.Orders, code int) {
orderInfo := &ordersmodel.Orders{}
orderInfo, code = services.OrderFindOne(&ordersmodel.Orders{}, builder.Eq{"id": orderId})
if code != errorcode.Success {
return
}
code = services.OrderStatusCheck(*orderInfo)
return *orderInfo, code
}

View File

@ -1,147 +1,166 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>收银台页面</title>
{{define "payPage.html"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>收银台页面</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: #333;
}
p {
color: #666;
}
ul {
list-style: none;
padding: 0;
}
li {
margin: 10px 0;
}
</style>
</head>
{{if eq .code 200 }}
<body>
<!-- 页面内容 -->
{{/*<h1>收银台页面</h1>*/}}
<p>订单号:{{ .id }}</p>
<p>订单金额:{{ .amount }} 元</p>
<ul id="payment-list"></ul>
</body>
<script>
// 从 URL 中提取参数
function getQueryParam(param) {
const queryString = window.location.search.substring(1);
const params = queryString.split('&');
for (let i = 0; i < params.length; i++) {
const [key, value] = params[i].split('=');
if (key === param) {
return decodeURIComponent(value);
<style>
/* 段落样式 */
p {
color: #666666;
margin-bottom: 10px;
}
}
return null;
}
// 获取支付方式列表
function fetchPaymentMethods() {
const id = getQueryParam('no');
console.log('Order no:', id);
if (id) {
fetch(`/pay/front/api/v1/payPage/list?id=${id}`, {
method: 'POST',
})
.then(async response => {
if (response.ok) {
const data = await response.json()
console.log(data)
if (data.code !== 200) {
throw new Error('无效');
/* 无序列表样式 */
ul {
list-style: none;
padding: 0;
}
/* 列表项样式 */
li {
margin: 10px 0;
}
/* 按钮样式 */
button {
background-color: #007BFF;
color: #fff;
border: none;
padding: 10px 20px;
cursor: pointer;
transition: background-color 0.3s ease;
}
/* 按钮悬停样式 */
button:hover {
background-color: #0056b3;
}
</style>
</head>
{{if eq .code 200 }}
<body>
<!-- 页面内容 -->
<div>
<p>{{.desc}}</p>
<p>交易号:{{ .id }}</p>
<p>交易金额:{{ .amount }} 元</p>
</div>
<div>
<div id="pay"></div>
<!-- 支付方式列表 -->
<ul id="payment-list"></ul>
</div>
</body>
<script>
// 从 URL 中提取参数
function getQueryParam(param) {
const queryString = window.location.search.substring(1);
const params = queryString.split('&');
for (let i = 0; i < params.length; i++) {
const [key, value] = params[i].split('=');
if (key === param) {
return decodeURIComponent(value);
}
}
return null;
}
// 获取支付方式列表
function fetchPaymentMethods() {
const id = getQueryParam('no');
console.log('Order no:', id);
if (id) {
fetch(`/pay/front/api/v1/payPage/list?id=${id}`, {
method: 'POST',
})
.then(async response => {
if (response.ok) {
const data = await response.json()
console.log(data)
if (data.code !== 200) {
throw new Error('无效');
} else {
return data;
}
} else {
return data;
throw new Error('无效');
}
})
.then(data => {
// 处理返回的数据,例如渲染支付方式列表
if (data === null || data.data.length === 0) {
const pay = document.getElementById('pay');
pay.innerHTML = '<h3>支付环境异常,请检查</h3>';
} else {
renderPaymentMethods(data.data);
}
} else {
throw new Error('无效');
}
})
.then(data => {
console.log('Received payment methods:', data);
// 处理返回的数据,例如渲染支付方式列表
renderPaymentMethods(data.data);
})
.catch(error => {
console.error('data', data);
})
.catch(error => {
console.error('data', data);
});
} else {
console.error('Order no not found in URL');
}
}
// 渲染支付方式列表
function renderPaymentMethods(paymentMethods) {
const paymentList = document.getElementById('payment-list');
paymentList.innerHTML = '';
paymentMethods.forEach(method => {
const listItem = document.createElement('li');
const radioInput = document.createElement('input');
radioInput.type = 'radio';
radioInput.name = 'paymentMethod';
radioInput.value = method.pay_channel_id;
listItem.appendChild(radioInput);
listItem.appendChild(document.createTextNode(method.pay_name));
paymentList.appendChild(listItem);
});
// 添加提交按钮
const submitButton = document.createElement('button');
submitButton.type = 'button';
submitButton.textContent = '提交';
submitButton.onclick = function () {
const no = getQueryParam('no');
const selectedMethod = document.querySelector('input[name="paymentMethod"]:checked');
if (selectedMethod) {
window.location.href = `/pay/front/api/v1/payPage/submit?pay_channel_id=${selectedMethod.value}&no=${no}`;
});
} else {
alert('请选择支付方式');
console.error('Order no not found in URL');
}
};
paymentList.appendChild(submitButton);
}
}
// 渲染支付方式列表
function renderPaymentMethods(paymentMethods) {
const pay = document.getElementById('pay');
pay.innerHTML = '<h3>请选择支付方式</h3>';
const paymentList = document.getElementById('payment-list');
paymentList.innerHTML = '';
paymentMethods.forEach(method => {
const listItem = document.createElement('li');
const radioInput = document.createElement('input');
radioInput.type = 'radio';
radioInput.name = 'paymentMethod';
radioInput.value = method.pay_channel_id;
listItem.appendChild(radioInput);
listItem.appendChild(document.createTextNode(method.pay_name));
paymentList.appendChild(listItem);
});
// 添加提交按钮
const submitButton = document.createElement('button');
submitButton.type = 'button';
submitButton.textContent = '提交';
submitButton.onclick = function () {
const no = getQueryParam('no');
const selectedMethod = document.querySelector('input[name="paymentMethod"]:checked');
if (selectedMethod) {
window.location.href = `/pay/front/api/v1/payPage/submit?pay_channel_id=${selectedMethod.value}&no=${no}`;
} else {
alert('请选择支付方式');
}
};
paymentList.appendChild(submitButton);
}
// 在页面加载时调用 fetchPaymentMethods 函数
window.onload = fetchPaymentMethods;
</script>
// 在页面加载时调用 fetchPaymentMethods 函数
window.onload = fetchPaymentMethods;
</script>
{{ else}}
<body>
<p>{{.message}}</p>
</body>
{{ else}}
<body>
<p>{{.message}}</p>
</body>
{{end}}
{{end}}
</html>
</html>
{{end}}

View File

@ -1,15 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
<script>
// 网页默认跳转url
window.location.href = "{{.payUrl}}";
</script>
</html>

View File

@ -0,0 +1,23 @@
{{ define "payTemplateDefault.html"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{{/* <title>Title</title>*/}}
</head>
<body>
</body>
{{ if eq .code 200}}
<script>
// 网页默认跳转url
window.location.href = "{{.payUrl}}";
</script>
{{else}}
<script>
alert("{{.code}}" + "{{.message}}");
</script>
{{end}}
</html>
{{ end }}