@commercetools/checkout-browser-sdk
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -1,8 +0,1 @@ | ||
!function(){var e;let o,t={};class n extends Error{constructor(e){super(`Missing field in the configuration: ${e}`),Object.setPrototypeOf(this,n.prototype)}}let c=()=>{if(!t.host)throw new n("host");return t.host},r=()=>i()?c():window.location.origin,i=()=>!!t.iframe,l=()=>t,a=(e,o=!1)=>(o&&(t={}),// if ('callbackUrl' in newConfig) announceDeprecatedField('callbackUrl', 'returnUrl') | ||
t={//not documented | ||
iframe:"iframe"in e?!!e.iframe:t.iframe,// deprecated | ||
applicationId:"applicationId"in e?e.applicationId:t.applicationId,callbackUrl:"callbackUrl"in e?e.callbackUrl:t.callbackUrl,sellerId:"sellerId"in e?e.sellerId:t.sellerId,// documented | ||
accessToken:"accessToken"in e?e.accessToken:t.accessToken,applicationKey:"applicationKey"in e?e.applicationKey:t.applicationKey,returnUrl:"callbackUrl"in e?e.returnUrl:t.returnUrl,cartId:"cartId"in e?e.cartId:t.cartId,locale:"locale"in e?e.locale:t.locale,projectKey:"projectKey"in e?e.projectKey:t.projectKey,storeKey:"storeKey"in e?e.storeKey:t.storeKey,host:"host"in e?e.host:t.host,skipShipping:"skipShipping"in e?e.skipShipping:t.skipShipping,styles:"styles"in e?e.styles:t.styles,languageOverrides:"languageOverrides"in e?e.languageOverrides:t.languageOverrides,forms:"forms"in e?e.forms:t.forms,currencyLocale:"currencyLocale"in e?e.currencyLocale:t.currencyLocale,showTaxes:"showTaxes"in e?e.showTaxes:t.showTaxes}),s="commercetools-checkout",d=document.body.style.overflow,u=()=>{let e=document.querySelectorAll("[data-ctc]");return e.length>0&&(e[0]instanceof HTMLDivElement||e[0]instanceof HTMLSpanElement)?e[0]:null};(e||(e={})).InitCheckout="init_checkout";let p=()=>{let e=document.getElementById(s);e&&(document.body.style.overflow=d,e.remove());let t=u();t&&(t.innerHTML=""),o&&window.removeEventListener("message",o)},y=e=>{o=({origin:o,data:t})=>{if(o===r()){if((null==t?void 0:t.code)==="checkout_cancelled"&&p(),(null==t?void 0:t.code)==="checkout_loaded"){let o=Object.assign({},l());delete o.callbackUrl;let t=e?e.contentWindow:window;t.postMessage({code:"init_checkout",payload:o},r())}if((null==t?void 0:t.code)==="checkout_completed"){var n;p();let{id:e}=(null==t?void 0:null===(n=t.payload)||void 0===n?void 0:n.order)||{},{callbackUrl:o}=l();o&&(window.location.href=`${o}?orderId=${e}`)}}},window.addEventListener("message",o)},f=()=>{// create the iframe and append to the document body | ||
let e=document.createElement("iframe");return y(e),e.src=c(),e.title="Checkout",e.setAttribute("id",s),e.setAttribute("style","display: none; top: 0; position: fixed; height: 100%; width: 100%; z-index: 100000;"),e.onload=()=>{// scroll to the iframe | ||
e.scrollIntoView({behavior:"smooth",block:"center"})},document.body.appendChild(e),document.body.style.overflow="hidden",e},h=async()=>{if(p(),y(),!u()){let e=document.createElement("div");e.setAttribute("id",s),document.body.appendChild(e),document.body.style.overflow="hidden"}if("object"==typeof window.commercetoolsCheckout&&"function"==typeof window.commercetoolsCheckout.render)window.commercetoolsCheckout.render();else{let e=document.createElement("script");e.src=`${c()}/main.js`,document.head.appendChild(e)}},m=e=>o=>{if(!e||!o)return!1;if("*"===e||o===e)return!0;let t=e.split(":"),n=o.split(":");return!!n[0]&&t[0]===n[0]&&m(t.slice(1).join(":"))(n.slice(1).join(":"))},w={},g=(e,o)=>{let t=`${e}_${Math.round(1e15*Math.random())}`,n=({origin:t,data:n})=>{if(t!==r()||"commercetoolsCheckout"!==n.source)return;let c=n||{};m(e)(c.type)&&o(c)};return window.addEventListener("message",n),w[t]=n,t},k=e=>window.removeEventListener("message",w[e]),b={},v=e=>{[["info","onInfo"],["warn","onWarn"],["error","onError"]].forEach(o=>{let t=o[0],n=o[1];"function"==typeof e[n]&&(b[n]&&k(b[n]),b[n]=g(`${t}:*`,e[n]))})},E=e=>{[["info","logInfo"],["warn","logWarn"],["error","logError"]].forEach(o=>{let t=o[0],n=o[1];!0===e[n]&&(b[n]&&k(b[n]),b[n]=g(`${t}:*`,function(e){console[t]("[ctc]",e.type,e.code,e.payload||{})}))})},I={checkout:e=>{if(a(e),i()){let e=document.getElementById(s)||f();e.style.display="block"}else h()},init:e=>{if("object"!=typeof e)throw Error("Bad config, please review docs");"object"==typeof e.checkoutConfig&&a(e.checkoutConfig),v(e),E(e)},subscribe:g,unsubscribe:k};function j(e){// eslint-disable-next-line prefer-rest-params | ||
return I[e]&&I[e].apply(null,Array.prototype.slice.call(arguments,1))}window.ctc&&Array.isArray(window.ctc.queue)&&window.ctc.queue.forEach(e=>j.apply(null,e)),window.ctc&&Array.isArray(window.ctc.q)&&window.ctc.q.forEach(e=>j.apply(null,e)),window.ctc=j}(); | ||
!function(){var e;let o,t={};class n extends Error{constructor(e){super(`Missing field in the configuration: ${e}`),Object.setPrototypeOf(this,n.prototype)}}let r=()=>{if(!t.host)throw new n("host");return t.host},c=()=>i()?r():window.location.origin,i=()=>!!t.iframe,l=()=>t,a=(e,o=!1)=>(o&&(t={}),t={iframe:"iframe"in e?!!e.iframe:t.iframe,applicationId:"applicationId"in e?e.applicationId:t.applicationId,callbackUrl:"callbackUrl"in e?e.callbackUrl:t.callbackUrl,sellerId:"sellerId"in e?e.sellerId:t.sellerId,accessToken:"accessToken"in e?e.accessToken:t.accessToken,applicationKey:"applicationKey"in e?e.applicationKey:t.applicationKey,returnUrl:"returnUrl"in e?e.returnUrl:t.returnUrl,cartId:"cartId"in e?e.cartId:t.cartId,locale:"locale"in e?e.locale:t.locale,projectKey:"projectKey"in e?e.projectKey:t.projectKey,storeKey:"storeKey"in e?e.storeKey:t.storeKey,host:"host"in e?e.host:t.host,skipShipping:"skipShipping"in e?e.skipShipping:t.skipShipping,styles:"styles"in e?e.styles:t.styles,languageOverrides:"languageOverrides"in e?e.languageOverrides:t.languageOverrides,forms:"forms"in e?e.forms:t.forms,currencyLocale:"currencyLocale"in e?e.currencyLocale:t.currencyLocale,showTaxes:"showTaxes"in e?e.showTaxes:t.showTaxes}),s="commercetools-checkout",d=document.body.style.overflow,u=()=>{let e=document.querySelectorAll("[data-ctc]");return e.length>0&&(e[0]instanceof HTMLDivElement||e[0]instanceof HTMLSpanElement)?e[0]:null};(e||(e={})).InitCheckout="init_checkout";let p=()=>{let e=document.getElementById(s);e&&(document.body.style.overflow=d,e.remove());let t=u();t&&(t.innerHTML=""),o&&window.removeEventListener("message",o)},y=e=>{o=({origin:o,data:t})=>{if(o===c()){if((null==t?void 0:t.code)==="checkout_cancelled"&&p(),(null==t?void 0:t.code)==="checkout_loaded"){let o=Object.assign({},l());delete o.callbackUrl,delete o.returnUrl,(e?e.contentWindow:window).postMessage({code:"init_checkout",payload:o},c())}if((null==t?void 0:t.code)==="checkout_completed"){var n;p();let{id:e}=(null==t?void 0:null===(n=t.payload)||void 0===n?void 0:n.order)||{},{callbackUrl:o,returnUrl:r}=l();r?window.location.href=`${r}?orderId=${e}`:o&&(window.location.href=`${o}?orderId=${e}`)}}},window.addEventListener("message",o)},h=()=>{let e=document.createElement("iframe");return y(e),e.src=r(),e.title="Checkout",e.setAttribute("id",s),e.setAttribute("style","display: none; top: 0; position: fixed; height: 100%; width: 100%; z-index: 100000;"),e.onload=()=>{e.scrollIntoView({behavior:"smooth",block:"center"})},document.body.appendChild(e),document.body.style.overflow="hidden",e},f=async()=>{if(p(),y(),!u()){let e=document.createElement("div");e.setAttribute("id",s),document.body.appendChild(e),document.body.style.overflow="hidden"}if("object"==typeof window.commercetoolsCheckout&&"function"==typeof window.commercetoolsCheckout.render)window.commercetoolsCheckout.render();else{let e=document.createElement("script");e.src=`${r()}/main.js`,document.head.appendChild(e)}},w=e=>o=>{if(!e||!o)return!1;if("*"===e||o===e)return!0;let t=e.split(":"),n=o.split(":");return!!n[0]&&t[0]===n[0]&&w(t.slice(1).join(":"))(n.slice(1).join(":"))},m={},g=(e,o)=>{let t=`${e}_${Math.round(1e15*Math.random())}`,n=({origin:t,data:n})=>{if(t!==c()||"commercetoolsCheckout"!==n.source)return;let r=n||{};w(e)(r.type)&&o(r)};return window.addEventListener("message",n),m[t]=n,t},k=e=>window.removeEventListener("message",m[e]),b={},v=e=>{[["info","onInfo"],["warn","onWarn"],["error","onError"]].forEach(o=>{let t=o[0],n=o[1];"function"==typeof e[n]&&(b[n]&&k(b[n]),b[n]=g(`${t}:*`,e[n]))})},I=e=>{[["info","logInfo"],["warn","logWarn"],["error","logError"]].forEach(o=>{let t=o[0],n=o[1];!0===e[n]&&(b[n]&&k(b[n]),b[n]=g(`${t}:*`,function(e){console[t]("[ctc]",e.type,e.code,e.payload||{})}))})},E={checkout:e=>{a(e),i()?(document.getElementById(s)||h()).style.display="block":f()},init:e=>{if("object"!=typeof e)throw Error("Bad config, please review docs");"object"==typeof e.checkoutConfig&&a(e.checkoutConfig),v(e),I(e)},subscribe:g,unsubscribe:k};function j(e){return E[e]&&E[e].apply(null,Array.prototype.slice.call(arguments,1))}window.ctc&&Array.isArray(window.ctc.queue)&&window.ctc.queue.forEach(e=>j.apply(null,e)),window.ctc&&Array.isArray(window.ctc.q)&&window.ctc.q.forEach(e=>j.apply(null,e)),window.ctc=j}(); |
# @commercetools/checkout-browser-sdk | ||
## 0.5.0 | ||
### Minor Changes | ||
- 3a60dc3: New feature that provides a custom layout for address summary | ||
- ec362cd: bump | ||
- 5d50238: Added support for custom fields in address forms | ||
## 0.4.0 | ||
@@ -4,0 +12,0 @@ |
@@ -46,2 +46,3 @@ "use strict"; | ||
delete checkoutConfig.callbackUrl; | ||
delete checkoutConfig.returnUrl; | ||
const dest = iframe ? iframe.contentWindow : window; | ||
@@ -53,4 +54,7 @@ dest.postMessage({ code: PostMessageCodes.InitCheckout, payload: checkoutConfig }, (0, config_1.getInterlocutor)()); | ||
const { id } = ((_a = data === null || data === void 0 ? void 0 : data.payload) === null || _a === void 0 ? void 0 : _a.order) || {}; | ||
const { callbackUrl } = (0, config_1.getCheckoutConfig)(); | ||
if (callbackUrl) { | ||
const { callbackUrl, returnUrl } = (0, config_1.getCheckoutConfig)(); | ||
if (returnUrl) { | ||
window.location.href = `${returnUrl}?orderId=${id}`; | ||
} | ||
else if (callbackUrl) { | ||
window.location.href = `${callbackUrl}?orderId=${id}`; | ||
@@ -57,0 +61,0 @@ } |
@@ -38,2 +38,10 @@ type Definitions = Record<string, string> | { | ||
address?: { | ||
layout?: Array<Array<string>>; | ||
summaryLayout?: Array<Array<string | [string, string?]>>; | ||
custom?: { | ||
type: { | ||
key: string; | ||
}; | ||
fields?: Record<string, SellerFieldConfig>; | ||
}; | ||
disableDefaultValidations?: boolean; | ||
@@ -54,2 +62,33 @@ fields: { | ||
}; | ||
type SellerFieldConfig = { | ||
label?: string; | ||
customType?: 'Boolean' | 'String' | 'Enum' | 'Number' | 'Date' | 'Time' | 'DateTime'; | ||
componentType?: FieldComponentType; | ||
initialValue?: string | number | boolean; | ||
valueOptions?: FieldValueOption[]; | ||
validation?: { | ||
required?: { | ||
value: boolean; | ||
message: string; | ||
}; | ||
minLength?: { | ||
value: number; | ||
message: string; | ||
}; | ||
maxLength?: { | ||
value: number; | ||
message: string; | ||
}; | ||
pattern?: { | ||
value: Record<string, unknown>; | ||
message: string; | ||
}; | ||
}; | ||
}; | ||
declare const fieldComponentTypes: string[]; | ||
type FieldComponentType = (typeof fieldComponentTypes)[number]; | ||
type FieldValueOption = { | ||
label: string; | ||
value: string | number | boolean; | ||
}; | ||
export type CheckoutData = { | ||
@@ -60,3 +99,3 @@ accessToken?: string; | ||
applicationKey?: string; | ||
/** @deprecated use callbackUrl instead */ | ||
/** @deprecated use returnUrl instead */ | ||
callbackUrl?: string; | ||
@@ -67,3 +106,3 @@ returnUrl?: string; | ||
locale?: string; | ||
/** @deprecated use sellerId instead */ | ||
/** @deprecated use projectKey instead */ | ||
sellerId?: string; | ||
@@ -70,0 +109,0 @@ projectKey?: string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.updateCheckoutConfig = exports.getCheckoutConfig = exports.getIsIframe = exports.getInterlocutor = exports.getAppHost = exports.MissingConfigurationField = void 0; | ||
const fieldComponentTypes = [ | ||
'text', | ||
'checkbox', | ||
'radio', | ||
'select', | ||
'email', | ||
'number', | ||
'tel', | ||
'date', | ||
'time', | ||
'datetime', | ||
'url', | ||
]; | ||
let checkoutConfig = {}; | ||
@@ -25,9 +38,5 @@ class MissingConfigurationField extends Error { | ||
exports.getCheckoutConfig = getCheckoutConfig; | ||
// const announceDeprecatedField = (oldField: string, newField: string) => { | ||
// console.warn('[ctc] DEPRECATED: ' + oldField + ' is deprecated, please use ' + newField + ' instead'); | ||
// } | ||
const updateCheckoutConfig = (newConfig, reset = false) => { | ||
if (reset) | ||
checkoutConfig = {}; | ||
// if ('callbackUrl' in newConfig) announceDeprecatedField('callbackUrl', 'returnUrl') | ||
checkoutConfig = { | ||
@@ -43,3 +52,3 @@ //not documented | ||
applicationKey: 'applicationKey' in newConfig ? newConfig.applicationKey : checkoutConfig.applicationKey, | ||
returnUrl: 'callbackUrl' in newConfig ? newConfig.returnUrl : checkoutConfig.returnUrl, | ||
returnUrl: 'returnUrl' in newConfig ? newConfig.returnUrl : checkoutConfig.returnUrl, | ||
cartId: 'cartId' in newConfig ? newConfig.cartId : checkoutConfig.cartId, | ||
@@ -46,0 +55,0 @@ locale: 'locale' in newConfig ? newConfig.locale : checkoutConfig.locale, |
{ | ||
"private": false, | ||
"name": "@commercetools/checkout-browser-sdk", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "types": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
34331
449