One-off Purchases
The Allegro SDK provides allegro.purchase.initiate() to start a Stripe
Checkout session for a term. After the member completes payment, Stripe
redirects them back to your site and the SDK automatically exchanges the result
for a JWT and entitlement data.
Prerequisites
- A Stripe account connected in the admin. See Payment Provider.
- The Stripe webhook configured on the platform (recommended for reliable fulfillment).
- The term ID for the offer term you want to sell. Find term IDs in the admin under Entitlements → Offers.
Initiating Checkout
Call allegro.purchase.initiate(termId) to redirect the member to Stripe
Checkout:
window.allegro.push(function (allegro) {
allegro.purchase.initiate('term_abc123');
});
The member is redirected to Stripe. After payment, Stripe redirects them back to the current page URL by default.
Custom Return URL
Pass a second argument to control where Stripe redirects after checkout:
window.allegro.push(function (allegro) {
allegro.purchase.initiate('term_abc123', 'https://example.com/thank-you');
});
Use this if you want to send members to a dedicated confirmation page after checkout rather than the page they were on.
Triggering from a Button
A typical integration attaches the call to a button click:
<button id="subscribe-btn">Subscribe — $9.99/mo</button>
<script>
document
.getElementById('subscribe-btn')
.addEventListener('click', function () {
window.allegro.push(function (allegro) {
allegro.purchase.initiate('term_abc123');
});
});
</script>
Handling the Return
After checkout, Stripe redirects the member back to your site. The SDK detects the result automatically on page load.
Success — Stripe appends allegro_purchase_token to the return URL. The SDK
exchanges it for a JWT and entitlement, then:
- Signs the member in (or upgrades their session if already signed in).
- Dispatches
allegro:purchase:completedonwindow. - Removes
allegro_purchase_tokenfrom the URL.
Failure or cancellation — Stripe appends
allegro_purchase_error=payment_failed. The SDK:
- Dispatches
allegro:purchase:failedonwindow. - Removes the error param from the URL.
Listening for Completion
window.addEventListener('allegro:purchase:completed', function (event) {
// event.detail.entitlement contains the newly granted entitlement
const entitlement = event.detail.entitlement;
console.log('Access granted to:', entitlement.resource.name);
// e.g. show a confirmation banner, unlock content, etc.
});
window.addEventListener('allegro:purchase:failed', function (event) {
// event.detail.reason is 'payment_failed' or similar
console.log('Purchase failed:', event.detail.reason);
});
The allegro:purchase:completed Event
The detail object on allegro:purchase:completed contains:
| Property | Type | Description |
|---|---|---|
method | string | Always 'purchase'. |
entitlement | object | The newly created entitlement (see below). |
The entitlement object:
| Property | Type | Description |
|---|---|---|
id | string | Entitlement ID. |
resource.id | string | Resource ID. |
resource.slug | string | Resource slug. |
resource.name | string | Human-readable resource name. |
started_at | string | null | ISO 8601 start date, or null if indefinite. |
ended_at | string | null | ISO 8601 end date, or null if indefinite. |
is_active | boolean | Whether the entitlement is currently active. |
Checking Validation State
allegro.purchase.validation reflects the outcome of token exchange for the
current page load:
| Value | Meaning |
|---|---|
'completed' | Purchase was verified and entitlements granted. |
'failed' | Payment failed or was cancelled. |
null | No purchase token present on this page load. |
validation at allegro:ready timeToken exchange is asynchronous. allegro.purchase.validation may still be
null when allegro:ready fires. Listen for allegro:purchase:completed or
allegro:purchase:failed instead.
Complete Example
<!DOCTYPE html>
<html>
<head>
<script src="https://your-allegro-instance.com/client.js"></script>
</head>
<body>
<div id="content-gate">
<p>Subscribe for full access.</p>
<button id="subscribe-btn">Subscribe — $9.99/mo</button>
</div>
<div id="confirmation" style="display:none">
<p>Thanks for subscribing! You now have access.</p>
</div>
<script>
document
.getElementById('subscribe-btn')
.addEventListener('click', function () {
window.allegro.push(function (allegro) {
allegro.purchase.initiate('term_abc123');
});
});
window.addEventListener('allegro:purchase:completed', function () {
document.getElementById('content-gate').style.display = 'none';
document.getElementById('confirmation').style.display = 'block';
});
window.addEventListener('allegro:purchase:failed', function () {
alert('Payment was not completed. Please try again.');
});
</script>
</body>
</html>
What Happens Server-Side
When a purchase is fulfilled:
- Allegro creates a
Purchaserecord linked to the member, term, and Stripe session. - For each resource on the term, Allegro creates an
Entitlementfor the member with the term's configured duration. - A
purchaseevent is recorded in the member's event history.
If the webhook fires before the member returns to your site (e.g., the browser was closed), fulfillment is still completed via the webhook. The token exchange on the return page detects the already-fulfilled purchase without creating duplicates.
Related
- Stripe Webhook Setup — Platform-level webhook configuration for reliable fulfillment
- Payment Provider (Product) — Connecting a payment provider as an operator
- Entitlements (Product) — What members receive after purchase
- Offers and Terms (Product) — Configuring terms and offers