diff --git a/README.md b/README.md index 7ce90c0..8f3d296 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Currently supports **v2** only, if you want to use **v1**, use **v1.1.4** git ta * GET /v2/payments/sale/**ID** * POST /v2/payments/sale/**ID**/refund * GET /v2/payments/refund/**ID** + * POST /v2/checkout/orders * GET /v2/checkout/orders/**ID** * POST /v2/checkout/orders/**ID**/authorize * POST /v2/checkout/orders/**ID**/capture @@ -108,6 +109,12 @@ refund, err := c.GetRefund("O-4J082351X3132253H") order, err := c.GetOrder("O-4J082351X3132253H") ``` +### Create an Order + +```go +order, err := c.CreateOrder(paypalsdk.OrderIntentCapture, []paypalsdk.PurchaseUnitRequest{paypalsdk.PurchaseUnitRequest{ReferenceID: "ref-id", Amount: paypalsdk.Amount{Total: "7.00", Currency: "USD"}}}) +``` + ### Authorize Order ```go diff --git a/integration_test.go b/integration_test.go index f78254a..e17a2d7 100644 --- a/integration_test.go +++ b/integration_test.go @@ -108,6 +108,16 @@ func TestGetOrder(t *testing.T) { } } +func TestCreateOrder(t *testing.T) { + c, _ := NewClient(testClientID, testSecret, APIBaseSandBox) + c.GetAccessToken() + + order, err := c.CreateOrder(OrderIntentCapture, nil, nil, nil) + if err == nil { + t.Errorf("CreateOrder expects error") + } +} + func TestAuthorizeOrder(t *testing.T) { c, _ := NewClient(testClientID, testSecret, APIBaseSandBox) c.GetAccessToken() diff --git a/order.go b/order.go index e07394d..1cc1412 100644 --- a/order.go +++ b/order.go @@ -19,6 +19,27 @@ func (c *Client) GetOrder(orderID string) (*Order, error) { return order, nil } +// Create Order - Use this call to create an order +// Endpoint: POST /v2/checkout/orders +func (c *Client) CreateOrder(intent string, purchaseUnits []PurchaseUnitRequest, payer *PayerInfo, appContext *ApplicationContext) (*Order, error) { + type createOrderRequest struct { + Intent string `json:"intent"` + Payer *PayerInfo `json:"payer,omitempty"` + PurchaseUnits []PurchaseUnitRequest `json:"purchase_units"` + ApplicationContext *ApplicationContext `json:"application_context,omitempty"` + } + + order := &Order{} + + req, err := c.NewRequest("POST", "/v2/checkout/orders", createOrderRequest{Intent: intent, PurchaseUnits: purchaseUnits, Payer: payer, ApplicationContext: appContext}) + + if err = c.SendWithAuth(req, order); err != nil { + return order, err + } + + return order, nil +} + // AuthorizeOrder - Use this call to authorize an order. // Endpoint: POST /v2/checkout/orders/ID/authorize func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization, error) { diff --git a/types.go b/types.go index 345862a..5ff7fe1 100644 --- a/types.go +++ b/types.go @@ -54,6 +54,14 @@ const ( AllowedPaymentImmediatePay string = "IMMEDIATE_PAY" ) +// Possible value for `intent` in CreateOrder +// +// https://developer.paypal.com/docs/api/orders/v2/#orders_create +const ( + OrderIntentCapture string = "CAPTURE" + OrderIntentAuthorize string = "AUTHORIZE" +) + type ( // JSONTime overrides MarshalJson method to format in ISO8601 JSONTime time.Time @@ -325,6 +333,19 @@ type ( Amount *PurchaseUnitAmount `json:"amount,omitempty"` } + // PurchaseUnitRequest struct + PurchaseUnitRequest struct { + ReferenceID string `json:"reference_id"` + Amount *PurchaseUnitAmount `json:"amount"` + Payee Payee `json:"payee,omitempty"` + Description string `json:"description,omitempty"` + CustomID string `json:"custom_id,omitempty"` + InvoiceID string `json:"invoice_id,omitempty"` + SoftDescriptor string `json:"soft_descriptor,omitempty"` + Items []Item `json:"items,omitempty"` + Shipping ShippingDetail `json:"shipping,omitempty"` + } + // MerchantPreferences struct MerchantPreferences struct { SetupFee *AmountPayout `json:"setup_fee,omitempty"` @@ -507,6 +528,17 @@ type ( Phone string `json:"phone,omitempty"` } + // Name struct + Name struct { + FullName string `json:"full_name,omitempty"` + } + + // ShippingDetail struct + ShippingDetail struct { + Name Name `json:"name,omitempty"` + Address Address `json:"address,omitempty"` + } + expirationTime int64 // TokenResponse is for API response for the /oauth2/token endpoint diff --git a/unit_test.go b/unit_test.go index bbfd71c..f2d3c02 100644 --- a/unit_test.go +++ b/unit_test.go @@ -228,6 +228,42 @@ func TestTypePayoutResponse(t *testing.T) { } } +func TestOrderUnmarshal(t *testing.T) { + response := `{ + "id": "5O190127TN364715T", + "status": "CREATED", + "links": [ + { + "href": "https://api.paypal.com/v2/checkout/orders/5O190127TN364715T", + "rel": "self", + "method": "GET" + }, + { + "href": "https://api.sandbox.paypal.com/checkoutnow?token=5O190127TN364715T", + "rel": "approve", + "method": "GET" + }, + { + "href": "https://api.paypal.com/v2/checkout/orders/5O190127TN364715T/capture", + "rel": "capture", + "method": "POST" + } + ] + }` + + order := &Order{} + err := json.Unmarshal([]byte(response), order) + if err != nil { + t.Errorf("Order Unmarshal failed") + } + + if order.ID != "5O190127TN364715T" || + order.Status != "CREATED" || + order.Links[0].Href != "https://api.paypal.com/v2/checkout/orders/5O190127TN364715T" { + t.Errorf("Order decoded result is incorrect, Given: %+v", order) + } +} + func TestTypePayoutItemResponse(t *testing.T) { response := `{ "payout_item_id":"9T35G83YA546X",