Skip to main content

Embed Checkout

Embed Checkout​

Load the embed script once, then show the checkout with either an HTML element or a JavaScript call. Both use the same short_code.

Get the short code​

The checkout identifies what to pay by a short_code. It is returned by Create Charge and Create Payment Link as data.short_code, and by their Get responses, so you can read it again later. Morph does not keep any link between a short_code and your own orders; store it with your order if you need it.

caution

short_code is an empty string "" when the short link could not be generated. Check for an empty value before embedding, and fall back to the Hosted Checkout URL.

Load the script​

<script src="https://embed-pay.morph.network/embed/v1/embed.js"></script>
note

Load it as a classic script, not type="module", and always from the Morph embed domain. The checkout address is built into the script, so the iframe can only ever point at Morph Payments checkout, whichever page loads it. There is nothing to configure and no checkout URL to enter.

HTML element​

<morph-checkout
short-code="abc123"
lang="en-US"
redirect-on-completion="always"
height="760"
></morph-checkout>
AttributeRequiredDefaultDescription
short-codeYesThe short_code.
langNoen-USCheckout language in xx-XX form, for example zh-CN. Any other value falls back to en-US.
redirect-on-completionNoalwaysalways: after payment the customer's "Back to Merchant" button navigates your page to the return_url of the Payment Link or Charge. never: the checkout does not navigate; you decide what happens next (see After payment).
heightNo760Initial height in pixels. The checkout then reports its real height and the frame resizes itself. The minimum is 240.

JavaScript API​

Use this when you build the page dynamically or the short_code arrives after load.

const checkout = Morph.checkout.create({
shortCode: 'abc123',
lang: 'en-US',
redirectOnCompletion: 'never',
height: 760,
onSuccess: () => showThankYou(),
onError: ({ reason }) => showError(reason),
});

checkout.mount('#checkout'); // a CSS selector or an HTMLElement
// later
checkout.destroy();
OptionTypeDescription
shortCodestringRequired. The short_code.
langstringSame as the lang attribute.
redirectOnCompletion'always' | 'never'Same as redirect-on-completion.
heightnumberSame as height.
onReady, onSuccess, onPending, onExpired, onError, onReturnfunctionEvent callbacks. See Events.

mount(target) throws if the selector matches nothing. Calling mount again replaces the previous frame. destroy() removes the frame and stops listening.

Layout​

The element is display: block and fills the width of its container. Give the container the width you want; the checkout adapts. There are no theming options.

Events​

The checkout tells your page what the customer is doing so you can update your UI.

Do not fulfil orders from frontend events

These events come from the browser and can be faked or missed. A customer can close the tab before an event fires, and anyone can run dispatchEvent in their own browser. Use them only to change what the customer sees. Confirm payment with the signed webhook. See Events Reference and Signature Verification.

Event (element)Callback (JavaScript API)When
morph:readyonReadyThe checkout finished loading.
morph:successonSuccessThe customer's payment succeeded.
morph:pendingonPendingA payment was detected and is still being confirmed, or is incomplete (for example underpaid or sent in the wrong token).
morph:expiredonExpiredThe payment window ended without a successful payment.
morph:erroronErrorThe checkout could not load or the payment failed. Carries reason.
morph:returnonReturnThe customer clicked Back to Merchant. Only with redirect-on-completion="never".

Listen on the element. Events bubble, so you can also listen on a parent element.

<morph-checkout
id="checkout"
short-code="abc123"
redirect-on-completion="never"
></morph-checkout>

<script>
const el = document.getElementById('checkout');
el.addEventListener('morph:success', () => showPaidMessage());
el.addEventListener('morph:error', (e) => showError(e.detail.reason));
el.addEventListener('morph:return', () => {
window.location.href = '/orders/1001';
});
</script>

morph:error and onError carry detail.reason / { reason }:

reasonMeaning
token_invalidThe short_code is wrong or no longer valid.
link_unavailableThe Payment Link is inactive, or the Charge is closed, expired, already paid, or deleted.
load_failedThe checkout could not load its data.
payment_failedThe payment failed.

The reason is intentionally coarse.

After payment​

With redirect-on-completion="always" (default) the customer's Back to Merchant button navigates your page to the return_url set on the Payment Link or Charge. Only http and https URLs are followed, and if no return_url was set nothing happens. See Return URL.

With never, the checkout never navigates. Use onReturn to send the customer where you want, and keep waiting for the webhook before treating the order as paid.