paypale/order.go

113 lines
3.4 KiB
Go
Raw Normal View History

2019-08-21 15:50:20 +02:00
package paypal
2015-12-17 08:50:25 +01:00
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-08-19 15:33:46 +02:00
// CreateOrder - Use this call to create an order
2019-06-27 06:16:21 +02:00
// 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-08-19 15:33:46 +02:00
if err != nil {
return order, err
}
2019-06-27 06:16:21 +02:00
if err = c.SendWithAuth(req, order); err != nil {
return order, err
}
return order, nil
}
2019-07-30 15:12:58 +02:00
// UpdateOrder updates the order by ID
// Endpoint: PATCH /v2/checkout/orders/ID
func (c *Client) UpdateOrder(orderID string, purchaseUnits []PurchaseUnitRequest) (*Order, error) {
order := &Order{}
req, err := c.NewRequest("PATCH", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/checkout/orders/", orderID), purchaseUnits)
if err != nil {
return order, err
}
if err = c.SendWithAuth(req, order); err != nil {
return order, err
}
return order, nil
}
// AuthorizeOrder - https://developer.paypal.com/docs/api/orders/v2/#orders_authorize
2019-04-21 05:08:48 +02:00
// Endpoint: POST /v2/checkout/orders/ID/authorize
2019-07-30 15:12:58 +02:00
func (c *Client) AuthorizeOrder(orderID string, authorizeOrderRequest AuthorizeOrderRequest) (*Authorization, error) {
2015-12-17 08:50:25 +01:00
auth := &Authorization{}
2019-07-30 15:12:58 +02:00
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/authorize"), authorizeOrderRequest)
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-07-30 15:12:58 +02:00
// CaptureOrder - https://developer.paypal.com/docs/api/orders/v2/#orders_capture
2019-04-21 05:08:48 +02:00
// Endpoint: POST /v2/checkout/orders/ID/capture
2019-08-21 08:24:18 +02:00
func (c *Client) CaptureOrder(orderID string, captureOrderRequest CaptureOrderRequest) (*CaptureOrderResponse, error) {
capture := &CaptureOrderResponse{}
2015-12-17 08:50:25 +01:00
c.SetReturnRepresentation()
2019-07-30 15:12:58 +02:00
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/capture"), captureOrderRequest)
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
}
// RefundCapture - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
// Endpoint: POST /v2/payments/captures/ID/refund
func (c *Client) RefundCapture(captureID string, refundCaptureRequest RefundCaptureRequest) (*RefundResponse, error) {
refund := &RefundResponse{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/captures/"+captureID+"/refund"), refundCaptureRequest)
if err != nil {
return refund, err
}
if err = c.SendWithAuth(req, refund); err != nil {
return refund, err
}
return refund, nil
}