Keep customers in step and offer the customer portal

Find the Railbed customer behind one of your users, correct their contact details from your own app without overwriting a newer change, and send them to your customer portal already signed in.

On this page

What this guide covers

Railbed keeps one customer per email address in each mode, made when an order, invoice or subscription first names them. If your own app also keeps accounts for those people, three things come up: finding the Railbed customer behind one of your users, keeping their contact details the same in both places, and giving them their orders, receipts and invoices without building those screens yourself. This guide does all three with a Full access key.

You want toCall
Find the customer for one of your usersGET /v1/customers?email=…, or the order's customer_id
Correct their name, phone, addresses or emailPATCH /v1/customers/:id with expected_version
Check whether the portal is on, and what it showsGET /v1/customer_portal
Send a signed-in user to their portalPOST /v1/customer_portal/sessions, then redirect to url

Find the customer behind your user

A customer is made when an order, invoice or subscription first names their email. To tie them to your own account from the start, send your user's id as customer.id on each checkout session: it's kept in the customer's store_accounts and on each order. Store the cus_… id once you have it, from data.customer.id in your payment.paid webhook, from an order's customer_id in the API, or from a lookup by email:

Find a customer by email
curl -G https://pay.railbed.io/v1/customers \
  --data-urlencode "email=maya@example.com" \
  -H "Authorization: Bearer $RAILBED_SECRET_KEY"

The email matches the whole address in any letter case, and the answer is a page with at most one customer. An empty page means nobody with that email has an order or invoice in this mode yet. Test and Live customers are separate, so a Test key never finds a Live customer.

Correct their contact details

When a user changes their details in your app, send the change to Railbed so invoices, receipts and the portal show the same thing. Update a customer changes only the fields you send; null clears one, and the email can be changed but never removed.

Contact details can change in four places: the orders and checkouts they pay (each order's details replace the saved ones), your team's edits in the dashboard, the customer's own Your details in the portal, and your calls. So read before you write, and send back the version you read as expected_version. If anyone changed the details in between, Railbed answers 409 edit_conflict instead of overwriting their change.

Node.js: copy a user's new phone number to Railbed
const API = 'https://pay.railbed.io/v1';
const auth = { Authorization: `Bearer ${process.env.RAILBED_SECRET_KEY}` };

async function syncPhone(customerId, phone) {
  for (let attempt = 0; attempt < 3; attempt++) {
    const customer = await fetch(`${API}/customers/${customerId}`, { headers: auth }).then((r) => r.json());
    if (customer.erased) return; // you deleted their data: leave it deleted
    if (customer.phone === phone) return;

    const res = await fetch(`${API}/customers/${customerId}`, {
      method: 'PATCH',
      headers: { ...auth, 'Content-Type': 'application/json' },
      body: JSON.stringify({ phone, expected_version: customer.version }),
    });
    if (res.ok) return;
    const { error } = await res.json();
    if (error.code !== 'edit_conflict') throw new Error(`${error.code}: ${error.message}`);
    // Someone else changed their details since we read them: read again and decide afresh.
  }
}
AnswerWhat to do
200Done. The response is the customer as changed, with its new version
409 edit_conflictTheir details changed since you read them. Read again, then decide whether your change still applies
409 email_in_useAnother of your customers in this mode already has that email. Two people can't share one; leave it as it is and tell your team
409 erasedYou deleted this customer's data. Don't send it back
400 unknown_fieldA field this call doesn't take, such as note: the team's private note is the dashboard's alone

Keep in mind:

  • A checkout's details replace yours. The next order a buyer pays brings the name, phone and addresses they give there, and those replace the saved ones. A detail given at a checkout doesn't move version, so expected_version guards against edits by your team, the portal and other calls, not against a checkout in between. Read the customer just before you write.
  • Changing the email moves their sign-in. The customer signs in to the portal with their email, so after you change it they use the new one. Their orders, invoices and subscriptions stay with them.
  • The email list needs their consent. Nothing is sent to the customer when you edit them, so set accepts_marketing only from a choice they made.
  • Every edit is recorded. The customer's activity in the dashboard shows it as your key's or agent's, naming the fields but never their values.
  • No webhook is sent for an edit. If you need to know about changes made in the dashboard or the portal, read the customer when you need it, or when their next payment.* event brings the customer as data.customer.

Offer the customer portal

The customer portal is one page for your business where each customer sees their orders with a receipt for each one paid, their invoices with Pay, their subscriptions and their contact details. It's off until the business's owner turns it on in the dashboard, under Customers → Customer portal; its settings are changed only there.

Check that it's on

Retrieve the customer portal
curl https://pay.railbed.io/v1/customer_portal \
  -H "Authorization: Bearer $RAILBED_SECRET_KEY"

Show a link to it only while enabled is true. sections tells you what customers see there (invoices, receipts, subscriptions, edit_details and the rest), so the words beside your link can match: "Orders, receipts and invoices" when receipts and invoices are on. url is the portal's own address, where a customer signs in with a code sent to their email; it never changes, so you can put it on a help page or in your own emails. It's null while the portal is off.

Send a signed-in user straight in

When the customer is already signed in to your site, skip the emailed code. Make a portal session when they choose your link, and redirect them to its url at once:

Node.js (Express): an Orders and receipts route
app.get('/account/orders', requireLogin, async (req, res) => {
  const response = await fetch('https://pay.railbed.io/v1/customer_portal/sessions', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.RAILBED_SECRET_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      customer: req.user.railbedCustomerId, // or send email: req.user.email in place of customer
      return_url: 'https://northwind.example/account',
    }),
  });
  if (!response.ok) {
    const { error } = await response.json();
    // 404 not_found: no orders with you yet. 409 portal_off: the portal was turned off.
    return res.render('orders-empty', { reason: error.code });
  }
  const session = await response.json();
  res.redirect(303, session.url);
});
  • Make the link when they ask, never ahead of time. A session's url opens once, within 5 minutes, and signs in whoever opens it first. Don't email it, log it or put it in a page someone else could see.
  • It lasts an hour. The portal's Back link returns to your return_url. After an hour the customer is asked for a code, or they come back through your link again.
  • Each call makes a new link. A call that failed is safe to repeat, and a link nobody opened expires by itself.
  • Test mode works. A Test key signs a Test customer in, so you can try the whole flow. Railbed never emails a Test customer a code, so a session is the only way into a Test customer's portal from your app.

The portal never takes or refunds money itself. Pay opens the invoice's own page, and a refund is between you and the customer.

A buyer's usual card provider

When a customer pays by card, Railbed remembers the provider, and their next checkout with the same email offers it first when it can take them. It's not in the API or in webhooks, and a checkout never says why it leads; your customers see it, and can forget it, in the portal. See The usual way to pay.

What the API doesn't do yet

The API can't change the portal's settings, delete a customer's data (that's done in the dashboard, on the customer's page), create a customer on its own (one is made by an order, an invoice or a subscription), or send a webhook when a customer is edited. Everything here works with a Full access key or an agent with full access; a Read only key can find customers and read the portal's settings, but not edit customers or make portal sessions.

Updated · This page as Markdown