Skip to main content

Actions

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

ClientPurpose
ClientPayPal Invoicing API v2: invoice CRUD, send/remind/cancel, payments, refunds, search, QR codes, and templates.

Client

PayPal Invoicing API v2: invoice CRUD, send/remind/cancel, payments, refunds, search, QR codes, and templates.

Configuration

FieldTypeDefaultDescription
authOAuth2ClientCredentialsGrantConfigRequiredOAuth 2.0 client credentials grant configuration. Requires clientId and clientSecret. Default token URL is https://api-m.sandbox.paypal.com/v1/oauth2/token.
httpVersionHttpVersionHTTP_2_0HTTP protocol version.
timeoutdecimal30Request timeout in seconds.
retryConfigRetryConfig()Retry configuration for failed requests.
secureSocketClientSecureSocket()SSL/TLS configuration.
proxyProxyConfig()Proxy server configuration.
validationbooleantrueEnable/disable payload validation.
laxDataBindingbooleantrueUse lax data binding for response payloads.

Initializing the client

import ballerinax/paypal.invoices;

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

invoices:Client invoicesClient = check new ({
auth: {
clientId: clientId,
clientSecret: clientSecret
}
});

Operations

Invoice CRUD

List invoices

Lists invoices with optional pagination and field filtering.

Parameters:

NameTypeRequiredDescription
headersmap<string|string[]>NoOptional HTTP headers.
queriesInvoicesListQueriesNoQuery parameters: page (default 1), page_size (default 20), fields (default "all"), total_required (default false).

Returns: Invoices|error

Sample code:

invoices:Invoices result = check invoicesClient->/invoices();

Sample response:

{
"total_items": 2,
"total_pages": 1,
"items": [
{
"id": "INV2-XXXX-XXXX-XXXX-XXXX",
"status": "DRAFT",
"detail": {
"currency_code": "USD",
"invoice_number": "#001",
"invoice_date": "2024-01-15"
}
}
],
"links": [
{"href": "https://api-m.sandbox.paypal.com/v2/invoicing/invoices?page=1&page_size=20&total_required=false", "rel": "self", "method": "GET"}
]
}
Create draft invoice

Creates a draft invoice. The invoice must include a detail record with at least a currency_code.

Parameters:

NameTypeRequiredDescription
payloadInvoiceYesInvoice data including detail, items, invoicer, and recipient information.
headersInvoicesCreateHeadersNoOptional headers. Prefer defaults to "return=representation" to return the full invoice.

Returns: Invoice|error

Sample code:

invoices:Invoice invoice = check invoicesClient->/invoices.post({
detail: {
currency_code: "USD",
invoice_number: "#INV-001",
invoice_date: "2024-03-15",
payment_term: {
term_type: "NET_30"
}
},
invoicer: {
business_name: "Acme Corp",
email_address: "[email protected]"
},
primary_recipients: [
{
billing_info: {
email_address: "[email protected]"
}
}
],
items: [
{
name: "Consulting Services",
quantity: "10",
unit_amount: {
currency_code: "USD",
value: "150.00"
},
unit_of_measure: "HOURS"
}
]
});

Sample response:

{
"id": "INV2-XXXX-XXXX-XXXX-XXXX",
"status": "DRAFT",
"detail": {
"currency_code": "USD",
"invoice_number": "#INV-001",
"invoice_date": "2024-03-15",
"payment_term": {"term_type": "NET_30"}
},
"invoicer": {
"business_name": "Acme Corp",
"email_address": "[email protected]"
},
"primary_recipients": [{"billing_info": {"email_address": "[email protected]"}}],
"items": [{"name": "Consulting Services", "quantity": "10", "unit_amount": {"currency_code": "USD", "value": "150.00"}, "unit_of_measure": "HOURS"}],
"amount": {"currency_code": "USD", "value": "1500.00"},
"links": [{"href": "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/INV2-XXXX-XXXX-XXXX-XXXX", "rel": "self", "method": "GET"}]
}
Show invoice details

Retrieves the full details of an invoice by its ID.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID (e.g., "INV2-XXXX-XXXX-XXXX-XXXX").
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Invoice|error

Sample code:

invoices:Invoice invoice = check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"];

Sample response:

{
"id": "INV2-XXXX-XXXX-XXXX-XXXX",
"status": "SENT",
"detail": {
"currency_code": "USD",
"invoice_number": "#INV-001",
"invoice_date": "2024-03-15",
"payment_term": {"term_type": "NET_30", "due_date": "2024-04-14"}
},
"amount": {"currency_code": "USD", "value": "1500.00"},
"due_amount": {"currency_code": "USD", "value": "1500.00"}
}
Fully update invoice

Fully updates an invoice by replacing the entire invoice object. Only invoices in DRAFT or SENT status can be updated.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
payloadInvoiceYesThe full updated invoice object.
headersInvoicesUpdateHeadersNoOptional headers. Prefer defaults to "return=representation".
queriesInvoicesUpdateQueriesNoQuery parameters: send_to_recipient (default true), send_to_invoicer (default true).

Returns: Invoice|error

Sample code:

invoices:Invoice updated = check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"].put({
detail: {
currency_code: "USD",
invoice_number: "#INV-001",
invoice_date: "2024-03-15",
note: "Updated terms"
},
items: [
{
name: "Consulting Services",
quantity: "15",
unit_amount: {
currency_code: "USD",
value: "150.00"
},
unit_of_measure: "HOURS"
}
]
});

Sample response:

{
"id": "INV2-XXXX-XXXX-XXXX-XXXX",
"status": "DRAFT",
"detail": {
"currency_code": "USD",
"invoice_number": "#INV-001",
"invoice_date": "2024-03-15",
"note": "Updated terms"
},
"items": [{"name": "Consulting Services", "quantity": "15", "unit_amount": {"currency_code": "USD", "value": "150.00"}, "unit_of_measure": "HOURS"}],
"amount": {"currency_code": "USD", "value": "2250.00"}
}
Delete invoice

Deletes a draft invoice. Only invoices in DRAFT status can be deleted.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"].delete();

Send, remind & cancel

Send invoice

Sends an invoice to the recipient. Changes the invoice status from DRAFT to SENT.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
payloadNotificationYesNotification options including subject, note, and recipient settings.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: LinkDescription|'202Response|error

Sample code:

invoices:LinkDescription|invoices:'202Response result = check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"]/send.post({
send_to_invoicer: true,
send_to_recipient: true,
subject: "Invoice #INV-001 from Acme Corp",
note: "Please find your invoice attached."
});

Sample response:

{
"href": "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/INV2-XXXX-XXXX-XXXX-XXXX",
"rel": "self",
"method": "GET"
}
Send invoice reminder

Sends a reminder for an unpaid invoice to the recipient.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
payloadNotificationYesNotification options for the reminder.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"]/remind.post({
send_to_recipient: true,
subject: "Reminder: Invoice #INV-001 is due",
note: "This is a friendly reminder that your invoice is due."
});
Cancel sent invoice

Cancels a sent invoice. The invoice status changes to CANCELLED.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
payloadNotificationYesNotification options for the cancellation notice.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"]/cancel.post({
send_to_recipient: true,
send_to_invoicer: true,
subject: "Invoice #INV-001 cancelled",
note: "This invoice has been cancelled."
});

Payments

Record payment for invoice

Records an external payment against an invoice. Use this to mark payments made outside of PayPal.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
payloadPaymentDetailYesPayment details including method, amount, and date.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: PaymentReference|error

Sample code:

invoices:PaymentReference paymentRef = check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"]/payments.post({
method: "CHECK",
payment_date: "2024-03-20",
amount: {
currency_code: "USD",
value: "1500.00"
}
});

Sample response:

{"payment_id": "EXTR-XXXX-XXXX-XXXX-XXXX"}
Delete external payment

Deletes an externally recorded payment from an invoice.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
transactionIdstringYesThe payment transaction ID.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"]/payments/["EXTR-XXXX-XXXX-XXXX-XXXX"].delete();

Refunds

Record refund for invoice

Records an external refund against an invoice.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
payloadRefundDetailYesRefund details including method, amount, and date.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: RefundReference|error

Sample code:

invoices:RefundReference refundRef = check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"]/refunds.post({
method: "BANK_TRANSFER",
refund_date: "2024-04-01",
amount: {
currency_code: "USD",
value: "500.00"
}
});

Sample response:

{"refund_id": "EXTR-XXXX-XXXX-XXXX-XXXX"}
Delete external refund

Deletes an externally recorded refund from an invoice.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
transactionIdstringYesThe refund transaction ID.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"]/refunds/["EXTR-XXXX-XXXX-XXXX-XXXX"].delete();

Search & utilities

Search for invoices

Searches for invoices matching the specified criteria such as status, dates, recipient, or amount ranges.

Parameters:

NameTypeRequiredDescription
payloadSearchDataYesSearch criteria including status, date ranges, recipient info, currency code, and more.
headersmap<string|string[]>NoOptional HTTP headers.
queriesInvoicesSearchInvoicesQueriesNoQuery parameters: page (default 1), page_size (default 20), total_required (default false).

Returns: Invoices|error

Sample code:

invoices:Invoices results = check invoicesClient->/search\-invoices.post({
status: ["SENT", "UNPAID"],
invoice_date_range: {
'start: "2024-01-01",
end: "2024-12-31"
}
});

Sample response:

{
"total_items": 5,
"total_pages": 1,
"items": [
{
"id": "INV2-XXXX-XXXX-XXXX-XXXX",
"status": "SENT",
"detail": {"currency_code": "USD", "invoice_number": "#INV-001", "invoice_date": "2024-03-15"},
"amount": {"currency_code": "USD", "value": "1500.00"},
"due_amount": {"currency_code": "USD", "value": "1500.00"}
}
]
}
Generate invoice number

Generates the next sequential invoice number.

Parameters:

NameTypeRequiredDescription
headersInvoicingGenerateNextInvoiceNumberHeadersNoOptional headers. Content-Type defaults to "application/json".

Returns: InvoiceNumber|error

Sample code:

invoices:InvoiceNumber nextNumber = check invoicesClient->/generate\-next\-invoice\-number.post();

Sample response:

{"invoice_number": "#0042"}
Generate QR code

Generates a QR code for an invoice. The QR code can be used to direct payers to the invoice payment page.

Parameters:

NameTypeRequiredDescription
invoiceIdstringYesThe PayPal invoice ID.
payloadQrConfigYesQR code configuration: width (default 500), height (default 500), action (default "pay").
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check invoicesClient->/invoices/["INV2-XXXX-XXXX-XXXX-XXXX"]/generate\-qr\-code.post({
width: 300,
height: 300,
action: "pay"
});

Templates

List templates

Lists all invoice templates with optional pagination and field filtering.

Parameters:

NameTypeRequiredDescription
headersmap<string|string[]>NoOptional HTTP headers.
queriesTemplatesListQueriesNoQuery parameters: page (default 1), page_size (default 20), fields (default "all").

Returns: Templates|error

Sample code:

invoices:Templates templates = check invoicesClient->/templates();

Sample response:

{
"templates": [
{
"id": "TEMP-XXXX-XXXX-XXXX-XXXX",
"name": "Standard Invoice",
"default_template": true,
"standard_template": false
}
],
"links": [{"href": "https://api-m.sandbox.paypal.com/v2/invoicing/templates?page=1&page_size=20", "rel": "self", "method": "GET"}]
}
Create template

Creates a new invoice template.

Parameters:

NameTypeRequiredDescription
payloadTemplateYesTemplate data including name, settings, and template info.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Template|error

Sample code:

invoices:Template template = check invoicesClient->/templates.post({
name: "Monthly Retainer",
default_template: false,
template_info: {
detail: {
currency_code: "USD",
note: "Thank you for your business."
},
items: [
{
name: "Monthly Retainer Fee",
quantity: "1",
unit_amount: {
currency_code: "USD",
value: "2000.00"
}
}
]
}
});

Sample response:

{
"id": "TEMP-XXXX-XXXX-XXXX-XXXX",
"name": "Monthly Retainer",
"default_template": false,
"standard_template": false,
"template_info": {
"detail": {"currency_code": "USD", "note": "Thank you for your business."},
"items": [{"name": "Monthly Retainer Fee", "quantity": "1", "unit_amount": {"currency_code": "USD", "value": "2000.00"}}]
},
"links": [{"href": "https://api-m.sandbox.paypal.com/v2/invoicing/templates/TEMP-XXXX-XXXX-XXXX-XXXX", "rel": "self", "method": "GET"}]
}
Show template details

Retrieves the full details of an invoice template by its ID.

Parameters:

NameTypeRequiredDescription
templateIdstringYesThe PayPal template ID.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Template|error

Sample code:

invoices:Template template = check invoicesClient->/templates/["TEMP-XXXX-XXXX-XXXX-XXXX"];

Sample response:

{
"id": "TEMP-XXXX-XXXX-XXXX-XXXX",
"name": "Monthly Retainer",
"default_template": false,
"standard_template": false,
"template_info": {
"detail": {"currency_code": "USD"},
"items": [{"name": "Monthly Retainer Fee", "quantity": "1", "unit_amount": {"currency_code": "USD", "value": "2000.00"}}]
}
}
Update template

Fully updates an invoice template by replacing the entire template object.

Parameters:

NameTypeRequiredDescription
templateIdstringYesThe PayPal template ID.
payloadTemplateYesThe full updated template object.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Template|error

Sample code:

invoices:Template updated = check invoicesClient->/templates/["TEMP-XXXX-XXXX-XXXX-XXXX"].put({
name: "Updated Retainer Template",
default_template: true,
template_info: {
detail: {
currency_code: "USD",
note: "Updated terms apply."
}
}
});

Sample response:

{
"id": "TEMP-XXXX-XXXX-XXXX-XXXX",
"name": "Updated Retainer Template",
"default_template": true,
"standard_template": false,
"template_info": {
"detail": {"currency_code": "USD", "note": "Updated terms apply."}
}
}
Delete template

Deletes an invoice template.

Parameters:

NameTypeRequiredDescription
templateIdstringYesThe PayPal template ID.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check invoicesClient->/templates/["TEMP-XXXX-XXXX-XXXX-XXXX"].delete();