Browse Source

no message

xingzai 11 months ago
parent
commit
bc2703ee08
3 changed files with 80 additions and 0 deletions
  1. 1 0
      go.mod
  2. 3 0
      go.sum
  3. 76 0
      services/cygx/wx_pay.go

+ 1 - 0
go.mod

@@ -98,6 +98,7 @@ require (
 	github.com/tidwall/match v1.1.1 // indirect
 	github.com/tidwall/pretty v1.2.0 // indirect
 	github.com/tjfoc/gmsm v1.3.2 // indirect
+	github.com/wechatpay-apiv3/wechatpay-go v0.2.18 // indirect
 	github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect
 	github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
 	golang.org/x/crypto v0.5.0 // indirect

+ 3 - 0
go.sum

@@ -13,6 +13,7 @@ github.com/SebastiaanKlippert/go-wkhtmltopdf v1.7.2 h1:LORAatv6KuKheYq8HXehiwx3f
 github.com/SebastiaanKlippert/go-wkhtmltopdf v1.7.2/go.mod h1:TY8r0gmwEL1c5Lbd66NgQCkL4ZjGDJCMVqvbbFvUx20=
 github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
 github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
+github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw=
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
@@ -419,6 +420,8 @@ github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhso
 github.com/tjfoc/gmsm v1.3.2 h1:7JVkAn5bvUJ7HtU08iW6UiD+UTmJTIToHCfeFzkcCxM=
 github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
 github.com/ugorji/go v0.0.0-20171122102828-84cb69a8af83/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
+github.com/wechatpay-apiv3/wechatpay-go v0.2.18 h1:vj5tvSmnEIz3ZsnFNNUzg+3Z46xgNMJbrO4aD4wP15w=
+github.com/wechatpay-apiv3/wechatpay-go v0.2.18/go.mod h1:A254AUBVB6R+EqQFo3yTgeh7HtyqRRtN2w9hQSOrd4Q=
 github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc=
 github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj09jdMlkY0aiA6+Skbtl3/c=
 github.com/xuri/efp v0.0.0-20220603152613-6918739fd470/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=

+ 76 - 0
services/cygx/wx_pay.go

@@ -0,0 +1,76 @@
+package cygx
+
+import (
+	"context"
+	"github.com/wechatpay-apiv3/wechatpay-go/core"
+	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
+	"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
+	payUtils "github.com/wechatpay-apiv3/wechatpay-go/utils"
+	"log"
+)
+
+const (
+	//MchPKFileName              = "./utils/cert/apiclient_key.pem"
+	MchPKFileName              = "../cert/cygx/apiclient_key.pem"
+	Mchid                      = "1624495680"
+	MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
+	MchAPIv3Key                = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
+)
+
+// 微信商户建立连接
+func getWechatClient() (context.Context, *core.Client, error) {
+	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
+	mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
+	if err != nil {
+		log.Print("load merchant private key error")
+		return nil, nil, 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 nil, nil, err
+	}
+	return ctx, client, nil
+}
+
+func init1212() {
+	ctx, client, err := getWechatClient()
+	if err != nil {
+		log.Printf("getWechatClientt err:%s", err)
+		return
+	}
+	svc := refunddomestic.RefundsApiService{Client: client}
+	resp, result, err := svc.Create(ctx,
+		refunddomestic.CreateRequest{
+			//SubMchid:      core.String(Mchid),
+			TransactionId: core.String("4200002189202403252248752161"),
+			OutTradeNo:    core.String("OD202403251018320707"),
+			OutRefundNo:   core.String("RefundOD202403251018320707"),
+			Reason:        core.String("嘿!延边!"),
+			NotifyUrl:     core.String("https://testmfyx.hzinsights.com/api/wechat/wxpay/refunds/notify"),
+			FundsAccount:  refunddomestic.REQFUNDSACCOUNT_AVAILABLE.Ptr(),
+			Amount: &refunddomestic.AmountReq{
+				Currency: core.String("CNY"),
+				//From: []refunddomestic.FundsFromItem{refunddomestic.FundsFromItem{
+				//	Account: refunddomestic.ACCOUNT_AVAILABLE.Ptr(),
+				//	Amount:  core.Int64(444),
+				//}},
+				Refund: core.Int64(1),
+				Total:  core.Int64(1),
+			},
+		},
+	)
+
+	if err != nil {
+		// 处理错误
+		log.Printf("call Create err:%s", err)
+	} else {
+		// 处理返回结果
+		log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
+	}
+}