2
This commit is contained in:
@@ -5,8 +5,10 @@ import (
|
||||
"errors"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||
"hepa-calc-api/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 创建客户端
|
||||
@@ -38,3 +40,66 @@ func createClient() (*core.Client, error) {
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
type WxPayResult struct {
|
||||
OrderStatus *int `json:"order_status"` // 订单状态(1:待支付 2:已完成 3:已取消)
|
||||
PayStatus *int `json:"pay_status"` // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||||
PayTime *time.Time `json:"pay_time"` // 支付时间
|
||||
}
|
||||
|
||||
// HandlePayStatus 处理支付状态
|
||||
func HandlePayStatus(t *payments.Transaction) (w *WxPayResult, err error) {
|
||||
// 支付成功
|
||||
if *t.TradeState == "SUCCESS" {
|
||||
orderStatus := 2
|
||||
w.OrderStatus = &orderStatus
|
||||
|
||||
payStatus := 2
|
||||
w.PayStatus = &payStatus
|
||||
|
||||
parse, err := time.Parse("2006-01-02T15:04:05+07:00", *t.SuccessTime)
|
||||
if err != nil {
|
||||
return nil, errors.New("支付时间处理错误")
|
||||
}
|
||||
|
||||
w.PayTime = &parse
|
||||
if err != nil {
|
||||
return nil, errors.New("支付时间错误")
|
||||
}
|
||||
}
|
||||
|
||||
switch *t.TradeState {
|
||||
case "SUCCESS": // 支付成功
|
||||
orderStatus := 2
|
||||
w.OrderStatus = &orderStatus
|
||||
|
||||
payStatus := 2
|
||||
w.PayStatus = &payStatus
|
||||
|
||||
parse, err := time.Parse("2006-01-02T15:04:05+07:00", *t.SuccessTime)
|
||||
if err != nil {
|
||||
return nil, errors.New("支付时间处理错误")
|
||||
}
|
||||
|
||||
w.PayTime = &parse
|
||||
if err != nil {
|
||||
return nil, errors.New("支付时间错误")
|
||||
}
|
||||
case "CLOSED": // 已关闭
|
||||
payStatus := 6
|
||||
w.PayStatus = &payStatus
|
||||
case "REVOKED": // 已撤销(付款码支付)
|
||||
payStatus := 7
|
||||
w.PayStatus = &payStatus
|
||||
case "USERPAYING": // 用户支付中(付款码支付)
|
||||
payStatus := 3
|
||||
w.PayStatus = &payStatus
|
||||
case "PAYERROR": // 支付失败(其他原因,如银行返回失败)
|
||||
payStatus := 4
|
||||
w.PayStatus = &payStatus
|
||||
default:
|
||||
return nil, errors.New("未知支付状态")
|
||||
}
|
||||
|
||||
return w, nil
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package weChat
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/downloader"
|
||||
@@ -14,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
// ParseNotify 回调通知的验签与解密
|
||||
func ParseNotify(c *gin.Context) (notifyReq *notify.Request, err error) {
|
||||
func ParseNotify(c *gin.Context) (notifyReq *notify.Request, t *payments.Transaction, err error) {
|
||||
mchId := config.C.Wechat.Pay1659662936.MchId // 商户号
|
||||
mchCertificateSerialNumber := config.C.Wechat.Pay1659662936.MchCertificateSerialNumber // 商户证书序列号
|
||||
v3ApiSecret := config.C.Wechat.Pay1659662936.V3ApiSecret // 商户APIv3密钥
|
||||
@@ -23,13 +22,13 @@ func ParseNotify(c *gin.Context) (notifyReq *notify.Request, err error) {
|
||||
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
|
||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(privateKeyPath)
|
||||
if err != nil {
|
||||
return nil, errors.New("微信支付生成失败")
|
||||
return nil, nil, errors.New("微信支付生成失败")
|
||||
}
|
||||
|
||||
// 1. 使用 `RegisterDownloaderWithPrivateKey` 注册下载器
|
||||
err = downloader.MgrInstance().RegisterDownloaderWithPrivateKey(c, mchPrivateKey, mchCertificateSerialNumber, mchId, v3ApiSecret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// 2. 获取商户号对应的微信支付平台证书访问器
|
||||
@@ -41,9 +40,8 @@ func ParseNotify(c *gin.Context) (notifyReq *notify.Request, err error) {
|
||||
notifyReq, err = handler.ParseNotifyRequest(context.Background(), c.Request, transaction)
|
||||
// 如果验签未通过,或者解密失败
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
fmt.Println(transaction)
|
||||
return notifyReq, nil
|
||||
return notifyReq, transaction, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user