-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(miniprogram): add customer service, risk control, service markert struct (#60) * refactor(mini-program): update all request params struct (#62) * fix(payment): add tls http client * refactor(payment): 修改requestSendRedPack 结构体增加json tag * fix(payment): add tls http client * fix(payment): update v2 md5 sign * feat(payment): update Wxappid to WxappID * refactor(go.mod): remove local replace Co-authored-by: Walle <[email protected]> Co-authored-by: Matrix-X <[email protected]> Co-authored-by: Alvin <[email protected]>
- Loading branch information
1 parent
e9eef5a
commit 6c18ad6
Showing
44 changed files
with
606 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,43 @@ | ||
package support | ||
|
||
import ( | ||
"context" | ||
"crypto/hmac" | ||
"crypto/md5" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"github.com/ArtisanCloud/PowerLibs/object" | ||
"time" | ||
"github.com/ArtisanCloud/PowerWeChat/src/kernel/power" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type GenerateSigner struct { | ||
Method string // 接口提交方法。"GET", "POST"等 | ||
CanonicalURL string // 微信支付接口路径。 例如: /v3/pay/transactions/jsapi | ||
SignBody string // 提交的body字符串。 例如; {"amount":{"total":1},"appid":"ww16143ea0101327c7","attach":"自定义数据说明","description":"Image形象店-深圳腾大-QQ公仔","mchid":"1611854986","notify_url":"https://pay.wangchaoyi.com/wx/notify","out_trade_no":"5519778939773395659222199361","payer":{"openid":"oAuaP0TRUMwP169nQfg7XCEAw3HQ"}} | ||
timestamp int64 // 单元测试传入的固定时间戳 | ||
nonce string // 单元测试传入的固定随机数 | ||
} | ||
|
||
func GenerateSign(signer *SHA256WithRSASigner, gs GenerateSigner) (authorization string, err error) { | ||
|
||
timestamp := time.Now().Unix() | ||
nonce := object.QuickRandom(32) | ||
|
||
// Under ci mode, go fixed value | ||
// 在ci模式下面,走固定值 | ||
if gs.timestamp != 0 && gs.nonce != "" { | ||
timestamp = gs.timestamp | ||
nonce = gs.nonce | ||
func PaymentV2ParamsJoin(params *power.StringMap, key string) string { | ||
var arr []string | ||
for k, v := range *params { | ||
if v == "" { | ||
continue | ||
} | ||
arr = append(arr, k) | ||
} | ||
|
||
// Splice the string to be signed | ||
// 拼接出需要签名的字符串 | ||
message := fmt.Sprintf(SignatureMessageFormat, gs.Method, gs.CanonicalURL, fmt.Sprintf("%d", timestamp), nonce, gs.SignBody) | ||
|
||
signatureResult, err := signer.Sign(context.TODO(), message) | ||
if err != nil { | ||
return "", err | ||
sort.Strings(arr) | ||
for i, k := range arr { | ||
arr[i] = fmt.Sprintf("%s=%s", k, (*params)[k]) | ||
} | ||
return fmt.Sprintf("%s&key=%s", strings.Join(arr, "&"), key) | ||
} | ||
|
||
authorization = fmt.Sprintf( | ||
HeaderAuthorizationFormat, | ||
signer.GetAuthorizationType(), | ||
signatureResult.MchID, | ||
nonce, | ||
timestamp, | ||
signatureResult.CertificateSerialNo, | ||
signatureResult.Signature, | ||
) | ||
|
||
return authorization, err | ||
// GenerateSignMD5 适用于微信支付V2 MD5签名算法 | ||
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3 | ||
func GenerateSignMD5(params *power.StringMap, key string) string { | ||
sign := fmt.Sprintf("%x", md5.Sum([]byte(PaymentV2ParamsJoin(params, key)))) | ||
return strings.ToUpper(sign) | ||
} | ||
|
||
func GetEncryptMethod(signType string, secretKey string) string { | ||
return "" | ||
// GenerateSignHmacSHA256 适用于微信支付V2 HMAC-SHA256签名算法 | ||
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3 | ||
func GenerateSignHmacSHA256(params *power.StringMap, key string) string { | ||
h := hmac.New(sha256.New, []byte(key)) | ||
h.Write([]byte(PaymentV2ParamsJoin(params, key))) | ||
|
||
sign := hex.EncodeToString(h.Sum(nil)) | ||
return strings.ToUpper(sign) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,77 @@ | ||
package support | ||
|
||
import ( | ||
"github.com/ArtisanCloud/PowerWeChat/src/kernel/power" | ||
"github.com/go-playground/assert/v2" | ||
"log" | ||
"testing" | ||
) | ||
|
||
// Authorization: WECHATPAY2-SHA256-RSA2048 mchid="1611854986",nonce_str="sacfXg6R9YwKqo3sfPoOVJQd4jPb0KOe",timestamp="1626623583",serial_no="2655A2CD634B06C2A86B28780228A997D047B01C",signature="dvB2r+z5v8JOsKw0goAmXNNTdLtpwmCkZHzuVdAi63kBZIvFugQuYx1nCUiZQckIV7ebq0JkK5/3N2gkmX70RJSnEa/Rjq7n2K//0OahXPGI+2qgFr5qUZ586en66QZjuQVeqoW6aYsaAwHPnszva56uJmopvHnuPPdUzKTTWf8sDNdIph/y+BpDXGTIvgifYR3RnJc2qh5n9eOo1Tqr4Ei6y6HhdPhMWMrr9RXY4bOCjtDkZhQ+mUXEP6aHLPau+5Th2cGlb5dyUY3o/MzgfjvvXjv4JDXhHFuo9BZAwp4XQcs/6jh/XAakf9lHx7ESvoQyT406Sfn30An3Y+p4wg==" | ||
func TestGetEncryptMethod(t *testing.T) { | ||
signer := &SHA256WithRSASigner{ | ||
MchID: "161186666", | ||
CertificateSerialNo: "2355A2CD634B06C2A86B28780228A997D017B011", | ||
PrivateKey: getPrivateKey(), | ||
} | ||
//func TestGetEncryptMethod(t *testing.T) { | ||
// signer := &SHA256WithRSASigner{ | ||
// MchID: "161186666", | ||
// CertificateSerialNo: "2355A2CD634B06C2A86B28780228A997D017B011", | ||
// PrivateKey: getPrivateKey(), | ||
// } | ||
// | ||
// signBody := "{\"amount\":{\"total\":1},\"appid\":\"ww16143ea0101327c7\",\"attach\":\"自定义数据说明\",\"description\":\"Image形象店-深圳腾大-QQ公仔\",\"mchid\":\"1611854986\",\"notify_url\":\"https://pay.wangchaoyi.com/power/notify\",\"out_trade_no\":\"5519778939773395659222199361\",\"payer\":{\"openid\":\"oAuaP0TRUMwP169nQfg7XCEAw3HQ\"}}" | ||
// | ||
// authorization, err := GenerateSign(signer, GenerateSigner{ | ||
// Method: "POST", | ||
// CanonicalURL: "/v3/pay/transactions/jsapi", | ||
// SignBody: signBody, | ||
// timestamp: 1626747079, | ||
// nonce: "W2XLk2c8KYM1aRNBzwmeGBnVqZ3QbvHS", | ||
// }) | ||
// | ||
// if err != nil { | ||
// log.Fatalln(err) | ||
// } | ||
// | ||
// expectAuthorization := "WECHATPAY2-SHA256-RSA2048 mchid=\"161186666\",nonce_str=\"W2XLk2c8KYM1aRNBzwmeGBnVqZ3QbvHS\",timestamp=\"1626747079\",serial_no=\"2355A2CD634B06C2A86B28780228A997D017B011\",signature=\"eUF/u5p9UjRtflGwOIk5jXUzD2Aduaj/NLhlwWwkliFPpF2I9jtTtM7gARrMHYuX2tQNS6OfY5Jf350D6OsJ4YKaKK4C8HOQ62maQ90DASJUrcqRI/EA4uyCrkqbUWnl+Xm2dE5wuVpfTSbRaMzOdXQwFB376uZgfUQtnD8C5PUfyqJ07qjxvh6NGi+R1vNCyG2rHhWityYtd66CZX4lBTOG5bJocn4GpOZOnyJO5/paQVQ8rKmQn+Wm7XCSCFL+4QLIa7ATyra/JMy0SLswq8ORfjCV0wFsHNR3h0u0vJo9JFRcqhhr/L6uQRc5x0vAC/wciOiDejAWYWBY90LEnA==\"" | ||
// | ||
// assert.Equal(t, authorization, expectAuthorization) | ||
//} | ||
|
||
signBody := "{\"amount\":{\"total\":1},\"appid\":\"ww16143ea0101327c7\",\"attach\":\"自定义数据说明\",\"description\":\"Image形象店-深圳腾大-QQ公仔\",\"mchid\":\"1611854986\",\"notify_url\":\"https://pay.wangchaoyi.com/power/notify\",\"out_trade_no\":\"5519778939773395659222199361\",\"payer\":{\"openid\":\"oAuaP0TRUMwP169nQfg7XCEAw3HQ\"}}" | ||
func TestPaymentV2ParamsJoin(t *testing.T) { | ||
params := &power.StringMap{ | ||
"appid": "f323", | ||
"c": "12", | ||
"d": "34", | ||
"mch_id": "2323532", | ||
} | ||
key := "HelloPowerWeChat" | ||
expectText := "appid=f323&c=12&d=34&mch_id=2323532&key=HelloPowerWeChat" | ||
text := PaymentV2ParamsJoin(params, key) | ||
|
||
authorization, err := GenerateSign(signer, GenerateSigner{ | ||
Method: "POST", | ||
CanonicalURL: "/v3/pay/transactions/jsapi", | ||
SignBody: signBody, | ||
timestamp: 1626747079, | ||
nonce: "W2XLk2c8KYM1aRNBzwmeGBnVqZ3QbvHS", | ||
}) | ||
assert.Equal(t, expectText, text) | ||
} | ||
|
||
if err != nil { | ||
log.Fatalln(err) | ||
func TestGenerateSignMD5(t *testing.T) { | ||
params := &power.StringMap{ | ||
"appid": "f323", | ||
"c": "12", | ||
"d": "34", | ||
"mch_id": "2323532", | ||
} | ||
key := "HelloPowerWeChat" | ||
expectSignMD5 := "D18B5A9F4D01EB18CDEFFA39C78EB0F5" | ||
signMD5 := GenerateSignMD5(params, key) | ||
|
||
expectAuthorization := "WECHATPAY2-SHA256-RSA2048 mchid=\"161186666\",nonce_str=\"W2XLk2c8KYM1aRNBzwmeGBnVqZ3QbvHS\",timestamp=\"1626747079\",serial_no=\"2355A2CD634B06C2A86B28780228A997D017B011\",signature=\"eUF/u5p9UjRtflGwOIk5jXUzD2Aduaj/NLhlwWwkliFPpF2I9jtTtM7gARrMHYuX2tQNS6OfY5Jf350D6OsJ4YKaKK4C8HOQ62maQ90DASJUrcqRI/EA4uyCrkqbUWnl+Xm2dE5wuVpfTSbRaMzOdXQwFB376uZgfUQtnD8C5PUfyqJ07qjxvh6NGi+R1vNCyG2rHhWityYtd66CZX4lBTOG5bJocn4GpOZOnyJO5/paQVQ8rKmQn+Wm7XCSCFL+4QLIa7ATyra/JMy0SLswq8ORfjCV0wFsHNR3h0u0vJo9JFRcqhhr/L6uQRc5x0vAC/wciOiDejAWYWBY90LEnA==\"" | ||
assert.Equal(t, expectSignMD5, signMD5) | ||
} | ||
|
||
assert.Equal(t, authorization, expectAuthorization) | ||
func TestGenerateSignHmacSHA256(t *testing.T) { | ||
params := &power.StringMap{ | ||
"appid": "f323", | ||
"c": "12", | ||
"d": "34", | ||
"mch_id": "2323532", | ||
} | ||
key := "HelloPowerWeChat" | ||
expectSignHmacSHA256 := "CF3C3C7B038A12682967DC5ABADDAD56CE612FEE9B0E0A885B3B41E9E72B9A10" | ||
signHmacSHA256 := GenerateSignHmacSHA256(params, key) | ||
|
||
assert.Equal(t, expectSignHmacSHA256, signHmacSHA256) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package request | ||
|
||
type RequestGetPaidUnionID struct { | ||
OpenID string `json:"openid"` // 支付用户唯一标识 | ||
TransactionID string `json:"transaction_id,omitempty"` // 微信支付订单号 | ||
MchID string `json:"mch_id,omitempty"` // 微信支付分配的商户号,和商户订单号配合使用 | ||
OutTradeNo string `json:"out_trade_no,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/miniProgram/customerServiceMessage/request/customerServiceMessage.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package request | ||
|
||
type CustomerServiceMsgText struct { | ||
Content string `json:"content"` | ||
} | ||
|
||
type CustomerServiceMsgImage struct { | ||
MediaID string `json:"media_id"` | ||
} | ||
|
||
type CustomerServiceMsgLink struct { | ||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
Url string `json:"url"` | ||
ThumbUrl string `json:"thumb_url"` | ||
} | ||
|
||
type CustomerServiceMsgMpPage struct { | ||
Title string `json:"title"` | ||
PagePath string `json:"pagepath"` | ||
ThumbMediaID string `json:"thumb_media_id"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package request | ||
|
||
type RequestGetPerformanceData struct { | ||
Time *GetPerformanceDataTime `json:"time"` | ||
Module string `json:"module"` | ||
Params []*GetPerformanceDataParams `json:"params"` | ||
} | ||
|
||
type GetPerformanceDataTime struct { | ||
// int64类型: time.Now().Unix() | ||
BeginTimestamp int64 `json:"begin_timestamp"` | ||
EndTimestamp int64 `json:"end_timestamp"` | ||
} | ||
|
||
type GetPerformanceDataParams struct { | ||
Field string `json:"field"` | ||
Value string `json:"value"` | ||
} |
Oops, something went wrong.