wechat payment for mobile page is ready

This commit is contained in:
RockYang
2024-09-19 17:59:27 +08:00
parent 4d9f89f630
commit 26e3ababcf
12 changed files with 196 additions and 140 deletions

View File

@@ -58,7 +58,6 @@ type AlipayConfig struct {
AlipayPublicKey string // 支付宝公钥文件路径
RootCert string // Root 秘钥路径
NotifyURL string // 异步通知地址
ReturnURL string // 同步回调地址
}
type WechatPayConfig struct {
@@ -69,7 +68,6 @@ type WechatPayConfig struct {
PrivateKey string // 用户私钥文件路径
ApiV3Key string // API V3 秘钥
NotifyURL string // 异步通知地址
ReturnURL string // 同步回调地址
}
type HuPiPayConfig struct { //虎皮椒第四方支付配置
@@ -78,7 +76,6 @@ type HuPiPayConfig struct { //虎皮椒第四方支付配置
AppSecret string // app 密钥
ApiURL string // 支付网关
NotifyURL string // 异步通知地址
ReturnURL string // 同步回调地址
}
// GeekPayConfig GEEK支付配置
@@ -88,7 +85,6 @@ type GeekPayConfig struct {
PrivateKey string // 私钥
ApiURL string // API 网关
NotifyURL string // 异步通知地址
ReturnURL string // 同步回调地址
Methods []string // 支付方式
}

View File

@@ -22,3 +22,18 @@ type OrderRemark struct {
Price float64 `json:"price"`
Discount float64 `json:"discount"`
}
var PayMethods = map[string]string{
"alipay": "支付宝商号",
"wechat": "微信商号",
"hupi": "虎皮椒",
"geek": "易支付",
}
var PayNames = map[string]string{
"alipay": "支付宝",
"wxpay": "微信支付",
"qqpay": "QQ钱包",
"jdpay": "京东支付",
"douyin": "抖音支付",
"paypal": "PayPal支付",
}

View File

@@ -67,6 +67,16 @@ func (h *OrderHandler) List(c *gin.Context) {
order.Id = item.Id
order.CreatedAt = item.CreatedAt.Unix()
order.UpdatedAt = item.UpdatedAt.Unix()
payMethod, ok := types.PayMethods[item.PayWay]
if !ok {
payMethod = item.PayWay
}
payName, ok := types.PayNames[item.PayType]
if !ok {
payName = item.PayWay
}
order.PayMethod = payMethod
order.PayName = payName
list = append(list, order)
} else {
logger.Error(err)

View File

@@ -48,6 +48,16 @@ func (h *OrderHandler) List(c *gin.Context) {
order.Id = item.Id
order.CreatedAt = item.CreatedAt.Unix()
order.UpdatedAt = item.UpdatedAt.Unix()
payMethod, ok := types.PayMethods[item.PayWay]
if !ok {
payMethod = item.PayWay
}
payName, ok := types.PayNames[item.PayType]
if !ok {
payName = item.PayWay
}
order.PayMethod = payMethod
order.PayName = payName
list = append(list, order)
} else {
logger.Error(err)

View File

@@ -97,6 +97,76 @@ func (h *PaymentHandler) Pay(c *gin.Context) {
resp.NotAuth(c)
return
}
amount, _ := decimal.NewFromFloat(product.Price).Sub(decimal.NewFromFloat(product.Discount)).Float64()
var payURL string
if payWay == "alipay" { // 支付宝
returnURL := fmt.Sprintf("%s/payReturn", utils.GetBaseURL(h.App.Config.AlipayConfig.NotifyURL))
money := fmt.Sprintf("%.2f", amount)
payURL, err = h.alipayService.PayPC(payment.AlipayParams{
OutTradeNo: orderNo,
Subject: product.Name,
TotalFee: money,
ReturnURL: returnURL,
NotifyURL: h.App.Config.AlipayConfig.NotifyURL,
})
if err != nil {
resp.ERROR(c, "error with generate pay url: "+err.Error())
return
}
} else if payWay == "hupi" { // 虎皮椒支付
returnURL := fmt.Sprintf("%s/payReturn", utils.GetBaseURL(h.App.Config.HuPiPayConfig.NotifyURL))
r, err := h.huPiPayService.Pay(payment.HuPiPayParams{
Version: "1.1",
TradeOrderId: orderNo,
TotalFee: fmt.Sprintf("%f", amount),
Title: product.Name,
NotifyURL: h.App.Config.HuPiPayConfig.NotifyURL,
ReturnURL: returnURL,
WapName: "GeekAI助手",
})
if err != nil {
resp.ERROR(c, err.Error())
return
}
payURL = r.URL
} else if payWay == "wechat" {
payURL, err = h.wechatPayService.PayUrlNative(payment.WechatPayParams{
OutTradeNo: orderNo,
TotalFee: int(amount * 100),
Subject: product.Name,
NotifyURL: h.App.Config.WechatPayConfig.NotifyURL,
})
if err != nil {
resp.ERROR(c, err.Error())
return
}
} else if payWay == "geek" {
returnURL := fmt.Sprintf("%s/payReturn", utils.GetBaseURL(h.App.Config.GeekPayConfig.NotifyURL))
if device == "wechat" {
returnURL = fmt.Sprintf("%s/mobile/profile", utils.GetBaseURL(h.App.Config.GeekPayConfig.NotifyURL))
}
params := payment.GeekPayParams{
OutTradeNo: orderNo,
Method: "web",
Name: product.Name,
Money: fmt.Sprintf("%f", amount),
ClientIP: c.ClientIP(),
Device: device,
Type: payType,
ReturnURL: returnURL,
NotifyURL: h.App.Config.GeekPayConfig.NotifyURL,
}
res, err := h.geekPayService.Pay(params)
if err != nil {
resp.ERROR(c, err.Error())
return
}
payURL = res.PayURL
}
// 创建订单
remark := types.OrderRemark{
Days: product.Days,
@@ -105,9 +175,6 @@ func (h *PaymentHandler) Pay(c *gin.Context) {
Price: product.Price,
Discount: product.Discount,
}
amount, _ := decimal.NewFromFloat(product.Price).Sub(decimal.NewFromFloat(product.Discount)).Float64()
order := model.Order{
UserId: user.Id,
Username: user.Username,
@@ -125,78 +192,6 @@ func (h *PaymentHandler) Pay(c *gin.Context) {
resp.ERROR(c, "error with create order: "+err.Error())
return
}
var payURL string
if payWay == "alipay" { // 支付宝
money := fmt.Sprintf("%.2f", order.Amount)
if device == "mobile" {
payURL, err = h.alipayService.PayMobile(payment.AlipayParams{
OutTradeNo: orderNo,
Subject: product.Name,
TotalFee: money,
NotifyURL: h.App.Config.AlipayConfig.NotifyURL,
})
} else {
payURL, err = h.alipayService.PayPC(payment.AlipayParams{
OutTradeNo: orderNo,
Subject: product.Name,
TotalFee: money,
ReturnURL: h.App.Config.AlipayConfig.ReturnURL,
NotifyURL: h.App.Config.AlipayConfig.NotifyURL,
})
}
if err != nil {
resp.ERROR(c, "error with generate pay url: "+err.Error())
return
}
} else if order.PayWay == "hupi" { // 虎皮椒支付
r, err := h.huPiPayService.Pay(payment.HuPiPayParams{
Version: "1.1",
TradeOrderId: orderNo,
TotalFee: fmt.Sprintf("%f", order.Amount),
Title: order.Subject,
NotifyURL: h.App.Config.HuPiPayConfig.NotifyURL,
ReturnURL: h.App.Config.HuPiPayConfig.ReturnURL,
WapName: "GeekAI助手",
})
if err != nil {
resp.ERROR(c, err.Error())
return
}
payURL = r.URL
} else if order.PayWay == "wechat" {
payURL, err = h.wechatPayService.PayUrlNative(payment.WechatPayParams{
OutTradeNo: orderNo,
TotalFee: int(order.Amount * 100),
Subject: order.Subject,
NotifyURL: h.App.Config.WechatPayConfig.NotifyURL,
ReturnURL: h.App.Config.WechatPayConfig.ReturnURL,
})
if err != nil {
resp.ERROR(c, err.Error())
return
}
} else if order.PayWay == "geek" {
params := payment.GeekPayParams{
OutTradeNo: orderNo,
Method: "web",
Name: order.Subject,
Money: fmt.Sprintf("%f", order.Amount),
ClientIP: c.ClientIP(),
Device: device,
Type: payType,
ReturnURL: h.App.Config.GeekPayConfig.ReturnURL,
NotifyURL: h.App.Config.GeekPayConfig.NotifyURL,
}
res, err := h.geekPayService.Pay(params)
if err != nil {
resp.ERROR(c, err.Error())
return
}
payURL = res.PayURL
}
resp.SUCCESS(c, payURL)
}

View File

@@ -82,6 +82,17 @@ func (s *Service) Run() {
logger.Errorf("taking task with error: %v", err)
continue
}
// translate prompt
if utils.HasChinese(task.Prompt) {
content, err := utils.OpenAIRequest(s.db, fmt.Sprintf(service.TranslatePromptTemplate, task.Prompt), "gpt-4o-mini")
if err == nil {
task.Prompt = content
} else {
logger.Warnf("error with translate prompt: %v", err)
}
}
var r LumaRespVo
r, err = s.LumaCreate(task)
if err != nil {

View File

@@ -17,5 +17,7 @@ type Order struct {
PayTime int64 `json:"pay_time"`
PayWay string `json:"pay_way"`
PayType string `json:"pay_type"`
PayMethod string `json:"pay_method"`
PayName string `json:"pay_name"`
Remark types.OrderRemark `json:"remark"`
}

View File

@@ -9,6 +9,7 @@ package utils
import (
"encoding/json"
"fmt"
"geekai/core/types"
logger2 "geekai/logger"
"io"
@@ -76,3 +77,11 @@ func DownloadImage(imageURL string, proxy string) ([]byte, error) {
return imageBytes, nil
}
func GetBaseURL(strURL string) string {
u, err := url.Parse(strURL)
if err != nil {
return ""
}
return fmt.Sprintf("%s://%s", u.Scheme, u.Host)
}