mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 10:21:03 +01:00
Merge pull request #91 from roopakv/roopakv/create_order
Create Order API Call
This commit is contained in:
commit
973f502217
|
@ -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**
|
* GET /v2/payments/sale/**ID**
|
||||||
* POST /v2/payments/sale/**ID**/refund
|
* POST /v2/payments/sale/**ID**/refund
|
||||||
* GET /v2/payments/refund/**ID**
|
* GET /v2/payments/refund/**ID**
|
||||||
|
* POST /v2/checkout/orders
|
||||||
* GET /v2/checkout/orders/**ID**
|
* GET /v2/checkout/orders/**ID**
|
||||||
* POST /v2/checkout/orders/**ID**/authorize
|
* POST /v2/checkout/orders/**ID**/authorize
|
||||||
* POST /v2/checkout/orders/**ID**/capture
|
* POST /v2/checkout/orders/**ID**/capture
|
||||||
|
@ -108,6 +109,12 @@ refund, err := c.GetRefund("O-4J082351X3132253H")
|
||||||
order, err := c.GetOrder("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
|
### Authorize Order
|
||||||
|
|
||||||
```go
|
```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) {
|
func TestAuthorizeOrder(t *testing.T) {
|
||||||
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
|
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
|
||||||
c.GetAccessToken()
|
c.GetAccessToken()
|
||||||
|
|
21
order.go
21
order.go
|
@ -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) {
|
||||||
|
|
32
types.go
32
types.go
|
@ -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
|
||||||
|
|
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) {
|
func TestTypePayoutItemResponse(t *testing.T) {
|
||||||
response := `{
|
response := `{
|
||||||
"payout_item_id":"9T35G83YA546X",
|
"payout_item_id":"9T35G83YA546X",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user