Skip to content

Commit cd72fd3

Browse files
authored
Add verify webhook signature response (#117)
1 parent 8e38113 commit cd72fd3

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
.vscode

example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package paypal_test
22

3-
import paypal "github.com/plutov/paypal/v3"
3+
import "github.com/plutov/paypal/v3"
44

55
func Example() {
66
// Initialize client

types.go

+4
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,10 @@ type (
852852
BankTXNPendingURL string `json:"bank_txn_pending_url,omitempty"`
853853
UserAction string `json:"user_action,omitempty"`
854854
}
855+
856+
VerifyWebhookResponse struct {
857+
VerificationStatus string `json:"verification_status,omitempty"`
858+
}
855859
)
856860

857861
// Error method implementation for ErrorResponse struct

webhooks.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)