|
| 1 | +package paypal |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +// VerifyWebhookSignature - Use this to verify the signature of a webhook recieved from paypal. |
| 11 | +// Endpoint: POST /v1/notifications/verify-webhook-signature |
| 12 | +func (c *Client) VerifyWebhookSignature(httpReq *http.Request, webhookID string) (*VerifyWebhookResponse, error) { |
| 13 | + type verifyWebhookSignatureRequest struct { |
| 14 | + AuthAlgo string `json:"auth_algo,omitempty"` |
| 15 | + CertURL string `json:"cert_url,omitempty"` |
| 16 | + TransmissionID string `json:"transmission_id,omitempty"` |
| 17 | + TransmissionSig string `json:"transmission_sig,omitempty"` |
| 18 | + TransmissionTime string `json:"transmission_time,omitempty"` |
| 19 | + WebhookID string `json:"webhook_id,omitempty"` |
| 20 | + WebhookEvent json.RawMessage `json:"webhook_event"` |
| 21 | + } |
| 22 | + getBody := httpReq.GetBody |
| 23 | + bodyReadCloser, err := getBody() |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + body, err := ioutil.ReadAll(bodyReadCloser) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + verifyRequest := verifyWebhookSignatureRequest{ |
| 33 | + AuthAlgo: httpReq.Header.Get("PAYPAL-AUTH-ALGO"), |
| 34 | + CertURL: httpReq.Header.Get("PAYPAL-CERT-URL"), |
| 35 | + TransmissionID: httpReq.Header.Get("PAYPAL-TRANSMISSION-ID"), |
| 36 | + TransmissionSig: httpReq.Header.Get("PAYPAL-TRANSMISSION-SIG"), |
| 37 | + TransmissionTime: httpReq.Header.Get("PAYPAL-TRANSMISSION-TIME"), |
| 38 | + WebhookID: webhookID, |
| 39 | + WebhookEvent: json.RawMessage(body), |
| 40 | + } |
| 41 | + |
| 42 | + response := &VerifyWebhookResponse{} |
| 43 | + |
| 44 | + req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/notifications/verify-webhook-signature"), verifyRequest) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + if err = c.SendWithAuth(req, response); err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + return response, nil |
| 54 | +} |
0 commit comments