微信红包

This commit is contained in:
ziming 2025-11-06 10:28:36 +08:00
parent 5e2e300333
commit 0428383cdf
4 changed files with 99 additions and 119 deletions

View File

@ -2,12 +2,16 @@ package main
// main 这只是一个演示
func main() {
//wechatQueryCpn()
WechatRedPackV2Query()
//alipayOrderRedPack()
//zltxQuery()
//wechatQueryCpn()
//alipayQueryRedPack()
//wechatOrderCpn()
//wechatQueryCpn()
//WechatRedPackV2Order()
//WechatRedPackV2Query()
WechatRedPackV2Notify()
//alipayOrderRedPack()
//alipayQueryRedPack()
//zltxQuery()
//zltxCardNotify()
}

View File

@ -35,6 +35,55 @@ func getWechatRedPackV2Conf() []byte {
return marshal
}
func WechatRedPackV2Order() {
orderExtra := struct {
Appid string `json:"app_id"`
NotifyUrl string `json:"notify_url"`
}{
Appid: "wxe3bd59243545fa8a",
NotifyUrl: "https://transfer.86698.cn/notify/v1/wx_1652322442", // 通知url必须为公网可访问的URL必须为HTTPS不能携带参数,因此路由设计 “wx_1652322442” 处理
}
orderExtraBytes, _ := json.Marshal(orderExtra)
productExtra := struct {
BatchName string `json:"batch_name"`
BatchRemark string `json:"batch_remark"`
}{
BatchName: "LSXD测试",
BatchRemark: "LSXD测试",
}
productExtraBytes, _ := json.Marshal(productExtra)
err := manage.Add(wechatRedpackConfig)
if err != nil {
log.Fatalln(err)
}
defer manage.Close()
request := &proto.OrderRequest{
Config: getWechatRedPackV2Conf(),
Order: &proto.OrderRequest_Order{
OrderNo: "20251106001",
Account: "ojbqr6HpeWKFy9Sgdx8yCmmeVJiw",
Quantity: 1,
Amount: 0.1,
Extra: orderExtraBytes,
},
Product: &proto.OrderRequest_Product{
ProductNo: "",
Price: 0.1,
Type: 0,
Extra: productExtraBytes,
},
}
res, err := instance.Order(context.Background(), wechatRedpackConfig.Tag, request)
if err != nil {
log.Fatalln(err)
}
log.Printf("Order res:%+v", res)
}
func WechatRedPackV2Query() {
err := manage.Add(wechatRedpackConfig)
@ -43,7 +92,7 @@ func WechatRedPackV2Query() {
}
defer manage.Close()
queryRequest := &proto.QueryRequest{
request := &proto.QueryRequest{
Config: getWechatRedPackV2Conf(),
Order: &proto.QueryRequest_Order{
OrderNo: "19497351672832450564",
@ -52,9 +101,31 @@ func WechatRedPackV2Query() {
Extra: []byte(``),
},
}
resQuery, err := instance.Query(context.Background(), wechatRedpackConfig.Tag, queryRequest)
res, err := instance.Query(context.Background(), wechatRedpackConfig.Tag, request)
if err != nil {
log.Fatalln(err)
}
log.Printf("Query res:%+v", resQuery)
log.Printf("Query res:%+v", res)
}
func WechatRedPackV2Notify() {
body := []byte(`{"id":"fd06376a-3e1b-5516-81f8-9b69cf1ba416","create_time":"2025-07-28T16:10:15+08:00","resource_type":"encrypt-resource","event_type":"MCHTRANSFER.BILL.FINISHED","summary":"商家转账单据终态通知","resource":{"original_type":"mch_payment","algorithm":"AEAD_AES_256_GCM","ciphertext":"XJBIhrHgbe9NR5q/jLYmZKdT/3xuKm2x7EFu3T52Hj2hjPzarRSA2HCsGTxGojfD+CFyJHIULlL2adqLijAjpi3B6TaYKY4LqhtJ/RYSQtYNxYvBpWX1yLOWe8luJbWxmQvKZxIekFs8lGVgkPBUw0IfEAvJ6jHAGCcgxLIqxgOf6UtGUqxCCNp/V3xy8zCiHB0Mvlw8eXCTuG+ZESJIXvloVGNS79R6iNeqk4kNKRSaV86MNh1KQlmoBxZ4yEshD/vIlMulU3xEc+mM25y8vUS4Ot6pxEpUdUyjwcb9QTwTTnZzm6i+VWYymcItAVBQrvsKBMmqWnPtNXG8++13k3DeO1LyVKURmnWXXT1mImmGx/teN/1xPV5y6nChu/HTbcJGDQy2twuq6TPFbbYlTjZH047z/ZtozJNvGNeh","associated_data":"mch_payment","nonce":"YN3eW5H8mxLs"}}`)
hds := `{"Accept":["*/*"],"Cache-Control":["no-cache"],"Connection":["close"],"Content-Length":["775"],"Content-Type":["application/json"],"Pragma":["no-cache"],"User-Agent":["Mozilla/4.0"],"Wechatpay-Nonce":["dF8R9izUJnPBjVLa2cAcCaa7j6QUgitl"],"Wechatpay-Serial":["PUB_KEY_ID_0116523224422025061800192371001800"],"Wechatpay-Signature":["SoXIiTRTr6jofXXxGlfO+wyf1IzXFXcsfvEU2EggQfRKFu+8h3TT6QMQ8zIf8dpkkTPexB/3igGiATrR3uZY4ZeOpRrhIFHSJj0Ala0Ri2Nt4zk+MuBQnhybSYJ4Cn3/sHC4i2HFoOSil7OqlSr79hjod3h0tjYVQLtZ4+Cjp0IeMNB4p5qmIuERuhtfRqcyqXik9/uYNYxw8/Wkf1mMnTsBxyXK3iHAoinXNrEiqCCrQHCfnORMYosr7l+Ox8v9u1c8FFt+rt09vKssVCqYaZ/XRala3mjslDRiluFKSuqb7/JO3AxQjBK6M0iSZOlnmiXSIAq+UxJg4cem6wHi+g=="],"Wechatpay-Signature-Type":["WECHATPAY2-SHA256-RSA2048"],"Wechatpay-Timestamp":["1753690220"],"X-Forwarded-For":["121.51.58.168"],"X-Forwarded-Proto":["https"],"X-Real-Ip":["121.51.58.168"]}`
if err := manage.Add(wechatRedpackConfig); err != nil {
log.Fatalln(err)
}
defer manage.Close()
request := &proto.NotifyRequest{
Config: getWechatRedPackV2Conf(),
Queries: nil,
Headers: []byte(hds),
Body: body,
}
res, err := instance.Notify(context.Background(), wechatRedpackConfig.Tag, request)
if err != nil {
log.Fatalln(err)
}
log.Printf("Query res:%+v", res)
}

View File

@ -82,6 +82,20 @@ func (p *WeChatRedPackService) Query(ctx context.Context, request *proto.QueryRe
return queryResp(request, resp)
}
func (p *WeChatRedPackService) err(err error) error {
if e, ok := err.(*utils.ApiException); ok {
apiErr, err3 := transfer.BuildTransferErr(e.Body())
if err3 != nil {
return err3
}
return fmt.Errorf("请求微信API错误: %s", apiErr.Message)
}
return proto.ErrorRequestFail("请求微信返回错误:%v", err)
}
func (p *WeChatRedPackService) Notify(ctx context.Context, request *proto.NotifyRequest) (resp2 *proto.NotifyResponse, respErr error) {
defer func() {
@ -107,22 +121,8 @@ func (p *WeChatRedPackService) Notify(ctx context.Context, request *proto.Notify
resp, err := c.TransferNotify(ctx, &httpHeaders, request.Body)
if err != nil {
return nil, p.err(err)
return nil, proto.ErrorSignFail("err:%v", err)
}
return notifyResp(resp), nil
}
func (p *WeChatRedPackService) err(err error) error {
if e, ok := err.(*utils.ApiException); ok {
apiErr, err3 := transfer.BuildTransferErr(e.Body())
if err3 != nil {
return err3
}
return fmt.Errorf("请求微信API错误: %s", apiErr.Message)
}
return proto.ErrorRequestFail("请求微信返回错误:%v", err)
}

View File

@ -1,95 +0,0 @@
package internal
import (
"context"
"encoding/json"
"fmt"
"gitea.cdlsxd.cn/sdk/plugin/proto"
"github.com/stretchr/testify/assert"
"plugins/utils/wechat"
"testing"
)
var server = &WeChatRedPackService{}
func config() []byte {
c := &wechat.Server{
MchID: "1629276485",
MchCertificateSerialNumber: "3C7E21B74C04BE6227A690EB44184F219D763F92",
}
marshal, _ := json.Marshal(c)
return marshal
}
func TestConfig(t *testing.T) {
t.Run("TestConfig", func(t *testing.T) {
c := config()
fmt.Printf("%+s\n", string(c))
assert.NotEmpty(t, c)
})
}
func TestOrder(t *testing.T) {
request := &proto.OrderRequest{
Config: config(),
Order: &proto.OrderRequest_Order{
OrderNo: "240403164049635931",
Account: "oO3vO5AxRWgTjmMD38FTvnB5Rq6o",
Quantity: 1,
Amount: 0.01,
Extra: []byte(`{"app_id":"13100720242", "out_detail_no":"stock_creator_mchid"}`),
},
Product: &proto.OrderRequest_Product{
ProductNo: "",
Extra: []byte(`{"batch_name":"13100720242", "batch_remark":"stock_creator_mchid"}`),
},
}
t.Run("TestOrder", func(t *testing.T) {
got, err := server.Order(context.Background(), request)
if err != nil {
t.Errorf("Order() error = %v", err)
return
}
fmt.Printf("%+v", got)
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
})
}
func TestQuery(t *testing.T) {
request := &proto.QueryRequest{
Config: config(),
Order: &proto.QueryRequest_Order{
OrderNo: "202409141056275034200012",
TradeNo: "",
Account: "",
Extra: []byte(`{"out_detail_no":"0a2511525cc94a27bac18328771dc53e"}`),
},
}
t.Run("TestQuery", func(t *testing.T) {
got, err := server.Query(context.Background(), request)
if err != nil {
t.Errorf("Query() error = %v", err)
return
}
fmt.Printf("%+v \n", got)
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
})
}
func TestNotify(t *testing.T) {
in := &proto.NotifyRequest{
Config: config(),
Queries: []byte(``),
Headers: []byte(``),
Body: []byte(``),
}
t.Run("TestNotify", func(t *testing.T) {
got, err := server.Notify(context.Background(), in)
if err != nil {
t.Errorf("Notify() error = %v", err)
return
}
fmt.Printf("TestNotify : %+v \n", got)
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
})
}