paypale/order.go

76 lines
2.3 KiB
Go
Raw Normal View History

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
2019-06-24 13:01:15 +02:00
func (c *Client) AuthorizeOrder(orderID string, paymentSource PaymentSource) (*Authorization, error) {
2015-12-17 08:50:25 +01:00
auth := &Authorization{}
2019-06-24 13:01:15 +02:00
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/authorize"), paymentSource)
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
}
2019-06-24 13:01:15 +02:00
// CaptureOrder - Use this call to capture a payment on an order.
2019-04-21 05:08:48 +02:00
// Endpoint: POST /v2/checkout/orders/ID/capture
2019-06-24 13:01:15 +02:00
func (c *Client) CaptureOrder(orderID string, paymentSource PaymentSource) (*Capture, error) {
2015-12-17 08:50:25 +01:00
capture := &Capture{}
2019-06-24 13:01:15 +02:00
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/capture"), paymentSource)
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
}