incomingPayment
The monetization
event’s incomingPayment
attribute can be used to verify the receipt of a payment. Today, the monetization
event fires when the WM agent successfully creates an outgoing payment request at the WM provider. Since it’s only a request to send an outgoing payment, there’s no guarantee at this point that a payment has been received.
The incomingPayment
attribute returns the URL that represents the incoming payment at the WM receiver. The returned value is the value it was initialized with. By querying the URL, you can get the receivedAmount
.
Example
/** @type {MonetizationEvent} event */
async function verifyPayment(event) {
// Legacy receivers don't support returning incoming payment URLs
if (!event.incomingPayment) {
throw new Error('No incoming payment URL')
}
const response = await fetch(event.incomingPayment, {
method: 'GET',
credentials: 'same-origin',
mode: 'same-origin',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
})
if (response.ok) {
// The incoming payment was fetched successfully
const { receivedAmount } = JSON.parse(response.json())
const { amount, assetCode, assetScale } = receivedAmount
console.log(`Received ${assetCode}${amount / Math.pow(10, assetScale)}.`)
}
}
Specifications
Specification |
---|
Web Monetization #incomingpayment-attribute |