-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* save * recip * more build out * testing page * ?
- Loading branch information
Showing
22 changed files
with
249 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const embeddedCheckoutIFrameId = "crossmint-embedded-checkout.iframe"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./embed"; |
107 changes: 64 additions & 43 deletions
107
packages/core/base/src/services/embed/crossmintIFrameService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,93 @@ | ||
import { embeddedCheckoutIFrameId } from "@/consts"; | ||
import { | ||
CrossmintEmbeddedCheckoutProps, | ||
CrossmintEvent, | ||
CrossmintEvents, | ||
CrossmintInternalEvent, | ||
CrossmintInternalEvents, | ||
IncomingInternalEvent, | ||
IncomingInternalEvents, | ||
OutgoingInternalEvent, | ||
} from "@/types"; | ||
|
||
import { getEnvironmentBaseUrl, isFiatEmbeddedCheckoutProps } from "../../utils"; | ||
|
||
// TODO: Emit updatable parameters | ||
export function crossmintIFrameService(props: CrossmintEmbeddedCheckoutProps) { | ||
return { | ||
getUrl, | ||
listenToEvents, | ||
listenToInternalEvents, | ||
}; | ||
} | ||
const targetOrigin = getEnvironmentBaseUrl(props.environment); | ||
|
||
function getUrl(props: CrossmintEmbeddedCheckoutProps) { | ||
const baseUrl = getEnvironmentBaseUrl(props.environment); | ||
const path = isFiatEmbeddedCheckoutProps(props) ? "/sdk/paymentElement" : "/sdk/2023-06-09/embeddedCheckout"; // TODO: v2.0 - remove '/sdk/paymentElement' | ||
function getUrl(props: CrossmintEmbeddedCheckoutProps) { | ||
const path = isFiatEmbeddedCheckoutProps(props) ? "/sdk/paymentElement" : "/sdk/2023-06-09/embeddedCheckout"; // TODO: v2.0 - remove '/sdk/paymentElement' | ||
|
||
const queryParams = new URLSearchParams(); | ||
const queryParams = new URLSearchParams(); | ||
|
||
let key: keyof CrossmintEmbeddedCheckoutProps; | ||
for (key in props) { | ||
const value = props[key] as unknown; | ||
const paramsToExclude = ["environment"]; | ||
|
||
if (!value || typeof value === "function") { | ||
continue; | ||
} | ||
if (typeof value === "object") { | ||
queryParams.append(key, JSON.stringify(value)); | ||
} else if (typeof value === "string") { | ||
if (value === "undefined") { | ||
let key: keyof CrossmintEmbeddedCheckoutProps; | ||
for (key in props) { | ||
const value = props[key] as unknown; | ||
|
||
if (!value || typeof value === "function" || paramsToExclude.includes(key)) { | ||
continue; | ||
} | ||
queryParams.append(key, value); | ||
} else if (["boolean", "number"].includes(typeof value)) { | ||
queryParams.append(key, value.toString()); | ||
if (typeof value === "object") { | ||
queryParams.append(key, JSON.stringify(value)); | ||
} else if (typeof value === "string") { | ||
if (value === "undefined") { | ||
continue; | ||
} | ||
queryParams.append(key, value); | ||
} else if (["boolean", "number"].includes(typeof value)) { | ||
queryParams.append(key, value.toString()); | ||
} | ||
} | ||
|
||
return `${targetOrigin}${path}?${queryParams.toString()}`; | ||
} | ||
|
||
return `${baseUrl}${path}?${queryParams.toString()}`; | ||
} | ||
function _listenToEvents<EP = CrossmintEvent | CrossmintInternalEvent>( | ||
callback: (event: MessageEvent<EP>) => void, | ||
validEventTypes: { | ||
[key: string]: CrossmintEvents | CrossmintInternalEvents; | ||
} | ||
) { | ||
function _onEvent(event: MessageEvent) { | ||
if (event.origin !== targetOrigin) { | ||
console.log("[Crossmint] Received event from invalid origin", event.origin, targetOrigin); | ||
return; | ||
} | ||
if (Object.values(validEventTypes).includes(event.data.type)) { | ||
callback(event); | ||
} | ||
} | ||
|
||
function _listenToEvents<EP = CrossmintEvent | CrossmintInternalEvent>( | ||
callback: (event: MessageEvent<EP>) => void, | ||
validEventTypes: { | ||
[key: string]: CrossmintEvents | CrossmintInternalEvents; | ||
window.addEventListener("message", _onEvent); | ||
return () => { | ||
window.removeEventListener("message", _onEvent); | ||
}; | ||
} | ||
) { | ||
function _onEvent(event: MessageEvent) { | ||
if (event.origin !== window.origin) { | ||
|
||
const listenToEvents = (callback: (event: MessageEvent<CrossmintEvent>) => void) => | ||
_listenToEvents(callback, CrossmintEvents); | ||
const listenToInternalEvents = (callback: (event: MessageEvent<IncomingInternalEvent>) => void) => | ||
_listenToEvents(callback, IncomingInternalEvents); | ||
|
||
function emitInternalEvent(event: OutgoingInternalEvent) { | ||
const iframe = document.getElementById(embeddedCheckoutIFrameId) as HTMLIFrameElement | null; | ||
if (iframe == null) { | ||
console.error("[Crossmint] Failed to find crossmint-embedded-checkout.iframe"); | ||
return; | ||
} | ||
if (Object.values(validEventTypes).includes(event.data.type)) { | ||
callback(event); | ||
try { | ||
iframe.contentWindow?.postMessage(event, targetOrigin); | ||
} catch (e) { | ||
console.error("[Crossmint] Failed to emit internal event", event, e); | ||
} | ||
} | ||
|
||
window.addEventListener("message", _onEvent); | ||
return () => { | ||
window.removeEventListener("message", _onEvent); | ||
return { | ||
getUrl, | ||
listenToEvents, | ||
listenToInternalEvents, | ||
emitInternalEvent, | ||
}; | ||
} | ||
|
||
const listenToEvents = (callback: (event: MessageEvent<CrossmintEvent>) => void) => | ||
_listenToEvents(callback, CrossmintEvents); | ||
const listenToInternalEvents = (callback: (event: MessageEvent<CrossmintInternalEvent>) => void) => | ||
_listenToEvents(callback, CrossmintInternalEvents); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
// All internal events | ||
export const CrossmintInternalEvents = { | ||
PARAMS_UPDATE: "params-update", | ||
export const IncomingInternalEvents = { | ||
UI_HEIGHT_CHANGED: "ui:height.changed", | ||
CRYPTO_PAYMENT_INCOMING_TRANSACTION: "crypto-payment:incoming-transaction", | ||
} as const; | ||
export type IncomingInternalEvents = (typeof IncomingInternalEvents)[keyof typeof IncomingInternalEvents]; | ||
|
||
export const OutgoingInternalEvents = { | ||
PARAMS_UPDATE: "params-update", | ||
CRYPTO_PAYMENT_USER_ACCEPTED: "crypto-payment:user-accepted", | ||
CRYPTO_PAYMENT_USER_REJECTED: "crypto-payment:user-rejected", | ||
CRYPTO_PAYMENT_INCOMING_TRANSACTION: "crypto-payment:incoming-transaction", | ||
} as const; | ||
export type OutgoingInternalEvents = (typeof OutgoingInternalEvents)[keyof typeof OutgoingInternalEvents]; | ||
|
||
// All internal events | ||
export const CrossmintInternalEvents = { | ||
...IncomingInternalEvents, | ||
...OutgoingInternalEvents, | ||
} as const; | ||
export type CrossmintInternalEvents = (typeof CrossmintInternalEvents)[keyof typeof CrossmintInternalEvents]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import { CrossmintInternalEvents } from "./events"; | ||
import { IncomingInternalEvents, OutgoingInternalEvents } from "./events"; | ||
import { CrossmintInternalEventMap } from "./payloads"; | ||
|
||
export * from "./events"; | ||
export * from "./payloads"; | ||
|
||
export type CrossmintInternalEvent = { | ||
[K in CrossmintInternalEvents]: { | ||
export type IncomingInternalEvent = { | ||
[K in IncomingInternalEvents]: { | ||
type: K; | ||
payload: CrossmintInternalEventMap[K]; | ||
}; | ||
}[CrossmintInternalEvents]; | ||
}[IncomingInternalEvents]; | ||
|
||
export type OutgoingInternalEvent = { | ||
[K in OutgoingInternalEvents]: { | ||
type: K; | ||
payload: CrossmintInternalEventMap[K]; | ||
}; | ||
}[OutgoingInternalEvents]; | ||
|
||
export type CrossmintInternalEvent = IncomingInternalEvent | OutgoingInternalEvent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,38 @@ | ||
import { useState } from "react"; | ||
|
||
import { CrossmintPaymentElement } from "@crossmint/client-sdk-react-ui"; | ||
|
||
export default function PaymentElementPage() { | ||
const [count, setCount] = useState(1); | ||
|
||
return ( | ||
<CrossmintPaymentElement | ||
environment="http://localhost:3000" | ||
collectionId="<COLLECTION_ID>" | ||
projectId="<PROJECT_ID>" | ||
mintConfig={{ totalPrice: "0.001", quantity: "1" }} | ||
recipient={{ email: "[email protected]" }} | ||
/> | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
width: "100%", | ||
gap: "20px", | ||
}} | ||
> | ||
<button onClick={() => setCount(count + 1)}>Increment count: {count}</button> | ||
<CrossmintPaymentElement | ||
environment="https://crossmint-main-git-checkout-embedded-new-element-p3-crossmint.vercel.app" | ||
clientId="db218e78-d042-4761-83af-3c4e5e6659dd" | ||
recipient={{ wallet: "maxfQWBno84Zfu4sXgmjYvsvLn4LzGFSgSkFMFuzved" }} | ||
mintConfig={{ | ||
testCount: count, | ||
}} | ||
paymentMethod="ETH" | ||
signer={{ | ||
address: "0xdC9bb9929b79b62d630A7C3568c979a2843eFd8b", | ||
signAndSendTransaction: async (tx) => { | ||
return "0x1234"; | ||
}, | ||
}} | ||
onEvent={(event) => { | ||
console.log(event); | ||
}} | ||
/> | ||
</div> | ||
); | ||
} |
13 changes: 0 additions & 13 deletions
13
packages/ui/react-ui/src/components/embed/CryptoEmbeddedCheckout.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.