Skip to main content

Manage billing statements with API

In this section, learn how to create and manage billing statements using PayRex API. Manage customers, create a billing statement, and send it.

Managing billing statements via API is best if you want to embed the workflow within your internal system or the platform you currently use. If you prefer to use our Dashboard to manage billing statements, please refer to the no-code section.

If you want to learn about the general overview of billing statements, you can refer to this link.

1. Set up the server-side

Install one of the server-side SDKs and initialize with your secret API key

Install the appropriate SDK depending on your programming language and import it. If we don't support yet the programming language you're using, you may skip this step and use your preferred REST client.

  npm install payrex-node
create_billing_statement.js
// Protect your PayRex Secret API key at all costs. One common approach
// to store it in an environment variable.
// Add your PayRex test secret API key.
const payrexSecretApiKey = '';

const payrex = require('payrex-node')(payrexSecretApiKey);

// Create a customer. If you have an existing customer resource,
// you can use it instead of creating a new one.
const customer = await payrex.customers.create({
currency: 'PHP',
name: 'Juan Dela Cruz',
email: 'juandelacruz@gmail.com'
})

// Create a billing statement
const billingStatement = await payrex.billingStatements.create({
customer_id: customer.id,
payment_settings: {
payment_methods: ['gcash', 'card']
}
});

// some lines of code are removed...

Create a customer resource or retrieve an existing one

A customer resource represents the customer that will pay the billing statement. If you haven't created a customer resource, create one and you can store its ID for future purchases. If a customer resource already exists, use its ID when creating a draft billing statement.

About the customer resource

A customer resource represents the customer that will pay the billing statement. If you haven't created a customer resource, create one, and you can store its ID for future purchases. If a customer resource exists, use its ID when creating a draft billing statement.

create_billing_statement.js
// Protect your PayRex Secret API key at all costs. One common approach
// to store it in an environment variable.
// Add your PayRex test secret API key.
const payrexSecretApiKey = '';

const payrex = require('payrex-node')(payrexSecretApiKey);

// If a customer doesn't exist, create it first.
const customer = await payrex.customers.create({
currency: 'PHP',
name: 'Juan Dela Cruz',
email: 'juandelacruz@gmail.com'
})
// OR
// Retrieve the customer via ID if it already exists
const customer = await payrex.customers.retrieve('cus_xxxxx')
// OR
// Retrieve the customer via metadata or email
const customers = await payrex.customers.list({
email: 'juandelacruz@gmail.com'
})

// Create a billing statement
const billingStatement = await payrex.billingStatements.create({
customer_id: customer.id,
payment_settings: {
payment_methods: ['gcash', 'card']
}
});

// some lines of code are removed...

Create a billing statement

Create a billing statement after a customer resource is created or retrieved. A billing statement has draft status once created.

create_billing_statement.js
// Protect your PayRex Secret API key at all costs. One common approach
// to store it in an environment variable.
// Add your PayRex test secret API key.
const payrexSecretApiKey = '';

const payrex = require('payrex-node')(payrexSecretApiKey);

// If a customer doesn't exist, create it first.
const customer = await payrex.customers.create({
currency: 'PHP',
name: 'Juan Dela Cruz',
email: 'juandelacruz@gmail.com'
})
// OR
// Retrieve the customer via ID if it already exists
const customer = await payrex.customers.retrieve('cus_xxxxx')
// OR
// Retrieve the customer via metadata or email
const customers = await payrex.customers.list({
email: 'juandelacruz@gmail.com'
})

// Create a billing statement
const billingStatement = await payrex.billingStatements.create({
customer_id: customer.id,
payment_settings: {
payment_methods: ['gcash', 'card']
}
});

// some lines of code are removed...

Complete the required details of a billing statement

After you create a draft billing statement, you must update the required details before you can finalize it and send it to your customer.

create_billing_statement.js
// Protect your PayRex Secret API key at all costs. One common approach
// to store it in an environment variable.
// Add your PayRex test secret API key.
const payrexSecretApiKey = '';

const payrex = require('payrex-node')(payrexSecretApiKey);

// If a customer doesn't exist, create it first.
const customer = await payrex.customers.create({
currency: 'PHP',
name: 'Juan Dela Cruz',
email: 'juandelacruz@gmail.com'
})
// OR
// Retrieve the customer via ID if it already exists
const customer = await payrex.customers.retrieve('cus_xxxxx')
// OR
// Retrieve the customer via metadata or email
const customers = await payrex.customers.list({
email: 'juandelacruz@gmail.com'
})

// Create a billing statement
let billingStatement = await payrex.billingStatements.create({
customer_id: customer.id,
payment_settings: {
payment_methods: ['gcash', 'card']
}
});

billingStatement = await payrex.billingStatements.update(
billingStatement.id,
{
// due_at is set to current epoch timestamp. You can set either past or future date
due_at: Math.floor((new Date()) / 1000)
}
)

// Create a single line item just to finalize the billing statement later.
await payrex.billingStatementLineItems.create(
{
billing_statement_id: billingStatement.id,
unit_price: 2000,
quantity: 1,
description: "My Product"
}
)
// some lines of code are removed...

Finalize a billing statement

Once the required details are filled out, you can finalize the billing statement. Once finalized, you cannot edit the details anymore, so make sure that the details are correct. If you need to edit the details, you must create a new billing statement.

create_billing_statement.js
// Protect your PayRex Secret API key at all costs. One common approach
// to store it in an environment variable.
// Add your PayRex test secret API key.
const payrexSecretApiKey = '';

const payrex = require('payrex-node')(payrexSecretApiKey);

// If a customer doesn't exist, create it first.
const customer = await payrex.customers.create({
currency: 'PHP',
name: 'Juan Dela Cruz',
email: 'juandelacruz@gmail.com'
})
// OR
// Retrieve the customer via ID if it already exists
const customer = await payrex.customers.retrieve('cus_xxxxx')
// OR
// Retrieve the customer via metadata or email
const customers = await payrex.customers.list({
email: 'juandelacruz@gmail.com'
})

// Create a billing statement
let billingStatement = await payrex.billingStatements.create({
customer_id: customer.id,
payment_settings: {
payment_methods: ['gcash', 'card']
}
});

billingStatement = await payrex.billingStatements.update(
billingStatement.id,
{
// due_at is set to current epoch timestamp. You can set either past or future date
due_at: Math.floor((new Date()) / 1000)
}
)

// Create a single line item just to finalize the billing statement later.
await payrex.billingStatementLineItems.create(
{
unit_price: 2000,
quantity: 1,
description: "My Product"
}
)

billingStatement = await payrex.billingStatements.finalize(billingStatement.id)

2. Send the billing statement to your customer

Once the billing statement is finalized, some details will be updated e.g. the billing statement's URL. You can now send the billing statement to your customer.

The billing statement has a URL attribute where you can copy and send to your customer. You can send to any IMs such as Viber, messenger, etc.

3. Subscribe to your customer payments

The best way to monitor payments from your customers is through webhooks. You can create a webhook and subscribe to payment_intent.succeeded event. A billing statement has a payment intent attribute you can reference from your webhook. If you don't know yet about payment intents, refer to this guide.

You can refer to this guide to know more about actions you can take after receiving a payment.

COMING SOON

We will create billing_statement related webhook events soon. In the meantime, you can refer to the payment_intent events to get notified about customer payments.

Voiding, marking as uncollectible and deleting billing statements [OPTIONAL]

You can call our APIs to automate voiding, marking as uncollectible, and deleting billing statements.

Voiding a billing statement

void_billing_statement.js
// Protect your PayRex Secret API key at all costs. One common approach
// to store it in an environment variable.
// Add your PayRex test secret API key.
const payrexSecretApiKey = '';

const payrex = require('payrex-node')(payrexSecretApiKey);


await payrex.billingStatements.void('<insert billing statement id>');

Mark uncollectible a billing statement

mark_uncollectible_billing_statement.js
// Protect your PayRex Secret API key at all costs. One common approach
// to store it in an environment variable.
// Add your PayRex test secret API key.
const payrexSecretApiKey = '';

const payrex = require('payrex-node')(payrexSecretApiKey);


await payrex.billingStatements.markUncollectible('<insert billing statement id>');

Delete a billing statement

delete_billing_statement.js
// Protect your PayRex Secret API key at all costs. One common approach
// to store it in an environment variable.
// Add your PayRex test secret API key.
const payrexSecretApiKey = '';

const payrex = require('payrex-node')(payrexSecretApiKey);


await payrex.billingStatements.delete('<insert billing statement id>');