From 2bf313ff65068953ede36701c6274375617312f3 Mon Sep 17 00:00:00 2001 From: Ludovic Levalleux Date: Wed, 18 Oct 2023 17:01:48 +0100 Subject: [PATCH] feat: add redemption widgets params for callbacks URL --- public/example.html | 64 +++++++++++++++++++++++- public/scripts/boson-widgets.js | 39 +++++++++++++++ src/components/widgets/redeem/Redeem.tsx | 61 ++++++++++++++++++++++ tests/redemptionBackend/src/router.ts | 31 ++++++++++++ 4 files changed, 193 insertions(+), 2 deletions(-) diff --git a/public/example.html b/public/example.html index 7e4eb25..a0a0227 100644 --- a/public/example.html +++ b/public/example.html @@ -109,13 +109,23 @@

Redemption Widget

+ + DeliveryInfo + + + + + + + PostDeliveryInfoURL - + @@ -125,7 +135,45 @@

Redemption Widget

- + + + + + PostRedemptionSubmittedURL + + + + + + + + + PostRedemptionSubmittedHeader + + + + + + + + + PostRedemptionConfirmedURL + + + + + + + + + PostRedemptionConfirmedHeader + + + + + @@ -162,11 +210,23 @@

Redemption Widget

setValue('select-redeem-exchange-state', 'Committed') setValue('input-post-delivery-info-url', '') setValue('input-post-delivery-info-header', '') + setValue('input-post-redemption-submitted-url', '') + setValue('input-post-redemption-submitted-header', '') + setValue('input-post-redemption-confirmed-url', '') + setValue('input-post-redemption-confirmed-header', '') + setValue('input-delivery-info', '') } function clearFinanceInputs() { setValue('input-finance-seller-id', '') updateFinanceButton('disabled', true) } + function fullDecodeUri(s) { + const s2 = decodeURI(s); + if (s2 !== s) { + return fullDecodeUri(s2) + } + return s2 + } \ No newline at end of file diff --git a/public/scripts/boson-widgets.js b/public/scripts/boson-widgets.js index 0cf5686..616d528 100644 --- a/public/scripts/boson-widgets.js +++ b/public/scripts/boson-widgets.js @@ -13,8 +13,13 @@ const constants = { exchangeStateTag: "data-exchange-state", showRedemptionOverviewTag: "data-show-redemption-overview", widgetActionTag: "data-widget-action", + deliveryInfoTag: "data-delivery-info", postDeliveryInfoUrlTag: "data-post-delivery-info-url", postDeliveryInfoHeadersTag: "data-post-delivery-info-headers", + postRedemptionSubmittedUrlTag: "data-post-redemption-submitted-url", + postRedemptionSubmittedHeadersTag: "data-post-redemption-submitted-headers", + postRedemptionConfirmedUrlTag: "data-post-redemption-confirmed-url", + postRedemptionConfirmedHeadersTag: "data-post-redemption-confirmed-headers", sellerIdTag: "data-seller-id", configIdTag: "data-config-id", accountTag: "data-account", @@ -127,10 +132,22 @@ function bosonWidgetReload() { showRedeemId.attributes[constants.showRedemptionOverviewTag]?.value; const widgetAction = showRedeemId.attributes[constants.widgetActionTag]?.value; + const deliveryInfo = + showRedeemId.attributes[constants.deliveryInfoTag]?.value; const postDeliveryInfoUrl = showRedeemId.attributes[constants.postDeliveryInfoUrlTag]?.value; const postDeliveryInfoHeaders = showRedeemId.attributes[constants.postDeliveryInfoHeadersTag]?.value; + const postRedemptionSubmittedUrl = + showRedeemId.attributes[constants.postRedemptionSubmittedUrlTag]?.value; + const postRedemptionSubmittedHeaders = + showRedeemId.attributes[constants.postRedemptionSubmittedHeadersTag] + ?.value; + const postRedemptionConfirmedUrl = + showRedeemId.attributes[constants.postRedemptionConfirmedUrlTag]?.value; + const postRedemptionConfirmedHeaders = + showRedeemId.attributes[constants.postRedemptionConfirmedHeadersTag] + ?.value; const configId = showRedeemId.attributes[constants.configIdTag]?.value; const account = showRedeemId.attributes[constants.accountTag]?.value; bosonWidgetShowRedeem({ @@ -139,8 +156,13 @@ function bosonWidgetReload() { exchangeState, showRedemptionOverview, widgetAction, + deliveryInfo, postDeliveryInfoUrl, postDeliveryInfoHeaders, + postRedemptionSubmittedUrl, + postRedemptionSubmittedHeaders, + postRedemptionConfirmedUrl, + postRedemptionConfirmedHeaders, configId, account }); @@ -165,8 +187,25 @@ function bosonWidgetShowRedeem(args) { value: args.showRedemptionOverview?.toString() // to allow passing either a real boolean or a string }, { tag: "widgetAction", value: args.widgetAction }, + { tag: "deliveryInfo", value: args.deliveryInfo }, { tag: "postDeliveryInfoUrl", value: args.postDeliveryInfoUrl }, { tag: "postDeliveryInfoHeaders", value: args.postDeliveryInfoHeaders }, + { + tag: "postRedemptionSubmittedUrl", + value: args.postRedemptionSubmittedUrl + }, + { + tag: "postRedemptionSubmittedHeaders", + value: args.postRedemptionSubmittedHeaders + }, + { + tag: "postRedemptionConfirmedUrl", + value: args.postRedemptionConfirmedUrl + }, + { + tag: "postRedemptionConfirmedHeaders", + value: args.postRedemptionConfirmedHeaders + }, { tag: "configId", value: args.configId }, { tag: "account", value: args.account } ]); diff --git a/src/components/widgets/redeem/Redeem.tsx b/src/components/widgets/redeem/Redeem.tsx index 9f40c2a..e002ae8 100644 --- a/src/components/widgets/redeem/Redeem.tsx +++ b/src/components/widgets/redeem/Redeem.tsx @@ -22,6 +22,22 @@ export function Redeem() { const exchangeState: subgraph.ExchangeState = checkExchangeState( searchParams.get("exchangeState") || undefined ); + const deliveryInfo = searchParams.get("deliveryInfo") || undefined; + let deliveryInfoDecoded = undefined; + if (deliveryInfo) { + try { + deliveryInfoDecoded = JSON.parse(deliveryInfo); + for (const key of Object.keys(deliveryInfoDecoded)) { + deliveryInfoDecoded[key] = decodeURIComponent(deliveryInfoDecoded[key]); + } + console.log("deliveryInfoDecoded", deliveryInfoDecoded); + } catch (e) { + console.error( + `Unable to parse JSON from deliveryInfo='${deliveryInfo}': ${e}` + ); + } + } + const postDeliveryInfoUrl = searchParams.get("postDeliveryInfoUrl") || undefined; const postDeliveryInfoHeaders = @@ -40,6 +56,46 @@ export function Redeem() { ); } } + const postRedemptionSubmittedUrl = + searchParams.get("postRedemptionSubmittedUrl") || undefined; + const postRedemptionSubmittedHeaders = + searchParams.get("postRedemptionSubmittedHeaders") || undefined; + let postRedemptionSubmittedHeadersDecoded = undefined; + if (postRedemptionSubmittedHeaders) { + try { + postRedemptionSubmittedHeadersDecoded = JSON.parse( + postRedemptionSubmittedHeaders + ); + console.log( + "postRedemptionSubmittedHeadersDecoded", + postRedemptionSubmittedHeadersDecoded + ); + } catch (e) { + console.error( + `Unable to parse JSON from postRedemptionSubmittedHeaders='${postRedemptionSubmittedHeaders}': ${e}` + ); + } + } + const postRedemptionConfirmedUrl = + searchParams.get("postRedemptionConfirmedUrl") || undefined; + const postRedemptionConfirmedHeaders = + searchParams.get("postRedemptionConfirmedHeaders") || undefined; + let postRedemptionConfirmedHeadersDecoded = undefined; + if (postRedemptionConfirmedHeaders) { + try { + postRedemptionConfirmedHeadersDecoded = JSON.parse( + postRedemptionConfirmedHeaders + ); + console.log( + "postRedemptionConfirmedHeadersDecoded", + postRedemptionConfirmedHeadersDecoded + ); + } catch (e) { + console.error( + `Unable to parse JSON from postRedemptionConfirmedHeaders='${postRedemptionConfirmedHeaders}': ${e}` + ); + } + } const configId = searchParams.get("configId") as ConfigId; if (!configId) { return

Missing 'configId' query param

; @@ -92,8 +148,13 @@ export function Redeem() { }} modalMargin="2%" widgetAction={widgetAction} + deliveryInfo={deliveryInfoDecoded} postDeliveryInfoUrl={postDeliveryInfoUrl} postDeliveryInfoHeaders={postDeliveryInfoHeadersDecoded} + postRedemptionSubmittedUrl={postRedemptionSubmittedUrl} + postRedemptionSubmittedHeaders={postRedemptionSubmittedHeadersDecoded} + postRedemptionConfirmedUrl={postRedemptionConfirmedUrl} + postRedemptionConfirmedHeaders={postRedemptionConfirmedHeadersDecoded} > ); } diff --git a/tests/redemptionBackend/src/router.ts b/tests/redemptionBackend/src/router.ts index 5eac54b..4e72f23 100644 --- a/tests/redemptionBackend/src/router.ts +++ b/tests/redemptionBackend/src/router.ts @@ -37,6 +37,37 @@ router.post("/fail2", (req, res, next) => { }, 2000); }); +router.post("/fail3", (req, res, next) => { + console.log( + "Receive POST request", + req.url, + req.query, + req.params, + req.body, + req.headers + ); + setTimeout(() => { + res.status(409).json({ + accepted: false, + reason: `Something has gone wrong` + }); + }, 2000); +}); + +router.post("/fail4", (req, res, next) => { + console.log( + "Receive POST request", + req.url, + req.query, + req.params, + req.body, + req.headers + ); + setTimeout(() => { + res.status(409).send(`Help me please....`); + }, 2000); +}); + router.post("/deliveryInfo", (req, res, next) => { console.log( "Receive POST request",