T.R.I.E. Donation Page Example

This guide walks through a complete implementation of a donation page for T.R.I.E. (The Real Internet Experience) using TodaQ’s micropayment system. This example demonstrates how to integrate multiple payment elements with different donation amounts.

1. HTML Structure

The page uses a simple HTML structure with three donation options:

<body>
  <main>
    <h1><img id="logo" src="golden_TRIEangle.svg" alt="T.R.I.E."></h1>
    <h2>Donate to T.R.I.E.</h2>
    <h3>and feel good about yourself</h3>

    <!-- Three donation options with unique IDs -->
    <div class="payment" id="donate5"></div>
    <div class="payment" id="donate05"></div>
    <div class="payment" id="donate005"></div>
  </main>
</body>

2. TodaQ Micropayment CDN Integration

Load the TodaQ micropayment library from CDN:

<!-- Load TodaQ micropayment library from CDN -->
<script type="module" crossorigin="anonymous" src="https://cdn.m.todaq.net/micropay.js"></script>

3. Core Initialization Process

The JavaScript code initializes the payment elements after the DOM is fully loaded (repeat for each unique amount):

document.addEventListener("DOMContentLoaded", async () => {
  // Initialize MicroPayments with your account's public secret
  const micro = await loadMicroPayments(
    "<PUBLIC_SECRET>",      // Your account's public secret
    { apiVersion: 'main' }  // API Configuration
  );

  const elements = micro.elements();

  // Create payment element
  const element = await elements.create('payment', {
    hash: '<HASH_5_DOLLARS>', // Commodity hash of the $5 donation
    theme: "light",
    styles: {
      colorPrimary: '#000000',
      colorBackground: '#FDB902', // Yellow background
      borderRadius: '0',          // Square corners
    },
  });

  // create() resolves to null when the hash cannot be loaded
  if (element) {
    element.mount("#donate5");
  }
});

Replace <PUBLIC_SECRET> with your account’s public secret and each <HASH_…> with the commodity hash of that donation amount (<HASH_50_CENTS> for #donate05, <HASH_5_CENTS> for #donate005). Both are public by design: they end up in the page that every visitor loads.

4. Payment Event Handling

Handle successful payments by listening for the payment event that payment-js dispatches on document:

// One listener on document fires for every payment button on the page
document.addEventListener('payment', (event) => {
  // event.detail: { hash, hostname, nonce, timestamp }
  // event.detail.hash tells you which donation was paid
  location.assign('thanks.html'); // Redirect to thank you page
});

A redirect to a thank-you page is enough here because a donation delivers nothing. A site that hands over something in return for a payment must first confirm the payment on its server: see Confirm each payment on your server.

5. Key Implementation Patterns

  1. CDN Loading: The TodaQ Micropay Library loads from https://cdn.m.todaq.net/micropay.js

  2. Initialization Flow:

    • Wait for DOM content to load
    • Initialize MicroPayments with merchant ID (public secret)
    • Create elements with unique hashes (commodity hashes)
    • Mount elements to specific DOM containers
  3. Event Handling:

    • Listens for the global payment event, which fires for any of the page’s buttons
    • Redirects to thanks.html on successful payment
  4. Styling Consistency:

    • Black text on yellow background
    • Square borders
    • Light theme

6. Considerations

7. Dependencies

This implementation provides a complete, working example that demonstrates the core concepts covered in the Getting Started guide, showing how to integrate multiple payment options with consistent styling and proper event handling.