Skip to main content

Checkout

Learn how to receive payments through PayRex Checkout by creating a PayRex-hosted Checkout Page and save time in your integration.

Checkout

1. Set up the server-side

Install the server-side SDK 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_payment_intent.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 CheckoutSession
const checkoutSession = await payrex.checkoutSessions.create({
currency: 'PHP',
// URL where your customer will be redirected after a successful payment.
success_url: 'https://your-website.com/some-url',
// URL where your customer will be redirected if they decide to not proceed with a payment
cancel_url: 'https://your-website.com/some-url',
payment_methods: ['gcash', 'card'],
line_items: [{
name: 'Item 1',
// Amount is in cents. The sample below is 100.00.
amount: 10000,
quantity: 1,
// optional
image: 'Publicly accessible image URL'
}]
});

const output = {
url: checkoutSession.url,
}

console.log(JSON.stringify(output));

Create a checkout session

Create a CheckoutSession from the server side. A CheckoutSession resource represents a one-time use PayRex-hosted checkout page and will expire at a certain period. Return the CheckoutSession's url attribute so that you can redirect your customer to this URL.

After initializing the SDK with your secret API key, you must create a CheckoutSession. Once a CheckoutSession is created, return the CheckoutSession's url attribute in the response. The CheckoutSession's url will be used on the client side, which is the next step of the integration process. Some notable attributes you will normally specify when creating a CheckoutSession:

AttributeDescription
line_itemsThe line_items attribute is the list of items that your customer will buy. For example, the line_items for an e-commerce store are the list of products they will buy from your e-commerce store. If you prefer not to list all the items they bought, you can add just a single line_item with generic line_item name.

The line_items attribute will compute the final amount that your customer will pay. The formula for the final amount is the sum of line_items.quantity * line_items.amount which is automatically computed by the Checkout Session.
success_urlYou must create a page where the customer will be redirected once they successfully paid a CheckoutSession. The CheckoutSession will handle the redirection for you.
cancel_urlYou must create a page where the customer will be redirected once they decided that they don't want to pay and go back to your website. The CheckoutSession will handle the redirection for you.
payment_methodsThe list of payment methods allowed for the CheckoutSession. This is the same payment_methods attribute from the PaymentIntent resource.
info

The CheckoutSession creates a PaymentIntent resource under the hood. If you are not familiar yet with PaymentIntents, you can refer to this guide.

create_payment_intent.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 CheckoutSession
const checkoutSession = await payrex.checkoutSessions.create({
currency: 'PHP',
// URL where your customer will be redirected after a successful payment.
success_url: 'https://your-website.com/some-url',
// URL where your customer will be redirected if they decide to not proceed with a payment
cancel_url: 'https://your-website.com/some-url',
payment_methods: ['gcash', 'card'],
line_items: [{
name: 'Item 1',
// Amount is in cents. The sample below is 100.00.
amount: 10000,
quantity: 1,
// optional
image: 'Publicly accessible image URL'
}]
});

const output = {
url: checkoutSession.url,
}

console.log(JSON.stringify(output));

2. Redirect the customer to the CheckoutSession url and let the customer complete the payment

After creating the CheckoutSession, redirect your customer to the URL for the Checkout page returned in the response from step 1. The URL of the CheckoutSession is referenced via url attribute.

Once the customer completed the payment, they will be redirected to your nominated success_url.

info

A CheckoutSession expires at some point. If the CheckoutSession has transitioned to expired, you must create a new CheckoutSession if you want your customer to complete the payment.