Merge pull request #91 from roopakv/roopakv/create_order

Create Order API Call
This commit is contained in:
Alex Pliutau 2019-06-27 08:54:10 +02:00 committed by GitHub
commit 973f502217
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 0 deletions

View File

@ -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

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) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()

View File

@ -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) {

View File

@ -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

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) {
response := `{
"payout_item_id":"9T35G83YA546X",