fix update order patchrequest (#240)

Co-authored-by: andy <xxxandyfenxxx@xxx.com>
This commit is contained in:
Andy 2022-06-18 13:04:14 -07:00 committed by GitHub
parent c9ae5c4190
commit 7dc1f997d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 3 deletions

View File

@ -72,14 +72,19 @@ func (c *Client) UpdateOrder(ctx context.Context, orderID string, op string, pat
Path string `json:"path"` Path string `json:"path"`
Value map[string]string `json:"value"` Value map[string]string `json:"value"`
} }
order := &Order{}
req, err := c.NewRequest(ctx, "PATCH", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/checkout/orders/", orderID), patchRequest{Op: op, Path: path, Value: value}) req, err := c.NewRequest(ctx, "PATCH", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/checkout/orders/", orderID), []patchRequest{
{
Op: op,
Path: path,
Value: value,
},
})
if err != nil { if err != nil {
return err return err
} }
if err = c.SendWithAuth(req, order); err != nil { if err = c.SendWithAuth(req, nil); err != nil {
return err return err
} }
return nil return nil

66
order_test.go Normal file
View File

@ -0,0 +1,66 @@
package paypal
import (
"context"
"testing"
)
var testClientID = "AXy9orp-CDaHhBZ9C78QHW2BKZpACgroqo85_NIOa9mIfJ9QnSVKzY-X_rivR_fTUUr6aLjcJsj6sDur"
var testSecret = "EBoIiUSkCKeSk49hHSgTem1qnjzzJgRQHDEHvGpzlLEf_nIoJd91xu8rPOBDCdR_UYNKVxJE-UgS2iCw"
func TestUpdateOrder(t *testing.T) {
ctx := context.Background()
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
_, _ = c.GetAccessToken(ctx)
orderResponse, err := c.CreateOrder(
ctx,
OrderIntentCapture,
[]PurchaseUnitRequest{
{
Amount: &PurchaseUnitAmount{
Value: "7.00",
Currency: "USD",
},
},
},
&CreateOrderPayer{},
&ApplicationContext{},
)
if err != nil {
t.Errorf("Not expected error for CreateOrder(), got %s", err.Error())
}
order, err := c.GetOrder(ctx, orderResponse.ID)
if err != nil {
t.Errorf("Not expected error for GetOrder(), got %s", err.Error())
}
if order.PurchaseUnits[0].Amount.Value != "7.00" {
t.Errorf("CreateOrder amount incorrect")
}
err = c.UpdateOrder(
ctx,
orderResponse.ID,
"replace",
"/purchase_units/@reference_id=='default'/amount",
map[string]string{
"currency_code": "USD",
"value": "2.00",
},
)
if err != nil {
t.Errorf("Not expected error for UpdateOrder(), got %s", err.Error())
}
order, err = c.GetOrder(ctx, orderResponse.ID)
if err != nil {
t.Errorf("Not expected error for GetOrder(), got %s", err.Error())
}
if order.PurchaseUnits[0].Amount.Value != "2.00" {
t.Errorf("CreateOrder after update amount incorrect")
}
}