@amos.com/amos-js 0.9.4 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +98 -90
- package/dist/messaging.d.ts +14 -0
- package/dist/types.d.ts +3 -0
- package/package.json +1 -1
- package/src/apple-pay.ts +2 -1
- package/src/index.ts +1 -0
- package/src/messaging.ts +73 -18
- package/src/mount.ts +1 -1
- package/src/types.ts +4 -0
package/README.md
CHANGED
|
@@ -287,6 +287,10 @@ Validates the embedded card/bank iframe form. Returns `Promise<boolean>` (resolv
|
|
|
287
287
|
|
|
288
288
|
Forward an embed JWT to the iframe so it can complete the payment / setup intent confirmation. The matching `payment_intent_id` / `setup_intent_id` is extracted from the JWT and forwarded automatically.
|
|
289
289
|
|
|
290
|
+
### `resetForm({ iframe })`
|
|
291
|
+
|
|
292
|
+
Clears all field values and API errors in the embedded card/bank iframe form. Call after `onResult` when the customer wants to try again.
|
|
293
|
+
|
|
290
294
|
### `attachPaymentMethodFormListeners(iframe, options)`
|
|
291
295
|
|
|
292
296
|
Lower-level helper that wires up the host-page side of the credit-card or bank-account iframe message protocol on an existing `<iframe>` element. The iframe is expected to have been added to the DOM with the correct `src` already (see `getCreditCardFormSrc` / `getBankAccountFormSrc`). Returns `{ update, destroy }`.
|
|
@@ -315,7 +319,7 @@ Advanced helpers exposed for integrators that need to construct or inspect the m
|
|
|
315
319
|
|
|
316
320
|
## Notes and potential gotchas
|
|
317
321
|
|
|
318
|
-
- **`iframe` argument**: every messaging helper (`validateForm`, `confirmPaymentIntent`, `confirmSetupIntent`) accepts the `iframe` element directly. With the mount helpers, use `controller.iframe`.
|
|
322
|
+
- **`iframe` argument**: every messaging helper (`validateForm`, `confirmPaymentIntent`, `confirmSetupIntent`, `resetForm`) accepts the `iframe` element directly. With the mount helpers, use `controller.iframe`.
|
|
319
323
|
- **Same components for payment vs setup intents**: `mountAmosCreditCardPaymentMethodForm` and `mountAmosBankAccountPaymentMethodForm` support both payment intents and setup intents. The flow differs only by which server call you make and which confirmation function you use. Handle both outcomes via `onResult`.
|
|
320
324
|
- **Amount format**: for `mountAmosGooglePayButton` and `mountAmosApplePayButton`, `amount` is a string (e.g. `"5000"` for $50.00). For `components["schemas"]["CreatePaymentIntentInput"]` on the server, `amount` is a number in cents (e.g. `5000`).
|
|
321
325
|
- **Browser-only**: the mount and messaging helpers require `window` and the DOM. They are not safe to call during server-side rendering — call them from client-side code only (for example, inside a `useEffect`-like hook in your framework of choice).
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export { attachApplePayButtonListeners, getApplePayButtonInitialHeight, getApple
|
|
|
4
4
|
export type { FormattedGooglePayPaymentData, GooglePayButtonController, GooglePayButtonListenerOptions, } from './google-pay';
|
|
5
5
|
export { attachGooglePayButtonListeners, formatGooglePayPaymentData, getGooglePayButtonInitialHeight, getGooglePayButtonSrc, } from './google-pay';
|
|
6
6
|
export { decodeJwt, getEmbedOrigin } from './jwt';
|
|
7
|
-
export { confirmPaymentIntent, confirmSetupIntent, sendConfirmationResult, sendParentReadyMessage, updateAmount, updateAppearance, updateMerchantName, validateForm, } from './messaging';
|
|
7
|
+
export { confirmPaymentIntent, confirmSetupIntent, resetForm, sendConfirmationResult, sendParentReadyMessage, updateAmount, updateAppearance, updateMerchantName, validateForm, } from './messaging';
|
|
8
8
|
export type { AmosApplePayButtonMountController, AmosApplePayButtonOptions, AmosBankAccountPaymentMethodFormOptions, AmosCreditCardPaymentMethodFormOptions, AmosGooglePayButtonMountController, AmosGooglePayButtonOptions, AmosPaymentMethodFormMountController, } from './mount';
|
|
9
9
|
export { mountAmosApplePayButton, mountAmosBankAccountPaymentMethodForm, mountAmosCreditCardPaymentMethodForm, mountAmosGooglePayButton, } from './mount';
|
|
10
10
|
export type { BillingAddressRequirement, CreditCardAdditionalFields, PaymentMethodFormController, PaymentMethodFormListenerOptions, } from './payment-method-form';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=`data-amos-apple-pay-waiting`;function t({onCancel:t}){let n=document.querySelector(`[${e}]`);if(n)return n;let r=document.createElement(`div`);r.setAttribute(e,`true`),r.setAttribute(`role`,`dialog`),r.setAttribute(`aria-modal`,`true`),r.setAttribute(`aria-labelledby`,`amos-apple-pay-waiting-title`),Object.assign(r.style,{position:`fixed`,inset:`0`,zIndex:`2147483646`,display:`flex`,alignItems:`center`,justifyContent:`center`,padding:`24px`,boxSizing:`border-box`,background:`rgba(0, 0, 0, 0.55)`,fontFamily:`-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif`});let i=document.createElement(`div`);Object.assign(i.style,{width:`100%`,maxWidth:`360px`,borderRadius:`12px`,background:`#fff`,padding:`28px 24px 20px`,boxSizing:`border-box`,textAlign:`center`,boxShadow:`0 12px 40px rgba(0, 0, 0, 0.25)`});let a=document.createElement(`div`);a.setAttribute(`aria-hidden`,`true`),Object.assign(a.style,{display:`inline-flex`,alignItems:`center`,justifyContent:`center`,gap:`6px`,marginBottom:`16px`,fontSize:`28px`,fontWeight:`600`,letterSpacing:`-0.02em`,color:`#000`,lineHeight:`1`}),a.innerHTML=`<svg width="22" height="26" viewBox="0 0 814 1000" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M788.1 340.9c-5.8 4.5-108.2 62.2-108.2 190.5 0 148.4 130.3 200.9 134.2 202.2-.6 3.2-20.7 71.9-68.7 141.9-42.8 61.6-87.5 123.1-155.5 123.1s-85.5-39.5-164-39.5c-76.5 0-103.7 40.8-165.9 40.8s-105.6-57-155.5-127C46.7 790.7 0 663 0 541.8c0-194.4 126.4-297.5 250.8-297.5 66.1 0 121.2 43.4 162.7 43.4 39.5 0 101.1-46 176.3-46 28.5 0 130.9 2.6 198.3 99.2zm-234-181.5c31.1-36.9 53.1-88.1 53.1-139.3 0-7.1-.6-14.3-1.9-20.1-50.6 1.9-110.8 33.7-147.1 75.8-28.5 32.4-55.1 83.6-55.1 135.5 0 7.8 1.3 15.6 1.9 18.1 3.2.6 8.4 1.3 13.6 1.3 45.4 0 102.5-30.4 135.5-71.3z"/></svg><span>Pay</span>`;let o=document.createElement(`p`);o.id=`amos-apple-pay-waiting-title`,Object.assign(o.style,{margin:`0 0 20px`,fontSize:`15px`,lineHeight:`1.45`,color:`#1a1a1a`}),o.textContent=`Complete your payment in the open Apple Pay window, or close Apple Pay to continue paying another way.`;let s=document.createElement(`button`);return s.type=`button`,s.textContent=`Cancel payment`,Object.assign(s.style,{display:`block`,width:`100%`,border:`none`,borderRadius:`8px`,padding:`12px 16px`,background:`#2c2c2e`,color:`#fff`,fontSize:`15px`,fontWeight:`500`,cursor:`pointer`}),s.addEventListener(`click`,t),i.append(a,o,s),r.append(i),document.body.append(r),r}function n(){document.querySelector(`[${e}]`)?.remove()}function r(e){let[t=``,n=``,r=``]=e?.split(`.`)??[],i=typeof atob==`function`?atob:e=>Buffer.from(e,`base64`).toString(`utf8`);return{header:JSON.parse(i(t)),payload:JSON.parse(i(n)),signature:r}}function i(e){let{env:t=`sandbox`}=r(e).payload;switch(t){case`production`:return`https://embed.amos.com`;case`sandbox`:return`https://embed-sandbox.amos.com`;default:return`https://embed-sandbox.amos.com`}}function a(e){return e}function o(e){e?.contentWindow?.postMessage(a({type:`PARENT_ACKNOWLEDGED_IFRAME_READY`}),`*`)}function s({iframe:e,appearance:t={}}){e?.contentWindow?.postMessage(a({type:`UPDATE_APPEARANCE`,appearance:t}),`*`)}function c({iframe:e,amount:t}){e?.contentWindow?.postMessage(a({type:`UPDATE_AMOUNT`,amount:t}),`*`)}function l({iframe:e,merchantName:t}){e?.contentWindow?.postMessage(a({type:`UPDATE_MERCHANT_NAME`,merchantName:t}),`*`)}function u({iframe:e}){let t=crypto.randomUUID();return new Promise(n=>{e?.contentWindow?.postMessage(a({type:`VALIDATE_FORM`,requestId:t}),`*`);let r=setTimeout(()=>{window.removeEventListener(`message`,i),n(!1)},5e3);function i(e){e.data.type===`VALIDATE_FORM`&&e.data.requestId===t&&(window.removeEventListener(`message`,i),clearTimeout(r),n(e.data.isValid??!1))}window.addEventListener(`message`,i)})}function d({iframe:e,token:t}){let{payment_intent_id:n}=r(t).payload;e?.contentWindow?.postMessage(a({type:`CONFIRM_PAYMENT_INTENT`,token:t,id:n??void 0}),`*`)}function f({iframe:e,token:t}){let{setup_intent_id:n}=r(t).payload;e?.contentWindow?.postMessage(a({type:`CONFIRM_SETUP_INTENT`,token:t,id:n??void 0}),`*`)}function p({iframe:e,result:t}){e?.contentWindow?.postMessage(a({type:`CONFIRMATION_RESULT`,result:t}),`*`)}function m(e){return`${i(e)}/iframe/apple-pay?token=${e}`}function h(){return`40px`}function g(e){e.contentWindow?.postMessage(a({type:`APPLE_PAY_CANCEL`}),`*`)}function _(e,r){let i={...r};function a(){c({iframe:e,amount:i.amount})}function u(){l({iframe:e,merchantName:i.merchantName})}function f(r){if(r.source===e.contentWindow)switch(r.data.type){case`IFRAME_READY`:o(e),s({iframe:e,appearance:i.appearance}),a(),u();break;case`UPDATE_HEIGHT`:i.onHeightChange?.(r.data.height);break;case`UPDATE_APPEARANCE`:s({iframe:e,appearance:r.data.appearance});break;case`UPDATED_APPEARANCE`:i.onAppearanceReady?.();break;case`APPLE_PAY_WINDOW_OPEN`:t({onCancel:()=>{g(e),n()}});break;case`APPLE_PAY_WINDOW_CLOSE`:n();break;case`CREATE_PAYMENT_INTENT`:i.onInitiatePaymentIntentRequest({paymentIntentCreateAttributes:r.data.paymentIntentCreateAttributes,customerCreateAttributes:r.data.customerCreateAttributes}).then(t=>{d({iframe:e,token:t})}).catch(e=>{n(),i.onResult({status:`failed`,errorMessage:e instanceof Error?e.message:`Unknown error`})});break;case`CONFIRMATION_RESULT`:n(),i.onResult(r.data.result)}}return window.addEventListener(`message`,f),{update(t){let n=`appearance`in t,r=`amount`in t,o=`merchantName`in t;i={...i,...t},n&&s({iframe:e,appearance:i.appearance}),r&&a(),o&&u()},destroy(){window.removeEventListener(`message`,f),n()}}}function v(e){return`${i(e)}/iframe/google-pay?token=${e}`}function y(){return`40px`}function b(e,t){let n={...t};function r(){c({iframe:e,amount:n.amount})}function i(){l({iframe:e,merchantName:n.merchantName})}function a(t){if(t.source===e.contentWindow)switch(t.data.type){case`IFRAME_READY`:o(e),s({iframe:e,appearance:n.appearance}),r(),i();break;case`UPDATE_HEIGHT`:n.onHeightChange?.(t.data.height);break;case`UPDATE_APPEARANCE`:s({iframe:e,appearance:t.data.appearance});break;case`UPDATED_APPEARANCE`:n.onAppearanceReady?.();break;case`CREATE_PAYMENT_INTENT`:n.onInitiatePaymentIntentRequest({paymentIntentCreateAttributes:t.data.paymentIntentCreateAttributes,customerCreateAttributes:t.data.customerCreateAttributes}).then(t=>{d({iframe:e,token:t})}).catch(e=>{n.onResult({status:`failed`,errorMessage:e instanceof Error?e.message:`Unknown error`})});break;case`CONFIRMATION_RESULT`:n.onResult(t.data.result)}}return window.addEventListener(`message`,a),{update(t){let a=`appearance`in t,o=`amount`in t,c=`merchantName`in t;n={...n,...t},a&&s({iframe:e,appearance:n.appearance}),o&&r(),c&&i()},destroy(){window.removeEventListener(`message`,a)}}}function x({paymentData:e}){return{paymentMethod:{type:`googlepay`,billing_address_attributes:{name:e.shippingAddress?.name,address_line1:e.shippingAddress?.address1,address_line2:e.shippingAddress?.address2,city:e.shippingAddress?.locality,state:e.shippingAddress?.administrativeArea,postal_code:e.shippingAddress?.postalCode,country:e.shippingAddress?.countryCode,email:e.email,phone:e.shippingAddress?.phoneNumber},card_profile_attributes:{wallet_payload:e.paymentMethodData.tokenizationData.token}}}}var S={country:212,full:452},C=80,w={country:400,full:640};function T(e,t={cardholderName:!1},n=`country`){let r=Object.entries(t).filter(([,e])=>e).map(([e])=>e).join(`,`),a=new URLSearchParams({token:e,additionalFields:r,billingAddressRequirement:n});return`${i(e)}/iframe/card?${a}`}function E(e,t=`country`){let n=new URLSearchParams({token:e,billingAddressRequirement:t});return`${i(e)}/iframe/bank?${n}`}function D(e={cardholderName:!1},t=`country`){return`${(S[t]??S.country)+(e.cardholderName?C:0)}px`}function O(e=`country`){return`${w[e]??w.country}px`}function k(e,t){let n={...t};function r(t){if(t.source===e.contentWindow)switch(t.data.type){case`IFRAME_READY`:o(e),s({iframe:e,appearance:n.appearance});break;case`UPDATE_HEIGHT`:n.onHeightChange?.(t.data.height);break;case`UPDATE_APPEARANCE`:s({iframe:e,appearance:t.data.appearance});break;case`UPDATED_APPEARANCE`:n.onAppearanceReady?.();break;case`CONFIRMATION_RESULT`:n.onResult(t.data.result)}}return window.addEventListener(`message`,r),{update(t){let r=`appearance`in t;n={...n,...t},r&&s({iframe:e,appearance:n.appearance})},destroy(){window.removeEventListener(`message`,r)}}}function A(e){if(typeof e==`string`){let t=document.querySelector(e);if(!(t instanceof HTMLElement))throw Error(`[amos-js] Container "${e}" did not match any HTMLElement.`);return t}return e}var j={width:`calc(100% + 8px)`,transition:`opacity 150ms ease-in, height 200ms ease-in-out`,margin:`0 -4px`,opacity:`0`,border:`0`};function M({src:e,title:t,name:n,height:r,allow:i}){let a=document.createElement(`iframe`);return a.src=e,a.title=t,a.name=n,a.setAttribute(`role`,`presentation`),a.scrolling=`no`,i&&(a.allow=i),Object.assign(a.style,j,{height:r}),a}function N(e,t){let n=A(e),{renderToken:r,additionalFields:i={cardholderName:!1},billingAddressRequirement:a=`country`,...o}=t,s=M({src:T(r,i,a),title:`Secure credit card payment method form powered by Amos`,name:`amos-credit-card-payment-method-form`,height:D(i,a)});n.appendChild(s);let c=k(s,{...o,onHeightChange:e=>{s.style.height=e,o.onHeightChange?.(e)},onAppearanceReady:()=>{s.style.opacity=`1`,o.onAppearanceReady?.()}});return{iframe:s,update:c.update,destroy(){c.destroy(),s.remove()}}}function P(e,t){let n=A(e),{renderToken:r,billingAddressRequirement:i=`country`,...a}=t,o=M({src:E(r,i),title:`Secure bank account payment method form powered by Amos`,name:`amos-bank-account-payment-method-form`,height:O(i)});n.appendChild(o);let s=k(o,{...a,onHeightChange:e=>{o.style.height=e,a.onHeightChange?.(e)},onAppearanceReady:()=>{o.style.opacity=`1`,a.onAppearanceReady?.()}});return{iframe:o,update:s.update,destroy(){s.destroy(),o.remove()}}}function F(e,t){let n=A(e),{renderToken:r,...i}=t,a=M({src:v(r),title:`Secure Google Pay button powered by Amos`,name:`amos-google-pay-button`,height:y(),allow:`payment`});n.appendChild(a);let o=b(a,{...i,onHeightChange:e=>{a.style.height=e,i.onHeightChange?.(e)},onAppearanceReady:()=>{a.style.opacity=`1`,i.onAppearanceReady?.()}});return{iframe:a,update:o.update,destroy(){o.destroy(),a.remove()}}}function I(e,t){let n=A(e),{renderToken:r,...i}=t,a=M({src:m(r),title:`Secure Apple Pay button powered by Amos`,name:`amos-apple-pay-button`,height:h(),allow:`payment`});n.appendChild(a);let o=_(a,{...i,onHeightChange:e=>{a.style.height=e,i.onHeightChange?.(e)},onAppearanceReady:()=>{a.style.opacity=`1`,i.onAppearanceReady?.()}});return{iframe:a,update:o.update,destroy(){o.destroy(),a.remove()}}}exports.attachApplePayButtonListeners=_,exports.attachGooglePayButtonListeners=b,exports.attachPaymentMethodFormListeners=k,exports.confirmPaymentIntent=d,exports.confirmSetupIntent=f,exports.createMessage=a,exports.decodeJwt=r,exports.formatGooglePayPaymentData=x,exports.getApplePayButtonInitialHeight=h,exports.getApplePayButtonSrc=m,exports.getBankAccountFormInitialHeight=O,exports.getBankAccountFormSrc=E,exports.getCreditCardFormInitialHeight=D,exports.getCreditCardFormSrc=T,exports.getEmbedOrigin=i,exports.getGooglePayButtonInitialHeight=y,exports.getGooglePayButtonSrc=v,exports.mountAmosApplePayButton=I,exports.mountAmosBankAccountPaymentMethodForm=P,exports.mountAmosCreditCardPaymentMethodForm=N,exports.mountAmosGooglePayButton=F,exports.sendConfirmationResult=p,exports.sendParentReadyMessage=o,exports.updateAmount=c,exports.updateAppearance=s,exports.updateMerchantName=l,exports.validateForm=u;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=`data-amos-apple-pay-waiting`;function t({onCancel:t}){let n=document.querySelector(`[${e}]`);if(n)return n;let r=document.createElement(`div`);r.setAttribute(e,`true`),r.setAttribute(`role`,`dialog`),r.setAttribute(`aria-modal`,`true`),r.setAttribute(`aria-labelledby`,`amos-apple-pay-waiting-title`),Object.assign(r.style,{position:`fixed`,inset:`0`,zIndex:`2147483646`,display:`flex`,alignItems:`center`,justifyContent:`center`,padding:`24px`,boxSizing:`border-box`,background:`rgba(0, 0, 0, 0.55)`,fontFamily:`-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif`});let i=document.createElement(`div`);Object.assign(i.style,{width:`100%`,maxWidth:`360px`,borderRadius:`12px`,background:`#fff`,padding:`28px 24px 20px`,boxSizing:`border-box`,textAlign:`center`,boxShadow:`0 12px 40px rgba(0, 0, 0, 0.25)`});let a=document.createElement(`div`);a.setAttribute(`aria-hidden`,`true`),Object.assign(a.style,{display:`inline-flex`,alignItems:`center`,justifyContent:`center`,gap:`6px`,marginBottom:`16px`,fontSize:`28px`,fontWeight:`600`,letterSpacing:`-0.02em`,color:`#000`,lineHeight:`1`}),a.innerHTML=`<svg width="22" height="26" viewBox="0 0 814 1000" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M788.1 340.9c-5.8 4.5-108.2 62.2-108.2 190.5 0 148.4 130.3 200.9 134.2 202.2-.6 3.2-20.7 71.9-68.7 141.9-42.8 61.6-87.5 123.1-155.5 123.1s-85.5-39.5-164-39.5c-76.5 0-103.7 40.8-165.9 40.8s-105.6-57-155.5-127C46.7 790.7 0 663 0 541.8c0-194.4 126.4-297.5 250.8-297.5 66.1 0 121.2 43.4 162.7 43.4 39.5 0 101.1-46 176.3-46 28.5 0 130.9 2.6 198.3 99.2zm-234-181.5c31.1-36.9 53.1-88.1 53.1-139.3 0-7.1-.6-14.3-1.9-20.1-50.6 1.9-110.8 33.7-147.1 75.8-28.5 32.4-55.1 83.6-55.1 135.5 0 7.8 1.3 15.6 1.9 18.1 3.2.6 8.4 1.3 13.6 1.3 45.4 0 102.5-30.4 135.5-71.3z"/></svg><span>Pay</span>`;let o=document.createElement(`p`);o.id=`amos-apple-pay-waiting-title`,Object.assign(o.style,{margin:`0 0 20px`,fontSize:`15px`,lineHeight:`1.45`,color:`#1a1a1a`}),o.textContent=`Complete your payment in the open Apple Pay window, or close Apple Pay to continue paying another way.`;let s=document.createElement(`button`);return s.type=`button`,s.textContent=`Cancel payment`,Object.assign(s.style,{display:`block`,width:`100%`,border:`none`,borderRadius:`8px`,padding:`12px 16px`,background:`#2c2c2e`,color:`#fff`,fontSize:`15px`,fontWeight:`500`,cursor:`pointer`}),s.addEventListener(`click`,t),i.append(a,o,s),r.append(i),document.body.append(r),r}function n(){document.querySelector(`[${e}]`)?.remove()}function r(e){let[t=``,n=``,r=``]=e?.split(`.`)??[],i=typeof atob==`function`?atob:e=>Buffer.from(e,`base64`).toString(`utf8`);return{header:JSON.parse(i(t)),payload:JSON.parse(i(n)),signature:r}}function i(e){let{env:t=`sandbox`}=r(e).payload;switch(t){case`production`:return`https://embed.amos.com`;case`sandbox`:return`https://embed-sandbox.amos.com`;default:return`https://embed-sandbox.amos.com`}}function a(e){return e}function o(e){return new URL(e.src).origin}function s(e){e?.contentWindow&&e.contentWindow.postMessage(a({type:`PARENT_ACKNOWLEDGED_IFRAME_READY`}),o(e))}function c({iframe:e,appearance:t={}}){e?.contentWindow&&e.contentWindow.postMessage(a({type:`UPDATE_APPEARANCE`,appearance:t}),o(e))}function l({iframe:e,amount:t}){e?.contentWindow&&e.contentWindow.postMessage(a({type:`UPDATE_AMOUNT`,amount:t}),o(e))}function u({iframe:e,merchantName:t}){e?.contentWindow&&e.contentWindow.postMessage(a({type:`UPDATE_MERCHANT_NAME`,merchantName:t}),o(e))}function d({iframe:e}){let t=crypto.randomUUID();return new Promise(n=>{e?.contentWindow&&e.contentWindow.postMessage(a({type:`VALIDATE_FORM`,requestId:t}),o(e));let r=setTimeout(()=>{window.removeEventListener(`message`,i),n(!1)},5e3);function i(e){e.data.type===`VALIDATE_FORM`&&e.data.requestId===t&&(window.removeEventListener(`message`,i),clearTimeout(r),n(e.data.isValid??!1))}window.addEventListener(`message`,i)})}function f({iframe:e}){e?.contentWindow&&e.contentWindow.postMessage(a({type:`RESET_FORM`}),o(e))}function p({iframe:e,token:t}){if(!e?.contentWindow)return;let{payment_intent_id:n}=r(t).payload;e.contentWindow.postMessage(a({type:`CONFIRM_PAYMENT_INTENT`,token:t,id:n??void 0}),o(e))}function m({iframe:e,token:t}){if(!e?.contentWindow)return;let{setup_intent_id:n}=r(t).payload;e.contentWindow.postMessage(a({type:`CONFIRM_SETUP_INTENT`,token:t,id:n??void 0}),o(e))}function h({iframe:e,result:t}){e?.contentWindow&&e.contentWindow.postMessage(a({type:`CONFIRMATION_RESULT`,result:t}),o(e))}function g(e){return`${i(e)}/iframe/apple-pay?token=${e}`}function _(){return`40px`}function v(e){e.contentWindow?.postMessage(a({type:`APPLE_PAY_CANCEL`}),o(e))}function y(e,r){let i={...r};function a(){l({iframe:e,amount:i.amount})}function o(){u({iframe:e,merchantName:i.merchantName})}function d(r){if(r.source===e.contentWindow)switch(r.data.type){case`IFRAME_READY`:s(e),c({iframe:e,appearance:i.appearance}),a(),o();break;case`UPDATE_HEIGHT`:i.onHeightChange?.(r.data.height);break;case`UPDATE_APPEARANCE`:c({iframe:e,appearance:r.data.appearance});break;case`UPDATED_APPEARANCE`:i.onAppearanceReady?.();break;case`APPLE_PAY_WINDOW_OPEN`:t({onCancel:()=>{v(e),n()}});break;case`APPLE_PAY_WINDOW_CLOSE`:n();break;case`CREATE_PAYMENT_INTENT`:i.onInitiatePaymentIntentRequest({paymentIntentCreateAttributes:r.data.paymentIntentCreateAttributes,customerCreateAttributes:r.data.customerCreateAttributes}).then(t=>{p({iframe:e,token:t})}).catch(e=>{n(),i.onResult({status:`failed`,errorMessage:e instanceof Error?e.message:`Unknown error`})});break;case`CONFIRMATION_RESULT`:n(),i.onResult(r.data.result)}}return window.addEventListener(`message`,d),{update(t){let n=`appearance`in t,r=`amount`in t,s=`merchantName`in t;i={...i,...t},n&&c({iframe:e,appearance:i.appearance}),r&&a(),s&&o()},destroy(){window.removeEventListener(`message`,d),n()}}}function b(e){return`${i(e)}/iframe/google-pay?token=${e}`}function x(){return`40px`}function S(e,t){let n={...t};function r(){l({iframe:e,amount:n.amount})}function i(){u({iframe:e,merchantName:n.merchantName})}function a(t){if(t.source===e.contentWindow)switch(t.data.type){case`IFRAME_READY`:s(e),c({iframe:e,appearance:n.appearance}),r(),i();break;case`UPDATE_HEIGHT`:n.onHeightChange?.(t.data.height);break;case`UPDATE_APPEARANCE`:c({iframe:e,appearance:t.data.appearance});break;case`UPDATED_APPEARANCE`:n.onAppearanceReady?.();break;case`CREATE_PAYMENT_INTENT`:n.onInitiatePaymentIntentRequest({paymentIntentCreateAttributes:t.data.paymentIntentCreateAttributes,customerCreateAttributes:t.data.customerCreateAttributes}).then(t=>{p({iframe:e,token:t})}).catch(e=>{n.onResult({status:`failed`,errorMessage:e instanceof Error?e.message:`Unknown error`})});break;case`CONFIRMATION_RESULT`:n.onResult(t.data.result)}}return window.addEventListener(`message`,a),{update(t){let a=`appearance`in t,o=`amount`in t,s=`merchantName`in t;n={...n,...t},a&&c({iframe:e,appearance:n.appearance}),o&&r(),s&&i()},destroy(){window.removeEventListener(`message`,a)}}}function C({paymentData:e}){return{paymentMethod:{type:`googlepay`,billing_address_attributes:{name:e.shippingAddress?.name,address_line1:e.shippingAddress?.address1,address_line2:e.shippingAddress?.address2,city:e.shippingAddress?.locality,state:e.shippingAddress?.administrativeArea,postal_code:e.shippingAddress?.postalCode,country:e.shippingAddress?.countryCode,email:e.email,phone:e.shippingAddress?.phoneNumber},card_profile_attributes:{wallet_payload:e.paymentMethodData.tokenizationData.token}}}}var w={country:212,full:452},T=80,E={country:400,full:640};function D(e,t={cardholderName:!1},n=`country`){let r=Object.entries(t).filter(([,e])=>e).map(([e])=>e).join(`,`),a=new URLSearchParams({token:e,additionalFields:r,billingAddressRequirement:n});return`${i(e)}/iframe/card?${a}`}function O(e,t=`country`){let n=new URLSearchParams({token:e,billingAddressRequirement:t});return`${i(e)}/iframe/bank?${n}`}function k(e={cardholderName:!1},t=`country`){return`${(w[t]??w.country)+(e.cardholderName?T:0)}px`}function A(e=`country`){return`${E[e]??E.country}px`}function j(e,t){let n={...t};function r(t){if(t.source===e.contentWindow)switch(t.data.type){case`IFRAME_READY`:s(e),c({iframe:e,appearance:n.appearance});break;case`UPDATE_HEIGHT`:n.onHeightChange?.(t.data.height);break;case`UPDATE_APPEARANCE`:c({iframe:e,appearance:t.data.appearance});break;case`UPDATED_APPEARANCE`:n.onAppearanceReady?.();break;case`CONFIRMATION_RESULT`:n.onResult(t.data.result)}}return window.addEventListener(`message`,r),{update(t){let r=`appearance`in t;n={...n,...t},r&&c({iframe:e,appearance:n.appearance})},destroy(){window.removeEventListener(`message`,r)}}}function M(e){if(typeof e==`string`){let t=document.querySelector(e);if(!(t instanceof HTMLElement))throw Error(`[amos-js] Container "${e}" did not match any HTMLElement.`);return t}return e}var N={width:`calc(100% + 8px)`,transition:`opacity 150ms ease-in, height 200ms ease-in-out`,margin:`0 -4px`,opacity:`0`,border:`0`};function P({src:e,title:t,name:n,height:r,allow:i}){let a=document.createElement(`iframe`);return a.src=e,a.title=t,a.name=n,a.setAttribute(`role`,`presentation`),a.scrolling=`no`,i&&(a.allow=i),Object.assign(a.style,N,{height:r}),a}function F(e,t){let n=M(e),{renderToken:r,additionalFields:i={cardholderName:!1},billingAddressRequirement:a=`country`,...o}=t,s=P({src:D(r,i,a),title:`Secure credit card payment method form powered by Amos`,name:`amos-credit-card-payment-method-form`,height:k(i,a)});n.appendChild(s);let c=j(s,{...o,onHeightChange:e=>{s.style.height=e,o.onHeightChange?.(e)},onAppearanceReady:()=>{s.style.opacity=`1`,o.onAppearanceReady?.()}});return{iframe:s,update:c.update,destroy(){c.destroy(),s.remove()}}}function I(e,t){let n=M(e),{renderToken:r,billingAddressRequirement:i=`country`,...a}=t,o=P({src:O(r,i),title:`Secure bank account payment method form powered by Amos`,name:`amos-bank-account-payment-method-form`,height:A(i)});n.appendChild(o);let s=j(o,{...a,onHeightChange:e=>{o.style.height=e,a.onHeightChange?.(e)},onAppearanceReady:()=>{o.style.opacity=`1`,a.onAppearanceReady?.()}});return{iframe:o,update:s.update,destroy(){s.destroy(),o.remove()}}}function L(e,t){let n=M(e),{renderToken:r,...i}=t,a=P({src:b(r),title:`Secure Google Pay button powered by Amos`,name:`amos-google-pay-button`,height:x(),allow:`payment`});n.appendChild(a);let o=S(a,{...i,onHeightChange:e=>{a.style.height=e,i.onHeightChange?.(e)},onAppearanceReady:()=>{a.style.opacity=`1`,i.onAppearanceReady?.()}});return{iframe:a,update:o.update,destroy(){o.destroy(),a.remove()}}}function R(e,t){let n=M(e),{renderToken:r,...i}=t,a=P({src:g(r),title:`Secure Apple Pay button powered by Amos`,name:`amos-apple-pay-button`,height:_(),allow:`payment`});n.appendChild(a);let o=y(a,{...i,onHeightChange:e=>{a.style.height=e,i.onHeightChange?.(e)},onAppearanceReady:()=>{a.style.opacity=`1`,i.onAppearanceReady?.()}});return{iframe:a,update:o.update,destroy(){o.destroy(),a.remove()}}}exports.attachApplePayButtonListeners=y,exports.attachGooglePayButtonListeners=S,exports.attachPaymentMethodFormListeners=j,exports.confirmPaymentIntent=p,exports.confirmSetupIntent=m,exports.createMessage=a,exports.decodeJwt=r,exports.formatGooglePayPaymentData=C,exports.getApplePayButtonInitialHeight=_,exports.getApplePayButtonSrc=g,exports.getBankAccountFormInitialHeight=A,exports.getBankAccountFormSrc=O,exports.getCreditCardFormInitialHeight=k,exports.getCreditCardFormSrc=D,exports.getEmbedOrigin=i,exports.getGooglePayButtonInitialHeight=x,exports.getGooglePayButtonSrc=b,exports.mountAmosApplePayButton=R,exports.mountAmosBankAccountPaymentMethodForm=I,exports.mountAmosCreditCardPaymentMethodForm=F,exports.mountAmosGooglePayButton=L,exports.resetForm=f,exports.sendConfirmationResult=h,exports.sendParentReadyMessage=s,exports.updateAmount=l,exports.updateAppearance=c,exports.updateMerchantName=u,exports.validateForm=d;
|
package/dist/index.mjs
CHANGED
|
@@ -90,33 +90,36 @@ function a(e) {
|
|
|
90
90
|
//#endregion
|
|
91
91
|
//#region src/messaging.ts
|
|
92
92
|
function o(e) {
|
|
93
|
-
|
|
93
|
+
return new URL(e.src).origin;
|
|
94
94
|
}
|
|
95
|
-
function s(
|
|
96
|
-
e?.contentWindow
|
|
95
|
+
function s(e) {
|
|
96
|
+
e?.contentWindow && e.contentWindow.postMessage(a({ type: "PARENT_ACKNOWLEDGED_IFRAME_READY" }), o(e));
|
|
97
|
+
}
|
|
98
|
+
function c({ iframe: e, appearance: t = {} }) {
|
|
99
|
+
e?.contentWindow && e.contentWindow.postMessage(a({
|
|
97
100
|
type: "UPDATE_APPEARANCE",
|
|
98
101
|
appearance: t
|
|
99
|
-
}),
|
|
102
|
+
}), o(e));
|
|
100
103
|
}
|
|
101
|
-
function
|
|
102
|
-
e?.contentWindow
|
|
104
|
+
function l({ iframe: e, amount: t }) {
|
|
105
|
+
e?.contentWindow && e.contentWindow.postMessage(a({
|
|
103
106
|
type: "UPDATE_AMOUNT",
|
|
104
107
|
amount: t
|
|
105
|
-
}),
|
|
108
|
+
}), o(e));
|
|
106
109
|
}
|
|
107
|
-
function
|
|
108
|
-
e?.contentWindow
|
|
110
|
+
function u({ iframe: e, merchantName: t }) {
|
|
111
|
+
e?.contentWindow && e.contentWindow.postMessage(a({
|
|
109
112
|
type: "UPDATE_MERCHANT_NAME",
|
|
110
113
|
merchantName: t
|
|
111
|
-
}),
|
|
114
|
+
}), o(e));
|
|
112
115
|
}
|
|
113
|
-
function
|
|
116
|
+
function d({ iframe: e }) {
|
|
114
117
|
let t = crypto.randomUUID();
|
|
115
118
|
return new Promise((n) => {
|
|
116
|
-
e?.contentWindow
|
|
119
|
+
e?.contentWindow && e.contentWindow.postMessage(a({
|
|
117
120
|
type: "VALIDATE_FORM",
|
|
118
121
|
requestId: t
|
|
119
|
-
}),
|
|
122
|
+
}), o(e));
|
|
120
123
|
let r = setTimeout(() => {
|
|
121
124
|
window.removeEventListener("message", i), n(!1);
|
|
122
125
|
}, 5e3);
|
|
@@ -126,66 +129,71 @@ function u({ iframe: e }) {
|
|
|
126
129
|
window.addEventListener("message", i);
|
|
127
130
|
});
|
|
128
131
|
}
|
|
129
|
-
function
|
|
132
|
+
function f({ iframe: e }) {
|
|
133
|
+
e?.contentWindow && e.contentWindow.postMessage(a({ type: "RESET_FORM" }), o(e));
|
|
134
|
+
}
|
|
135
|
+
function p({ iframe: e, token: t }) {
|
|
136
|
+
if (!e?.contentWindow) return;
|
|
130
137
|
let { payment_intent_id: n } = r(t).payload;
|
|
131
|
-
e
|
|
138
|
+
e.contentWindow.postMessage(a({
|
|
132
139
|
type: "CONFIRM_PAYMENT_INTENT",
|
|
133
140
|
token: t,
|
|
134
141
|
id: n ?? void 0
|
|
135
|
-
}),
|
|
142
|
+
}), o(e));
|
|
136
143
|
}
|
|
137
|
-
function
|
|
144
|
+
function m({ iframe: e, token: t }) {
|
|
145
|
+
if (!e?.contentWindow) return;
|
|
138
146
|
let { setup_intent_id: n } = r(t).payload;
|
|
139
|
-
e
|
|
147
|
+
e.contentWindow.postMessage(a({
|
|
140
148
|
type: "CONFIRM_SETUP_INTENT",
|
|
141
149
|
token: t,
|
|
142
150
|
id: n ?? void 0
|
|
143
|
-
}),
|
|
151
|
+
}), o(e));
|
|
144
152
|
}
|
|
145
|
-
function
|
|
146
|
-
e?.contentWindow
|
|
153
|
+
function h({ iframe: e, result: t }) {
|
|
154
|
+
e?.contentWindow && e.contentWindow.postMessage(a({
|
|
147
155
|
type: "CONFIRMATION_RESULT",
|
|
148
156
|
result: t
|
|
149
|
-
}),
|
|
157
|
+
}), o(e));
|
|
150
158
|
}
|
|
151
159
|
//#endregion
|
|
152
160
|
//#region src/apple-pay.ts
|
|
153
|
-
function
|
|
161
|
+
function g(e) {
|
|
154
162
|
return `${i(e)}/iframe/apple-pay?token=${e}`;
|
|
155
163
|
}
|
|
156
|
-
function
|
|
164
|
+
function _() {
|
|
157
165
|
return "40px";
|
|
158
166
|
}
|
|
159
|
-
function
|
|
160
|
-
e.contentWindow?.postMessage(a({ type: "APPLE_PAY_CANCEL" }),
|
|
167
|
+
function v(e) {
|
|
168
|
+
e.contentWindow?.postMessage(a({ type: "APPLE_PAY_CANCEL" }), o(e));
|
|
161
169
|
}
|
|
162
|
-
function
|
|
170
|
+
function y(e, r) {
|
|
163
171
|
let i = { ...r };
|
|
164
172
|
function a() {
|
|
165
|
-
|
|
173
|
+
l({
|
|
166
174
|
iframe: e,
|
|
167
175
|
amount: i.amount
|
|
168
176
|
});
|
|
169
177
|
}
|
|
170
|
-
function
|
|
171
|
-
|
|
178
|
+
function o() {
|
|
179
|
+
u({
|
|
172
180
|
iframe: e,
|
|
173
181
|
merchantName: i.merchantName
|
|
174
182
|
});
|
|
175
183
|
}
|
|
176
|
-
function
|
|
184
|
+
function d(r) {
|
|
177
185
|
if (r.source === e.contentWindow) switch (r.data.type) {
|
|
178
186
|
case "IFRAME_READY":
|
|
179
|
-
|
|
187
|
+
s(e), c({
|
|
180
188
|
iframe: e,
|
|
181
189
|
appearance: i.appearance
|
|
182
|
-
}), a(),
|
|
190
|
+
}), a(), o();
|
|
183
191
|
break;
|
|
184
192
|
case "UPDATE_HEIGHT":
|
|
185
193
|
i.onHeightChange?.(r.data.height);
|
|
186
194
|
break;
|
|
187
195
|
case "UPDATE_APPEARANCE":
|
|
188
|
-
|
|
196
|
+
c({
|
|
189
197
|
iframe: e,
|
|
190
198
|
appearance: r.data.appearance
|
|
191
199
|
});
|
|
@@ -195,7 +203,7 @@ function _(e, r) {
|
|
|
195
203
|
break;
|
|
196
204
|
case "APPLE_PAY_WINDOW_OPEN":
|
|
197
205
|
t({ onCancel: () => {
|
|
198
|
-
|
|
206
|
+
v(e), n();
|
|
199
207
|
} });
|
|
200
208
|
break;
|
|
201
209
|
case "APPLE_PAY_WINDOW_CLOSE":
|
|
@@ -206,7 +214,7 @@ function _(e, r) {
|
|
|
206
214
|
paymentIntentCreateAttributes: r.data.paymentIntentCreateAttributes,
|
|
207
215
|
customerCreateAttributes: r.data.customerCreateAttributes
|
|
208
216
|
}).then((t) => {
|
|
209
|
-
|
|
217
|
+
p({
|
|
210
218
|
iframe: e,
|
|
211
219
|
token: t
|
|
212
220
|
});
|
|
@@ -220,40 +228,40 @@ function _(e, r) {
|
|
|
220
228
|
case "CONFIRMATION_RESULT": n(), i.onResult(r.data.result);
|
|
221
229
|
}
|
|
222
230
|
}
|
|
223
|
-
return window.addEventListener("message",
|
|
231
|
+
return window.addEventListener("message", d), {
|
|
224
232
|
update(t) {
|
|
225
|
-
let n = "appearance" in t, r = "amount" in t,
|
|
233
|
+
let n = "appearance" in t, r = "amount" in t, s = "merchantName" in t;
|
|
226
234
|
i = {
|
|
227
235
|
...i,
|
|
228
236
|
...t
|
|
229
|
-
}, n &&
|
|
237
|
+
}, n && c({
|
|
230
238
|
iframe: e,
|
|
231
239
|
appearance: i.appearance
|
|
232
|
-
}), r && a(),
|
|
240
|
+
}), r && a(), s && o();
|
|
233
241
|
},
|
|
234
242
|
destroy() {
|
|
235
|
-
window.removeEventListener("message",
|
|
243
|
+
window.removeEventListener("message", d), n();
|
|
236
244
|
}
|
|
237
245
|
};
|
|
238
246
|
}
|
|
239
247
|
//#endregion
|
|
240
248
|
//#region src/google-pay.ts
|
|
241
|
-
function
|
|
249
|
+
function b(e) {
|
|
242
250
|
return `${i(e)}/iframe/google-pay?token=${e}`;
|
|
243
251
|
}
|
|
244
|
-
function
|
|
252
|
+
function x() {
|
|
245
253
|
return "40px";
|
|
246
254
|
}
|
|
247
|
-
function
|
|
255
|
+
function S(e, t) {
|
|
248
256
|
let n = { ...t };
|
|
249
257
|
function r() {
|
|
250
|
-
|
|
258
|
+
l({
|
|
251
259
|
iframe: e,
|
|
252
260
|
amount: n.amount
|
|
253
261
|
});
|
|
254
262
|
}
|
|
255
263
|
function i() {
|
|
256
|
-
|
|
264
|
+
u({
|
|
257
265
|
iframe: e,
|
|
258
266
|
merchantName: n.merchantName
|
|
259
267
|
});
|
|
@@ -261,7 +269,7 @@ function b(e, t) {
|
|
|
261
269
|
function a(t) {
|
|
262
270
|
if (t.source === e.contentWindow) switch (t.data.type) {
|
|
263
271
|
case "IFRAME_READY":
|
|
264
|
-
|
|
272
|
+
s(e), c({
|
|
265
273
|
iframe: e,
|
|
266
274
|
appearance: n.appearance
|
|
267
275
|
}), r(), i();
|
|
@@ -270,7 +278,7 @@ function b(e, t) {
|
|
|
270
278
|
n.onHeightChange?.(t.data.height);
|
|
271
279
|
break;
|
|
272
280
|
case "UPDATE_APPEARANCE":
|
|
273
|
-
|
|
281
|
+
c({
|
|
274
282
|
iframe: e,
|
|
275
283
|
appearance: t.data.appearance
|
|
276
284
|
});
|
|
@@ -283,7 +291,7 @@ function b(e, t) {
|
|
|
283
291
|
paymentIntentCreateAttributes: t.data.paymentIntentCreateAttributes,
|
|
284
292
|
customerCreateAttributes: t.data.customerCreateAttributes
|
|
285
293
|
}).then((t) => {
|
|
286
|
-
|
|
294
|
+
p({
|
|
287
295
|
iframe: e,
|
|
288
296
|
token: t
|
|
289
297
|
});
|
|
@@ -299,21 +307,21 @@ function b(e, t) {
|
|
|
299
307
|
}
|
|
300
308
|
return window.addEventListener("message", a), {
|
|
301
309
|
update(t) {
|
|
302
|
-
let a = "appearance" in t, o = "amount" in t,
|
|
310
|
+
let a = "appearance" in t, o = "amount" in t, s = "merchantName" in t;
|
|
303
311
|
n = {
|
|
304
312
|
...n,
|
|
305
313
|
...t
|
|
306
|
-
}, a &&
|
|
314
|
+
}, a && c({
|
|
307
315
|
iframe: e,
|
|
308
316
|
appearance: n.appearance
|
|
309
|
-
}), o && r(),
|
|
317
|
+
}), o && r(), s && i();
|
|
310
318
|
},
|
|
311
319
|
destroy() {
|
|
312
320
|
window.removeEventListener("message", a);
|
|
313
321
|
}
|
|
314
322
|
};
|
|
315
323
|
}
|
|
316
|
-
function
|
|
324
|
+
function C({ paymentData: e }) {
|
|
317
325
|
return { paymentMethod: {
|
|
318
326
|
type: "googlepay",
|
|
319
327
|
billing_address_attributes: {
|
|
@@ -332,14 +340,14 @@ function x({ paymentData: e }) {
|
|
|
332
340
|
}
|
|
333
341
|
//#endregion
|
|
334
342
|
//#region src/payment-method-form.ts
|
|
335
|
-
var
|
|
343
|
+
var w = {
|
|
336
344
|
country: 212,
|
|
337
345
|
full: 452
|
|
338
|
-
},
|
|
346
|
+
}, T = 80, E = {
|
|
339
347
|
country: 400,
|
|
340
348
|
full: 640
|
|
341
349
|
};
|
|
342
|
-
function
|
|
350
|
+
function D(e, t = { cardholderName: !1 }, n = "country") {
|
|
343
351
|
let r = Object.entries(t).filter(([, e]) => e).map(([e]) => e).join(","), a = new URLSearchParams({
|
|
344
352
|
token: e,
|
|
345
353
|
additionalFields: r,
|
|
@@ -347,25 +355,25 @@ function T(e, t = { cardholderName: !1 }, n = "country") {
|
|
|
347
355
|
});
|
|
348
356
|
return `${i(e)}/iframe/card?${a}`;
|
|
349
357
|
}
|
|
350
|
-
function
|
|
358
|
+
function O(e, t = "country") {
|
|
351
359
|
let n = new URLSearchParams({
|
|
352
360
|
token: e,
|
|
353
361
|
billingAddressRequirement: t
|
|
354
362
|
});
|
|
355
363
|
return `${i(e)}/iframe/bank?${n}`;
|
|
356
364
|
}
|
|
357
|
-
function
|
|
358
|
-
return `${(
|
|
365
|
+
function k(e = { cardholderName: !1 }, t = "country") {
|
|
366
|
+
return `${(w[t] ?? w.country) + (e.cardholderName ? T : 0)}px`;
|
|
359
367
|
}
|
|
360
|
-
function
|
|
361
|
-
return `${
|
|
368
|
+
function A(e = "country") {
|
|
369
|
+
return `${E[e] ?? E.country}px`;
|
|
362
370
|
}
|
|
363
|
-
function
|
|
371
|
+
function j(e, t) {
|
|
364
372
|
let n = { ...t };
|
|
365
373
|
function r(t) {
|
|
366
374
|
if (t.source === e.contentWindow) switch (t.data.type) {
|
|
367
375
|
case "IFRAME_READY":
|
|
368
|
-
|
|
376
|
+
s(e), c({
|
|
369
377
|
iframe: e,
|
|
370
378
|
appearance: n.appearance
|
|
371
379
|
});
|
|
@@ -374,7 +382,7 @@ function k(e, t) {
|
|
|
374
382
|
n.onHeightChange?.(t.data.height);
|
|
375
383
|
break;
|
|
376
384
|
case "UPDATE_APPEARANCE":
|
|
377
|
-
|
|
385
|
+
c({
|
|
378
386
|
iframe: e,
|
|
379
387
|
appearance: t.data.appearance
|
|
380
388
|
});
|
|
@@ -391,7 +399,7 @@ function k(e, t) {
|
|
|
391
399
|
n = {
|
|
392
400
|
...n,
|
|
393
401
|
...t
|
|
394
|
-
}, r &&
|
|
402
|
+
}, r && c({
|
|
395
403
|
iframe: e,
|
|
396
404
|
appearance: n.appearance
|
|
397
405
|
});
|
|
@@ -403,7 +411,7 @@ function k(e, t) {
|
|
|
403
411
|
}
|
|
404
412
|
//#endregion
|
|
405
413
|
//#region src/mount.ts
|
|
406
|
-
function
|
|
414
|
+
function M(e) {
|
|
407
415
|
if (typeof e == "string") {
|
|
408
416
|
let t = document.querySelector(e);
|
|
409
417
|
if (!(t instanceof HTMLElement)) throw Error(`[amos-js] Container "${e}" did not match any HTMLElement.`);
|
|
@@ -411,26 +419,26 @@ function A(e) {
|
|
|
411
419
|
}
|
|
412
420
|
return e;
|
|
413
421
|
}
|
|
414
|
-
var
|
|
422
|
+
var N = {
|
|
415
423
|
width: "calc(100% + 8px)",
|
|
416
424
|
transition: "opacity 150ms ease-in, height 200ms ease-in-out",
|
|
417
425
|
margin: "0 -4px",
|
|
418
426
|
opacity: "0",
|
|
419
427
|
border: "0"
|
|
420
428
|
};
|
|
421
|
-
function
|
|
429
|
+
function P({ src: e, title: t, name: n, height: r, allow: i }) {
|
|
422
430
|
let a = document.createElement("iframe");
|
|
423
|
-
return a.src = e, a.title = t, a.name = n, a.setAttribute("role", "presentation"), a.scrolling = "no", i && (a.allow = i), Object.assign(a.style,
|
|
431
|
+
return a.src = e, a.title = t, a.name = n, a.setAttribute("role", "presentation"), a.scrolling = "no", i && (a.allow = i), Object.assign(a.style, N, { height: r }), a;
|
|
424
432
|
}
|
|
425
|
-
function
|
|
426
|
-
let n =
|
|
427
|
-
src:
|
|
433
|
+
function F(e, t) {
|
|
434
|
+
let n = M(e), { renderToken: r, additionalFields: i = { cardholderName: !1 }, billingAddressRequirement: a = "country", ...o } = t, s = P({
|
|
435
|
+
src: D(r, i, a),
|
|
428
436
|
title: "Secure credit card payment method form powered by Amos",
|
|
429
437
|
name: "amos-credit-card-payment-method-form",
|
|
430
|
-
height:
|
|
438
|
+
height: k(i, a)
|
|
431
439
|
});
|
|
432
440
|
n.appendChild(s);
|
|
433
|
-
let c =
|
|
441
|
+
let c = j(s, {
|
|
434
442
|
...o,
|
|
435
443
|
onHeightChange: (e) => {
|
|
436
444
|
s.style.height = e, o.onHeightChange?.(e);
|
|
@@ -447,15 +455,15 @@ function N(e, t) {
|
|
|
447
455
|
}
|
|
448
456
|
};
|
|
449
457
|
}
|
|
450
|
-
function
|
|
451
|
-
let n =
|
|
452
|
-
src:
|
|
458
|
+
function I(e, t) {
|
|
459
|
+
let n = M(e), { renderToken: r, billingAddressRequirement: i = "country", ...a } = t, o = P({
|
|
460
|
+
src: O(r, i),
|
|
453
461
|
title: "Secure bank account payment method form powered by Amos",
|
|
454
462
|
name: "amos-bank-account-payment-method-form",
|
|
455
|
-
height:
|
|
463
|
+
height: A(i)
|
|
456
464
|
});
|
|
457
465
|
n.appendChild(o);
|
|
458
|
-
let s =
|
|
466
|
+
let s = j(o, {
|
|
459
467
|
...a,
|
|
460
468
|
onHeightChange: (e) => {
|
|
461
469
|
o.style.height = e, a.onHeightChange?.(e);
|
|
@@ -472,16 +480,16 @@ function P(e, t) {
|
|
|
472
480
|
}
|
|
473
481
|
};
|
|
474
482
|
}
|
|
475
|
-
function
|
|
476
|
-
let n =
|
|
477
|
-
src:
|
|
483
|
+
function L(e, t) {
|
|
484
|
+
let n = M(e), { renderToken: r, ...i } = t, a = P({
|
|
485
|
+
src: b(r),
|
|
478
486
|
title: "Secure Google Pay button powered by Amos",
|
|
479
487
|
name: "amos-google-pay-button",
|
|
480
|
-
height:
|
|
488
|
+
height: x(),
|
|
481
489
|
allow: "payment"
|
|
482
490
|
});
|
|
483
491
|
n.appendChild(a);
|
|
484
|
-
let o =
|
|
492
|
+
let o = S(a, {
|
|
485
493
|
...i,
|
|
486
494
|
onHeightChange: (e) => {
|
|
487
495
|
a.style.height = e, i.onHeightChange?.(e);
|
|
@@ -498,16 +506,16 @@ function F(e, t) {
|
|
|
498
506
|
}
|
|
499
507
|
};
|
|
500
508
|
}
|
|
501
|
-
function
|
|
502
|
-
let n =
|
|
503
|
-
src:
|
|
509
|
+
function R(e, t) {
|
|
510
|
+
let n = M(e), { renderToken: r, ...i } = t, a = P({
|
|
511
|
+
src: g(r),
|
|
504
512
|
title: "Secure Apple Pay button powered by Amos",
|
|
505
513
|
name: "amos-apple-pay-button",
|
|
506
|
-
height:
|
|
514
|
+
height: _(),
|
|
507
515
|
allow: "payment"
|
|
508
516
|
});
|
|
509
517
|
n.appendChild(a);
|
|
510
|
-
let o =
|
|
518
|
+
let o = y(a, {
|
|
511
519
|
...i,
|
|
512
520
|
onHeightChange: (e) => {
|
|
513
521
|
a.style.height = e, i.onHeightChange?.(e);
|
|
@@ -525,4 +533,4 @@ function I(e, t) {
|
|
|
525
533
|
};
|
|
526
534
|
}
|
|
527
535
|
//#endregion
|
|
528
|
-
export {
|
|
536
|
+
export { y as attachApplePayButtonListeners, S as attachGooglePayButtonListeners, j as attachPaymentMethodFormListeners, p as confirmPaymentIntent, m as confirmSetupIntent, a as createMessage, r as decodeJwt, C as formatGooglePayPaymentData, _ as getApplePayButtonInitialHeight, g as getApplePayButtonSrc, A as getBankAccountFormInitialHeight, O as getBankAccountFormSrc, k as getCreditCardFormInitialHeight, D as getCreditCardFormSrc, i as getEmbedOrigin, x as getGooglePayButtonInitialHeight, b as getGooglePayButtonSrc, R as mountAmosApplePayButton, I as mountAmosBankAccountPaymentMethodForm, F as mountAmosCreditCardPaymentMethodForm, L as mountAmosGooglePayButton, f as resetForm, h as sendConfirmationResult, s as sendParentReadyMessage, l as updateAmount, c as updateAppearance, u as updateMerchantName, d as validateForm };
|
package/dist/messaging.d.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { components } from '@amos.com/node';
|
|
2
2
|
import { Appearance, Message } from './types';
|
|
3
3
|
type Iframe = HTMLIFrameElement | null | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* Resolve the Amos embed origin from an iframe's configured `src`.
|
|
6
|
+
*
|
|
7
|
+
* Integrators are expected to set `src` via the SDK's `get*Src` helpers
|
|
8
|
+
* before calling messaging functions.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getIframeTargetOrigin(iframe: HTMLIFrameElement): string;
|
|
4
11
|
/**
|
|
5
12
|
* Notify the embedded iframe that the host page is ready to receive
|
|
6
13
|
* messages. The SDK calls this internally after receiving an
|
|
@@ -44,6 +51,13 @@ export declare function updateMerchantName({ iframe, merchantName, }: {
|
|
|
44
51
|
export declare function validateForm({ iframe }: {
|
|
45
52
|
iframe: Iframe;
|
|
46
53
|
}): Promise<boolean>;
|
|
54
|
+
/**
|
|
55
|
+
* Clear all field values and API errors in the embedded credit-card or
|
|
56
|
+
* bank-account iframe form.
|
|
57
|
+
*/
|
|
58
|
+
export declare function resetForm({ iframe }: {
|
|
59
|
+
iframe: Iframe;
|
|
60
|
+
}): void;
|
|
47
61
|
/**
|
|
48
62
|
* Confirm a payment intent in the embedded iframe flow.
|
|
49
63
|
*
|
package/dist/types.d.ts
CHANGED
|
@@ -135,6 +135,9 @@ export type Message = {
|
|
|
135
135
|
} | {
|
|
136
136
|
/** Parent → embed: user cancelled from the host-page waiting overlay. */
|
|
137
137
|
type: "APPLE_PAY_CANCEL";
|
|
138
|
+
} | {
|
|
139
|
+
/** Parent → embed: clear all form field values and API errors. */
|
|
140
|
+
type: "RESET_FORM";
|
|
138
141
|
};
|
|
139
142
|
/**
|
|
140
143
|
* Identity helper that brands an object as a typed `Message`.
|
package/package.json
CHANGED
package/src/apple-pay.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
import { getEmbedOrigin } from "./jwt";
|
|
7
7
|
import {
|
|
8
8
|
confirmPaymentIntent,
|
|
9
|
+
getIframeTargetOrigin,
|
|
9
10
|
sendParentReadyMessage,
|
|
10
11
|
updateAmount as sendUpdateAmount,
|
|
11
12
|
updateAppearance as sendUpdateAppearance,
|
|
@@ -94,7 +95,7 @@ export type ApplePayButtonController = {
|
|
|
94
95
|
function sendApplePayCancel(iframe: HTMLIFrameElement): void {
|
|
95
96
|
iframe.contentWindow?.postMessage(
|
|
96
97
|
createMessage({ type: "APPLE_PAY_CANCEL" }),
|
|
97
|
-
|
|
98
|
+
getIframeTargetOrigin(iframe),
|
|
98
99
|
);
|
|
99
100
|
}
|
|
100
101
|
|
package/src/index.ts
CHANGED
package/src/messaging.ts
CHANGED
|
@@ -4,15 +4,29 @@ import { type Appearance, createMessage, type Message } from "./types";
|
|
|
4
4
|
|
|
5
5
|
type Iframe = HTMLIFrameElement | null | undefined;
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Resolve the Amos embed origin from an iframe's configured `src`.
|
|
9
|
+
*
|
|
10
|
+
* Integrators are expected to set `src` via the SDK's `get*Src` helpers
|
|
11
|
+
* before calling messaging functions.
|
|
12
|
+
*/
|
|
13
|
+
export function getIframeTargetOrigin(iframe: HTMLIFrameElement): string {
|
|
14
|
+
return new URL(iframe.src).origin;
|
|
15
|
+
}
|
|
16
|
+
|
|
7
17
|
/**
|
|
8
18
|
* Notify the embedded iframe that the host page is ready to receive
|
|
9
19
|
* messages. The SDK calls this internally after receiving an
|
|
10
20
|
* `IFRAME_READY` event from the iframe.
|
|
11
21
|
*/
|
|
12
22
|
export function sendParentReadyMessage(iframe: Iframe): void {
|
|
13
|
-
iframe?.contentWindow
|
|
23
|
+
if (!iframe?.contentWindow) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
iframe.contentWindow.postMessage(
|
|
14
28
|
createMessage({ type: "PARENT_ACKNOWLEDGED_IFRAME_READY" }),
|
|
15
|
-
|
|
29
|
+
getIframeTargetOrigin(iframe),
|
|
16
30
|
);
|
|
17
31
|
}
|
|
18
32
|
|
|
@@ -31,9 +45,13 @@ export function updateAppearance({
|
|
|
31
45
|
iframe: Iframe;
|
|
32
46
|
appearance?: Appearance;
|
|
33
47
|
}): void {
|
|
34
|
-
iframe?.contentWindow
|
|
48
|
+
if (!iframe?.contentWindow) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
iframe.contentWindow.postMessage(
|
|
35
53
|
createMessage({ type: "UPDATE_APPEARANCE", appearance }),
|
|
36
|
-
|
|
54
|
+
getIframeTargetOrigin(iframe),
|
|
37
55
|
);
|
|
38
56
|
}
|
|
39
57
|
|
|
@@ -47,9 +65,13 @@ export function updateAmount({
|
|
|
47
65
|
iframe: Iframe;
|
|
48
66
|
amount: string;
|
|
49
67
|
}): void {
|
|
50
|
-
iframe?.contentWindow
|
|
68
|
+
if (!iframe?.contentWindow) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
iframe.contentWindow.postMessage(
|
|
51
73
|
createMessage({ type: "UPDATE_AMOUNT", amount }),
|
|
52
|
-
|
|
74
|
+
getIframeTargetOrigin(iframe),
|
|
53
75
|
);
|
|
54
76
|
}
|
|
55
77
|
|
|
@@ -64,9 +86,13 @@ export function updateMerchantName({
|
|
|
64
86
|
iframe: Iframe;
|
|
65
87
|
merchantName: string;
|
|
66
88
|
}): void {
|
|
67
|
-
iframe?.contentWindow
|
|
89
|
+
if (!iframe?.contentWindow) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
iframe.contentWindow.postMessage(
|
|
68
94
|
createMessage({ type: "UPDATE_MERCHANT_NAME", merchantName }),
|
|
69
|
-
|
|
95
|
+
getIframeTargetOrigin(iframe),
|
|
70
96
|
);
|
|
71
97
|
}
|
|
72
98
|
|
|
@@ -81,10 +107,12 @@ export function validateForm({ iframe }: { iframe: Iframe }): Promise<boolean> {
|
|
|
81
107
|
const requestId = crypto.randomUUID();
|
|
82
108
|
|
|
83
109
|
return new Promise((resolve) => {
|
|
84
|
-
iframe?.contentWindow
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
110
|
+
if (iframe?.contentWindow) {
|
|
111
|
+
iframe.contentWindow.postMessage(
|
|
112
|
+
createMessage({ type: "VALIDATE_FORM", requestId }),
|
|
113
|
+
getIframeTargetOrigin(iframe),
|
|
114
|
+
);
|
|
115
|
+
}
|
|
88
116
|
|
|
89
117
|
const timeoutId = setTimeout(() => {
|
|
90
118
|
window.removeEventListener("message", handleMessage);
|
|
@@ -106,6 +134,21 @@ export function validateForm({ iframe }: { iframe: Iframe }): Promise<boolean> {
|
|
|
106
134
|
});
|
|
107
135
|
}
|
|
108
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Clear all field values and API errors in the embedded credit-card or
|
|
139
|
+
* bank-account iframe form.
|
|
140
|
+
*/
|
|
141
|
+
export function resetForm({ iframe }: { iframe: Iframe }): void {
|
|
142
|
+
if (!iframe?.contentWindow) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
iframe.contentWindow.postMessage(
|
|
147
|
+
createMessage({ type: "RESET_FORM" }),
|
|
148
|
+
getIframeTargetOrigin(iframe),
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
109
152
|
/**
|
|
110
153
|
* Confirm a payment intent in the embedded iframe flow.
|
|
111
154
|
*
|
|
@@ -119,15 +162,19 @@ export function confirmPaymentIntent({
|
|
|
119
162
|
}: {
|
|
120
163
|
iframe: Iframe;
|
|
121
164
|
} & Pick<components["schemas"]["EmbedToken"], "token">): void {
|
|
165
|
+
if (!iframe?.contentWindow) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
122
169
|
const { payment_intent_id: id }: components["schemas"]["EmbedTokenJwt"] =
|
|
123
170
|
decodeJwt(token).payload;
|
|
124
|
-
iframe
|
|
171
|
+
iframe.contentWindow.postMessage(
|
|
125
172
|
createMessage({
|
|
126
173
|
type: "CONFIRM_PAYMENT_INTENT",
|
|
127
174
|
token,
|
|
128
175
|
id: id ?? undefined,
|
|
129
176
|
}),
|
|
130
|
-
|
|
177
|
+
getIframeTargetOrigin(iframe),
|
|
131
178
|
);
|
|
132
179
|
}
|
|
133
180
|
|
|
@@ -144,15 +191,19 @@ export function confirmSetupIntent({
|
|
|
144
191
|
}: {
|
|
145
192
|
iframe: Iframe;
|
|
146
193
|
} & Pick<components["schemas"]["EmbedToken"], "token">): void {
|
|
194
|
+
if (!iframe?.contentWindow) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
147
198
|
const { setup_intent_id: id }: components["schemas"]["EmbedTokenJwt"] =
|
|
148
199
|
decodeJwt(token).payload;
|
|
149
|
-
iframe
|
|
200
|
+
iframe.contentWindow.postMessage(
|
|
150
201
|
createMessage({
|
|
151
202
|
type: "CONFIRM_SETUP_INTENT",
|
|
152
203
|
token,
|
|
153
204
|
id: id ?? undefined,
|
|
154
205
|
}),
|
|
155
|
-
|
|
206
|
+
getIframeTargetOrigin(iframe),
|
|
156
207
|
);
|
|
157
208
|
}
|
|
158
209
|
|
|
@@ -167,11 +218,15 @@ export function sendConfirmationResult({
|
|
|
167
218
|
iframe: Iframe;
|
|
168
219
|
result: Extract<Message, { type: "CONFIRMATION_RESULT" }>["result"];
|
|
169
220
|
}): void {
|
|
170
|
-
iframe?.contentWindow
|
|
221
|
+
if (!iframe?.contentWindow) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
iframe.contentWindow.postMessage(
|
|
171
226
|
createMessage({
|
|
172
227
|
type: "CONFIRMATION_RESULT",
|
|
173
228
|
result,
|
|
174
229
|
}),
|
|
175
|
-
|
|
230
|
+
getIframeTargetOrigin(iframe),
|
|
176
231
|
);
|
|
177
232
|
}
|
package/src/mount.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -265,6 +265,10 @@ export type Message =
|
|
|
265
265
|
| {
|
|
266
266
|
/** Parent → embed: user cancelled from the host-page waiting overlay. */
|
|
267
267
|
type: "APPLE_PAY_CANCEL";
|
|
268
|
+
}
|
|
269
|
+
| {
|
|
270
|
+
/** Parent → embed: clear all form field values and API errors. */
|
|
271
|
+
type: "RESET_FORM";
|
|
268
272
|
};
|
|
269
273
|
|
|
270
274
|
/**
|