Benefit provider
Keep the benefits of a pass in step with a read-only external system, with runtime.benefit.benefitProvider, its GetBenefits handler and its validation rules.
A benefit provider script returns, for one pass, the complete set of benefits an external system holds for the holder: vouchers, offers, a welcome gift. The Wallet Crew compares that set with the benefits the pass already has, adds the new ones, updates the changed ones and removes those that are no longer returned. It is the tool for a system that can only be read. This page is the reference for runtime.benefit.benefitProvider.
Three tools grant benefits, and the choice depends on who starts the exchange:
The system can call The Wallet Crew: use the benefit API.
The Wallet Crew issues the benefit from a benefit template, and only its code comes from a script: use the benefit code generator.
The system can only be read: use this endpoint.
Registration
The key is runtime.benefit.benefitProvider. Registering it is enough: no configuration file refers to it, and from then on it applies to every pass of the tenant that is not itself a benefit.
export default function (context) {
context.register('runtime.benefit.benefitProvider', {
GetBenefits: async (passId, passType, identifiers) => undefined
});
}When it runs
The sync runs when a pass is created and on every update that recomputes the pass metadata. It does not run for the passes that carry a benefit id identifier (the passes that carry a benefit), so it never runs recursively.
A distributed lock per pass prevents two syncs of the same pass at once during a burst of updates. A sync waits up to one minute for the lock; when it is not obtained, the sync is skipped and a warning is logged.
Because the script runs on every metadata recompute, a call to the external system uses the Cache option of fetch.
Handlers
GetBenefits
passId, passType, identifiers
a promise of an array of benefits
Required
GetBenefits must be an async function: the platform reads its result as a promise, so a plain function that returns an array fails, and the failure is handled like a thrown error (see below). It is required: a missing method would skip every pass.
passIdis the id of the pass andpassTypeits pass type.identifiersis an object of the pass identifiers, keyed without theid.prefix: a pass carryingcrm.customerIdis read asidentifiers['crm.customerId'].
Return value
The result is the complete set of benefits of the pass, as an array of objects with camelCase keys.
An array of benefits
The pass gets exactly these benefits. A benefit supplied earlier and now absent is removed.
[]
Every benefit supplied by the script is removed.
undefined or null
A warning is logged and nothing changes.
A benefit that comes back after it was removed is revived, not duplicated. The benefits are stored with origin.generator set to script.
The benefit object
origin.externalId
string
Required. A stable identifier, unique across the tenant. It is the identity used to compare the returned benefits with the existing ones.
origin.processId
string
Optional. The benefit template id, when the benefit relates to a template.
type
string
OneTime (default), MultiUse or Unlimited.
category
string
A category. Other by default.
priority, tags
number, array
Optional ordering and tags.
content.title, content.description
object
Each is { default, localizations }.
appearance.images
array
Each { slot, url }: slot is Main or Thumbnail, url is { default, localizations } with absolute http or https URLs. One image per slot.
links
object
Keyed by link type (CallToAction, Website, ...), each { title, link }.
data.content
string
Free-form text.
behavior.target
number
The allowance of a MultiUse benefit. 1 by default.
behavior.mode
array
Where the benefit appears. ShowOnBenefitListPage only by default; [] shows the benefit nowhere.
code.value, code.format
string
The code and its format. Code128 by default.
activationDate, deactivationDate, deletionDate
string
ISO 8601 strings. A JavaScript Date object is refused.
passType
string
Creates a standalone pass for the benefit.
usedQuantity
number
When set, the external system owns the redemption: the balance is the allowance minus usedQuantity, and the value must only grow.
The platform owns the benefit id, the balance movements and origin.generator: a script does not set them.
Errors and edge cases
GetBenefitsthrows or its promise rejects: the error is swallowed and the pass keeps its benefits as they are.GetBenefitsreturnsundefinedornull: a warning is logged and nothing changes. To remove every benefit, return[].A benefit has an unknown category, code format, mode, link type or image slot: the value is replaced by its default, or ignored, and logged.
A benefit has an unknown
type, or an unreadable date (a JavaScriptDateobject, or a day-first or month-first string): the whole result is refused.The lock is not obtained: the sync is skipped with a warning.
No script registers the key: nothing happens.
Several files register the key: the last registered wins, and the load order of files is not defined.
The pass carries a benefit id identifier: the provider does not run for it.
Each sync logs one line with the number of benefits supplied, inserted, updated, retired, uses recorded, and benefits without a free slot.
Example
The script reads the vouchers of a customer in a partner API. A failure of the API returns undefined, so that the pass keeps its benefits instead of losing them. Dates are ISO strings, and the externalId is the voucher number.
An empty items array returns [], which removes the benefits the script supplied earlier: that is the intended effect when the customer holds no voucher any more.
Checking the result
GET /api/{tenantId}/passes/{passId}/benefits lists the benefits of a test pass. The platform's monitoring records one line per sync, which tells how many benefits were inserted, updated or retired, and a warning when a result was ignored.
Common pitfalls
The
externalIdchanges from one call to the next, so benefits are removed and created again.A call to the external system without a
Cache, repeated on every metadata recompute.
Last updated

