Skip to main content

Actions

The ballerinax/paypal.orders package exposes the following clients:

ClientPurpose
ClientManages the full PayPal order lifecycle: creation, retrieval, update, authorization, capture, and shipment tracking.

Client

Manages the full PayPal order lifecycle: creation, retrieval, update, authorization, capture, and shipment tracking.

Configuration

FieldTypeDefaultDescription
authOAuth2ClientCredentialsGrantConfigRequiredOAuth 2.0 client credentials configuration with clientId, clientSecret, and tokenUrl.
httpVersionhttp:HttpVersionHTTP_2_0HTTP protocol version.
timeoutdecimal30Request timeout in seconds.
retryConfighttp:RetryConfig()Retry configuration for failed requests.
secureSockethttp:ClientSecureSocket()SSL/TLS configuration.
proxyhttp:ProxyConfig()Proxy server configuration.
circuitBreakerhttp:CircuitBreakerConfig()Circuit breaker configuration for fault tolerance.
compressionhttp:CompressionAUTOHTTP compression setting.
validationbooleantrueEnable/disable payload validation.

Initializing the client

import ballerinax/paypal.orders;

configurable string clientId = ?;
configurable string clientSecret = ?;

orders:Client paypalOrders = check new ({
auth: {
clientId: clientId,
clientSecret: clientSecret,
tokenUrl: "https://api-m.sandbox.paypal.com/v1/oauth2/token"
}
});

Operations

Order lifecycle

Create an order

Creates a new order with the specified intent (CAPTURE or AUTHORIZE) and purchase units.

Parameters:

NameTypeRequiredDescription
payloadOrderRequestYesThe order request containing intent and purchase_units.
headersOrdersCreateHeadersNoOptional headers including PayPal-Request-Id and Prefer.

Returns: Order|error

Sample code:

orders:Order createdOrder = check paypalOrders->/orders.post({
intent: "CAPTURE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "100.00"
}
}
]
});

Sample response:

{
"id": "5O190127TN364715T",
"status": "CREATED",
"intent": "CAPTURE",
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "USD",
"value": "100.00"
}
}
],
"links": [
{"href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/5O190127TN364715T", "rel": "self", "method": "GET"},
{"href": "https://www.sandbox.paypal.com/checkoutnow?token=5O190127TN364715T", "rel": "approve", "method": "GET"},
{"href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/5O190127TN364715T", "rel": "update", "method": "PATCH"},
{"href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/5O190127TN364715T/capture", "rel": "capture", "method": "POST"}
]
}
Show order details

Retrieves the details of an existing order by its ID.

Parameters:

NameTypeRequiredDescription
idstringYesThe PayPal order ID.
queriesOrdersGetQueriesNoOptional query parameters. Use fields: "payment_source" to include payment source details.

Returns: Order|error

Sample code:

orders:Order orderDetails = check paypalOrders->/orders/["5O190127TN364715T"];

Sample response:

{
"id": "5O190127TN364715T",
"status": "CREATED",
"intent": "CAPTURE",
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "USD",
"value": "100.00"
}
}
],
"links": [
{"href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/5O190127TN364715T", "rel": "self", "method": "GET"},
{"href": "https://www.sandbox.paypal.com/checkoutnow?token=5O190127TN364715T", "rel": "approve", "method": "GET"}
]
}
Update order

Updates an existing order using JSON Patch operations. Can modify amount, description, shipping, and other fields.

Parameters:

NameTypeRequiredDescription
idstringYesThe PayPal order ID.
payloadPatchRequest (Patch[])YesArray of JSON Patch operations to apply.

Returns: error?

Sample code:

check paypalOrders->/orders/[orderId].patch([
{
op: "replace",
path: "/purchase_units/@reference_id=='default'/amount",
value: {
currency_code: "USD",
value: "200.00",
breakdown: {
item_total: {
currency_code: "USD",
value: "200.00"
}
}
}
}
]);

Payment processing

Confirm the order

Confirms a payment source for an order before authorization or capture. Payer interaction may be required after confirmation.

Parameters:

NameTypeRequiredDescription
idstringYesThe PayPal order ID.
payloadConfirmOrderRequestYesThe confirm order request containing the payment source.
headersOrdersConfirmHeadersNoOptional headers including PayPal-Client-Metadata-Id and Prefer.

Returns: Order|error

Sample code:

orders:Order confirmedOrder = check paypalOrders->/orders/[orderId]/confirm\-payment\-source.post({
payment_source: {
card: {
number: "4111111111111111",
expiry: "2028-02",
name: "John Doe",
billing_address: {
address_line_1: "123 Main St",
admin_area_2: "San Jose",
admin_area_1: "CA",
postal_code: "95131",
country_code: "US"
}
}
}
});

Sample response:

{
"id": "5O190127TN364715T",
"status": "APPROVED",
"intent": "CAPTURE",
"payment_source": {
"card": {
"last_digits": "1111",
"brand": "VISA",
"type": "CREDIT"
}
},
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "USD",
"value": "200.00"
}
}
]
}
Authorize payment for order

Authorizes payment for an approved order. The authorization places a hold on the payer's funds but does not capture them.

Parameters:

NameTypeRequiredDescription
idstringYesThe PayPal order ID.
payloadOrderAuthorizeRequestYesThe authorize request, optionally containing a payment source.
headersOrdersAuthorizeHeadersNoOptional headers including PayPal-Request-Id and Prefer.

Returns: OrderAuthorizeResponse|error

Sample code:

orders:OrderAuthorizeResponse authResponse = check paypalOrders->/orders/[orderId]/authorize.post({});

Sample response:

{
"id": "5O190127TN364715T",
"status": "COMPLETED",
"purchase_units": [
{
"reference_id": "default",
"payments": {
"authorizations": [
{
"id": "0AW876160B5583922",
"status": "CREATED",
"amount": {
"currency_code": "USD",
"value": "200.00"
}
}
]
}
}
]
}
Capture payment for order

Captures payment for an approved order. For CAPTURE intent orders, this completes the payment. For AUTHORIZE intent orders, use after authorization.

Parameters:

NameTypeRequiredDescription
idstringYesThe PayPal order ID.
payloadOrderCaptureRequestYesThe capture request, optionally containing a payment source.
headersOrdersCaptureHeadersNoOptional headers including PayPal-Request-Id and Prefer.

Returns: Order|error

Sample code:

orders:Order capturedOrder = check paypalOrders->/orders/[orderId]/capture.post({});

Sample response:

{
"id": "5O190127TN364715T",
"status": "COMPLETED",
"intent": "CAPTURE",
"purchase_units": [
{
"reference_id": "default",
"payments": {
"captures": [
{
"id": "3C679366HH908993F",
"status": "COMPLETED",
"amount": {
"currency_code": "USD",
"value": "200.00"
}
}
]
}
}
]
}

Shipment tracking

Add tracking information for an order

Adds tracking information for a captured order, including carrier, tracking number, and shipment status.

Parameters:

NameTypeRequiredDescription
idstringYesThe PayPal order ID.
payloadOrderTrackerRequestYesTracker details including capture_id, tracking_number, and carrier.
headersOrdersTrackCreateHeadersNoOptional headers including PayPal-Auth-Assertion.

Returns: Order|error

Sample code:

orders:Order trackedOrder = check paypalOrders->/orders/[orderId]/track.post({
capture_id: captureId,
tracking_number: "1234567890",
carrier: "ARAMEX",
notify_payer: true
});

Sample response:

{
"id": "5O190127TN364715T",
"status": "COMPLETED",
"purchase_units": [
{
"reference_id": "default",
"shipping": {
"trackers": [
{
"id": "5O190127TN364715T-3C679366HH908993F",
"status": "SHIPPED",
"items": []
}
]
}
}
]
}
Update or cancel tracking information for a PayPal order

Updates tracking information for an existing tracker, such as changing the tracking status to CANCELLED.

Parameters:

NameTypeRequiredDescription
idstringYesThe PayPal order ID.
trackerIdstringYesThe tracker ID (format: orderId-captureId).
payloadPatchRequest (Patch[])YesArray of JSON Patch operations to apply to the tracker.

Returns: error?

Sample code:

check paypalOrders->/orders/[orderId]/trackers/[trackerId].patch([
{
op: "replace",
path: "/status",
value: "CANCELLED"
}
]);