Skip to main content

Actions

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

ClientPurpose
ClientPayPal Payments API v2: authorize, capture, void, and refund payments.

Client

PayPal Payments API v2: authorize, capture, void, and refund payments.

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:CompressionCOMPRESSION_AUTOHTTP compression configuration.
cachehttp:CacheConfig{}HTTP response cache configuration.
validationbooleantrueEnable or disable payload validation.
laxDataBindingbooleantrueAllow lax data binding for response payloads.

Initializing the client

import ballerinax/paypal.payments as paypal;

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

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

Operations

Authorizations

Show details for authorized payment

Retrieves the details of an authorized payment by its PayPal-generated authorization ID.

Parameters:

NameTypeRequiredDescription
authorizationIdstringYesThe PayPal-generated ID for the authorized payment.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Authorization2|error

Sample code:

paypal:Authorization2 auth = check paypal->/authorizations/[authId];

Sample response:

{
"id": "5O190127TN364715T",
"status": "CREATED",
"amount": {
"currency_code": "USD",
"value": "100.00"
},
"create_time": "2025-01-15T10:30:00Z",
"update_time": "2025-01-15T10:30:00Z",
"links": [
{
"href": "https://api-m.sandbox.paypal.com/v2/payments/authorizations/5O190127TN364715T",
"rel": "self",
"method": "GET"
}
]
}
Capture authorized payment

Captures an authorized payment, transferring the funds from the buyer to the merchant.

Parameters:

NameTypeRequiredDescription
authorizationIdstringYesThe PayPal-generated ID for the authorized payment.
payloadCaptureRequestYesCapture details including amount and optional note to payer.
headersAuthorizationsCaptureHeadersNoOptional headers including PayPal-Request-Id and Prefer.

Returns: Capture2|error

Sample code:

paypal:CaptureRequest capturePayload = {
amount: {
value: "100.00",
currency_code: "USD"
},
note_to_payer: "Payment for premium headphones"
};
paypal:Capture2 captureResponse = check paypal->/authorizations/[authId]/capture.post(capturePayload);

Sample response:

{
"id": "2GG279541U471931P",
"status": "COMPLETED",
"amount": {
"currency_code": "USD",
"value": "100.00"
},
"seller_receivable_breakdown": {
"gross_amount": {
"currency_code": "USD",
"value": "100.00"
},
"paypal_fee": {
"currency_code": "USD",
"value": "3.50"
},
"net_amount": {
"currency_code": "USD",
"value": "96.50"
}
},
"create_time": "2025-01-15T10:35:00Z",
"update_time": "2025-01-15T10:35:00Z",
"links": [
{
"href": "https://api-m.sandbox.paypal.com/v2/payments/captures/2GG279541U471931P",
"rel": "self",
"method": "GET"
}
]
}
Reauthorize authorized payment

Reauthorizes an authorized payment. Use this to extend the authorization period or reauthorize after the original authorization has expired.

Parameters:

NameTypeRequiredDescription
authorizationIdstringYesThe PayPal-generated ID for the authorized payment.
payloadReauthorizeRequestYesReauthorization details including the amount.
headersAuthorizationsReauthorizeHeadersNoOptional headers including PayPal-Request-Id and Prefer.

Returns: Authorization2|error

Sample code:

paypal:ReauthorizeRequest reauthorizePayload = {
amount: {
value: "100.00",
currency_code: "USD"
}
};
paypal:Authorization2 reauth = check paypal->/authorizations/[authId]/reauthorize.post(reauthorizePayload);

Sample response:

{
"id": "8AA831015G517922L",
"status": "CREATED",
"amount": {
"currency_code": "USD",
"value": "100.00"
},
"create_time": "2025-01-20T14:00:00Z",
"update_time": "2025-01-20T14:00:00Z",
"links": [
{
"href": "https://api-m.sandbox.paypal.com/v2/payments/authorizations/8AA831015G517922L",
"rel": "self",
"method": "GET"
}
]
}
Void authorized payment

Voids an authorized payment. Once voided, the authorization is no longer valid and funds cannot be captured.

Parameters:

NameTypeRequiredDescription
authorizationIdstringYesThe PayPal-generated ID for the authorized payment to void.
headersAuthorizationsVoidHeadersNoOptional headers including PayPal-Auth-Assertion and Prefer.

Returns: Authorization2|error?

Sample code:

paypal:Authorization2? voidResponse = check paypal->/authorizations/[authId]/void.post();

Sample response:

{
"id": "5O190127TN364715T",
"status": "VOIDED",
"amount": {
"currency_code": "USD",
"value": "100.00"
},
"create_time": "2025-01-15T10:30:00Z",
"update_time": "2025-01-16T09:00:00Z",
"links": [
{
"href": "https://api-m.sandbox.paypal.com/v2/payments/authorizations/5O190127TN364715T",
"rel": "self",
"method": "GET"
}
]
}

Captures

Show captured payment details

Retrieves the details of a captured payment by its PayPal-generated capture ID.

Parameters:

NameTypeRequiredDescription
captureIdstringYesThe PayPal-generated ID for the captured payment.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Capture2|error

Sample code:

paypal:Capture2 capture = check paypal->/captures/[captureId];

Sample response:

{
"id": "2GG279541U471931P",
"status": "COMPLETED",
"amount": {
"currency_code": "USD",
"value": "100.00"
},
"seller_receivable_breakdown": {
"gross_amount": {
"currency_code": "USD",
"value": "100.00"
},
"paypal_fee": {
"currency_code": "USD",
"value": "3.50"
},
"net_amount": {
"currency_code": "USD",
"value": "96.50"
}
},
"create_time": "2025-01-15T10:35:00Z",
"update_time": "2025-01-15T10:35:00Z",
"links": [
{
"href": "https://api-m.sandbox.paypal.com/v2/payments/captures/2GG279541U471931P",
"rel": "self",
"method": "GET"
}
]
}
Refund captured payment

Refunds a captured payment. You can issue a full or partial refund.

Parameters:

NameTypeRequiredDescription
captureIdstringYesThe PayPal-generated ID for the captured payment to refund.
payloadRefundRequestYesRefund details including optional amount (omit for full refund) and note to payer.
headersCapturesRefundHeadersNoOptional headers including PayPal-Request-Id, PayPal-Auth-Assertion, and Prefer.

Returns: Refund|error

Sample code:

paypal:RefundRequest refundPayload = {
amount: {
value: "50.00",
currency_code: "USD"
},
note_to_payer: "Partial refund: Month 1"
};
paypal:Refund refundResponse = check paypal->/captures/[captureId]/refund.post(refundPayload);

Sample response:

{
"id": "1JU08902781691411",
"status": "COMPLETED",
"amount": {
"currency_code": "USD",
"value": "50.00"
},
"seller_payable_breakdown": {
"gross_amount": {
"currency_code": "USD",
"value": "50.00"
},
"paypal_fee": {
"currency_code": "USD",
"value": "1.75"
},
"net_amount": {
"currency_code": "USD",
"value": "48.25"
}
},
"create_time": "2025-01-16T11:00:00Z",
"update_time": "2025-01-16T11:00:00Z",
"links": [
{
"href": "https://api-m.sandbox.paypal.com/v2/payments/refunds/1JU08902781691411",
"rel": "self",
"method": "GET"
}
]
}

Refunds

Show refund details

Retrieves the details of a refund by its PayPal-generated refund ID.

Parameters:

NameTypeRequiredDescription
refundIdstringYesThe PayPal-generated ID for the refund.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Refund|error

Sample code:

paypal:Refund refund = check paypal->/refunds/[refundId];

Sample response:

{
"id": "1JU08902781691411",
"status": "COMPLETED",
"amount": {
"currency_code": "USD",
"value": "50.00"
},
"seller_payable_breakdown": {
"gross_amount": {
"currency_code": "USD",
"value": "50.00"
},
"paypal_fee": {
"currency_code": "USD",
"value": "1.75"
},
"net_amount": {
"currency_code": "USD",
"value": "48.25"
}
},
"create_time": "2025-01-16T11:00:00Z",
"update_time": "2025-01-16T11:00:00Z",
"links": [
{
"href": "https://api-m.sandbox.paypal.com/v2/payments/refunds/1JU08902781691411",
"rel": "self",
"method": "GET"
}
]
}