github.com/plutov/paypal/v4@v4.7.1/order.go (about)

     1  package paypal
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  )
     8  
     9  // GetOrder retrieves order by ID
    10  // Endpoint: GET /v2/checkout/orders/ID
    11  func (c *Client) GetOrder(ctx context.Context, orderID string) (*Order, error) {
    12  	order := &Order{}
    13  
    14  	req, err := c.NewRequest(ctx, "GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/checkout/orders/", orderID), nil)
    15  	if err != nil {
    16  		return order, err
    17  	}
    18  
    19  	if err = c.SendWithAuth(req, order); err != nil {
    20  		return order, err
    21  	}
    22  
    23  	return order, nil
    24  }
    25  
    26  // CreateOrder - Use this call to create an order
    27  // Endpoint: POST /v2/checkout/orders
    28  func (c *Client) CreateOrder(ctx context.Context, intent string, purchaseUnits []PurchaseUnitRequest, payer *CreateOrderPayer, appContext *ApplicationContext) (*Order, error) {
    29  	return c.CreateOrderWithPaypalRequestID(ctx, intent, purchaseUnits, payer, appContext, "")
    30  }
    31  
    32  // CreateOrderWithPaypalRequestID - Use this call to create an order with idempotency
    33  // Endpoint: POST /v2/checkout/orders
    34  func (c *Client) CreateOrderWithPaypalRequestID(ctx context.Context,
    35  	intent string,
    36  	purchaseUnits []PurchaseUnitRequest,
    37  	payer *CreateOrderPayer,
    38  	appContext *ApplicationContext,
    39  	requestID string,
    40  ) (*Order, error) {
    41  	type createOrderRequest struct {
    42  		Intent             string                `json:"intent"`
    43  		Payer              *CreateOrderPayer     `json:"payer,omitempty"`
    44  		PurchaseUnits      []PurchaseUnitRequest `json:"purchase_units"`
    45  		ApplicationContext *ApplicationContext   `json:"application_context,omitempty"`
    46  	}
    47  
    48  	order := &Order{}
    49  
    50  	req, err := c.NewRequest(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders"), createOrderRequest{Intent: intent, PurchaseUnits: purchaseUnits, Payer: payer, ApplicationContext: appContext})
    51  	if err != nil {
    52  		return order, err
    53  	}
    54  
    55  	if requestID != "" {
    56  		req.Header.Set("PayPal-Request-Id", requestID)
    57  	}
    58  
    59  	if err = c.SendWithAuth(req, order); err != nil {
    60  		return order, err
    61  	}
    62  
    63  	return order, nil
    64  }
    65  
    66  // UpdateOrder updates the order by ID
    67  // Endpoint: PATCH /v2/checkout/orders/ID
    68  func (c *Client) UpdateOrder(ctx context.Context, orderID string, op string, path string, value map[string]string) error {
    69  
    70  	type patchRequest struct {
    71  		Op    string            `json:"op"`
    72  		Path  string            `json:"path"`
    73  		Value map[string]string `json:"value"`
    74  	}
    75  
    76  	req, err := c.NewRequest(ctx, "PATCH", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/checkout/orders/", orderID), []patchRequest{
    77  		{
    78  			Op:    op,
    79  			Path:  path,
    80  			Value: value,
    81  		},
    82  	})
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	if err = c.SendWithAuth(req, nil); err != nil {
    88  		return err
    89  	}
    90  	return nil
    91  }
    92  
    93  // AuthorizeOrder - https://developer.paypal.com/docs/api/orders/v2/#orders_authorize
    94  // Endpoint: POST /v2/checkout/orders/ID/authorize
    95  func (c *Client) AuthorizeOrder(ctx context.Context, orderID string, authorizeOrderRequest AuthorizeOrderRequest) (*AuthorizeOrderResponse, error) {
    96  	auth := &AuthorizeOrderResponse{}
    97  
    98  	req, err := c.NewRequest(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/authorize"), authorizeOrderRequest)
    99  	if err != nil {
   100  		return auth, err
   101  	}
   102  
   103  	if err = c.SendWithAuth(req, auth); err != nil {
   104  		return auth, err
   105  	}
   106  
   107  	return auth, nil
   108  }
   109  
   110  // CaptureOrder - https://developer.paypal.com/docs/api/orders/v2/#orders_capture
   111  // Endpoint: POST /v2/checkout/orders/ID/capture
   112  func (c *Client) CaptureOrder(ctx context.Context, orderID string, captureOrderRequest CaptureOrderRequest) (*CaptureOrderResponse, error) {
   113  	return c.CaptureOrderWithPaypalRequestId(ctx, orderID, captureOrderRequest, "", nil)
   114  }
   115  
   116  // CaptureOrder with idempotency - https://developer.paypal.com/docs/api/orders/v2/#orders_capture
   117  // Endpoint: POST /v2/checkout/orders/ID/capture
   118  // https://developer.paypal.com/docs/api/reference/api-requests/#http-request-headers
   119  func (c *Client) CaptureOrderWithPaypalRequestId(ctx context.Context,
   120  	orderID string,
   121  	captureOrderRequest CaptureOrderRequest,
   122  	requestID string,
   123  	mockResponse *CaptureOrderMockResponse,
   124  ) (*CaptureOrderResponse, error) {
   125  	capture := &CaptureOrderResponse{}
   126  
   127  	c.SetReturnRepresentation()
   128  	req, err := c.NewRequest(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/capture"), captureOrderRequest)
   129  	if err != nil {
   130  		return capture, err
   131  	}
   132  
   133  	if requestID != "" {
   134  		req.Header.Set("PayPal-Request-Id", requestID)
   135  	}
   136  
   137  	if mockResponse != nil {
   138  		mock, err := json.Marshal(mockResponse)
   139  		if err != nil {
   140  			return nil, err
   141  		}
   142  
   143  		req.Header.Set("PayPal-Mock-Response", string(mock))
   144  	}
   145  
   146  	if err = c.SendWithAuth(req, capture); err != nil {
   147  		return capture, err
   148  	}
   149  
   150  	return capture, nil
   151  }
   152  
   153  // RefundCapture - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
   154  // Endpoint: POST /v2/payments/captures/ID/refund
   155  func (c *Client) RefundCapture(ctx context.Context, captureID string, refundCaptureRequest RefundCaptureRequest) (*RefundResponse, error) {
   156  	return c.RefundCaptureWithPaypalRequestId(ctx, captureID, refundCaptureRequest, "")
   157  }
   158  
   159  // RefundCapture with idempotency - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
   160  // Endpoint: POST /v2/payments/captures/ID/refund
   161  func (c *Client) RefundCaptureWithPaypalRequestId(ctx context.Context,
   162  	captureID string,
   163  	refundCaptureRequest RefundCaptureRequest,
   164  	requestID string,
   165  ) (*RefundResponse, error) {
   166  	refund := &RefundResponse{}
   167  
   168  	req, err := c.NewRequest(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/captures/"+captureID+"/refund"), refundCaptureRequest)
   169  	if err != nil {
   170  		return refund, err
   171  	}
   172  
   173  	if requestID != "" {
   174  		req.Header.Set("PayPal-Request-Id", requestID)
   175  	}
   176  
   177  	if err = c.SendWithAuth(req, refund); err != nil {
   178  		return refund, err
   179  	}
   180  	return refund, nil
   181  }
   182  
   183  // CapturedDetail - https://developer.paypal.com/docs/api/payments/v2/#captures_get
   184  // Endpoint: GET /v2/payments/captures/ID
   185  func (c *Client) CapturedDetail(ctx context.Context, captureID string) (*CaptureDetailsResponse, error) {
   186  	response := &CaptureDetailsResponse{}
   187  
   188  	req, err := c.NewRequest(ctx, "GET", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/captures/"+captureID), nil)
   189  	if err != nil {
   190  		return response, err
   191  	}
   192  
   193  	if err = c.SendWithAuth(req, response); err != nil {
   194  		return response, err
   195  	}
   196  	return response, nil
   197  }