Accept guest payments
A visitor without a sign-in pays in MACH, which Apple Pay or Google Pay buys.
Overview
A guest payment is a MACH transfer from a guest account: a root Tempo Account whose owner is a non-extractable WebCrypto P256 key pair that the app's page keeps in its own store. The visitor signs in nowhere. As a card checkout charges a guest without an account, the app charges the visitor in one call.
accounts.pay with guest: true creates the guest account at the first payment. Each payment first asks Tempo ID to prepare the transfer, so Tempo ID refuses a payment that no purchase fixes, such as to a recipient that MACH's transfer policy does not permit, before the visitor pays. If the guest account holds less MACH than the amount, Tempo ID's purchase page opens in a popup, and sells the MACH that it lacks with Apple Pay, Google Pay, or a card. The guest account then signs the transfer, and Tempo ID's sponsor pays the network fee.
The demo pays MACH as a guest, shows the guest account's address, and forgets the guest account. Its default recipient is the initial merchant of MACH, which MACH's transfer policy permits.
Walkthrough
Create the instance
A guest payment needs no plugin. The instance's chain defaults to Tempo mainnet, and its store defaults to IndexedDB, which keeps the non-extractable key pair as it is. Register the page's origin as a redirect URI of the app, since Tempo ID's guest routes answer only those origins.
import { Accounts } from 'accounts'
export const accounts = Accounts.create()Charge the visitor
Call accounts.pay in a click handler, so the purchase page's popup opens within the click. The preparation and the read before the purchase are requests, so if they outlast the click's user activation, a dialog offers the popup, and its Continue opens it. The call resolves with the guest account's address and the transaction hash, once the chain includes the transfer.
import { useState } from 'react'
import { accounts } from './tempo.config.js'
export function Checkout() {
const [transaction, setTransaction] = useState<string>()
return (
<>
<button
onClick={async () => {
const { transaction } = await accounts.pay({
amount: '12.5',
guest: true,
to: '0xA74666CD3AAb591a2628e3eFa160530EC13f2f15',
token: 'mach',
})
setTransaction(transaction)
}}
type="button"
>
Pay 12.50 MACH
</button>
{transaction && <p>Paid in {transaction}.</p>}
</>
)
}import { Accounts } from 'accounts'
export const accounts = Accounts.create()The purchase page buys the larger of 5 MACH and the shortfall, rounded up to the cent, at most 100 MACH. The guest account keeps the rest for the next payment. If Tempo ID refuses the payment for another reason than the balance, such as the recipient, the app, the page's origin, or a rate limit, the call fails before the purchase page opens, also for a new guest account. The guest account signs only a transfer of the requested amount of MACH to to, and refuses any other transaction that Tempo ID returns.
A call fails in these cases, and the transfer does not send:
| The case | The error |
|---|---|
| A token other than MACH | Accounts.RequestError with the reason unsupported_token, before any request |
| A chain other than Tempo mainnet | Accounts.RequestError with the reason invalid_chain, before any request |
| A shortfall above 100 MACH | Accounts.RequestError with the reason insufficient_balance, before the purchase page opens |
A store that holds its values only in the page, such as Store.memory | Accounts.RequestError with the field store, before the guest account is created |
| The visitor selects Cancel on the page | Accounts.RequestError with the code access_denied |
| The visitor closes the popup or the dialog | A DOMException with the name AbortError |
| The browser blocks the popup | Actions.account.BlockedError |
Actions.guest.pay lists each refusal of Tempo ID.
Use cases
Each use case sets one option of accounts.pay, or uses a member of accounts.guest.
Attach a memo
memo is the transfer's memo, such as an order ID: text, sent as its UTF-8 bytes, or hex, of 1 to 32 bytes.
import { accounts } from './tempo.config.js'
export function pay(order: string) {
return accounts.pay({
amount: '12.5',
guest: true,
memo: order,
to: '0xA74666CD3AAb591a2628e3eFa160530EC13f2f15',
token: 'mach',
})
}Read the guest account
accounts.guest.get returns the guest account's address, or undefined before the first guest payment. The read creates no guest account.
import { accounts } from './tempo.config.js'
export const address = await accounts.guest.get()Forget the guest account
accounts.guest.disconnect removes the key pair from the store, and the next guest payment creates a new guest account. The MACH of the forgotten guest account stays on its address, and no key signs for it again.
import { accounts } from './tempo.config.js'
export async function forget() {
await accounts.guest.disconnect()
}Pay as the signed-in user
With a session, the call ignores guest, and the session's Tempo Account pays. So one button charges a visitor and a signed-in user alike.
import { accounts } from './tempo.config.js'
export function pay() {
// A signed-in user pays from the session's Tempo Account.
return accounts.pay({
amount: '12.5',
guest: true,
to: '0xA74666CD3AAb591a2628e3eFa160530EC13f2f15',
token: 'mach',
})
}Limitations
- A guest payment pays only in MACH, and only on Tempo mainnet.
- The guest account lives in the store of the page's origin. If the browser deletes the site's data, the guest account and its MACH are lost.
- No passkey or sign-in claims a guest account.