-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
50 lines (42 loc) · 1.21 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cdek_pay
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
)
type Webhook struct {
Payment PaymentEvent `json:"payment"`
Signature string `json:"signature"`
}
type PaymentEvent struct {
Amount int `json:"pay_amount"`
AccessKey string `json:"access_key"`
Currency string `json:"currency"`
Id int `json:"id"`
OrderId int `json:"order_id"`
}
func (n *Webhook) GetValuesForSignature() map[string]interface{} {
return FlattenStructToMap(n.Payment, "")
}
func (c *Client) ParseWebhook(requestBody io.Reader) (*Webhook, error) {
bytes, err := ioutil.ReadAll(requestBody)
if err != nil {
return nil, err
}
var webhook Webhook
err = json.Unmarshal(bytes, &webhook)
if err != nil {
return nil, err
}
valuesForSignature := webhook.GetValuesForSignature()
signature := generateSignature(valuesForSignature, c.secretKey)
if signature != webhook.Signature {
valsForTokenJSON, _ := json.Marshal(valuesForSignature)
return nil, fmt.Errorf("invalid signature: expected %s got %s.\nValues for signature: %s.\nWebhook: %s", signature, webhook.Signature, valsForTokenJSON, string(bytes))
}
return &webhook, nil
}
func (c *Client) GetWebhookSuccessResponse() string {
return "OK"
}