Skip to content

Commit

Permalink
Don't define window.klarnaAsyncCallback, and always detect script loa…
Browse files Browse the repository at this point in the history
…d locally
  • Loading branch information
sponglord committed Dec 5, 2024
1 parent baa4961 commit c69138d
Showing 1 changed file with 1 addition and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,12 @@ export function KlarnaWidget({ sdkData, paymentMethodType, payButton, ...props }

// Add Klarna Payments Widget SDK
useEffect(() => {
/**
* If the callback has already been defined by another instance of the widget, then it will not be called again by any second instance.
* So we set a boolean telling us to initialise the widget ourselves once the script is loaded
*
* Alternatively, we *never* define the callback function; and *always* initialise the widget ourselves once the script is loaded
* (Checking with Klarna on whether defining this callback is advised/mandatory)
*/
const initOnLoad = !!window.klarnaAsyncCallback;

window.klarnaAsyncCallback = function () {
console.log('### KlarnaWidget::klarnaWidget:: klarna async function called');
initializeKlarnaWidget();
};

console.log('\n### KlarnaWidget:::: useEffect sdkData.payment_method_category=', sdkData.payment_method_category);

const script = new Script(KLARNA_WIDGET_URL);
void script.load()?.then(() => {
console.log('### KlarnaWidget:::: useEffect script: LOADED ');
if (initOnLoad) {
console.log('### KlarnaWidget:::: manually init widget');
initializeKlarnaWidget();
}
initializeKlarnaWidget();
});

return () => {
Expand Down

0 comments on commit c69138d

Please sign in to comment.