Skip to content

Commit

Permalink
add buySessionId to currently live app coinify
Browse files Browse the repository at this point in the history
  • Loading branch information
adammino-ledger committed Jun 28, 2024
1 parent c8022fa commit e6d3779
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pages/buy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type QueryParams = {
mode?: "onRamp" | "offRamp";
fiatAmount?: string;
cryptoAmount?: string;
buySessionId?: string;
};

const Page = () => {
Expand All @@ -25,6 +26,7 @@ const Page = () => {
primaryColor,
fiatAmount,
cryptoAmount,
buySessionId,
} = router.query as QueryParams;

if (accountAddress && cryptoCurrencyId) {
Expand All @@ -38,6 +40,7 @@ const Page = () => {
cryptoAmount={cryptoAmount}
language={language}
primaryColor={primaryColor}
buySessionId={buySessionId}
/>
);
}
Expand Down
3 changes: 3 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type QueryParams = {
mode?: "onRamp" | "offRamp";
fiatAmount?: string;
cryptoAmount?: string;
buySessionId?: string;
};

type PageState = {
Expand Down Expand Up @@ -56,6 +57,7 @@ const Page = () => {
mode,
fiatAmount,
cryptoAmount,
buySessionId,
} = router.query as QueryParams;

if (state.data) {
Expand All @@ -69,6 +71,7 @@ const Page = () => {
defaultFiatAmount={fiatAmount}
language={language}
primaryColor={primaryColor}
buySessionId={buySessionId}
accounts={state.data.accounts}
currencies={state.data.currencies}
/>
Expand Down
25 changes: 21 additions & 4 deletions src/Coinify/CoinifyWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type CoinifyWidgetConfig = {
confirmMessages?: boolean;
buyAmount?: string;
sellAmount?: string;
partnerContext?: string;
};

type Props = {
Expand All @@ -72,6 +73,7 @@ type Props = {
fiatAmount?: string;
language?: string;
primaryColor?: string;
buySessionId?: string;
};

const CoinifyWidget = ({
Expand All @@ -82,6 +84,7 @@ const CoinifyWidget = ({
fiatAmount,
cryptoAmount,
primaryColor,
buySessionId = "",
}: Props) => {
const api = useApi();

Expand All @@ -91,7 +94,7 @@ const CoinifyWidget = ({
const [widgetLoaded, setWidgetLoaded] = useState(false);

const widgetRef: { current: null | HTMLIFrameElement } = useRef(null);

const partnerContext = { buySessionId };
const coinifyConfig = COINIFY_CONFIG[env];
const widgetConfig: CoinifyWidgetConfig = {
primaryColor,
Expand All @@ -100,6 +103,7 @@ const CoinifyWidget = ({
defaultFiatCurrency: fiatCurrencyId ? fiatCurrencyId : undefined,
address: account.address,
targetPage: mode,
partnerContext: JSON.stringify(partnerContext),
};

// FIXME: could use switch case?
Expand Down Expand Up @@ -150,6 +154,9 @@ const CoinifyWidget = ({
context: {
address,
confirmed: true,
partnerContext: {
buySessionId,
},
},
},
coinifyConfig.host
Expand All @@ -158,7 +165,7 @@ const CoinifyWidget = ({
console.log(`Coinify Confirm OnRamp End | currencyName: ${currency}`);
}
},
[coinifyConfig.host, currency, account, mode]
[coinifyConfig.host, currency, account, mode, buySessionId]
);

const handleOnResult = useCallback(() => {
Expand All @@ -171,6 +178,9 @@ const CoinifyWidget = ({
context: {
address: account.address,
confirmed: true,
partnerContext: {
buySessionId,
},
},
},
coinifyConfig.host
Expand All @@ -197,7 +207,7 @@ const CoinifyWidget = ({
}
}
}
}, [coinifyConfig.host, currency, account, mode]);
}, [coinifyConfig.host, currency, account, mode, buySessionId]);

const handleOnCancel = useCallback(() => {
if (widgetRef?.current?.contentWindow) {
Expand All @@ -209,6 +219,9 @@ const CoinifyWidget = ({
context: {
address: account.address,
confirmed: false,
partnerContext: {
buySessionId,
},
},
},
coinifyConfig.host
Expand All @@ -227,7 +240,7 @@ const CoinifyWidget = ({
);
}
}
}, [coinifyConfig.host, account, mode]);
}, [coinifyConfig.host, account, mode, buySessionId]);

const setTransactionId = useCallback(
(txId) => {
Expand Down Expand Up @@ -334,6 +347,9 @@ const CoinifyWidget = ({
context: {
confirmed: true,
tradeId: tradeId.current,
partnerContext: {
buySessionId,
},
},
},
coinifyConfig.host
Expand Down Expand Up @@ -386,6 +402,7 @@ const CoinifyWidget = ({
initSellFlow,
mode,
api,
buySessionId,
]);

return (
Expand Down
24 changes: 21 additions & 3 deletions src/Coinify/CoinifyWidgetBuy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type CoinifyWidgetConfig = {
confirmMessages?: boolean;
buyAmount?: string;
sellAmount?: string;
partnerContext?: string;
};

type Props = {
Expand All @@ -57,6 +58,7 @@ type Props = {
fiatAmount?: string;
language?: string;
primaryColor?: string;
buySessionId?: string;
};

const CoinifyWidgetBuy = ({
Expand All @@ -67,6 +69,7 @@ const CoinifyWidgetBuy = ({
fiatAmount,
cryptoAmount,
primaryColor,
buySessionId = "",
}: Props) => {
const api = useApi();

Expand All @@ -76,6 +79,7 @@ const CoinifyWidgetBuy = ({
const [widgetLoaded, setWidgetLoaded] = useState(false);

const widgetRef: { current: null | HTMLIFrameElement } = useRef(null);
const partnerContext = { buySessionId };

const coinifyConfig = COINIFY_CONFIG[env];
const widgetConfig: CoinifyWidgetConfig = {
Expand All @@ -85,6 +89,7 @@ const CoinifyWidgetBuy = ({
defaultFiatCurrency: fiatCurrencyId ? fiatCurrencyId : undefined,
address: accountAddress,
targetPage: mode,
partnerContext: JSON.stringify(partnerContext),
};

// FIXME: could use switch case?
Expand Down Expand Up @@ -139,6 +144,9 @@ const CoinifyWidgetBuy = ({
context: {
address,
confirmed: true,
partnerContext: {
buySessionId,
},
},
},
coinifyConfig.host
Expand All @@ -147,7 +155,7 @@ const CoinifyWidgetBuy = ({
console.log(`Coinify Confirm OnRamp End | currencyName: ${currency}`);
}
},
[coinifyConfig.host, currency, accountAddress, mode]
[coinifyConfig.host, currency, accountAddress, mode, buySessionId]
);

const handleOnResult = useCallback(() => {
Expand All @@ -160,6 +168,9 @@ const CoinifyWidgetBuy = ({
context: {
address: accountAddress,
confirmed: true,
partnerContext: {
buySessionId,
},
},
},
coinifyConfig.host
Expand All @@ -186,7 +197,7 @@ const CoinifyWidgetBuy = ({
}
}
}
}, [coinifyConfig.host, currency, accountAddress, mode]);
}, [coinifyConfig.host, currency, accountAddress, mode, buySessionId]);

const handleOnCancel = useCallback(() => {
if (widgetRef?.current?.contentWindow) {
Expand All @@ -198,6 +209,9 @@ const CoinifyWidgetBuy = ({
context: {
address: accountAddress,
confirmed: false,
partnerContext: {
buySessionId,
},
},
},
coinifyConfig.host
Expand All @@ -216,7 +230,7 @@ const CoinifyWidgetBuy = ({
);
}
}
}, [coinifyConfig.host, accountAddress, mode]);
}, [coinifyConfig.host, accountAddress, mode, buySessionId]);

useEffect(() => {
if (!accountAddress) return;
Expand All @@ -237,6 +251,9 @@ const CoinifyWidgetBuy = ({
context: {
confirmed: true,
tradeId: tradeId.current,
partnerContext: {
buySessionId,
},
},
},
coinifyConfig.host
Expand Down Expand Up @@ -282,6 +299,7 @@ const CoinifyWidgetBuy = ({
handleOnResultBuy,
mode,
api,
buySessionId,
]);

return (
Expand Down
4 changes: 4 additions & 0 deletions src/Coinify/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type CoinifyProps = {
defaultCryptoAmount?: string;
defaultFiatAmount?: string;
language?: string;
buySessionId?: string;
primaryColor?: string;
currencies: Currency[];
accounts: Account[];
Expand All @@ -78,6 +79,7 @@ const Coinify = ({
accounts,
language,
primaryColor,
buySessionId,
}: CoinifyProps) => {
const api = useApi();

Expand Down Expand Up @@ -127,6 +129,7 @@ const Coinify = ({
accounts,
selectedAccount,
selectedCurrency,
buySessionId,
});

const selectAccount = async () => {
Expand Down Expand Up @@ -190,6 +193,7 @@ const Coinify = ({
cryptoAmount={defaultCryptoAmount}
language={language}
primaryColor={primaryColor}
buySessionId={buySessionId}
/>
) : (
<>
Expand Down

0 comments on commit e6d3779

Please sign in to comment.