mirror of
https://github.com/plutov/paypal.git
synced 2025-01-22 18:01:04 +01:00
Add verify webhook signature response (#117)
This commit is contained in:
parent
8e3811377b
commit
cd72fd3aec
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
.idea
|
||||
.vscode
|
|
@ -1,6 +1,6 @@
|
|||
package paypal_test
|
||||
|
||||
import paypal "github.com/plutov/paypal/v3"
|
||||
import "github.com/plutov/paypal/v3"
|
||||
|
||||
func Example() {
|
||||
// Initialize client
|
||||
|
|
4
types.go
4
types.go
|
@ -852,6 +852,10 @@ type (
|
|||
BankTXNPendingURL string `json:"bank_txn_pending_url,omitempty"`
|
||||
UserAction string `json:"user_action,omitempty"`
|
||||
}
|
||||
|
||||
VerifyWebhookResponse struct {
|
||||
VerificationStatus string `json:"verification_status,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
// Error method implementation for ErrorResponse struct
|
||||
|
|
54
webhooks.go
Normal file
54
webhooks.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package paypal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// VerifyWebhookSignature - Use this to verify the signature of a webhook recieved from paypal.
|
||||
// Endpoint: POST /v1/notifications/verify-webhook-signature
|
||||
func (c *Client) VerifyWebhookSignature(httpReq *http.Request, webhookID string) (*VerifyWebhookResponse, error) {
|
||||
type verifyWebhookSignatureRequest struct {
|
||||
AuthAlgo string `json:"auth_algo,omitempty"`
|
||||
CertURL string `json:"cert_url,omitempty"`
|
||||
TransmissionID string `json:"transmission_id,omitempty"`
|
||||
TransmissionSig string `json:"transmission_sig,omitempty"`
|
||||
TransmissionTime string `json:"transmission_time,omitempty"`
|
||||
WebhookID string `json:"webhook_id,omitempty"`
|
||||
WebhookEvent json.RawMessage `json:"webhook_event"`
|
||||
}
|
||||
getBody := httpReq.GetBody
|
||||
bodyReadCloser, err := getBody()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body, err := ioutil.ReadAll(bodyReadCloser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
verifyRequest := verifyWebhookSignatureRequest{
|
||||
AuthAlgo: httpReq.Header.Get("PAYPAL-AUTH-ALGO"),
|
||||
CertURL: httpReq.Header.Get("PAYPAL-CERT-URL"),
|
||||
TransmissionID: httpReq.Header.Get("PAYPAL-TRANSMISSION-ID"),
|
||||
TransmissionSig: httpReq.Header.Get("PAYPAL-TRANSMISSION-SIG"),
|
||||
TransmissionTime: httpReq.Header.Get("PAYPAL-TRANSMISSION-TIME"),
|
||||
WebhookID: webhookID,
|
||||
WebhookEvent: json.RawMessage(body),
|
||||
}
|
||||
|
||||
response := &VerifyWebhookResponse{}
|
||||
|
||||
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/notifications/verify-webhook-signature"), verifyRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = c.SendWithAuth(req, response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user