End-to-end FramePay integration

This topic describes how to implement:

  • FramePay in a checkout page and use it to tokenize payments.
  • A server-side controller to complete a purchase transaction.
attention

This guide uses Node.js (Express) as the server-side language for the implementation.

1. Obtain IDs and API keys

  1. Obtain your organization ID and website ID:
    1. In the left navigation bar, click Settings icon.
    2. Click My organization & websites.
    3. In the Organization info section, note the ID value.
    4. In the Website section, note the ID value. A website and organization is created automatically when you sign up to Rebilly. For more information, see Organizations and websites.
  2. Obtain your publishable API key:
    1. In the left navigation bar, click Automation icon.
    2. Click API keys & webhooks, then click API keys.
    3. Optionally, if you have not created a publishable key:
      1. In top right of the screen, click Add API Key.
      2. In the API Key Type section, select Publishable, then complete the form and click Save API key.
      3. Go back to the API Keys page.
    4. Select a publishable key and copy the Key value.
  3. Obtain your secret API key:
    1. In the left navigation bar, click Automation icon.
    2. Click API keys & webhooks, then click API keys.
    3. Optionally, if you have not created a secret key:
      1. In top right of the screen, click Add API Key.
      2. In the API Key Type section, select Secret, then complete the form and click Save API key.
      3. Go back to the API Keys page.
    4. Select a secret key and copy the Key value.

2. Set up FramePay

This step describes how to:

  • Use FramePay to tokenize payment card information.
  • Set up a simple server using the Rebilly JS SDK.
  • Complete a purchase transaction using the tokenized payment.
attention

The codes described in this topic is available in the Rebilly framepay-integrated-example GitHub repository.

1

Set up the server

Install Rebilly JS SDK

Install the package, import it in to your code, and then initialize with your secret key and organization ID.

To install, run:

yarn add rebilly-js-sdk@^46.4.0

For more information, see JS SDK quickstart guide

server.js

Create a transaction

1. Add an endpoint on your server that creates a transaction. This endpoint must receive the FramePay-generated token as the payload

2. Create a controller:

  1. Create a customer using the billing address and the payment token.
  2. Using the customerId, create sale transaction.
server.js
2

Create a checkout page

Include the FramePay stylesheet

This adds default styles to FramePay elements on the page.

checkout.html

Include the FramePay script

This exposes FramePay in the global JS scope as Rebilly.

checkout.html

Create your checkout form

FramePay automatically gathers data from your checkout form.

To enable this, you must use data-rebilly attributes on your input fields.

checkout.html

Include the HTML mounting points

Add an empty div to your checkout form.

This is where FramePay will render the card field by injecting an iframe.

checkout.html

Initialize FramePay

Initialize FramePay with a configuration object.

Provide your publishable API key, organization ID, and website ID to connect with the Rebilly API.

For more information, see FramePay configuration reference

client.js

Optional: Style the card input

Customize the card payment field to match the look and feel of the rest of your form.

To customize, add the style property in your configuration object.

For all available properties, see Style reference.

client.js

Mount the card field

After initialization, mount the payment card field.

For more information, see:

Rebilly.on('ready', ...)

Rebilly.card.mount(...)

client.js

Handle the form submit event

Listen to the form's submit event to determine when user is ready to make the payment.
client.js

Create a payment token

To send the form data to FramePay, call Rebilly.createToken().

  • If the collected form data is valid, you will receive a successful result with a new payment token.
  • If the collected form data is not valid, you will receive an error explaining why.

For more information, see Rebilly.createToken(form).

client.js

Create a payment token

Once FramePay has return the payment token, you are now able to complete the purchase.

To do this, make a request to the POST /create-transaction endpoint.

Pass the token as part of the request payload, together with other properties.

client.js

Handle the API response

If no error occurs, inform your customer that the payment is successful. This example redirects the customer to a thank you (success) page, or to an approvalUrl if it exists.

client.js
Copy to clipboard
  • server.js
  • checkout.html
  • client.js
  • style.css
1const RebillyAPI = require("rebilly-js-sdk").default;
2const express = require("express");
3const app = express();
4const port = 3000;
5
6app.use(express.static("client"));
7app.use(express.json());
8
9// Use your own secret key, organization and website id
10const API_SECRET_KEY = "API_SECRET_KEY";
11const ORGANIZATION_ID = "ORGANIZATION_ID";
12const WEBISTE_ID = "WEBISTE_ID";
13
14const api = RebillyAPI({
15 apiKey: API_SECRET_KEY,
16 organizationId: ORGANIZATION_ID,
17 sandbox: true,
18});
19
20app.post("/create-transaction", async (req, res) => {
21 try {
22 const {
23 paymentToken,
24 billingAddress: primaryAddress,
25 currency,
26 amount,
27 } = req.body;
28
29 const { fields: customer } = await api.customers.create({
30 data: {
31 paymentToken,
32 primaryAddress
33 },
34 });
35
36 const { fields: transaction } = await api.transactions.create({
37 data: {
38 type: "sale",
39 websiteId: WEBISTE_ID,
40 customerId: customer.id,
41 currency,
42 amount,
43 },
44 });
45
46 res.status(201).send(transaction);
47 } catch (error) {
48 const message = err.details ?? "Internal Server Error";
49 const code = err.status ?? 500;
50 res.status(500).send({ code, message });
51 }
52});
53
54app.listen(port, () => {
55 console.log(`App listening at port: ${port}`);
56});

Interactive example

This is an interactive example of an end-to-end FramePay integration, including a sample Express server file.