支付宝 接入一系列修改

This commit is contained in:
2025-02-08 16:30:07 +08:00
parent 27aff930c7
commit cbdce12462
31 changed files with 1432 additions and 357 deletions

View File

@@ -0,0 +1,88 @@
package alipay
import (
"context"
"errors"
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/alipay/v3"
"github.com/go-pay/gopay/pkg/js"
"goutil/logUtilPlus"
"paycenter/internal/cert"
"strconv"
)
var (
ctx = context.Background()
client *alipay.ClientV3
err error
)
func init() {
// 初始化支付宝客V3户端
// appid应用ID
// privateKey应用私钥支持PKCS1和PKCS8
// isProd是否是正式环境沙箱环境请选择新版沙箱应用。
client, err = alipay.NewClientV3(cert.Appid, cert.PrivateKey, false)
if err != nil {
logUtilPlus.ErrorLog("new alipay client err:%s", err)
return
}
// 自定义配置http请求接收返回结果body大小默认 10MB
//client.SetBodySize() // 没有特殊需求,可忽略此配置
// Debug开关输出/关闭日志
client.DebugSwitch = gopay.DebugOn
// 设置自定义RequestId生成方法
//client.SetRequestIdFunc()
// 设置biz_content加密KEY设置此参数默认开启加密目前不可用
//client.SetAESKey("KvKUTqSVZX2fUgmxnFyMaQ==")
// 传入证书内容
err = client.SetCert(cert.AppPublicContent, cert.AlipayRootContent, cert.AlipayPublicContentRSA2)
if err != nil {
logUtilPlus.ErrorLog("set cert err:%s", err)
return
}
}
// AliPayPlace 函数用于发起预支付请求。
// 参数:
//
// outTradeNo: 商户订单号。
// currency: 订单金额,单位为分。
// storeId: 商户门店编号。
// clientIp: 用户的客户端IP。
// description: 订单描述。
//
// 返回值:
//
// 成功时返回预支付ID和nil错误。
// 失败时返回空字符串和错误对象。
func AliPayPlace(outTradeNo int64, currency int64, storeId string, clientIp string, description string) (string, error) {
// 请求参数
bm := make(gopay.BodyMap)
bm.Set("subject", "预创建创建订单").
Set("out_trade_no", strconv.FormatInt(outTradeNo, 10)).
Set("total_amount", currency)
rsp := new(struct {
OutTradeNo string `json:"out_trade_no"`
QrCode string `json:"qr_code"`
})
// 创建订单
res, err := client.DoAliPayAPISelfV3(ctx, alipay.MethodPost, alipay.V3TradePrecreate, bm, rsp)
if err != nil {
logUtilPlus.ErrorLog("client.TradePrecreate(), err:%v", err)
return "", err
}
logUtilPlus.DebugLog("aliRsp:%s", js.Marshal(rsp))
if res.StatusCode != alipay.Success {
logUtilPlus.ErrorLog("aliRsp.StatusCode:%d", res.StatusCode)
return "", errors.New("aliRsp.StatusCode:" + strconv.Itoa(res.StatusCode))
}
return "Success", nil
}

View File

@@ -0,0 +1,9 @@
package internal
import (
_ "paycenter/internal/alipay"
_ "paycenter/internal/cert"
_ "paycenter/internal/mesqueue"
_ "paycenter/internal/pay"
_ "paycenter/internal/wxpay"
)

View File

@@ -34,13 +34,13 @@ type GameMsg struct {
var (
//消息队列
msgQueue chan GameMsg = make(chan GameMsg, 100)
msgQueue = make(chan GameMsg, 100)
fileName = "ErrPushMsg"
)
func init() {
ConsumeQueue()
go ConsumeQueue()
}
// AddQueue 添加消息队列
@@ -50,42 +50,40 @@ func AddQueue(gameMsg GameMsg) {
// ConsumeQueue 消费消息队列
func ConsumeQueue() {
go func() {
//捕获异常
defer func() {
if err := recover(); err != nil {
//TODO 捕获异常
logUtilPlus.ErrorLog("推送充值信息到game异常 err:%s", err)
//捕获异常
defer func() {
if err := recover(); err != nil {
//TODO 捕获异常
logUtilPlus.ErrorLog("推送充值信息到game异常 err:%s", err)
//重新开启
restartConsumer()
}
}()
for {
gameMsg := <-msgQueue
url := fmt.Sprintf("http://www.game.com/pay %s", gameMsg.GameId)
//消费消息队列 推送重置信息到game
result, err := webUtil.GetWebData(url, map[string]string{})
if err != nil {
logUtilPlus.ErrorLog("推送充值信息到game异常 err:%s", err)
//放入消息队列重新推送
if gameMsg.pushCount < 3 {
msgQueue <- gameMsg
gameMsg.pushCount++
} else { //加入文件放弃推送
WriteErrPushMsg(url)
}
}
if string(result) != "" {
}
//重新开启
restartConsumer()
}
}()
for {
gameMsg := <-msgQueue
url := fmt.Sprintf("http://www.game.com/pay %s", gameMsg.GameId)
//消费消息队列 推送重置信息到game
result, err := webUtil.GetWebData(url, map[string]string{})
if err != nil {
logUtilPlus.ErrorLog("推送充值信息到game异常 err:%s", err)
//放入消息队列重新推送
if gameMsg.pushCount < 3 {
msgQueue <- gameMsg
gameMsg.pushCount++
} else { //加入文件放弃推送
WriteErrPushMsg(url)
}
}
if string(result) != "" {
}
}
}
// WriteErrPushMsg 推送异常消息 写入文件
@@ -117,7 +115,7 @@ func restartConsumer() {
return
}
logUtilPlus.InfoLog("重新启动消费者,重试次数: %d", retryCount+1)
ConsumeQueue()
go ConsumeQueue()
return
}
retryCount++

View File

@@ -2,12 +2,13 @@ package pay
import (
"common/remark"
"common/resultStatus"
"common/resultstatus"
"common/webServer"
"goutil/logUtilPlus"
"goutil/webUtil"
"net/http"
"paycenter/internal"
"paycenter/internal/alipay"
"paycenter/internal/wxpay"
"strconv"
)
@@ -74,7 +75,7 @@ func (a *PayApi) PlaceAnOrder(orderId int64, modelID int32, currency int64, stor
clientIp := webUtil.GetRequestIP(r)
//下微信订单
prepayId, err := internal.Prepay(orderId, currency, storeId, clientIp, "描述!!!!!!!!!!")
prepayId, err := wxpay.Prepay(orderId, currency, storeId, clientIp, "描述!!!!!!!!!!")
if err != nil {
responseObj.SetResultStatus(resultStatus.APIDataError)
return
@@ -139,7 +140,7 @@ func (a *PayApi) CallBack(orderIDStr string) (responseObj *webServer.ResponseObj
}
//查询订单状态
statusStr, err := internal.QueryOrderByOutTradeNo(orderID)
statusStr, err := wxpay.QueryOrderByOutTradeNo(orderID)
if err != nil {
responseObj.SetResultStatus(resultStatus.DataError)
return
@@ -193,7 +194,7 @@ func (a *PayApi) AliPayPlaceAnOrder(orderId int64, modelID int32, currency int64
clientIp := webUtil.GetRequestIP(r)
//下微信订单
prepayId, err := internal.AliPayPlace(orderId, currency, storeId, clientIp, "描述!!!!!!!!!!")
prepayId, err := alipay.AliPayPlace(orderId, currency, storeId, clientIp, "描述!!!!!!!!!!")
if err != nil {
responseObj.SetResultStatus(resultStatus.APIDataError)
return
@@ -209,4 +210,5 @@ func (a *PayApi) AliPayPlaceAnOrder(orderId int64, modelID int32, currency int64
resultMap["orderID"] = order.OrderID
resultMap["prepayId"] = order.PrepayId
responseObj.SetData(resultMap)
return responseObj
}

View File

@@ -2,94 +2,68 @@ package pay
import (
"common/connection"
"common/timer"
"goutil/logUtilPlus"
"paycenter/internal"
"paycenter/internal/wxpay"
"strconv"
"time"
)
func init() {
go CheckOrderStatus()
timer.Register(timer.TriggerTypeHalfHour, CheckOrderStatus)
}
// CheckOrderStatus 查订单状态
func CheckOrderStatus() {
// CheckOrderStatus 查订单状态
// @return error
func CheckOrderStatus(nowTime time.Time) error {
//捕获异常
defer func() {
if err := recover(); err != nil {
logUtilPlus.ErrorLog("CheckOrderStatus panic:", err)
restartConsumer()
//检索最近一个月的订单
for i := 0; i < 2; i++ {
//取i的负数
dbDate := connection.GetToMonthAdd(int32(-i))
var orders []Order // 使用切片存储查询结果
//这里使用原始sql
sql := "select * from order_" + strconv.Itoa(int(dbDate)) + " where order_status = 0"
dbResult := connection.GetPayDB().Exec(sql).Find(&orders)
if dbResult.Error != nil {
logUtilPlus.ErrorLog("查询订单状态失败", dbResult.Error.Error())
continue
}
}()
for {
//检索最近一个月的订单
for i := 0; i < 2; i++ {
// 处理查询结果
for _, order := range orders {
//取i的负数
dbDate := connection.GetToMonthAdd(int32(-i))
var orders []Order // 使用切片存储查询结果
//这里使用原始sql
sql := "select * from order_" + strconv.Itoa(int(dbDate)) + " where order_status = 0"
dbResult := connection.GetPayDB().Exec(sql).Find(&orders)
if dbResult.Error != nil {
logUtilPlus.ErrorLog("查询订单状态失败", dbResult.Error.Error())
//查询订单状态
statusStr, err := wxpay.QueryOrderByOutTradeNo(order.OrderID)
if err != nil {
logUtilPlus.ErrorLog("查询订单状态失败", err.Error())
continue
}
// 处理查询结果
for _, order := range orders {
//查询订单状态
statusStr, err := internal.QueryOrderByOutTradeNo(order.OrderID)
if statusStr == "SUCCESS" {
//修改订单状态
err = ChangeOrderStatus(order.OrderID, 1)
if err != nil {
logUtilPlus.ErrorLog("查询订单状态失败", err.Error())
logUtilPlus.ErrorLog("修改订单状态失败", err.Error())
continue
}
if statusStr == "SUCCESS" {
//修改订单状态
err = ChangeOrderStatus(order.OrderID, 1)
if err != nil {
logUtilPlus.ErrorLog("修改订单状态失败", err.Error())
continue
}
} else if statusStr == "CLOSED" { //已关闭
order.OrderStatus = 2
//修改订单状态
connection.AsyncSave(connection.GetPayDB(), &order)
} else if order.OrderTime.Add(time.Hour * 1).Before(time.Now()) { //超一个小时未支付 直接关闭订单
//直接关闭订单
internal.CloseOrder(order.OrderID)
order.OrderStatus = 2
connection.AsyncSave(connection.GetPayDB(), &order)
} else if statusStr == "CLOSED" { //已关闭
order.OrderStatus = 2
//修改订单状态
connection.AsyncSave(connection.GetPayDB(), &order)
} else if order.OrderTime.Add(time.Hour * 1).Before(time.Now()) { //超一个小时未支付 直接关闭订单
//直接关闭订单
err = wxpay.CloseOrder(order.OrderID)
if err != nil {
logUtilPlus.ErrorLog("关闭订单失败", err.Error())
continue
}
order.OrderStatus = 2
connection.AsyncSave(connection.GetPayDB(), &order)
}
}
//休息30分钟
time.Sleep(time.Minute * 30)
}
}
// restartConsumer 重启消费者
func restartConsumer() {
// 设置重试次数
maxRetries := 5
retryCount := 0
for {
select {
case <-time.After(5 * time.Second): // 等待5秒后重试
if retryCount >= maxRetries {
logUtilPlus.ErrorLog("查询订单状态,达到最大重试次数")
return
}
logUtilPlus.InfoLog("查询订单状态,重试次数: %d", retryCount+1)
go CheckOrderStatus()
return
}
retryCount++
}
return nil
}

View File

@@ -0,0 +1,64 @@
package wxpay
import (
"gopkg.in/yaml.v3"
"goutil/yamlUtil"
"log"
)
type WxPayConfig struct {
MchID string
MchCertificateSerialNumber string
MchAPIv3Key string
AppId string
NotifyUrl string
}
var (
wxPayConfig = &WxPayConfig{}
)
func init() {
//加载配置
reloadConfig()
//校验配置
CheckConfig()
}
// reloadConfig
//
// @description: reloadConfig
//
// parameter:
// return:
//
// @error: 错误信息
func reloadConfig() error {
yamlFile, err := yamlUtil.LoadFromFile("payconfig/wxpayconfig.yml")
if err != nil {
return err
}
// 解析 YAML 文件
err = yaml.Unmarshal(yamlFile, wxPayConfig)
if err != nil {
log.Fatalf("Error unmarshalling config file: %v", err)
return err
}
return nil
}
// CheckConfig 校验配置
func CheckConfig() error {
return nil
}
// GetWxPayConfig 获取配置
func GetWxPayConfig() *WxPayConfig {
return wxPayConfig
}

View File

@@ -0,0 +1,249 @@
package wxpay
import (
"context"
"errors"
"fmt"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/app"
"github.com/wechatpay-apiv3/wechatpay-go/utils"
"goutil/logUtilPlus"
"log"
"strconv"
"time"
)
var (
mchID string = GetWxPayConfig().MchID // 商户号
mchCertificateSerialNumber string = GetWxPayConfig().MchCertificateSerialNumber // 商户证书序列号
mchAPIv3Key string = GetWxPayConfig().MchAPIv3Key // 商户APIv3密钥
appId string = GetWxPayConfig().AppId // 应用ID
Address string = "成都市XXXXXXXXXXXXXXXXXXXXXXX" //公司地址
wxPayApiUrl string = GetWxPayConfig().NotifyUrl //支付成功回调地址
)
// Prepay 函数用于发起预支付请求。
// 参数:
//
// outTradeNo: 商户订单号。
// currency: 订单金额,单位为分。
// storeId: 商户门店编号。
// clientIp: 用户的客户端IP。
// description: 订单描述。
//
// 返回值:
//
// 成功时返回预支付ID和nil错误。
// 失败时返回空字符串和错误对象。
func Prepay(outTradeNo int64, currency int64, storeId string, clientIp string, description string) (string, error) {
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
if err != nil {
logUtilPlus.ErrorLog("load merchant private key error")
return "", err
}
ctx := context.Background()
// 使用商户私钥等初始化 client并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
logUtilPlus.ErrorLog("new wechat pay client err:%s", err)
return "", err
}
svc := app.AppApiService{Client: client}
resp, result, err := svc.Prepay(ctx,
app.PrepayRequest{
Appid: core.String(appId),
Mchid: core.String(mchID),
Description: core.String(description),
OutTradeNo: core.String(strconv.FormatInt(outTradeNo, 10)),
TimeExpire: core.Time(time.Now().Add(time.Hour * 2)), //支付时效时间 2 小时后失效
Attach: core.String(""), //附加数据 这里不需要有个订单id 可以获取订单详细信息
NotifyUrl: core.String(fmt.Sprintf(wxPayApiUrl, strconv.FormatInt(outTradeNo, 10))), //回调地址
//GoodsTag: core.String("WXG"),//优惠标记 这里没用
//LimitPay: []string{"LimitPay_example"},
SupportFapiao: core.Bool(false),
Amount: &app.Amount{
Currency: core.String("CNY"),
Total: core.Int64(currency),
},
Detail: &app.Detail{
//CostPrice: core.Int64(608800),
GoodsDetail: []app.GoodsDetail{app.GoodsDetail{
GoodsName: core.String(storeId), //商品编号
MerchantGoodsId: core.String(description),
Quantity: core.Int64(1),
UnitPrice: core.Int64(currency),
//WechatpayGoodsId: core.String("1001"),
}},
//InvoiceId: core.String("wx123"),
},
SceneInfo: &app.SceneInfo{
//DeviceId: core.String("013467007045764"),
PayerClientIp: core.String(clientIp),
StoreInfo: &app.StoreInfo{
Address: core.String(Address),
//AreaCode: core.String("440305"),
//Id: core.String("0001"),
//Name: core.String("腾讯大厦分店"),
},
},
SettleInfo: &app.SettleInfo{
ProfitSharing: core.Bool(false),
},
},
)
if err != nil {
// 处理错误
logUtilPlus.ErrorLog("call Prepay err:%s", err.Error())
return "", err
}
if result.Response.StatusCode != 200 {
errStr := fmt.Sprintf("status=%d resp=%s", result.Response.StatusCode, resp)
logUtilPlus.ErrorLog(errStr)
return "", errors.New(errStr)
}
return *resp.PrepayId, nil
}
// CloseOrder 关闭订单
func CloseOrder(outTradeNo int64) error {
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
if err != nil {
log.Print("加载商家私钥错误")
}
ctx := context.Background()
// 使用商户私钥等初始化 client并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
log.Printf("新的 WeChat Pay 客户端 Err:%s", err)
}
svc := app.AppApiService{Client: client}
_, err = svc.CloseOrder(ctx,
app.CloseOrderRequest{
//商户系统内部订单号只能是数字、大小写字母_-*且在同一个商户号下唯
OutTradeNo: core.String(strconv.FormatInt(outTradeNo, 10)),
//直连商户的商户号,由微信支付生成并下发。
Mchid: core.String(mchID),
},
)
if err != nil {
// 处理错误
logUtilPlus.ErrorLog("call CloseOrder err:%s", err)
return err
}
return nil
}
// QueryOrderById 根据商户订单号查询订单
func QueryOrderById() {
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
if err != nil {
log.Print("load merchant private key error")
}
ctx := context.Background()
// 使用商户私钥等初始化 client并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
log.Printf("new wechat pay client err:%s", err)
}
svc := app.AppApiService{Client: client}
resp, result, err := svc.QueryOrderById(ctx,
app.QueryOrderByIdRequest{
TransactionId: core.String("TransactionId_example"),
Mchid: core.String("Mchid_example"),
},
)
if err != nil {
// 处理错误
log.Printf("call QueryOrderById err:%s", err)
} else {
// 处理返回结果
log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
}
}
// QueryOrderByOutTradeNo 根据商户订单号查询订单
func QueryOrderByOutTradeNo(outTradeNo int64) (string, error) {
//循环查询次数
var count int = 0
loop:
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
if err != nil {
log.Print("load merchant private key error")
return "", err
}
ctx := context.Background()
// 使用商户私钥等初始化 client并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
log.Printf("new wechat pay client err:%s", err)
return "", err
}
svc := app.AppApiService{Client: client}
resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
app.QueryOrderByOutTradeNoRequest{
OutTradeNo: core.String(strconv.FormatInt(outTradeNo, 10)),
Mchid: &mchID,
},
)
if err != nil {
// 处理错误
log.Printf("call QueryOrderByOutTradeNo err:%s", err)
return "", err
} else {
// 处理返回结果
log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
//支付成功
if resp.TradeState == core.String("SUCCESS") {
return "SUCCESS", nil
} else if resp.TradeState == core.String("NOTPAY") && count < 3 { //未支付,循环查找订单
//休息200毫秒
time.Sleep(200 * time.Millisecond)
count++
goto loop
}
//支付失败
return *resp.TradeState, nil
}
}