Last updated
These docs are intended for a developer audience.Dismiss

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.

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

1. Obtain IDs and API keys

When you first log in to Rebilly, you create an organization as part of the setup process. A default website is created when a new organization is created. For more information, see Organizations and websites.

  1. Log in or sign up to Rebilly.

  2. Obtain your organization ID and website ID
    1. In the left navigation bar, press Settings .
    2. In the Management section, press My organization & websites.
    3. In the Organization details section, note the ID value.
    4. In the Website section, note the ID value.
  3. Obtain your publishable API key
    1. In the left navigation bar, press Automations .
    2. In the Development section, press API keys.
    3. Optionally, if you have not created a publishable key:
      1. In top right of the screen, press Create API key.
      2. In the API key type section, select Publishable.
      3. Optionally, in the Organizations dropdown, select the organizations that can use the API key.
      4. Optionally, in the Allowed IPs field, enter the IP addresses that are permitted to use the API key.
      5. Press Save API key.
      6. Go to the API keys page.
    4. Select a publishable key and copy the Key value.
  4. Obtain your secret API key
    1. In the left navigation bar, press Automations .
    2. In the Development section, press API keys.
    3. Optionally, if you have not created a secret key:
      1. In top right of the screen, press Create API key.
      2. In the API key type section, select Secret.
      3. Optionally, in the Organizations dropdown, select the organizations that can use the API key.
      4. Optionally, in the Allowed IPs field, enter the IP addresses that are permitted to use the API key.
      5. Press Save API key.
      6. Go 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.

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 Rebilly JS SDK

Install the package, import it into your code, and then initialize it 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:
    • Create a customer using the billing address and the payment token.
    • Using the customerId, create a sale transaction.
server.js
2

Create a checkout page

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 Framepay.

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:

client.js

Handle the form submit event

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

client.js

Create a payment token

To send the form data to FramePay, call Framepay.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 Framepay.createToken(form).

client.js

Create a payment token

Once FramePay has returned 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
  • checkout.html
  • server.js
  • 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.js"));
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 WEBSITE_ID = "WEBSITE_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: WEBSITE_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.