钱包结算所有, 第三方充值到钱包
This commit is contained in:
@@ -72,6 +72,14 @@ type QueryPaymentResponse struct {
|
||||
Raw map[string]string
|
||||
}
|
||||
|
||||
type VerifyNotifyResult struct {
|
||||
OK bool
|
||||
MatchedKey string
|
||||
Got string
|
||||
Expected map[string]string
|
||||
ParamKeys []string
|
||||
}
|
||||
|
||||
func NewClient(cfg config.LeshuaPaymentConfig) *Client {
|
||||
return &Client{
|
||||
cfg: cfg,
|
||||
@@ -171,19 +179,60 @@ func (c *Client) QueryPayment(ctx context.Context, thirdOrderID, providerOrderID
|
||||
}
|
||||
|
||||
func (c *Client) VerifyNotify(params map[string]string) bool {
|
||||
key := firstNonEmpty(c.cfg.NotifyKey, c.cfg.SignKey)
|
||||
if key == "" {
|
||||
return false
|
||||
}
|
||||
return c.VerifyNotifyDetail(params).OK
|
||||
}
|
||||
|
||||
func (c *Client) VerifyNotifyDetail(params map[string]string) VerifyNotifyResult {
|
||||
got := strings.ToUpper(params["sign"])
|
||||
if got == "" {
|
||||
return false
|
||||
result := VerifyNotifyResult{
|
||||
Got: got,
|
||||
Expected: map[string]string{},
|
||||
ParamKeys: notifyParamKeys(params),
|
||||
}
|
||||
expected := Sign(params, key, SignOptions{
|
||||
IncludeEmpty: true,
|
||||
ExcludeKeys: []string{"error_code", "sign"},
|
||||
})
|
||||
return got == expected
|
||||
if got == "" {
|
||||
return result
|
||||
}
|
||||
for _, item := range c.notifyKeyCandidates() {
|
||||
expected := Sign(params, item.key, SignOptions{
|
||||
IncludeEmpty: true,
|
||||
ExcludeKeys: []string{"error_code", "sign"},
|
||||
})
|
||||
result.Expected[item.name] = expected
|
||||
if got == expected {
|
||||
result.OK = true
|
||||
result.MatchedKey = item.name
|
||||
return result
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type notifyKeyCandidate struct {
|
||||
name string
|
||||
key string
|
||||
}
|
||||
|
||||
func (c *Client) notifyKeyCandidates() []notifyKeyCandidate {
|
||||
candidates := []notifyKeyCandidate{}
|
||||
if c.cfg.NotifyKey != "" {
|
||||
candidates = append(candidates, notifyKeyCandidate{name: "notify_key", key: c.cfg.NotifyKey})
|
||||
}
|
||||
if c.cfg.SignKey != "" && c.cfg.SignKey != c.cfg.NotifyKey {
|
||||
candidates = append(candidates, notifyKeyCandidate{name: "sign_key", key: c.cfg.SignKey})
|
||||
}
|
||||
return candidates
|
||||
}
|
||||
|
||||
func notifyParamKeys(params map[string]string) []string {
|
||||
keys := make([]string, 0, len(params))
|
||||
for key := range params {
|
||||
if key == "sign" || key == "error_code" {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
func (c *Client) validate() error {
|
||||
|
||||
Reference in New Issue
Block a user