paypale/order.go

176 lines
5.7 KiB
Go
Raw Permalink Normal View History

2019-08-21 15:50:20 +02:00
package paypal
2015-12-17 08:50:25 +01:00
2021-01-03 10:28:52 +01:00
import (
"context"
"fmt"
)
2015-12-17 08:50:25 +01:00
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
2021-01-03 10:28:52 +01:00
func (c *Client) GetOrder(ctx context.Context, orderID string) (*Order, error) {
2015-12-17 08:50:25 +01:00
order := &Order{}
2021-01-03 10:28:52 +01:00
req, err := c.NewRequest(ctx, "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
2021-01-03 10:28:52 +01:00
func (c *Client) CreateOrder(ctx context.Context, intent string, purchaseUnits []PurchaseUnitRequest, payer *CreateOrderPayer, appContext *ApplicationContext) (*Order, error) {
2021-01-25 21:52:19 +01:00
return c.CreateOrderWithPaypalRequestID(ctx, intent, purchaseUnits, payer, appContext, "")
}
// CreateOrderWithPaypalRequestID - Use this call to create an order with idempotency
// Endpoint: POST /v2/checkout/orders
func (c *Client) CreateOrderWithPaypalRequestID(ctx context.Context,
intent string,
purchaseUnits []PurchaseUnitRequest,
payer *CreateOrderPayer,
appContext *ApplicationContext,
requestID string,
) (*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{}
2021-01-03 10:28:52 +01:00
req, err := c.NewRequest(ctx, "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
2021-01-25 21:52:19 +01:00
if requestID != "" {
req.Header.Set("PayPal-Request-Id", requestID)
}
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
2021-01-03 10:28:52 +01:00
func (c *Client) UpdateOrder(ctx context.Context, orderID string, purchaseUnits []PurchaseUnitRequest) (*Order, error) {
2019-07-30 15:12:58 +02:00
order := &Order{}
2021-01-03 10:28:52 +01:00
req, err := c.NewRequest(ctx, "PATCH", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/checkout/orders/", orderID), purchaseUnits)
2019-07-30 15:12:58 +02:00
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
2021-01-03 10:28:52 +01:00
func (c *Client) AuthorizeOrder(ctx context.Context, orderID string, authorizeOrderRequest AuthorizeOrderRequest) (*Authorization, error) {
2015-12-17 08:50:25 +01:00
auth := &Authorization{}
2021-01-03 10:28:52 +01:00
req, err := c.NewRequest(ctx, "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
2021-01-03 10:28:52 +01:00
func (c *Client) CaptureOrder(ctx context.Context, orderID string, captureOrderRequest CaptureOrderRequest) (*CaptureOrderResponse, error) {
return c.CaptureOrderWithPaypalRequestId(ctx, orderID, captureOrderRequest, "")
}
// CaptureOrder with idempotency - https://developer.paypal.com/docs/api/orders/v2/#orders_capture
// Endpoint: POST /v2/checkout/orders/ID/capture
// https://developer.paypal.com/docs/api/reference/api-requests/#http-request-headers
2021-01-03 10:28:52 +01:00
func (c *Client) CaptureOrderWithPaypalRequestId(ctx context.Context,
orderID string,
captureOrderRequest CaptureOrderRequest,
requestID string,
) (*CaptureOrderResponse, error) {
2019-08-21 08:24:18 +02:00
capture := &CaptureOrderResponse{}
2015-12-17 08:50:25 +01:00
c.SetReturnRepresentation()
2021-01-03 10:28:52 +01:00
req, err := c.NewRequest(ctx, "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
}
if requestID != "" {
req.Header.Set("PayPal-Request-Id", requestID)
}
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
2021-01-03 10:28:52 +01:00
func (c *Client) RefundCapture(ctx context.Context, captureID string, refundCaptureRequest RefundCaptureRequest) (*RefundResponse, error) {
return c.RefundCaptureWithPaypalRequestId(ctx, captureID, refundCaptureRequest, "")
}
// RefundCapture with idempotency - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
// Endpoint: POST /v2/payments/captures/ID/refund
2021-01-03 10:28:52 +01:00
func (c *Client) RefundCaptureWithPaypalRequestId(ctx context.Context,
captureID string,
refundCaptureRequest RefundCaptureRequest,
requestID string,
) (*RefundResponse, error) {
refund := &RefundResponse{}
2021-01-03 10:28:52 +01:00
req, err := c.NewRequest(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/captures/"+captureID+"/refund"), refundCaptureRequest)
if err != nil {
return refund, err
}
if requestID != "" {
req.Header.Set("PayPal-Request-Id", requestID)
}
if err = c.SendWithAuth(req, refund); err != nil {
return refund, err
}
return refund, nil
}
2021-02-06 16:56:29 +01:00
// CapturedDetail - https://developer.paypal.com/docs/api/payments/v2/#captures_get
// Endpoint: GET /v2/payments/captures/ID
func (c *Client) CapturedDetail(ctx context.Context, captureID string ) (*CaptureDetailsResponse, error) {
response := &CaptureDetailsResponse{}
req, err := c.NewRequest(ctx, "GET", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/captures/"+captureID), nil)
if err != nil {
return response, err
}
if err = c.SendWithAuth(req, response); err != nil {
return response, err
}
return response, nil
}