diff --git a/go.mod b/go.mod index 3c0d675..b525d55 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,9 @@ module git.tetele.net/tgo/wechat go 1.16 require ( - git.tetele.net/tgo/helper v0.3.2 // indirect + git.tetele.net/tgo/helper v0.3.2 + github.com/google/btree v1.1.2 // indirect github.com/json-iterator/go v1.1.12 + github.com/kayon/iploc v0.0.0-20200312105652-bda3e968a794 + github.com/wechatpay-apiv3/wechatpay-go v0.2.16 ) diff --git a/go.sum b/go.sum index b3309fd..d83e5dd 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,17 @@ git.tetele.net/tgo/helper v0.3.2 h1:3Pd3Ih3Ux+zFSjNZZv+4MSsVQiZ5HOfQmSus5+7rKR0= git.tetele.net/tgo/helper v0.3.2/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk= +github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= +github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU= +github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kayon/iploc v0.0.0-20200312105652-bda3e968a794 h1:dWJxw+KQOMeVcoyxqG9I5fppPld1hh1FG8ngv0fKNsQ= +github.com/kayon/iploc v0.0.0-20200312105652-bda3e968a794/go.mod h1:IwrOeG3O3K9vVXmcVvc9T0XLabw67QePi5pKQt5U+Kw= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= @@ -13,5 +19,14 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/wechatpay-apiv3/wechatpay-go v0.2.16 h1:7wTQYLxx5PSkzv0Zu/LabGHDVva9VEvH56su/lzRPbE= +github.com/wechatpay-apiv3/wechatpay-go v0.2.16/go.mod h1:Ca9wvI7xFoIWiY163q1jzddarQBS+1NE17OM1ZV24nw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/refund.go b/refund.go new file mode 100644 index 0000000..7d810ae --- /dev/null +++ b/refund.go @@ -0,0 +1,126 @@ +package wechat + +import ( + "context" + "crypto/rsa" + "crypto/x509" + "errors" + "git.tetele.net/tgo/helper" + "github.com/wechatpay-apiv3/wechatpay-go/core" + "github.com/wechatpay-apiv3/wechatpay-go/core/option" + "github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic" + "github.com/wechatpay-apiv3/wechatpay-go/utils" + "io/ioutil" + "log" +) + +/** + * 商户订单号,退款单号,总金额,退款金额,微信商户id,微信商户平台API私钥,平台证书,商户平台证书序列号,商品名,商品id,退款原因,回调地址 + */ +func CreateRefund(order_sn, out_refund_no, total_amount, refund__amount, wx_mp_mch_id, wx_mch_apiclient_key, + wx_pay_cert, mch_serial_no, title, uuid, reason, notify_url string) (map[string]interface{}, error) { + total := helper.ToInt64(helper.FloatMul(total_amount, 100)) //总金额 + refund := helper.ToInt64(helper.FloatMul(refund__amount, 100)) //退款金额 + + log.Println("refund", order_sn, out_refund_no, refund, total, notify_url) + + var ( + ctx context.Context = context.Background() + client *core.Client + ) + + privateKey, err := utils.LoadPrivateKey(wx_mch_apiclient_key) + if err != nil { + log.Println("商户私钥有误:", err) + return map[string]interface{}{}, errors.New("商户私钥有误") + } + wechatPayCertificate, err := utils.LoadCertificate(wx_pay_cert) + if err != nil { + log.Println("平台证书有误:", err) + return map[string]interface{}{}, errors.New("商户平台证书有误") + } + client, err = CreateClient(ctx, wechatPayCertificate, privateKey, wx_mp_mch_id, mch_serial_no) + if err != nil { + return map[string]interface{}{}, errors.New("请检查支付参数配置") + } + + // now := time.Now() + + svc := refunddomestic.RefundsApiService{Client: client} + _, result, err := svc.Create(ctx, + refunddomestic.CreateRequest{ + Amount: &refunddomestic.AmountReq{ + Currency: core.String("CNY"), + // From: []refunddomestic.FundsFromItem{refunddomestic.FundsFromItem{ + // Account: refunddomestic.ACCOUNT_AVAILABLE.Ptr(), + // Amount: core.Int64(444), + // }}, + Refund: core.Int64(refund), + Total: core.Int64(total), + }, + // FundsAccount: refunddomestic.REQFUNDSACCOUNT_AVAILABLE.Ptr(), + GoodsDetail: []refunddomestic.GoodsDetail{refunddomestic.GoodsDetail{ + GoodsName: core.String(title), + MerchantGoodsId: core.String(uuid), + RefundAmount: core.Int64(refund), + RefundQuantity: core.Int64(1), + UnitPrice: core.Int64(refund), + // WechatpayGoodsId: core.String("1001"), + }}, + NotifyUrl: core.String(notify_url), + OutRefundNo: core.String(out_refund_no), + OutTradeNo: core.String(order_sn), + Reason: core.String(reason), + // SubMchid: core.String("1900000109"), + // TransactionId: core.String("1217752501201407033233368018"), + }, + ) + + res := map[string]interface{}{} + log.Println("Response:", result.Response) + log.Println("Response Body:", result.Response.Body) + if err != nil { + response_body := map[string]interface{}{} + body, _ := ioutil.ReadAll(result.Response.Body) + err_body := json.Unmarshal(body, &response_body) + if err_body != nil { + res["code"] = "RESPONSE_BODY_ERROR" + res["message"] = "退款回调参数异常" + return res, err_body + } + log.Println("response_body:", response_body) + res["code"] = response_body["code"] + res["message"] = response_body["message"] + return res, errors.New(helper.ToStr(response_body["message"])) + } + res["code"] = "success" + res["message"] = "退款成功" + return res, nil +} +func CreateClient(ctx context.Context, wechatPayCertificate *x509.Certificate, privateKey *rsa.PrivateKey, mchID, mchSerialNo string) (*core.Client, error) { + + var client *core.Client + var err error + + if wechatPayCertificate != nil { + + client, err = core.NewClient( + ctx, option.WithMerchantCredential(mchID, mchSerialNo, privateKey), + option.WithWechatPayCertificate([]*x509.Certificate{wechatPayCertificate}), + ) + if err != nil { + log.Println("创建 Client 失败:", err) + return nil, err + } + } else { + client, err = core.NewClient( + ctx, option.WithMerchantCredential(mchID, mchSerialNo, privateKey), option.WithoutValidator(), + ) + if err != nil { + log.Println("创建 Client 失败:", err) + return nil, err + } + } + + return client, nil +} diff --git a/refund_test.go b/refund_test.go new file mode 100644 index 0000000..a1f4dbe --- /dev/null +++ b/refund_test.go @@ -0,0 +1,27 @@ +package wechat + +import ( + "github.com/kayon/iploc" + "testing" +) + +func Test_CreateRefund(t *testing.T) { + wx_mch_apiclient_key := "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDsea/umgdNz68w\nBdISLMPg+5E5Od2EDblO0ifz60P92kyIIQgFMKnw7sawfYlQNMs7fTOKMUBFoSJH\nzUNdOXUxLTmvPg9Pnh2jBBDQkUqBCMvUSsk5WzqF2VTSAmESy2XnfbGrXz9Q4KhV\nsLwP9O0qFDCrxgqjhKStUew6wNBq03lPnyvOee7QQ3eBZYY2z98XXjHai4nH7Lwi\nzwfYN23P/1QjWcs7a+J53v9KXjlEofNQzoXKy0hJnkPySTF8J9V7vBkbNwRceDVD\ng/uCsZ+ZBYEqwVtu7TkBaFZS06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DY\nDNLVAgMBAAGjgYEwfzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIE8DBlBgNVHR8EXjBc\nMFqgWKBWhlRodHRwOi8vZXZjYS5pdHJ1cy5jb20uY24vcHVibGljL2l0cnVzY3Js\nP0NBPTFCRDQyMjBFNTBEQkMwNEIwNkFEMzk3NTQ5ODQ2QzAxQzNFOEVCRDIwDQYJ\nKoZIhvcNAQELBQADggEBAClIek1KQAFhAIoP1UKXb/zFOGdWVuBD4vg/3aT33MlW\nsXmCH+oB6QyuY0tqIaQ5XfnvUgFFuTk+kMQuq2Tayj6XNozu1pteVtQ//3u2V9DF\nGXNxiT588EeN1od9qT+zhLH6nuptw++mlxBorQQ3BJvgX3NIOgFU/ajCiSkGAnpj\nD4QcHWzsM6kwpK6JUuPDRq3E7O/1cCJkt6a20JQj+fbYJ0By28PgdvaphuK8lW3e\nuSlkSNLP/a1F7sp5xHMW4a/E+/OF9D4ETHfoQhVYwOTHlxpnXPLApQtj9zcwr64/\nU9DP6BALDin+8uBDDkxsF29VyCYZ/l6LdsnYD+ihbSo=\n-----END CERTIFICATE-----" + ret, err := CreateRefund("6006167143914016978", "111223", "0.01", "0.01", "1298704001", wx_mch_apiclient_key, wx_pay_cert, "4642B326DDDDEAC74C677C163FB82BC95DCE43C0", "中餐", "122331", "yuanyin", "https://ttl-mini.tetele.net/admin/user/user/ttt_refund") + + t.Log(ret) + t.Log(err) +} +func Test_Qqwry(t *testing.T) { + + loc, err := iploc.Open("qqwry.dat") + if err != nil { + panic(err) + } + + detail := loc.Find("219.131.197.178") + t.Log(detail.String()) + t.Log(detail.Country, detail.Province, detail.City, detail.County) + +}