mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 02:11:02 +01:00
Create Order API Call
This commit is contained in:
parent
ebc7df5708
commit
fd873cc35b
|
@ -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()
|
||||
|
|
21
order.go
21
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) {
|
||||
|
|
32
types.go
32
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
|
||||
|
|
36
unit_test.go
36
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",
|
||||
|
|
Loading…
Reference in New Issue
Block a user