Reset after reading (#118)

This commit is contained in:
Roopak Venkatakrishnan 2019-09-24 07:51:55 -07:00 committed by GitHub
parent cd72fd3aec
commit 06a298ef76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package paypal
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
@ -19,15 +20,14 @@ func (c *Client) VerifyWebhookSignature(httpReq *http.Request, webhookID string)
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
// Read the content
var bodyBytes []byte
if httpReq.Body != nil {
bodyBytes, _ = ioutil.ReadAll(httpReq.Body)
}
// Restore the io.ReadCloser to its original state
httpReq.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
verifyRequest := verifyWebhookSignatureRequest{
AuthAlgo: httpReq.Header.Get("PAYPAL-AUTH-ALGO"),
@ -36,7 +36,7 @@ func (c *Client) VerifyWebhookSignature(httpReq *http.Request, webhookID string)
TransmissionSig: httpReq.Header.Get("PAYPAL-TRANSMISSION-SIG"),
TransmissionTime: httpReq.Header.Get("PAYPAL-TRANSMISSION-TIME"),
WebhookID: webhookID,
WebhookEvent: json.RawMessage(body),
WebhookEvent: json.RawMessage(bodyBytes),
}
response := &VerifyWebhookResponse{}