2015-12-17 08:50:25 +01:00
|
|
|
package paypalsdk
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2016-12-22 06:06:00 +01:00
|
|
|
// GetOrder retrieves order by ID
|
2019-04-21 05:08:48 +02:00
|
|
|
// Endpoint: GET /v2/checkout/orders/ID
|
2015-12-17 08:50:25 +01:00
|
|
|
func (c *Client) GetOrder(orderID string) (*Order, error) {
|
|
|
|
order := &Order{}
|
|
|
|
|
2019-04-21 05:08:48 +02:00
|
|
|
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/checkout/orders/", orderID), nil)
|
2015-12-17 08:50:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return order, err
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:15:11 +01:00
|
|
|
if err = c.SendWithAuth(req, order); err != nil {
|
2015-12-17 08:50:25 +01:00
|
|
|
return order, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return order, nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 06:16:21 +02:00
|
|
|
// Create Order - Use this call to create an order
|
|
|
|
// Endpoint: POST /v2/checkout/orders
|
2019-07-22 09:23:07 +02:00
|
|
|
func (c *Client) CreateOrder(intent string, purchaseUnits []PurchaseUnitRequest, payer *CreateOrderPayer, appContext *ApplicationContext) (*Order, error) {
|
2019-06-27 06:16:21 +02:00
|
|
|
type createOrderRequest struct {
|
|
|
|
Intent string `json:"intent"`
|
2019-07-22 09:23:07 +02:00
|
|
|
Payer *CreateOrderPayer `json:"payer,omitempty"`
|
2019-06-27 06:16:21 +02:00
|
|
|
PurchaseUnits []PurchaseUnitRequest `json:"purchase_units"`
|
|
|
|
ApplicationContext *ApplicationContext `json:"application_context,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
order := &Order{}
|
|
|
|
|
2019-06-29 04:08:24 +02:00
|
|
|
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders"), createOrderRequest{Intent: intent, PurchaseUnits: purchaseUnits, Payer: payer, ApplicationContext: appContext})
|
2019-06-27 06:16:21 +02:00
|
|
|
|
|
|
|
if err = c.SendWithAuth(req, order); err != nil {
|
|
|
|
return order, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return order, nil
|
|
|
|
}
|
|
|
|
|
2015-12-29 10:21:11 +01:00
|
|
|
// AuthorizeOrder - Use this call to authorize an order.
|
2019-04-21 05:08:48 +02:00
|
|
|
// Endpoint: POST /v2/checkout/orders/ID/authorize
|
2015-12-17 08:50:25 +01:00
|
|
|
func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization, error) {
|
|
|
|
type authRequest struct {
|
|
|
|
Amount *Amount `json:"amount"`
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := &Authorization{}
|
|
|
|
|
2019-04-21 05:08:48 +02:00
|
|
|
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/authorize"), authRequest{Amount: amount})
|
2015-12-17 08:50:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return auth, err
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:15:11 +01:00
|
|
|
if err = c.SendWithAuth(req, auth); err != nil {
|
2015-12-17 08:50:25 +01:00
|
|
|
return auth, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return auth, nil
|
|
|
|
}
|
|
|
|
|
2015-12-29 10:21:11 +01:00
|
|
|
// CaptureOrder - Use this call to capture a payment on an order. To use this call, an original payment call must specify an intent of order.
|
2019-04-21 05:08:48 +02:00
|
|
|
// Endpoint: POST /v2/checkout/orders/ID/capture
|
2015-12-17 08:50:25 +01:00
|
|
|
func (c *Client) CaptureOrder(orderID string, amount *Amount, isFinalCapture bool, currency *Currency) (*Capture, error) {
|
|
|
|
type captureRequest struct {
|
|
|
|
Amount *Amount `json:"amount"`
|
|
|
|
IsFinalCapture bool `json:"is_final_capture"`
|
|
|
|
Currency *Currency `json:"transaction_fee"`
|
|
|
|
}
|
|
|
|
|
|
|
|
capture := &Capture{}
|
|
|
|
|
2019-04-21 05:08:48 +02:00
|
|
|
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/capture"), captureRequest{Amount: amount, IsFinalCapture: isFinalCapture, Currency: currency})
|
2015-12-17 08:50:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return capture, err
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:15:11 +01:00
|
|
|
if err = c.SendWithAuth(req, capture); err != nil {
|
2015-12-17 08:50:25 +01:00
|
|
|
return capture, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return capture, nil
|
|
|
|
}
|