mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 10:21:03 +01:00
Refactor/code improvements (#247)
* Code improvements * Revert log validation improvement
This commit is contained in:
parent
defe3e09a4
commit
f0575ee562
|
@ -72,7 +72,7 @@ func (c *Client) CreateBillingPlan(ctx context.Context, plan BillingPlan) (*Crea
|
||||||
// UpdateBillingPlan updates values inside a billing plan
|
// UpdateBillingPlan updates values inside a billing plan
|
||||||
// Endpoint: PATCH /v1/payments/billing-plans
|
// Endpoint: PATCH /v1/payments/billing-plans
|
||||||
func (c *Client) UpdateBillingPlan(ctx context.Context, planId string, pathValues map[string]map[string]interface{}) error {
|
func (c *Client) UpdateBillingPlan(ctx context.Context, planId string, pathValues map[string]map[string]interface{}) error {
|
||||||
patchData := []Patch{}
|
var patchData []Patch
|
||||||
for path, data := range pathValues {
|
for path, data := range pathValues {
|
||||||
patchData = append(patchData, Patch{
|
patchData = append(patchData, Patch{
|
||||||
Operation: "replace",
|
Operation: "replace",
|
||||||
|
|
16
client.go
16
client.go
|
@ -7,7 +7,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"time"
|
"time"
|
||||||
|
@ -105,14 +104,19 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer func(Body io.ReadCloser) error {
|
||||||
|
return Body.Close()
|
||||||
|
}(resp.Body)
|
||||||
|
|
||||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||||
errResp := &ErrorResponse{Response: resp}
|
errResp := &ErrorResponse{Response: resp}
|
||||||
data, err = ioutil.ReadAll(resp.Body)
|
data, err = io.ReadAll(resp.Body)
|
||||||
|
|
||||||
if err == nil && len(data) > 0 {
|
if err == nil && len(data) > 0 {
|
||||||
json.Unmarshal(data, errResp)
|
err := json.Unmarshal(data, errResp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return errResp
|
return errResp
|
||||||
|
@ -122,8 +126,8 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if w, ok := v.(io.Writer); ok {
|
if w, ok := v.(io.Writer); ok {
|
||||||
io.Copy(w, resp.Body)
|
_, err := io.Copy(w, resp.Body)
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.NewDecoder(resp.Body).Decode(v)
|
return json.NewDecoder(resp.Body).Decode(v)
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -536,7 +536,7 @@ func (ts *webprofileTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request
|
||||||
func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
|
func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -572,7 +572,7 @@ func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
|
||||||
func (ts *webprofileTestServer) createWithoutName(w http.ResponseWriter, r *http.Request) {
|
func (ts *webprofileTestServer) createWithoutName(w http.ResponseWriter, r *http.Request) {
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -600,7 +600,7 @@ func (ts *webprofileTestServer) createWithoutName(w http.ResponseWriter, r *http
|
||||||
func (ts *webprofileTestServer) updatevalid(w http.ResponseWriter, r *http.Request) {
|
func (ts *webprofileTestServer) updatevalid(w http.ResponseWriter, r *http.Request) {
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -96,12 +96,12 @@ func (c *Client) VerifyWebhookSignature(ctx context.Context, httpReq *http.Reque
|
||||||
// Read the content
|
// Read the content
|
||||||
var bodyBytes []byte
|
var bodyBytes []byte
|
||||||
if httpReq.Body != nil {
|
if httpReq.Body != nil {
|
||||||
bodyBytes, _ = ioutil.ReadAll(httpReq.Body)
|
bodyBytes, _ = io.ReadAll(httpReq.Body)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("Cannot verify webhook for HTTP Request with empty body.")
|
return nil, errors.New("Cannot verify webhook for HTTP Request with empty body.")
|
||||||
}
|
}
|
||||||
// Restore the io.ReadCloser to its original state
|
// Restore the io.ReadCloser to its original state
|
||||||
httpReq.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
|
httpReq.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||||
|
|
||||||
verifyRequest := verifyWebhookSignatureRequest{
|
verifyRequest := verifyWebhookSignatureRequest{
|
||||||
AuthAlgo: httpReq.Header.Get("PAYPAL-AUTH-ALGO"),
|
AuthAlgo: httpReq.Header.Get("PAYPAL-AUTH-ALGO"),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user