Skip to content

Commit 4e52c79

Browse files
authored
Add PayPal STC API (#296)
1 parent 544a4f7 commit 4e52c79

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

order.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (c *Client) GetOrder(ctx context.Context, orderID string) (*Order, error) {
2323
return order, nil
2424
}
2525

26-
// Create an order
26+
// CreateOrder Create an order
2727
// Endpoint: POST /v2/checkout/orders
2828
func (c *Client) CreateOrder(ctx context.Context, intent string, purchaseUnits []PurchaseUnitRequest, paymentSource *PaymentSource, appContext *ApplicationContext) (*Order, error) {
2929
return c.CreateOrderWithPaypalRequestID(ctx, intent, purchaseUnits, paymentSource, appContext, "")
@@ -113,7 +113,7 @@ func (c *Client) CaptureOrder(ctx context.Context, orderID string, captureOrderR
113113
return c.CaptureOrderWithPaypalRequestId(ctx, orderID, captureOrderRequest, "", nil)
114114
}
115115

116-
// CaptureOrder with idempotency - https://developer.paypal.com/docs/api/orders/v2/#orders_capture
116+
// CaptureOrderWithPaypalRequestId with idempotency - https://developer.paypal.com/docs/api/orders/v2/#orders_capture
117117
// Endpoint: POST /v2/checkout/orders/ID/capture
118118
// https://developer.paypal.com/docs/api/reference/api-requests/#http-request-headers
119119
func (c *Client) CaptureOrderWithPaypalRequestId(ctx context.Context,
@@ -143,6 +143,11 @@ func (c *Client) CaptureOrderWithPaypalRequestId(ctx context.Context,
143143
req.Header.Set("PayPal-Mock-Response", string(mock))
144144
}
145145

146+
//Add for STC API, we need to link order together
147+
//https://developer.paypal.com/limited-release/raas/integration-guide/#link-setthetransactioncontext
148+
//https://developer.paypal.com/docs/api/orders/v2/#orders_capture!in=header&path=PayPal-Client-Metadata-Id&t=request
149+
req.Header.Set("PayPal-Client-Metadata-Id", orderID)
150+
146151
if err = c.SendWithAuth(req, capture); err != nil {
147152
return capture, err
148153
}
@@ -156,7 +161,7 @@ func (c *Client) RefundCapture(ctx context.Context, captureID string, refundCapt
156161
return c.RefundCaptureWithPaypalRequestId(ctx, captureID, refundCaptureRequest, "")
157162
}
158163

159-
// RefundCapture with idempotency - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
164+
// RefundCaptureWithPaypalRequestId with idempotency - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
160165
// Endpoint: POST /v2/payments/captures/ID/refund
161166
func (c *Client) RefundCaptureWithPaypalRequestId(ctx context.Context,
162167
captureID string,

stc.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package paypal
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
// SetTransactionContext
9+
// PayPal partners and merchants can Set transaction context to send additional data about a customer to PayPal before the customer transaction is processed.
10+
// PayPal uses this data to complete a pre-transaction risk management evaluation
11+
// Endpoint: PUT <endpoint>/v1/risk/transaction-contexts/<merchant_id>/<tracking_id>
12+
func (c *Client) SetTransactionContext(ctx context.Context, merchantID, orderID string, tcPayload interface{}) error {
13+
14+
if merchantID == "" || orderID == "" {
15+
return fmt.Errorf("paypal: merchantID or orderID is empty")
16+
}
17+
18+
url := fmt.Sprintf("%s%s%s", c.APIBase, "/v1/risk/transaction-contexts/", merchantID+"/"+orderID)
19+
20+
req, err := c.NewRequest(ctx, "PUT", url, tcPayload)
21+
22+
if err != nil {
23+
return err
24+
}
25+
26+
if err = c.SendWithAuth(req, nil); err != nil {
27+
return err
28+
}
29+
30+
return nil
31+
}

0 commit comments

Comments
 (0)