KosmoKrator

data

PayPal Lua API for KosmoKrator Agents

Agent-facing Lua documentation and function reference for the PayPal KosmoKrator integration.

Lua Namespace

Agents call this integration through app.integrations.paypal.*. Use lua_read_doc("integrations.paypal") inside KosmoKrator to discover the same reference at runtime.

Call Lua from the Headless CLI

Use kosmo integrations:lua when a shell script, CI job, cron job, or another coding CLI should run a deterministic PayPal workflow without starting an interactive agent session.

Inline Lua call
kosmo integrations:lua --eval 'dump(app.integrations.paypal.get_order({order_id = "example_order_id"}))' --json
Read Lua docs headlessly
kosmo integrations:lua --eval 'print(docs.read("paypal"))' --json
kosmo integrations:lua --eval 'print(docs.read("paypal.get_order"))' --json

Workflow file

Put repeatable logic in a Lua file, then execute it with JSON output for the calling process.

workflow.lua
local paypal = app.integrations.paypal
local result = paypal.get_order({order_id = "example_order_id"})

dump(result)
Run the workflow
kosmo integrations:lua workflow.lua --json
kosmo integrations:lua workflow.lua --force --json
Namespace note. integrations:lua exposes app.integrations.paypal, app.mcp.*, docs.*, json.*, and regex.*. Use app.integrations.paypal.default.* or app.integrations.paypal.work.* when you configured named credential accounts.

MCP-only Lua

If the script only needs configured MCP servers and does not need PayPal, use the narrower mcp:lua command.

MCP Lua command
# Use mcp:lua for MCP-only scripts; use integrations:lua for this integration namespace.
kosmo mcp:lua --eval 'dump(mcp.servers())' --json

Agent-Facing Lua Docs

This is the rendered version of the full Lua documentation exposed to agents when they inspect the integration namespace.

PayPal — Lua API Reference

get_order

Get details of a specific PayPal checkout order.

Parameters

NameTypeRequiredDescription
order_idstringyesThe PayPal order ID

Example

local result = app.integrations.paypal.get_order({
  order_id = "5O190127TN364715T"
})

print("Status: " .. result.status)
print("Intent: " .. result.intent)

for _, unit in ipairs(result.purchase_units or {}) do
  print("Amount: " .. unit.amount.value .. " " .. unit.amount.currency_code)
end

create_order

Create a new PayPal checkout order.

Parameters

NameTypeRequiredDescription
intentstringyesCAPTURE or AUTHORIZE
purchase_unitsarrayyesArray of purchase units with amount objects
payerarraynoPayer information (name, email, address)
payment_sourcearraynoPayment source configuration (paypal, card)

Purchase Unit Structure

Each purchase unit must include an amount object:

{
  amount = {
    currency_code = "USD",
    value = "10.00"
  },
  description = "Order description"
}

Examples

-- Create a simple capture order
local result = app.integrations.paypal.create_order({
  intent = "CAPTURE",
  purchase_units = {
    {
      amount = {
        currency_code = "USD",
        value = "29.99"
      },
      description = "Premium subscription"
    }
  }
})

print("Order ID: " .. result.id)
print("Status: " .. result.status)

-- Get the approval URL
for _, link in ipairs(result.links or {}) do
  if link.rel == "approve" then
    print("Approval URL: " .. link.href)
  end
end
-- Create an order with payer details
local result = app.integrations.paypal.create_order({
  intent = "CAPTURE",
  purchase_units = {
    {
      amount = {
        currency_code = "EUR",
        value = "49.99",
        breakdown = {
          item_total = { currency_code = "EUR", value = "49.99" }
        }
      },
      items = {
        {
          name = "Service Plan",
          unit_amount = { currency_code = "EUR", value = "49.99" },
          quantity = "1"
        }
      }
    }
  },
  payer = {
    name = { given_name = "John", surname = "Doe" },
    email_address = "john@example.com"
  }
})

capture_order

Capture a previously approved PayPal checkout order.

PayPal Orders v2 supports creating, retrieving, and capturing orders by ID. It does not provide a general list-orders endpoint, so agents should store the order ID returned by create_order or retrieve it from the host application’s own payment records.

Parameters

NameTypeRequiredDescription
order_idstringyesThe approved PayPal order ID
payment_sourcearraynoOptional payment source data for the capture request

Example

local result = app.integrations.paypal.capture_order({
  order_id = "5O190127TN364715T"
})

print("Capture status: " .. result.status)

list_payments

List PayPal payments with optional filters.

Parameters

NameTypeRequiredDescription
countintegernoNumber of payments to return (default: 10, max: 20)
start_idstringnoPayment ID to start from (for pagination)
start_timestringnoStart time filter (ISO 8601)
end_timestringnoEnd time filter (ISO 8601)
sort_bystringnoSort field: create_time or update_time
sort_orderstringnoSort direction: asc or desc

Example

local result = app.integrations.paypal.list_payments({
  count = 10,
  sort_by = "create_time",
  sort_order = "desc"
})

for _, payment in ipairs(result.payments or {}) do
  print(payment.id .. " — " .. payment.state .. " — " .. payment.transactions[1].amount.total)
end

get_payment

Get details of a specific PayPal payment.

Parameters

NameTypeRequiredDescription
payment_idstringyesThe PayPal payment ID

Example

local result = app.integrations.paypal.get_payment({
  payment_id = "PAY-1AB23456CD789012EFGHIJKL"
})

print("State: " .. result.state)
print("Intent: " .. result.intent)

for _, tx in ipairs(result.transactions or {}) do
  print("Amount: " .. tx.amount.total .. " " .. tx.amount.currency)
  print("Description: " .. (tx.description or "N/A"))
end

list_invoices

List PayPal invoices.

Parameters

NameTypeRequiredDescription
pageintegernoPage number (default: 1)
page_sizeintegernoInvoices per page (default: 20, max: 100)
total_requiredbooleannoInclude total count in response (default: false)
fieldsstringnoComma-separated fields to return

Example

local result = app.integrations.paypal.list_invoices({
  page = 1,
  page_size = 20,
  total_required = true
})

if result.total_items then
  print("Total invoices: " .. result.total_items)
end

for _, inv in ipairs(result.items or {}) do
  print(inv.id .. " — " .. (inv.status or "UNKNOWN") .. " — " .. (inv.invoice_number or ""))
end

get_current_user

Get the authenticated PayPal user’s profile information.

Parameters

NameTypeRequiredDescription
schemastringnoSchema to return: paypalv1.1 or openid

Example

local result = app.integrations.paypal.get_current_user({})

print("Name: " .. (result.name or "N/A"))
print("Email: " .. (result.email or "N/A"))
print("User ID: " .. (result.user_id or "N/A"))

Multi-Account Usage

If you have multiple PayPal accounts configured, use account-specific namespaces:

-- Default account (always works)
app.integrations.paypal.get_current_user({})

-- Explicit default (portable across setups)
app.integrations.paypal.default.get_current_user({})

-- Named accounts
app.integrations.paypal.business.get_current_user({})
app.integrations.paypal.personal.get_current_user({})

All functions are identical across accounts — only the credentials differ.

Raw agent markdown
# PayPal — Lua API Reference

## get_order

Get details of a specific PayPal checkout order.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `order_id` | string | yes | The PayPal order ID |

### Example

```lua
local result = app.integrations.paypal.get_order({
  order_id = "5O190127TN364715T"
})

print("Status: " .. result.status)
print("Intent: " .. result.intent)

for _, unit in ipairs(result.purchase_units or {}) do
  print("Amount: " .. unit.amount.value .. " " .. unit.amount.currency_code)
end
```

---

## create_order

Create a new PayPal checkout order.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `intent` | string | yes | `CAPTURE` or `AUTHORIZE` |
| `purchase_units` | array | yes | Array of purchase units with amount objects |
| `payer` | array | no | Payer information (name, email, address) |
| `payment_source` | array | no | Payment source configuration (paypal, card) |

### Purchase Unit Structure

Each purchase unit must include an `amount` object:

```lua
{
  amount = {
    currency_code = "USD",
    value = "10.00"
  },
  description = "Order description"
}
```

### Examples

```lua
-- Create a simple capture order
local result = app.integrations.paypal.create_order({
  intent = "CAPTURE",
  purchase_units = {
    {
      amount = {
        currency_code = "USD",
        value = "29.99"
      },
      description = "Premium subscription"
    }
  }
})

print("Order ID: " .. result.id)
print("Status: " .. result.status)

-- Get the approval URL
for _, link in ipairs(result.links or {}) do
  if link.rel == "approve" then
    print("Approval URL: " .. link.href)
  end
end
```

```lua
-- Create an order with payer details
local result = app.integrations.paypal.create_order({
  intent = "CAPTURE",
  purchase_units = {
    {
      amount = {
        currency_code = "EUR",
        value = "49.99",
        breakdown = {
          item_total = { currency_code = "EUR", value = "49.99" }
        }
      },
      items = {
        {
          name = "Service Plan",
          unit_amount = { currency_code = "EUR", value = "49.99" },
          quantity = "1"
        }
      }
    }
  },
  payer = {
    name = { given_name = "John", surname = "Doe" },
    email_address = "john@example.com"
  }
})
```

---

## capture_order

Capture a previously approved PayPal checkout order.

PayPal Orders v2 supports creating, retrieving, and capturing orders by ID. It does not provide a general list-orders endpoint, so agents should store the order ID returned by `create_order` or retrieve it from the host application's own payment records.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `order_id` | string | yes | The approved PayPal order ID |
| `payment_source` | array | no | Optional payment source data for the capture request |

### Example

```lua
local result = app.integrations.paypal.capture_order({
  order_id = "5O190127TN364715T"
})

print("Capture status: " .. result.status)
```

---

## list_payments

List PayPal payments with optional filters.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `count` | integer | no | Number of payments to return (default: 10, max: 20) |
| `start_id` | string | no | Payment ID to start from (for pagination) |
| `start_time` | string | no | Start time filter (ISO 8601) |
| `end_time` | string | no | End time filter (ISO 8601) |
| `sort_by` | string | no | Sort field: `create_time` or `update_time` |
| `sort_order` | string | no | Sort direction: `asc` or `desc` |

### Example

```lua
local result = app.integrations.paypal.list_payments({
  count = 10,
  sort_by = "create_time",
  sort_order = "desc"
})

for _, payment in ipairs(result.payments or {}) do
  print(payment.id .. " — " .. payment.state .. " — " .. payment.transactions[1].amount.total)
end
```

---

## get_payment

Get details of a specific PayPal payment.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `payment_id` | string | yes | The PayPal payment ID |

### Example

```lua
local result = app.integrations.paypal.get_payment({
  payment_id = "PAY-1AB23456CD789012EFGHIJKL"
})

print("State: " .. result.state)
print("Intent: " .. result.intent)

for _, tx in ipairs(result.transactions or {}) do
  print("Amount: " .. tx.amount.total .. " " .. tx.amount.currency)
  print("Description: " .. (tx.description or "N/A"))
end
```

---

## list_invoices

List PayPal invoices.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `page` | integer | no | Page number (default: 1) |
| `page_size` | integer | no | Invoices per page (default: 20, max: 100) |
| `total_required` | boolean | no | Include total count in response (default: false) |
| `fields` | string | no | Comma-separated fields to return |

### Example

```lua
local result = app.integrations.paypal.list_invoices({
  page = 1,
  page_size = 20,
  total_required = true
})

if result.total_items then
  print("Total invoices: " .. result.total_items)
end

for _, inv in ipairs(result.items or {}) do
  print(inv.id .. " — " .. (inv.status or "UNKNOWN") .. " — " .. (inv.invoice_number or ""))
end
```

---

## get_current_user

Get the authenticated PayPal user's profile information.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `schema` | string | no | Schema to return: `paypalv1.1` or `openid` |

### Example

```lua
local result = app.integrations.paypal.get_current_user({})

print("Name: " .. (result.name or "N/A"))
print("Email: " .. (result.email or "N/A"))
print("User ID: " .. (result.user_id or "N/A"))
```

---

## Multi-Account Usage

If you have multiple PayPal accounts configured, use account-specific namespaces:

```lua
-- Default account (always works)
app.integrations.paypal.get_current_user({})

-- Explicit default (portable across setups)
app.integrations.paypal.default.get_current_user({})

-- Named accounts
app.integrations.paypal.business.get_current_user({})
app.integrations.paypal.personal.get_current_user({})
```

All functions are identical across accounts — only the credentials differ.
Metadata-derived Lua example
local result = app.integrations.paypal.get_order({order_id = "example_order_id"})
print(result)

Functions

get_order Read

Get details of a specific PayPal checkout order by its order ID. Returns full order information including status, payer details, and line items.

Lua path
app.integrations.paypal.get_order
Full name
paypal.paypal_get_order
ParameterTypeRequiredDescription
order_id string yes The PayPal order ID (e.g., "5O190127TN364715T").
create_order Write

Create a new PayPal checkout order. Specify the intent, purchase units with amounts, and optional payer details. Returns the created order with approval links.

Lua path
app.integrations.paypal.create_order
Full name
paypal.paypal_create_order
ParameterTypeRequiredDescription
intent string yes The order intent: "CAPTURE" to capture funds immediately, or "AUTHORIZE" to authorize and capture later.
purchase_units array yes Array of purchase units. Each unit must have an "amount" object with "currency_code" and "value". May include "description", "items", and "shipping".
payer array no Payer information: name, email_address, address, phone.
payment_source array no Payment source configuration (e.g., paypal, card).
capture_order Write

Capture a previously approved PayPal checkout order by order ID. Use after the payer approves an order created with intent CAPTURE.

Lua path
app.integrations.paypal.capture_order
Full name
paypal.paypal_capture_order
ParameterTypeRequiredDescription
order_id string yes The approved PayPal checkout order ID.
payment_source array no Optional payment source data to include in the capture request.
list_payments Read

List PayPal payments. Returns payment IDs, states, amounts, and transaction details. Use filters to narrow results by count or date range.

Lua path
app.integrations.paypal.list_payments
Full name
paypal.paypal_list_payments
ParameterTypeRequiredDescription
count integer no Number of payments to return (default: 10, max: 20).
start_id string no The ID of the first payment to return. Used for pagination.
start_time string no Start time for filtering (ISO 8601, e.g., "2025-01-01T00:00:00Z").
end_time string no End time for filtering (ISO 8601, e.g., "2025-12-31T23:59:59Z").
sort_by string no Sort field: "create_time" or "update_time".
sort_order string no Sort direction: "asc" or "desc".
get_payment Read

Get details of a specific PayPal payment by its payment ID. Returns full payment information including state, amount, payer details, and transactions.

Lua path
app.integrations.paypal.get_payment
Full name
paypal.paypal_get_payment
ParameterTypeRequiredDescription
payment_id string yes The PayPal payment ID (e.g., "PAY-1AB23456CD789012EFGHIJKL").
list_invoices Read

List PayPal invoices. Returns invoice IDs, numbers, statuses, amounts, and recipient details. Use pagination parameters to navigate through results.

Lua path
app.integrations.paypal.list_invoices
Full name
paypal.paypal_list_invoices
ParameterTypeRequiredDescription
page integer no Page number for pagination (default: 1).
page_size integer no Number of invoices per page (default: 20, max: 100).
total_required boolean no Whether to include the total count of invoices in the response (default: false).
fields string no Comma-separated list of fields to return (e.g., "items,payments").
get_current_user Read

Get the authenticated PayPal user's profile information. Returns the user's name, email, and other account details.

Lua path
app.integrations.paypal.get_current_user
Full name
paypal.paypal_get_current_user
ParameterTypeRequiredDescription
schema string no Schema to return: "paypalv1.1" or "openid". Default: "paypalv1.1".