Skip to Content
Docs

Pass custom data to routing rules

Update payment session metadata from Pay.com.js so routing rules can react to customer choices made at checkout.

Call checkout.updateMetadata() in Pay.com.js to attach key-value pairs to an active payment session from the browser. Pay.com stores each value server-side under a checkout_ prefix, so your routing rules can react to choices the customer makes on the checkout page, such as a shipping tier or a promo toggle.

Your server sets a session's metadata when it creates the session, and can replace it later with PATCH /v1/sessions/payment/{id}. Use checkout.updateMetadata() for choices that only the page knows about.

When to use this

Use checkout.updateMetadata() when:

  • A customer's choice on the checkout page changes which acquirer or processor handles the payment.
  • You need to attach a business signal (customer tier, cart category, promo flag) known only after the session is created.
  • You want routing rules to react to frontend state without calling PATCH /v1/sessions/payment/{id} from your server.

Requirements

Before you start, make sure you have:

  • Completed the Pay.com.js setup guide with a pay instance and a client_secret.
  • Key-value pairs where each value is a string, number, or boolean. Nested objects and arrays are not supported, and Pay.com stores every value as text.
  • Routing rules that match the metadata keys you plan to send. Create them in Rules → Routing in the Pay.com dashboard (see Routing rules).

Update session metadata

Follow these steps to initialize the checkout and send custom metadata before the customer submits payment.

  1. 1

    Create a payment session

    Create a payment session on your server and return the client_secret to your frontend.

    curl -X POST https://api.sandbox.pay.com/v1/sessions/payment \
      -u ':sk-pay-com-sandbox-…' \
      -H "Content-Type: application/json" \
      -H "idempotency-key: $(uuidgen)" \
      -d '{
        "currency": "usd",
        "amount": 8000,
        "customer_reference_id": "cust-001",
        "payment_method_types": ["card"],
        "return_url": "https://example.com/checkout/complete"
      }'

    The amount is in the smallest currency unit, so 8000 represents $80.00 USD.

  2. 2

    Initialize the SDK and render the checkout

    Once your page has loaded, initialize the SDK with your merchant ID, create a checkout instance with the session's client_secret, and render Universal checkout into a container element on your page.

    const pay = await Pay.com({ identifier: 'YOUR_MERCHANT_ID' })
    
    const checkout = pay.checkout({
      clientSecret: CLIENT_SECRET,
      onSuccess: (payment) => console.log('Payment succeeded:', payment),
      onFailure: (error) => console.log('Payment failed:', error),
    })
    
    checkout.universal({ container: '#payment-element' })

    Keep a reference to checkout so you can call updateMetadata() on it later.

  3. 3

    Send your data to the session

    Call checkout.updateMetadata() after the checkout finishes rendering. Wait for the content_ready (Universal checkout) or headless_ready (Headless checkout) event first. Calling it earlier rejects the promise.

    checkout.on(checkout.EVENT_TYPES.CONTENT_READY, async () => {
      await checkout.updateMetadata({
        customer_tier: 'gold',
        cart_items: 3,
        is_guest: false,
      })
    })

    You can also trigger updates from customer interactions on your page, such as a promotional toggle:

    document.querySelector('#enter-draw').addEventListener('change', async (e) => {
      await checkout.updateMetadata({ enter_draw: e.target.checked })
    })

    You can call updateMetadata() as many times as needed before the customer submits payment. Each call is a partial update. Keys you leave out stay unchanged, and keys you include are added or overwritten.

    Never send cardholder data, unnecessary PII, or secrets in metadata. Metadata is stored on the transaction and visible anywhere the transaction appears.

  4. 4

    Match metadata in routing rules

    Each key you send is stored on the session under a checkout_ prefix. For example, customer_tier is available to routing rules as metadata.checkout_customer_tier. Pass the plain key name in your code. The server adds the prefix automatically.

    Values are stored as text (true becomes "true", 3 becomes "3"), so match against string values in your rule conditions. The checkout_ prefix keeps browser-supplied metadata separate from server-supplied session metadata.

    Create a routing rule that matches these keys. For example:

    If metadata.checkout_enter_draw equals "true", route to Acquirer B. Otherwise, use the default route.

    To pick the key from a list in the rule builder, with its own label, add checkout_enter_draw as a merchant context field. See Fields you can route on.

Error handling

checkout.updateMetadata() returns a promise. Handle rejections to catch failed updates:

try {
  await checkout.updateMetadata({ customer_tier: 'gold' })
} catch (error) {
  // error.message -> 'Failed to update metadata'
  // error.data    -> the metadata object you passed
  console.error('Metadata update failed:', error.message)
}

Next steps

Was this page helpful?