Create Order API Call

This commit is contained in:
Roopak Venkatakrishnan 2019-06-26 21:16:21 -07:00
parent ebc7df5708
commit fd873cc35b
4 changed files with 99 additions and 0 deletions

View File

@ -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) { func TestAuthorizeOrder(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox) c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken() c.GetAccessToken()

View File

@ -19,6 +19,27 @@ func (c *Client) GetOrder(orderID string) (*Order, error) {
return order, nil 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. // AuthorizeOrder - Use this call to authorize an order.
// Endpoint: POST /v2/checkout/orders/ID/authorize // Endpoint: POST /v2/checkout/orders/ID/authorize
func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization, error) { func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization, error) {

View File

@ -54,6 +54,14 @@ const (
AllowedPaymentImmediatePay string = "IMMEDIATE_PAY" 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 ( type (
// JSONTime overrides MarshalJson method to format in ISO8601 // JSONTime overrides MarshalJson method to format in ISO8601
JSONTime time.Time JSONTime time.Time
@ -325,6 +333,19 @@ type (
Amount *PurchaseUnitAmount `json:"amount,omitempty"` 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
MerchantPreferences struct { MerchantPreferences struct {
SetupFee *AmountPayout `json:"setup_fee,omitempty"` SetupFee *AmountPayout `json:"setup_fee,omitempty"`
@ -507,6 +528,17 @@ type (
Phone string `json:"phone,omitempty"` 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 expirationTime int64
// TokenResponse is for API response for the /oauth2/token endpoint // TokenResponse is for API response for the /oauth2/token endpoint

View File

@ -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) { func TestTypePayoutItemResponse(t *testing.T) {
response := `{ response := `{
"payout_item_id":"9T35G83YA546X", "payout_item_id":"9T35G83YA546X",