-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
frontend: use postmessage to communicate with the iframe #3146
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,104 +10,123 @@ | |
|
||
<script lang="js"> | ||
;(() => { | ||
const { | ||
address, | ||
apiKey, | ||
baseCurrency, | ||
mode, | ||
quoteCurrency, | ||
} = window.frameElement?.dataset; | ||
|
||
if (mode === 'debug') { | ||
console.info(window.frameElement?.dataset); | ||
|
||
if (window.top === window) { | ||
showError('Unexpected error'); | ||
return; | ||
} | ||
|
||
if ( // this should never happen, but if it does we stop here | ||
!address | ||
|| !baseCurrency | ||
|| !quoteCurrency | ||
) { | ||
document.body.append( | ||
Object.assign(document.createElement('h1'), { | ||
style: 'color: red; padding: 1rem;', | ||
textContent: `Unexpected error: | ||
const onMessage = (event) => { | ||
switch (event.data?.action) { | ||
case 'configuration': | ||
const { | ||
address, | ||
apiKey, | ||
baseCurrency, | ||
mode, | ||
quoteCurrency, | ||
} = event.data || {}; | ||
|
||
if ( // this should never happen, but if it does we stop here | ||
!address | ||
|| !baseCurrency | ||
|| !quoteCurrency | ||
) { | ||
showError(`Unexpected error: | ||
${!address ? 'Address missing' : ''} | ||
${!baseCurrency ? 'BaseCurrency missing' : ''} | ||
${!quoteCurrency ? 'QuoteCurrency missing' : ''} | ||
` | ||
}) | ||
); | ||
return; | ||
} | ||
`); | ||
return; | ||
} | ||
const currency = baseCurrency.toUpperCase(); | ||
|
||
const currency = baseCurrency.toUpperCase(); | ||
// add the btcdirect CSS | ||
document.head.appendChild( | ||
Object.assign(document.createElement('link'), { | ||
href: ( | ||
mode === 'production' | ||
? 'https://cdn.btcdirect.eu/fiat-to-coin/fiat-to-coin.css' | ||
: 'https://cdn-sandbox.btcdirect.eu/fiat-to-coin/fiat-to-coin.css' | ||
), | ||
rel: 'stylesheet', | ||
}) | ||
); | ||
|
||
// add the btcdirect CSS | ||
document.head.appendChild( | ||
Object.assign(document.createElement('link'), { | ||
href: ( | ||
// add the btcdirect script | ||
(function (btc, d, i, r, e, c, t) { | ||
btc[r] = btc[r] || function () { | ||
(btc[r].q = btc[r].q || []).push(arguments) | ||
}; | ||
c = d.createElement(i); | ||
c.id = r; c.src = e; c.async = true; | ||
c.type = 'module'; c.dataset.btcdirect = ''; | ||
t = d.getElementsByTagName(i)[0]; | ||
t.parentNode.insertBefore(c, t); | ||
})(window, document, 'script', 'btcdirect', | ||
mode === 'production' | ||
? 'https://cdn.btcdirect.eu/fiat-to-coin/fiat-to-coin.css' | ||
: 'https://cdn-sandbox.btcdirect.eu/fiat-to-coin/fiat-to-coin.css' | ||
), | ||
rel: 'stylesheet', | ||
}) | ||
); | ||
|
||
// add the btcdirect script | ||
(function (btc, d, i, r, e, c, t) { | ||
btc[r] = btc[r] || function () { | ||
(btc[r].q = btc[r].q || []).push(arguments) | ||
}; | ||
c = d.createElement(i); | ||
c.id = r; c.src = e; c.async = true; | ||
c.type = 'module'; c.dataset.btcdirect = ''; | ||
t = d.getElementsByTagName(i)[0]; | ||
t.parentNode.insertBefore(c, t); | ||
})(window, document, 'script', 'btcdirect', | ||
mode === 'production' | ||
? 'https://cdn.btcdirect.eu/fiat-to-coin/fiat-to-coin.js' | ||
: 'https://cdn-sandbox.btcdirect.eu/fiat-to-coin/fiat-to-coin.js' | ||
); | ||
|
||
btcdirect('init', { | ||
token: apiKey, | ||
debug: mode === 'debug', | ||
locale: window.frameElement?.dataset.locale || 'en-US', | ||
theme: window.frameElement?.dataset.theme || 'light', | ||
}); | ||
|
||
// fiat to coin order | ||
btcdirect('wallet-addresses', { | ||
addresses: { | ||
address, | ||
currency, | ||
id: 'BitBox', | ||
walletName: 'BitBox' | ||
} | ||
}); | ||
|
||
btcdirect('set-parameters', | ||
mode === 'production' ? { | ||
baseCurrency: currency, | ||
fixedCurrency: true, | ||
quoteCurrency, | ||
// paymentMethod: any of 'bancontact', 'bankTransfer', 'creditCard', 'giropay', 'iDeal', 'sofort', 'applepay' | ||
showWalletAddress: false, | ||
} : { | ||
baseCurrency: currency, | ||
fixedCurrency: true, | ||
paymentMethod: 'sofort', // sandbox currently only supports sofort payment method | ||
quoteCurrency, | ||
showWalletAddress: false, | ||
? 'https://cdn.btcdirect.eu/fiat-to-coin/fiat-to-coin.js' | ||
: 'https://cdn-sandbox.btcdirect.eu/fiat-to-coin/fiat-to-coin.js' | ||
); | ||
|
||
btcdirect('init', { | ||
token: apiKey, | ||
debug: mode === 'debug', | ||
locale: window.frameElement?.dataset.locale || 'en-GB', | ||
theme: window.frameElement?.dataset.theme || 'light', | ||
}); | ||
|
||
// fiat to coin order | ||
btcdirect('wallet-addresses', { | ||
addresses: { | ||
address, | ||
currency, | ||
id: 'BitBox', | ||
walletName: 'BitBox' | ||
} | ||
}); | ||
|
||
btcdirect('set-parameters', | ||
mode === 'production' ? { | ||
baseCurrency: currency, | ||
fixedCurrency: true, | ||
quoteCurrency, | ||
// paymentMethod: any of 'bancontact', 'bankTransfer', 'creditCard', 'giropay', 'iDeal', 'sofort', 'applepay' | ||
showWalletAddress: false, | ||
} : { | ||
baseCurrency: currency, | ||
fixedCurrency: true, | ||
paymentMethod: 'sofort', // sandbox currently only supports sofort payment method | ||
quoteCurrency, | ||
showWalletAddress: false, | ||
} | ||
); | ||
|
||
window.addEventListener('btcdirect-embeddable-fiat-to-coin-order-confirmed', (event) => { | ||
console.log('btcdirect-embeddable-fiat-to-coin-order-confirmed', event); | ||
// Handle the event | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you could directly post a message to pass the order confirmation, maybe? And leave the TODO in the handler inside btcdirect.tsx. No strong opinion, tho There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only tested once, this event is fired after the user clicked proceed to payment and not really useful this way, I'll just remove. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed in #3152 |
||
// Note that the sent information from the widget is found inside event.detail | ||
}); | ||
|
||
break; | ||
} | ||
); | ||
}; | ||
|
||
window.addEventListener('message', onMessage); | ||
|
||
// Request the parent to send attributes | ||
window.parent.postMessage({ | ||
action: 'request-configuration' | ||
}, '*'); | ||
|
||
window.addEventListener('btcdirect-embeddable-fiat-to-coin-order-confirmed', (event) => { | ||
console.log('btcdirect-embeddable-fiat-to-coin-order-confirmed', event); | ||
// Handle the event | ||
// Note that the sent information from the widget is found inside event.detail | ||
}); | ||
function showError(message) { | ||
document.body.append( | ||
Object.assign(document.createElement('h1'), { | ||
style: 'color: red; padding: 1rem;', | ||
textContent: message, | ||
}) | ||
); | ||
} | ||
|
||
})(); | ||
</script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the file we host on but in order for it to communicate with the react app we need the changes in this PR in btcdirect.tsx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: take locale and theme from onMessage "configuration" / event.data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rebased this branch without the backend commit and fixed "locale" and "theme" in #3152