Skip to content

Instantly share code, notes, and snippets.

@Ekliptor
Last active May 28, 2021 18:18
Show Gist options
  • Save Ekliptor/127f6d5065fab172f659cae516d066a2 to your computer and use it in GitHub Desktop.
Save Ekliptor/127f6d5065fab172f659cae516d066a2 to your computer and use it in GitHub Desktop.
Listen to "paid" message of prompt.cash iframe and navigate parent page to "return_url"
(function () {
const gatewayOrigin = "https://prompt.cash"; // ignore messages from other frames
const onMessage = (event) => {
if (event.origin !== gatewayOrigin) {
return;
}
let data = event.data;
if (data.func === "onPromptPaid") {
// the iframe says the payment is confirmed
// note that a malicious script in a browser with CORS security disabled might be able to trigger this
// You should not rely on this message and instead the Callback to your server or use Prompt.Cash REST API to confirm:
// https://prompt.cash/pub/docs/
const payment = data.data;
window.location.assign(payment.return_url);
}
else
console.debug("Received unknwon Prompt.Cash message type: %s", data.func);
}
if (window.addEventListener) {
window.addEventListener("message", onMessage, false);
}
else if (window.attachEvent) {
window.attachEvent("onmessage", onMessage, false);
}
})();
@Ekliptor
Copy link
Author

This code belongs into the parent page (the one including the frame).

The above example reloads the parent page to the return_url you specified in /create-payment. You can replace window.location.assign() with your code as needed (call your own backend to update the payment status, ...).

All properties of the payment object are listed here: https://prompt.cash/pub/docs/#create-a-new-payment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment