diff --git a/alipay/client_test.go b/alipay/client_test.go index 75a86cea..2d56b229 100644 --- a/alipay/client_test.go +++ b/alipay/client_test.go @@ -66,7 +66,7 @@ func TestClient_PostAliPayAPISelfV2(t *testing.T) { // biz_content bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { bz.Set("subject", "预创建创建订单") - bz.Set("out_trade_no", util.GetRandomString(32)) + bz.Set("out_trade_no", util.RandomString(32)) bz.Set("total_amount", "100") }) @@ -82,7 +82,7 @@ func TestClient_PostAliPayAPISelfV2(t *testing.T) { func TestClient_PostAliPayAPISelf(t *testing.T) { bm := make(gopay.BodyMap) bm.Set("subject", "预创建创建订单") - bm.Set("out_trade_no", util.GetRandomString(32)) + bm.Set("out_trade_no", util.RandomString(32)) bm.Set("total_amount", "100") aliPsp := new(TradePrecreateResponse) diff --git a/alipay/payment_api_test.go b/alipay/payment_api_test.go index 85d9b643..5cfe9afd 100644 --- a/alipay/payment_api_test.go +++ b/alipay/payment_api_test.go @@ -13,7 +13,7 @@ func TestClient_TradePrecreate(t *testing.T) { // 请求参数 bm := make(gopay.BodyMap) bm.Set("subject", "预创建创建订单"). - Set("out_trade_no", util.GetRandomString(32)). + Set("out_trade_no", util.RandomString(32)). Set("total_amount", "0.01") // 创建订单 @@ -39,7 +39,7 @@ func TestClient_TradeCreate(t *testing.T) { bm := make(gopay.BodyMap) bm.Set("subject", "创建订单"). Set("buyer_id", "2088802095984694"). - Set("out_trade_no", util.GetRandomString(32)). + Set("out_trade_no", util.RandomString(32)). Set("total_amount", "0.01") // 创建订单 @@ -199,7 +199,7 @@ func TestClient_TradePageRefund(t *testing.T) { bm := make(gopay.BodyMap) bm.Set("out_trade_no", "GZ201909081743431443"). Set("refund_amount", "5"). - Set("out_request_no", util.GetRandomString(32)) + Set("out_request_no", util.RandomString(32)) // 发起退款请求 aliRsp, err := client.TradePageRefund(ctx, bm) diff --git a/alipay/zhima_api_test.go b/alipay/zhima_api_test.go index cd7e9d72..fbf63cde 100644 --- a/alipay/zhima_api_test.go +++ b/alipay/zhima_api_test.go @@ -65,7 +65,7 @@ func TestZhimaCreditEpSceneAgreementCancel(t *testing.T) { // 请求参数 bm := make(gopay.BodyMap) bm.Set("credit_order_no", "zme20181106154144733795615964647") - bm.Set("out_order_no", util.GetRandomString(64)) + bm.Set("out_order_no", util.RandomString(64)) bm.Set("biz_time", "2018-12-06 18:53:59") aliRsp, err := client.ZhimaCreditEpSceneAgreementCancel(ctx, bm) diff --git a/apple/notification_v2_test.go b/apple/notification_v2_test.go index a7b066f3..7affa077 100644 --- a/apple/notification_v2_test.go +++ b/apple/notification_v2_test.go @@ -71,7 +71,6 @@ func TestDecodeSignedPayload(t *testing.T) { "signedDate":1646387008228 } */ - // decode transactionInfo transactionInfo, err := payload.DecodeTransactionInfo() if err != nil { diff --git a/body_map_test.go b/body_map_test.go index cbdf37e0..408e38c1 100644 --- a/body_map_test.go +++ b/body_map_test.go @@ -117,7 +117,7 @@ func TestBodyMapMarshalSlice(t *testing.T) { bs, _ := json.Marshal(rs) bm := make(BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("transaction_id", "4208450740201411110007820472"). Set("out_order_no", "P20150806125346") diff --git a/constant.go b/constant.go index dd06de41..6d732490 100644 --- a/constant.go +++ b/constant.go @@ -7,7 +7,7 @@ const ( OK = "OK" DebugOff = 0 DebugOn = 1 - Version = "1.5.73" + Version = "1.5.74" ) type DebugSwitch int8 diff --git a/examples/wechat/wx_BatchQueryComment.go b/examples/wechat/wx_BatchQueryComment.go index 98395c0d..a113f8c4 100644 --- a/examples/wechat/wx_BatchQueryComment.go +++ b/examples/wechat/wx_BatchQueryComment.go @@ -19,7 +19,7 @@ func BatchQueryComment() { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("sign_type", wechat.SignType_HMAC_SHA256). Set("begin_time", "20190120000000"). Set("end_time", "20190122174000"). diff --git a/examples/wechat/wx_CloseOrder.go b/examples/wechat/wx_CloseOrder.go index c60323e1..8859abcb 100644 --- a/examples/wechat/wx_CloseOrder.go +++ b/examples/wechat/wx_CloseOrder.go @@ -19,7 +19,7 @@ func CloseOrder() { // 初始化参数结构体 bm := make(gopay.BodyMap) bm.Set("out_trade_no", "MfZC2segKxh0bnJSELbvKNeH3d9oWvvQ"). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("sign_type", wechat.SignType_MD5) // 请求关闭订单,成功后得到结果 diff --git a/examples/wechat/wx_DownloadBill.go b/examples/wechat/wx_DownloadBill.go index ecae81fe..f37f514a 100644 --- a/examples/wechat/wx_DownloadBill.go +++ b/examples/wechat/wx_DownloadBill.go @@ -18,7 +18,7 @@ func DownloadBill() { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("sign_type", wechat.SignType_MD5). Set("bill_date", "20190722"). Set("bill_type", "ALL") diff --git a/examples/wechat/wx_DownloadFundFlow.go b/examples/wechat/wx_DownloadFundFlow.go index f3ab38a8..52c56373 100644 --- a/examples/wechat/wx_DownloadFundFlow.go +++ b/examples/wechat/wx_DownloadFundFlow.go @@ -19,7 +19,7 @@ func DownloadFundFlow() { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("sign_type", wechat.SignType_HMAC_SHA256). Set("bill_date", "20190122"). Set("account_type", "Basic") diff --git a/examples/wechat/wx_Micropay.go b/examples/wechat/wx_Micropay.go index 879bd65c..21db1a0e 100644 --- a/examples/wechat/wx_Micropay.go +++ b/examples/wechat/wx_Micropay.go @@ -16,11 +16,11 @@ func Micropay() { // isProd:是否是正式环境 client := wechat.NewClient("wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", false) - number := util.GetRandomString(32) + number := util.RandomString(32) xlog.Debug("out_trade_no:", number) // 初始化参数Map bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("body", "扫用户付款码支付"). Set("out_trade_no", number). Set("total_fee", 1). diff --git a/examples/wechat/wx_QueryOrder.go b/examples/wechat/wx_QueryOrder.go index f9bd6d8d..6aa7b72f 100644 --- a/examples/wechat/wx_QueryOrder.go +++ b/examples/wechat/wx_QueryOrder.go @@ -19,7 +19,7 @@ func QueryOrder() { // 初始化参数结构体 bm := make(gopay.BodyMap) bm.Set("out_trade_no", "GW201908091551421156"). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("sign_type", wechat.SignType_MD5) // 请求订单查询,成功后得到结果 diff --git a/examples/wechat/wx_QueryRefund.go b/examples/wechat/wx_QueryRefund.go index f099fb62..5576270d 100644 --- a/examples/wechat/wx_QueryRefund.go +++ b/examples/wechat/wx_QueryRefund.go @@ -22,7 +22,7 @@ func QueryRefund() { //Set("out_refund_no", "vk4264I1UQ3Hm3E4AKsavK8npylGSgQA092f9ckUxp8A2gXmnsLEdsupURVTcaC7"). //Set("transaction_id", "97HiM5j6kGmM2fk7fYMc8MgKhPnEQ5Rk"). //Set("refund_id", "97HiM5j6kGmM2fk7fYMc8MgKhPnEQ5Rk"). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("sign_type", wechat.SignType_MD5) //请求申请退款 diff --git a/examples/wechat/wx_Refund.go b/examples/wechat/wx_Refund.go index cb1f1784..242603d8 100644 --- a/examples/wechat/wx_Refund.go +++ b/examples/wechat/wx_Refund.go @@ -20,12 +20,12 @@ func Refund() { // isProd:是否是正式环境 client := wechat.NewClient("wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", false) - s := util.GetRandomString(64) + s := util.RandomString(64) xlog.Debug("out_refund_no:", s) // 初始化参数结构体 bm := make(gopay.BodyMap) bm.Set("out_trade_no", "SdZBAqJHBQGKVwb7aMR2mUwC588NG2Sd"). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("sign_type", wechat.SignType_MD5). Set("out_refund_no", s). Set("total_fee", 1). diff --git a/examples/wechat/wx_Reverse.go b/examples/wechat/wx_Reverse.go index bd6b4346..e25e6536 100644 --- a/examples/wechat/wx_Reverse.go +++ b/examples/wechat/wx_Reverse.go @@ -18,7 +18,7 @@ func Reverse() { // 初始化参数Map bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("out_trade_no", "6aDCor1nUcAihrV5JBlI09tLvXbUp02B"). Set("sign_type", wechat.SignType_MD5) diff --git a/examples/wechat/wx_ServiceApi.go b/examples/wechat/wx_ServiceApi.go index 4db6c14a..f849d441 100644 --- a/examples/wechat/wx_ServiceApi.go +++ b/examples/wechat/wx_ServiceApi.go @@ -45,7 +45,7 @@ func GetOpenIdByAuthCode() { // apiKey:ApiKey // authCode:用户授权码 // nonceStr:随即字符串 - openIdRsp, err := wechat.GetOpenIdByAuthCode(ctx, "wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", "135127679952609396", util.GetRandomString(32)) + openIdRsp, err := wechat.GetOpenIdByAuthCode(ctx, "wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", "135127679952609396", util.RandomString(32)) if err != nil { xlog.Error("err:", err) return diff --git a/examples/wechat/wx_Transfer.go b/examples/wechat/wx_Transfer.go index 010c8010..3d068e40 100644 --- a/examples/wechat/wx_Transfer.go +++ b/examples/wechat/wx_Transfer.go @@ -24,8 +24,8 @@ func Transfer() { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). - Set("partner_trade_no", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). + Set("partner_trade_no", util.RandomString(32)). Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8"). Set("check_name", "FORCE_CHECK"). // NO_CHECK:不校验真实姓名 , FORCE_CHECK:强校验真实姓名 Set("re_user_name", "付明明"). // 收款用户真实姓名。 如果check_name设置为FORCE_CHECK,则必填用户真实姓名 diff --git a/examples/wechat/wx_UnifiedOrder.go b/examples/wechat/wx_UnifiedOrder.go index 6959034a..549ccf57 100644 --- a/examples/wechat/wx_UnifiedOrder.go +++ b/examples/wechat/wx_UnifiedOrder.go @@ -22,12 +22,12 @@ func UnifiedOrder() { //设置国家 client.SetCountry(wechat.China) - number := util.GetRandomString(32) + number := util.RandomString(32) xlog.Debug("out_trade_no:", number) //初始化参数Map bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("body", "H5支付"). Set("out_trade_no", number). Set("total_fee", 1). diff --git a/gopay_test.go b/gopay_test.go index 5cb87a25..07838e1e 100644 --- a/gopay_test.go +++ b/gopay_test.go @@ -1,7 +1,10 @@ package gopay import ( + "crypto/sha1" + "encoding/hex" "encoding/xml" + "fmt" "testing" "github.com/go-pay/gopay/pkg/xlog" @@ -11,7 +14,7 @@ func TestBodyMap_CheckParamsNull(t *testing.T) { bm := make(BodyMap) bm.Set("name", "jerry") bm.Set("age", 2) - bm.Set("phone", "") + bm.Set("phone", "123") bm.Set("pi", 3.1415926) err := bm.CheckEmptyError("name", "age", "phone") @@ -19,6 +22,12 @@ func TestBodyMap_CheckParamsNull(t *testing.T) { xlog.Errorf("bm.CheckEmptyError():error:%+v", err) return } + h := sha1.New() + h.Write([]byte("golang")) + bs := h.Sum(nil) + _signature := fmt.Sprintf("%s", hex.EncodeToString(bs)) + xlog.Info(_signature) // 771e417b9dcae54aead2f3cbbbff340787bc462f + // 771e417b9dcae54aead2f3cbbbff340787bc462f } func TestBodyMap_UnmarshalXML(t *testing.T) { diff --git a/paypal/order_test.go b/paypal/order_test.go index 6998188c..e1f8017e 100644 --- a/paypal/order_test.go +++ b/paypal/order_test.go @@ -11,7 +11,7 @@ import ( func TestCreateOrder(t *testing.T) { var pus []*PurchaseUnit var item = &PurchaseUnit{ - ReferenceId: util.GetRandomString(16), + ReferenceId: util.RandomString(16), Amount: &Amount{ CurrencyCode: "USD", Value: "8", diff --git a/pkg/aes/aes_cbc.go b/pkg/aes/aes_cbc.go new file mode 100644 index 00000000..2140a2f3 --- /dev/null +++ b/pkg/aes/aes_cbc.go @@ -0,0 +1,43 @@ +package aes + +import ( + "crypto/aes" + "crypto/cipher" + "errors" +) + +// AES-CBC 加密数据 +func CBCEncrypt(originData, key, iv []byte) ([]byte, error) { + return cbcEncrypt(originData, key, iv) +} + +// AES-CBC 解密数据 +func CBCDecrypt(secretData, key, iv []byte) ([]byte, error) { + return cbcDecrypt(secretData, key, iv) +} + +func cbcEncrypt(originData, key, iv []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + originData = PKCS7Padding(originData, block.BlockSize()) + secretData := make([]byte, len(originData)) + blockMode := cipher.NewCBCEncrypter(block, iv[:block.BlockSize()]) + blockMode.CryptBlocks(secretData, originData) + return secretData, nil +} + +func cbcDecrypt(secretData, key, iv []byte) (originByte []byte, err error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + originByte = make([]byte, len(secretData)) + blockMode := cipher.NewCBCDecrypter(block, iv[:block.BlockSize()]) + blockMode.CryptBlocks(originByte, secretData) + if len(originByte) == 0 { + return nil, errors.New("blockMode.CryptBlocks error") + } + return PKCS7UnPadding(originByte), nil +} diff --git a/pkg/aes/aes_cbc_decrypt.go b/pkg/aes/aes_cbc_decrypt.go deleted file mode 100644 index a250201f..00000000 --- a/pkg/aes/aes_cbc_decrypt.go +++ /dev/null @@ -1,47 +0,0 @@ -package aes - -import ( - "crypto/aes" - "crypto/cipher" - "errors" -) - -// 解密数据的Bytes数组 -func CBCDecryptData(secretData, key []byte) ([]byte, error) { - return decrypt(secretData, key) -} - -// 解密数据的Bytes数组 -func CBCDecryptIvData(secretData, key, iv []byte) ([]byte, error) { - return decryptIv(secretData, key, iv) -} - -func decrypt(secretData, key []byte) (originByte []byte, err error) { - block, err := aes.NewCipher(key) - if err != nil { - return nil, err - } - blockSize := block.BlockSize() - blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) - originByte = make([]byte, len(secretData)) - blockMode.CryptBlocks(originByte, secretData) - if len(originByte) == 0 { - return nil, errors.New("blockMode.CryptBlocks error") - } - return PKCS7UnPadding(originByte), nil -} - -func decryptIv(secretData, key, iv []byte) (originByte []byte, err error) { - block, err := aes.NewCipher(key) - if err != nil { - return nil, err - } - blockMode := cipher.NewCBCDecrypter(block, iv[:block.BlockSize()]) - - originByte = make([]byte, len(secretData)) - blockMode.CryptBlocks(originByte, secretData) - if len(originByte) == 0 { - return nil, errors.New("blockMode.CryptBlocks error") - } - return PKCS7UnPadding(originByte), nil -} diff --git a/pkg/aes/aes_cbc_encrypt.go b/pkg/aes/aes_cbc_encrypt.go deleted file mode 100644 index ce836180..00000000 --- a/pkg/aes/aes_cbc_encrypt.go +++ /dev/null @@ -1,43 +0,0 @@ -package aes - -import ( - "crypto/aes" - "crypto/cipher" -) - -// 加密后的Bytes数组 -func CBCEncryptData(originData, key []byte) ([]byte, error) { - return encrypt(originData, key) -} - -// 加密后的Bytes数组 -func CBCEncryptIvData(originData, key, iv []byte) ([]byte, error) { - return encryptIv(originData, key, iv) -} - -func encrypt(originData, key []byte) ([]byte, error) { - block, err := aes.NewCipher(key) - if err != nil { - return nil, err - } - blockSize := block.BlockSize() - blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) - - originData = PKCS7Padding(originData, blockSize) - secretData := make([]byte, len(originData)) - blockMode.CryptBlocks(secretData, originData) - return secretData, nil -} - -func encryptIv(originData, key, iv []byte) ([]byte, error) { - block, err := aes.NewCipher(key) - if err != nil { - return nil, err - } - blockMode := cipher.NewCBCEncrypter(block, iv[:block.BlockSize()]) - - originData = PKCS7Padding(originData, block.BlockSize()) - secretData := make([]byte, len(originData)) - blockMode.CryptBlocks(secretData, originData) - return secretData, nil -} diff --git a/pkg/aes/aes_ecb.go b/pkg/aes/aes_ecb.go new file mode 100644 index 00000000..d5a6cca1 --- /dev/null +++ b/pkg/aes/aes_ecb.go @@ -0,0 +1,105 @@ +package aes + +import ( + "crypto/aes" + "crypto/cipher" + "errors" +) + +// AES-ECB 加密数据 +func ECBEncrypt(originData, key []byte) ([]byte, error) { + return ecbEncrypt(originData, key) +} + +// AES-ECB 解密数据 +func ECBDecrypt(secretData, key []byte) ([]byte, error) { + return ecbDecrypt(secretData, key) +} + +func ecbEncrypt(originData, key []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + originData = PKCS7Padding(originData, block.BlockSize()) + secretData := make([]byte, len(originData)) + blockMode := newECBEncrypter(block) + blockMode.CryptBlocks(secretData, originData) + return secretData, nil +} + +func ecbDecrypt(secretData, key []byte) (originByte []byte, err error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + blockMode := newECBDecrypter(block) + originByte = make([]byte, len(secretData)) + blockMode.CryptBlocks(originByte, secretData) + if len(originByte) == 0 { + return nil, errors.New("blockMode.CryptBlocks error") + } + return PKCS7UnPadding(originByte), nil +} + +// =========== + +type ecb struct { + b cipher.Block + blockSize int +} + +func newECB(b cipher.Block) *ecb { + return &ecb{ + b: b, + blockSize: b.BlockSize(), + } +} + +type ecbEncrypter ecb + +// newECBEncrypter returns a BlockMode which encrypts in electronic code book +// mode, using the given Block. +func newECBEncrypter(b cipher.Block) cipher.BlockMode { + return (*ecbEncrypter)(newECB(b)) +} + +func (x *ecbEncrypter) BlockSize() int { return x.blockSize } + +func (x *ecbEncrypter) CryptBlocks(dst, src []byte) { + if len(src)%x.blockSize != 0 { + panic("crypto/cipher: input not full blocks") + } + if len(dst) < len(src) { + panic("crypto/cipher: output smaller than input") + } + for len(src) > 0 { + x.b.Encrypt(dst, src[:x.blockSize]) + src = src[x.blockSize:] + dst = dst[x.blockSize:] + } +} + +type ecbDecrypter ecb + +// newECBDecrypter returns a BlockMode which decrypts in electronic code book +// mode, using the given Block. +func newECBDecrypter(b cipher.Block) cipher.BlockMode { + return (*ecbDecrypter)(newECB(b)) +} + +func (x *ecbDecrypter) BlockSize() int { return x.blockSize } + +func (x *ecbDecrypter) CryptBlocks(dst, src []byte) { + if len(src)%x.blockSize != 0 { + panic("crypto/cipher: input not full blocks") + } + if len(dst) < len(src) { + panic("crypto/cipher: output smaller than input") + } + for len(src) > 0 { + x.b.Decrypt(dst, src[:x.blockSize]) + src = src[x.blockSize:] + dst = dst[x.blockSize:] + } +} diff --git a/pkg/aes/aes_gcm.go b/pkg/aes/aes_gcm.go new file mode 100644 index 00000000..0e8fa9e0 --- /dev/null +++ b/pkg/aes/aes_gcm.go @@ -0,0 +1,49 @@ +package aes + +import ( + "crypto/aes" + "crypto/cipher" + "fmt" + + "github.com/go-pay/gopher/util" +) + +// AES-GCM 加密数据 +func GCMEncrypt(originText, additional, key []byte) (nonce []byte, cipherText []byte, err error) { + return gcmEncrypt(originText, additional, key) +} + +// AES-GCM 解密数据 +func GCMDecrypt(cipherText, nonce, additional, key []byte) ([]byte, error) { + return gcmDecrypt(cipherText, nonce, additional, key) +} + +func gcmDecrypt(secretData, nonce, additional, key []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + gcm, err := cipher.NewGCM(block) + if err != nil { + return nil, fmt.Errorf("cipher.NewGCM(),error:%w", err) + } + originByte, err := gcm.Open(nil, nonce, secretData, additional) + if err != nil { + return nil, err + } + return originByte, nil +} + +func gcmEncrypt(originText, additional, key []byte) ([]byte, []byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, nil, err + } + nonce := []byte(util.RandomString(12)) + gcm, err := cipher.NewGCM(block) + if err != nil { + return nil, nil, fmt.Errorf("cipher.NewGCM(),error:%w", err) + } + cipherBytes := gcm.Seal(nil, nonce, originText, additional) + return nonce, cipherBytes, nil +} diff --git a/pkg/aes/aes_gcm_decrypt.go b/pkg/aes/aes_gcm_decrypt.go deleted file mode 100644 index aa5c5696..00000000 --- a/pkg/aes/aes_gcm_decrypt.go +++ /dev/null @@ -1,27 +0,0 @@ -package aes - -import ( - "crypto/aes" - "crypto/cipher" - "fmt" -) - -func GCMDecrypt(cipherText, nonce, additional, key []byte) ([]byte, error) { - return gcmDecrypt(cipherText, nonce, additional, key) -} - -func gcmDecrypt(secretData, nonce, additional, key []byte) ([]byte, error) { - block, err := aes.NewCipher(key) - if err != nil { - return nil, err - } - gcm, err := cipher.NewGCM(block) - if err != nil { - return nil, fmt.Errorf("cipher.NewGCM(),error:%w", err) - } - originByte, err := gcm.Open(nil, nonce, secretData, additional) - if err != nil { - return nil, err - } - return originByte, nil -} diff --git a/pkg/aes/aes_gcm_encrypt.go b/pkg/aes/aes_gcm_encrypt.go deleted file mode 100644 index 04b5309e..00000000 --- a/pkg/aes/aes_gcm_encrypt.go +++ /dev/null @@ -1,29 +0,0 @@ -package aes - -import ( - "crypto/aes" - "crypto/cipher" - "fmt" - - "github.com/go-pay/gopay/pkg/util" -) - -func GCMEncrypt(originText, additional, key []byte) (nonce []byte, cipherText []byte, err error) { - return gcmEncrypt(originText, additional, key) -} - -func gcmEncrypt(originText, additional, key []byte) ([]byte, []byte, error) { - block, err := aes.NewCipher(key) - if err != nil { - return nil, nil, err - } - nonce := []byte(util.GetRandomString(12)) - gcm, err := cipher.NewGCM(block) - if err != nil { - return nil, nil, fmt.Errorf("cipher.NewGCM(),error:%w", err) - } - - cipherBytes := gcm.Seal(nil, nonce, originText, additional) - - return nonce, cipherBytes, nil -} diff --git a/pkg/aes/aes_test.go b/pkg/aes/aes_test.go index 12746d9f..76a319a1 100644 --- a/pkg/aes/aes_test.go +++ b/pkg/aes/aes_test.go @@ -4,24 +4,28 @@ import ( "encoding/base64" "testing" - "github.com/go-pay/gopay/pkg/xlog" + "github.com/go-pay/gopher/xlog" ) var ( - secretKey = "GYBh3Rmey7nNzR/NpV0vAw==" + secretKey = "JYRn4wbCy8KgVIZJaPhYTcTn2zixVC4Y" iv = "JR3unO2glQuMhUx3" ) -func TestAesCBCEncryptDecrypt(t *testing.T) { - originData := "https://www.fmm.ink" +func init() { + xlog.Level = xlog.DebugLevel +} + +func TestAesECBEncryptDecrypt(t *testing.T) { + originData := "www.gopay.ink" xlog.Debug("originData:", originData) - encryptData, err := CBCEncryptData([]byte(originData), []byte(secretKey)) + encryptData, err := ECBEncrypt([]byte(originData), []byte(secretKey)) if err != nil { xlog.Error("AesCBCEncryptToString:", err) return } xlog.Debug("encryptData:", string(encryptData)) - origin, err := CBCDecryptData(encryptData, []byte(secretKey)) + origin, err := ECBDecrypt(encryptData, []byte(secretKey)) if err != nil { xlog.Error("AesDecryptToBytes:", err) return @@ -29,18 +33,18 @@ func TestAesCBCEncryptDecrypt(t *testing.T) { xlog.Debug("origin:", string(origin)) } -func TestAesCBCEncryptDecryptIv(t *testing.T) { - originData := "https://www.fmm.ink" +func TestAesCBCEncryptDecrypt(t *testing.T) { + originData := "www.gopay.ink" xlog.Debug("originData:", originData) - encryptData, err := CBCEncryptIvData([]byte(originData), []byte(secretKey), []byte(iv)) + encryptData, err := CBCEncrypt([]byte(originData), []byte(secretKey), []byte(iv)) if err != nil { - xlog.Error("CBCEncryptIvData:", err) + xlog.Error("CBCEncrypt:", err) return } xlog.Debug("encryptData:", string(encryptData)) - origin, err := CBCDecryptIvData(encryptData, []byte(secretKey), []byte(iv)) + origin, err := CBCDecrypt(encryptData, []byte(secretKey), []byte(iv)) if err != nil { - xlog.Error("CBCDecryptIvData:", err) + xlog.Error("CBCDecrypt:", err) return } xlog.Debug("origin:", string(origin)) diff --git a/pkg/util/random.go b/pkg/util/random.go index 65b005be..5f2420a6 100644 --- a/pkg/util/random.go +++ b/pkg/util/random.go @@ -6,7 +6,7 @@ import ( ) //随机生成字符串 -func GetRandomString(l int) string { +func RandomString(l int) string { str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" bytes := []byte(str) var result []byte = make([]byte, 0, l) @@ -18,7 +18,7 @@ func GetRandomString(l int) string { } //随机生成纯字符串 -func GetRandomPureString(l int) string { +func RandomPureString(l int) string { str := "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" bytes := []byte(str) var result []byte = make([]byte, 0, l) @@ -30,7 +30,7 @@ func GetRandomPureString(l int) string { } //随机生成数字字符串 -func GetRandomNumber(l int) string { +func RandomNumber(l int) string { str := "0123456789" bytes := []byte(str) var result []byte diff --git a/pkg/xhttp/client_test.go b/pkg/xhttp/client_test.go index 87a61c1f..0cb22af0 100644 --- a/pkg/xhttp/client_test.go +++ b/pkg/xhttp/client_test.go @@ -17,9 +17,7 @@ type HttpGet struct { Data interface{} `json:"data,omitempty"` } -var ( - ctx = context.Background() -) +var ctx = context.Background() func TestHttpGet(t *testing.T) { client := NewClient() diff --git a/pkg/xtime/xtime.go b/pkg/xtime/xtime.go index ff5b9f95..f4b7e509 100644 --- a/pkg/xtime/xtime.go +++ b/pkg/xtime/xtime.go @@ -14,6 +14,9 @@ type Time int64 func (t *Time) Scan(src interface{}) (err error) { switch sc := src.(type) { case time.Time: + if sc.IsZero() { + return + } *t = Time(sc.Unix()) case string: var i int64 @@ -39,6 +42,9 @@ func (t *Time) FromDB(bs []byte) error { if err != nil { return err } + if ti.IsZero() { + return nil + } *t = Time(ti.Unix()) return nil } diff --git a/qq/client_test.go b/qq/client_test.go index e9d087b4..ade7401a 100644 --- a/qq/client_test.go +++ b/qq/client_test.go @@ -36,7 +36,7 @@ func TestMain(m *testing.M) { func TestClient_MicroPay(t *testing.T) { bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)) + bm.Set("nonce_str", util.RandomString(32)) qqRsp, err := client.MicroPay(ctx, bm) if err != nil { diff --git a/release_note.txt b/release_note.txt index 8caee3c5..c21b2fe4 100644 --- a/release_note.txt +++ b/release_note.txt @@ -1,3 +1,7 @@ +版本号:Release 1.5.74 +修改记录: + (1) gopay:一些小改动,util.GetRandomString() -> util.RandomString() + 版本号:Release 1.5.73 修改记录: (1) Apple:新增内购支付通知V2解析 diff --git a/wechat/base_api_test.go b/wechat/base_api_test.go index f3da989d..069ad1a2 100644 --- a/wechat/base_api_test.go +++ b/wechat/base_api_test.go @@ -11,11 +11,11 @@ import ( ) func TestClient_UnifiedOrder(t *testing.T) { - number := util.GetRandomString(32) + number := util.RandomString(32) xlog.Info("out_trade_no:", number) // 初始化参数Map bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("body", "H5支付"). Set("out_trade_no", number). Set("total_fee", 1). @@ -59,11 +59,11 @@ func TestClient_UnifiedOrder(t *testing.T) { } func TestClient_Micropay(t *testing.T) { - number := util.GetRandomString(32) + number := util.RandomString(32) xlog.Info("out_trade_no:", number) // 初始化参数Map bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("body", "扫用户付款码支付"). Set("out_trade_no", number). Set("total_fee", 1). @@ -89,7 +89,7 @@ func TestClient_QueryOrder(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) bm.Set("out_trade_no", "MfZC2segKxh0bnJSELbvKNeH3d9oWvvQ"). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("sign_type", SignType_MD5) // 请求订单查询,成功后得到结果 @@ -106,7 +106,7 @@ func TestClient_CloseOrder(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) bm.Set("out_trade_no", "MfZC2segKxh0bnJSELbvKNeH3d9oWvvQ"). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("sign_type", SignType_MD5) // 请求关闭订单,成功后得到结果 @@ -120,11 +120,11 @@ func TestClient_CloseOrder(t *testing.T) { func TestClient_Refund(t *testing.T) { // 初始化参数结构体 - s := util.GetRandomString(64) + s := util.RandomString(64) xlog.Info("out_refund_no:", s) bm := make(gopay.BodyMap) bm.Set("out_trade_no", "QRcTBTbJLoDrWSW9FtpSFlgWhft2QbaY"). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("sign_type", SignType_MD5). Set("out_refund_no", s). Set("total_fee", 101). @@ -146,7 +146,7 @@ func TestClient_QueryRefund(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) bm.Set("out_trade_no", "97HiM5j6kGmM2fk7fYMc8MgKhPnEQ5Rk"). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("sign_type", SignType_MD5) /*. Set("transaction_id", "97HiM5j6kGmM2fk7fYMc8MgKhPnEQ5Rk"). Set("out_refund_no", "vk4264I1UQ3Hm3E4AKsavK8npylGSgQA092f9ckUxp8A2gXmnsLEdsupURVTcaC7"). @@ -165,7 +165,7 @@ func TestClient_QueryRefund(t *testing.T) { func TestClient_Reverse(t *testing.T) { // 初始化参数Map bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("out_trade_no", "6aDCor1nUcAihrV5JBlI09tLvXbUp02B"). Set("sign_type", SignType_MD5) diff --git a/wechat/client_test.go b/wechat/client_test.go index f527923b..46a5fb59 100644 --- a/wechat/client_test.go +++ b/wechat/client_test.go @@ -54,7 +54,7 @@ func TestMain(m *testing.M) { func TestClient_AuthCodeToOpenId(t *testing.T) { // 初始化参数Map bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("auth_code", "134753997737645794") wxRsp, err := client.AuthCodeToOpenId(ctx, bm) @@ -68,8 +68,8 @@ func TestClient_AuthCodeToOpenId(t *testing.T) { func TestClient_GetTransferInfo(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). - Set("partner_trade_no", util.GetRandomString(32)) + bm.Set("nonce_str", util.RandomString(32)). + Set("partner_trade_no", util.RandomString(32)) // 查询企业付款 // body:参数Body @@ -84,7 +84,7 @@ func TestClient_GetTransferInfo(t *testing.T) { func TestClient_DownloadBill(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("sign_type", SignType_MD5). Set("bill_date", "20190722"). Set("bill_type", "ALL") @@ -101,7 +101,7 @@ func TestClient_DownloadBill(t *testing.T) { func TestClient_DownloadFundFlow(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("sign_type", SignType_HMAC_SHA256). Set("bill_date", "20190122"). Set("account_type", "Basic") @@ -118,7 +118,7 @@ func TestClient_DownloadFundFlow(t *testing.T) { func TestClient_BatchQueryComment(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("sign_type", SignType_HMAC_SHA256). Set("begin_time", "20190120000000"). Set("end_time", "20190122174000"). diff --git a/wechat/entrust_test.go b/wechat/entrust_test.go index 2659a24e..a612ec90 100644 --- a/wechat/entrust_test.go +++ b/wechat/entrust_test.go @@ -71,14 +71,14 @@ func TestClient_EntrustH5(t *testing.T) { } func TestClient_EntrustPaying(t *testing.T) { - number := util.GetRandomString(32) + number := util.RandomString(32) xlog.Info("out_trade_no:", number) // 初始化参数结构体 bm := make(gopay.BodyMap) bm.Set("contract_mchid", mchId). Set("contract_appid", appId). Set("out_trade_no", number). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("body", "测试签约"). Set("total_fee", 1). Set("spbill_create_ip", "127.0.0.1"). diff --git a/wechat/merchant_test.go b/wechat/merchant_test.go index b91011fe..943c20b8 100644 --- a/wechat/merchant_test.go +++ b/wechat/merchant_test.go @@ -15,8 +15,8 @@ import ( func TestClient_Transfer(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). - Set("partner_trade_no", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). + Set("partner_trade_no", util.RandomString(32)). Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8"). Set("check_name", "FORCE_CHECK"). // NO_CHECK:不校验真实姓名 , FORCE_CHECK:强校验真实姓名 Set("re_user_name", "付明明"). // 收款用户真实姓名。 如果check_name设置为FORCE_CHECK,则必填用户真实姓名 @@ -44,7 +44,7 @@ func Test_ProfitSharing(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("transaction_id", "4208450740201411110007820472"). Set("out_order_no", "P20150806125346") @@ -78,7 +78,7 @@ func Test_ProfitSharing(t *testing.T) { func Test_ProfitSharingAddReceiver(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)) + bm.Set("nonce_str", util.RandomString(32)) receiver := make(gopay.BodyMap) receiver.Set("type", "MERCHANT_ID"). @@ -99,7 +99,7 @@ func Test_ProfitSharingAddReceiver(t *testing.T) { func Test_ProfitSharingRemoveReceiver(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)) + bm.Set("nonce_str", util.RandomString(32)) receiver := make(gopay.BodyMap) receiver.Set("type", "MERCHANT_ID"). @@ -117,7 +117,7 @@ func Test_ProfitSharingRemoveReceiver(t *testing.T) { func TestClient_GetRSAPublicKey(t *testing.T) { bm := make(gopay.BodyMap) - bm.Set("nonce_str", util.GetRandomString(32)). + bm.Set("nonce_str", util.RandomString(32)). Set("sign_type", SignType_MD5) publicKey, err := client.GetRSAPublicKey(ctx, bm) if err != nil { @@ -131,7 +131,7 @@ func TestClient_PayBank(t *testing.T) { // 初始化参数结构体 bm := make(gopay.BodyMap) bm.Set("partner_trade_no", mchId). - Set("nonce_str", util.GetRandomString(32)). + Set("nonce_str", util.RandomString(32)). Set("bank_code", "1001"). // 招商银行,https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_4&index=5 Set("amount", 1) diff --git a/wechat/param.go b/wechat/param.go index 4e1cb231..7f8a61aa 100644 --- a/wechat/param.go +++ b/wechat/param.go @@ -207,7 +207,7 @@ func GetSandBoxSign(ctx context.Context, mchId, apiKey string, bm gopay.BodyMap) sandBoxApiKey string h hash.Hash ) - if sandBoxApiKey, err = getSanBoxKey(ctx, mchId, util.GetRandomString(32), apiKey, SignType_MD5); err != nil { + if sandBoxApiKey, err = getSanBoxKey(ctx, mchId, util.RandomString(32), apiKey, SignType_MD5); err != nil { return } h = md5.New() diff --git a/wechat/sign.go b/wechat/sign.go index 2ef847b0..2d150f32 100644 --- a/wechat/sign.go +++ b/wechat/sign.go @@ -205,7 +205,7 @@ func GetSanBoxParamSign(ctx context.Context, appId, mchId, apiKey string, bm gop sandBoxApiKey string hashMd5 hash.Hash ) - if sandBoxApiKey, err = getSanBoxKey(ctx, mchId, util.GetRandomString(32), apiKey, SignType_MD5); err != nil { + if sandBoxApiKey, err = getSanBoxKey(ctx, mchId, util.RandomString(32), apiKey, SignType_MD5); err != nil { return } hashMd5 = md5.New() diff --git a/wechat/v3/cert.go b/wechat/v3/cert.go index 82be3a35..8e73b6d5 100644 --- a/wechat/v3/cert.go +++ b/wechat/v3/cert.go @@ -45,7 +45,7 @@ func GetPlatformCerts(ctx context.Context, mchid, apiV3Key, serialNo, privateKey } timestamp := time.Now().Unix() - nonceStr := util.GetRandomString(32) + nonceStr := util.RandomString(32) ts := util.Int642String(timestamp) _str := MethodGet + "\n" + v3GetCerts + "\n" + ts + "\n" + nonceStr + "\n" + jb + "\n" // Sign @@ -62,7 +62,7 @@ func GetPlatformCerts(ctx context.Context, mchid, apiV3Key, serialNo, privateKey var url = v3BaseUrlCh + v3GetCerts httpClient := xhttp.NewClient() httpClient.Header.Add(HeaderAuthorization, authorization) - httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.GetRandomString(21), time.Now().Unix())) + httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix())) httpClient.Header.Add(HeaderSerial, serialNo) httpClient.Header.Add("Accept", "*/*") res, bs, err := httpClient.Type(xhttp.TypeJSON).Get(url).EndBytes(ctx) diff --git a/wechat/v3/client.go b/wechat/v3/client.go index 1c6812be..ad46d9fc 100644 --- a/wechat/v3/client.go +++ b/wechat/v3/client.go @@ -45,7 +45,6 @@ func NewClientV3(mchid, serialNo, apiV3Key, privateKey string) (client *ClientV3 ctx: context.Background(), DebugSwitch: gopay.DebugOff, } - // 自动获取 return client, nil } @@ -79,7 +78,7 @@ func (c *ClientV3) doProdPostWithHeader(ctx context.Context, headerMap map[strin httpClient.Header.Add(k, v) } httpClient.Header.Add(HeaderAuthorization, authorization) - httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.GetRandomString(21), time.Now().Unix())) + httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix())) httpClient.Header.Add(HeaderSerial, c.WxSerialNo) httpClient.Header.Add("Accept", "*/*") res, bs, err = httpClient.Type(xhttp.TypeJSON).Post(url).SendBodyMap(bm).EndBytes(ctx) @@ -109,7 +108,7 @@ func (c *ClientV3) doProdPost(ctx context.Context, bm gopay.BodyMap, path, autho xlog.Debugf("Wechat_V3_Authorization: %s", authorization) } httpClient.Header.Add(HeaderAuthorization, authorization) - httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.GetRandomString(21), time.Now().Unix())) + httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix())) httpClient.Header.Add(HeaderSerial, c.WxSerialNo) httpClient.Header.Add("Accept", "*/*") res, bs, err = httpClient.Type(xhttp.TypeJSON).Post(url).SendBodyMap(bm).EndBytes(ctx) @@ -139,7 +138,7 @@ func (c *ClientV3) doProdGet(ctx context.Context, uri, authorization string) (re xlog.Debugf("Wechat_V3_Authorization: %s", authorization) } httpClient.Header.Add(HeaderAuthorization, authorization) - httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.GetRandomString(21), time.Now().Unix())) + httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix())) httpClient.Header.Add(HeaderSerial, c.WxSerialNo) httpClient.Header.Add("Accept", "*/*") res, bs, err = httpClient.Type(xhttp.TypeJSON).Get(url).EndBytes(ctx) @@ -169,7 +168,7 @@ func (c *ClientV3) doProdPut(ctx context.Context, bm gopay.BodyMap, path, author xlog.Debugf("Wechat_V3_Authorization: %s", authorization) } httpClient.Header.Add(HeaderAuthorization, authorization) - httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.GetRandomString(21), time.Now().Unix())) + httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix())) httpClient.Header.Add(HeaderSerial, c.WxSerialNo) httpClient.Header.Add("Accept", "*/*") res, bs, err = httpClient.Type(xhttp.TypeJSON).Put(url).SendBodyMap(bm).EndBytes(ctx) @@ -199,7 +198,7 @@ func (c *ClientV3) doProdDelete(ctx context.Context, bm gopay.BodyMap, path, aut xlog.Debugf("Wechat_V3_Authorization: %s", authorization) } httpClient.Header.Add(HeaderAuthorization, authorization) - httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.GetRandomString(21), time.Now().Unix())) + httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix())) httpClient.Header.Add(HeaderSerial, c.WxSerialNo) httpClient.Header.Add("Accept", "*/*") res, bs, err = httpClient.Type(xhttp.TypeJSON).Delete(url).SendBodyMap(bm).EndBytes(ctx) @@ -229,7 +228,7 @@ func (c *ClientV3) doProdPostFile(ctx context.Context, bm gopay.BodyMap, path, a xlog.Debugf("Wechat_V3_Authorization: %s", authorization) } httpClient.Header.Add(HeaderAuthorization, authorization) - httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.GetRandomString(21), time.Now().Unix())) + httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix())) httpClient.Header.Add(HeaderSerial, c.WxSerialNo) httpClient.Header.Add("Accept", "*/*") res, bs, err = httpClient.Type(xhttp.TypeMultipartFormData).Post(url).SendMultipartBodyMap(bm).EndBytes(ctx) @@ -259,7 +258,7 @@ func (c *ClientV3) doProdPatch(ctx context.Context, bm gopay.BodyMap, path, auth xlog.Debugf("Wechat_V3_Authorization: %s", authorization) } httpClient.Header.Add(HeaderAuthorization, authorization) - httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.GetRandomString(21), time.Now().Unix())) + httpClient.Header.Add(HeaderRequestID, fmt.Sprintf("%s-%d", util.RandomString(21), time.Now().Unix())) httpClient.Header.Add(HeaderSerial, c.WxSerialNo) httpClient.Header.Add("Accept", "*/*") res, bs, err = httpClient.Type(xhttp.TypeJSON).Patch(url).SendBodyMap(bm).EndBytes(ctx) diff --git a/wechat/v3/client_test.go b/wechat/v3/client_test.go index 53d18343..6844c9a2 100644 --- a/wechat/v3/client_test.go +++ b/wechat/v3/client_test.go @@ -129,7 +129,7 @@ func TestV3VerifySignByPK(t *testing.T) { } func TestV3Jsapi(t *testing.T) { - tradeNo := util.GetRandomString(32) + tradeNo := util.RandomString(32) xlog.Debug("tradeNo:", tradeNo) expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339) @@ -168,7 +168,7 @@ func TestV3Jsapi(t *testing.T) { } func TestV3Native(t *testing.T) { - tradeNo := util.GetRandomString(32) + tradeNo := util.RandomString(32) xlog.Debug("tradeNo:", tradeNo) expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339) @@ -196,7 +196,7 @@ func TestV3Native(t *testing.T) { } func TestV3PartnerNative(t *testing.T) { - tradeNo := util.GetRandomString(32) + tradeNo := util.RandomString(32) xlog.Debug("tradeNo:", tradeNo) expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339) @@ -227,7 +227,7 @@ func TestV3PartnerNative(t *testing.T) { } func TestV3TransactionH5(t *testing.T) { - number := util.GetRandomString(32) + number := util.RandomString(32) xlog.Info("out_trade_no:", number) // 初始化参数Map bm := make(gopay.BodyMap) diff --git a/wechat/v3/sign.go b/wechat/v3/sign.go index a69bfd49..62dd3137 100644 --- a/wechat/v3/sign.go +++ b/wechat/v3/sign.go @@ -52,7 +52,7 @@ func V3VerifySignByPK(timestamp, nonce, signBody, sign string, wxPublicKey *rsa. // 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_4.shtml func (c *ClientV3) PaySignOfJSAPI(appid, prepayid string) (jsapi *JSAPIPayParams, err error) { ts := util.Int642String(time.Now().Unix()) - nonceStr := util.GetRandomString(32) + nonceStr := util.RandomString(32) pkg := "prepay_id=" + prepayid _str := appid + "\n" + ts + "\n" + nonceStr + "\n" + pkg + "\n" @@ -76,7 +76,7 @@ func (c *ClientV3) PaySignOfJSAPI(appid, prepayid string) (jsapi *JSAPIPayParams // 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_4.shtml func (c *ClientV3) PaySignOfApp(appid, prepayid string) (app *AppPayParams, err error) { ts := util.Int642String(time.Now().Unix()) - nonceStr := util.GetRandomString(32) + nonceStr := util.RandomString(32) _str := appid + "\n" + ts + "\n" + nonceStr + "\n" + prepayid + "\n" sign, err := c.rsaSign(_str) @@ -119,7 +119,7 @@ func (c *ClientV3) authorization(method, path string, bm gopay.BodyMap) (string, var ( jb = "" timestamp = time.Now().Unix() - nonceStr = util.GetRandomString(32) + nonceStr = util.RandomString(32) ) if bm != nil { jb = bm.JsonBody()