@bigcommerce/checkout-sdk 1.904.1 → 1.904.2
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/dist/cjs/checkout-button.js +1 -1
- package/dist/cjs/checkout-sdk-essential.js +1 -1
- package/dist/cjs/checkout-sdk.js +1 -1
- package/dist/cjs/hosted-form-v2-iframe-host.js +1 -1
- package/dist/cjs/integrations/amazon-pay.js +1 -1
- package/dist/cjs/integrations/bigcommerce-payments.js +1 -1
- package/dist/cjs/integrations/braintree.js +1 -1
- package/dist/cjs/integrations/google-pay.js +1 -1
- package/dist/cjs/integrations/klarna.js +1 -1
- package/dist/cjs/integrations/paypal-commerce.js +1 -1
- package/dist/cjs/integrations/paypal-express.js +1 -1
- package/dist/cjs/integrations/zip.js +1 -1
- package/dist/esm/checkout-button.js +1 -1
- package/dist/esm/checkout-sdk-essential.js +1 -1
- package/dist/esm/checkout-sdk.js +1 -1
- package/dist/esm/hosted-form-v2-iframe-host.js +1 -1
- package/dist/esm/integrations/amazon-pay.js +1 -1
- package/dist/esm/integrations/bigcommerce-payments.js +1 -1
- package/dist/esm/integrations/braintree.js +1 -1
- package/dist/esm/integrations/google-pay.js +1 -1
- package/dist/esm/integrations/klarna.js +1 -1
- package/dist/esm/integrations/paypal-commerce.js +1 -1
- package/dist/esm/integrations/paypal-express.js +1 -1
- package/dist/esm/integrations/zip.js +1 -1
- package/dist/types/checkout-sdk.d.ts +1 -1
- package/dist/types/internal-mappers.d.ts +1 -1
- package/docs/interfaces/ShippingOption.md +1 -1
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{getScriptLoader as e}from"@bigcommerce/script-loader";import{includes as t}from"lodash";import{createRequestSender as n}from"@bigcommerce/request-sender";var i,a,r={};function o(e,t){return Object.assign(e,{resolveIds:t})}r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);class s extends Error{constructor(e){var t;super(e||"An unexpected error has occurred."),this.name="StandardError",this.type="standard",t=new.target.prototype,Object.setPrototypeOf?Object.setPrototypeOf(this,t):this.__proto__=t,"function"==typeof Error.captureStackTrace?Error.captureStackTrace(this,new.target):this.stack=new Error(this.message).stack}}class d extends s{constructor(e){super(e||"Invalid arguments have been provided."),this.name="InvalidArgumentError",this.type="invalid_argument"}}class c extends s{constructor(){super("The current order does not need to be finalized at this stage."),this.name="OrderFinalizationNotRequiredError",this.type="order_finalization_not_required"}}!function(e){e[e.MissingBillingAddress=0]="MissingBillingAddress",e[e.MissingCart=1]="MissingCart",e[e.MissingCheckout=2]="MissingCheckout",e[e.MissingConsignments=3]="MissingConsignments",e[e.MissingCustomer=4]="MissingCustomer",e[e.MissingCheckoutConfig=5]="MissingCheckoutConfig",e[e.MissingOrder=6]="MissingOrder",e[e.MissingOrderConfig=7]="MissingOrderConfig",e[e.MissingOrderId=8]="MissingOrderId",e[e.MissingPayment=9]="MissingPayment",e[e.MissingPaymentId=10]="MissingPaymentId",e[e.MissingPaymentInstrument=11]="MissingPaymentInstrument",e[e.MissingPaymentMethod=12]="MissingPaymentMethod",e[e.MissingPaymentRedirectUrl=13]="MissingPaymentRedirectUrl",e[e.MissingPaymentStatus=14]="MissingPaymentStatus",e[e.MissingPaymentToken=15]="MissingPaymentToken",e[e.MissingShippingAddress=16]="MissingShippingAddress"}(i||(i={}));class l extends s{constructor(e){super(function(e){switch(e){case i.MissingBillingAddress:return"Unable to proceed because billing address data is unavailable.";case i.MissingCart:return"Unable to proceed because cart data is unavailable.";case i.MissingConsignments:return"Unable to proceed because consignments data is unavailable.";case i.MissingCheckout:return"Unable to proceed because checkout data is unavailable.";case i.MissingCustomer:return"Unable to proceed because customer data is unavailable.";case i.MissingCheckoutConfig:case i.MissingOrderConfig:return"Unable to proceed because configuration data is unavailable.";case i.MissingOrder:return"Unable to proceed because order data is unavailable.";case i.MissingOrderId:return"Unable to proceed because order ID is unavailable or not generated yet.";case i.MissingPayment:return"Unable to proceed because payment data is unavailable.";case i.MissingPaymentToken:return"Unable to proceed because the token required to submit a payment is missing.";case i.MissingPaymentMethod:return"Unable to proceed because payment method data is unavailable or not properly configured.";case i.MissingShippingAddress:return"Unable to proceed because shipping address data is unavailable.";default:return"Unable to proceed because the required data is unavailable."}}(e)),this.subtype=e,this.name="MissingDataError",this.type="missing_data"}}!function(e){e[e.CheckoutButtonNotInitialized=0]="CheckoutButtonNotInitialized",e[e.CustomerNotInitialized=1]="CustomerNotInitialized",e[e.PaymentNotInitialized=2]="PaymentNotInitialized",e[e.ShippingNotInitialized=3]="ShippingNotInitialized",e[e.SpamProtectionNotInitialized=4]="SpamProtectionNotInitialized"}(a||(a={}));class u extends s{constructor(e){super(function(e){switch(e){case a.CustomerNotInitialized:return"Unable to proceed because the customer step of checkout has not been initialized.";case a.PaymentNotInitialized:return"Unable to proceed because the payment step of checkout has not been initialized.";case a.ShippingNotInitialized:return"Unable to proceed because the shipping step of checkout has not been initialized.";case a.SpamProtectionNotInitialized:return"Unable to proceed because the checkout spam protection has not been initialized.";default:return"Unable to proceed because the required component has not been initialized."}}(e)),this.subtype=e,this.name="NotInitializedError",this.type="not_initialized"}}class h extends s{constructor(e){super(e||"Payment process was cancelled."),this.name="PaymentMethodCancelledError",this.type="payment_cancelled"}}const p={body:{},headers:{},status:0};class m extends s{constructor(e,{message:t,errors:n}={}){const{body:i,headers:a,status:r}=e||p;super(t||"An unexpected error has occurred."),this.name="RequestError",this.type="request",this.body=i,this.headers=a,this.status=r,this.errors=n||[]}}class y extends m{constructor(e){super(e,{message:"There is a problem processing your payment. Please try again later."}),this.name="PaymentMethodInvalidError",this.type="payment_method_invalid"}}const g=["AT","BE","CA","CH","CZ","DE","DK","ES","FI","FR","GB","GR","IE","IT","NL","NO","NZ","PL","PT","SE"],b=["AU"];var v=function(e,t,n,i){return new(n||(n=Promise))(function(a,r){function o(e){try{d(i.next(e))}catch(e){r(e)}}function s(e){try{d(i.throw(e))}catch(e){r(e)}}function d(e){var t;e.done?a(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(o,s)}d((i=i.apply(e,t||[])).next())})};class f{constructor(e,t){this.paymentIntegrationService=e,this.klarnaScriptLoader=t}initialize(e){return v(this,void 0,void 0,function*(){this.klarnaCredit=yield this.klarnaScriptLoader.load(),this.unsubscribe=this.paymentIntegrationService.subscribe(t=>{t.isPaymentMethodInitialized({methodId:e.methodId,gatewayId:e.gatewayId})&&this.loadWidget(e)},e=>{const t=e.getCheckout();return t&&t.outstandingBalance},e=>{const t=e.getCheckout();return t&&t.coupons}),yield this.loadWidget(e)})}deinitialize(){return this.unsubscribe&&this.unsubscribe(),Promise.resolve()}execute(e,t){return v(this,void 0,void 0,function*(){if(!e.payment)throw new d('Unable to proceed because "payload.payment" argument is not provided.');const n=e.payment,{paymentData:i}=n,a=function(e,t){var n={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(i=Object.getOwnPropertySymbols(e);a<i.length;a++)t.indexOf(i[a])<0&&Object.prototype.propertyIsEnumerable.call(e,i[a])&&(n[i[a]]=e[i[a]])}return n}(n,["paymentData"]),{authorization_token:r}=yield this.authorize();yield this.paymentIntegrationService.initializePayment(a.methodId,{authorizationToken:r}),yield this.paymentIntegrationService.submitOrder(Object.assign(Object.assign({},e),{payment:a,useStoreCredit:e.useStoreCredit}),t)})}finalize(){return Promise.reject(new c)}loadWidget(e){return v(this,void 0,void 0,function*(){if(!e.klarna)throw new d('Unable to load widget because "options.klarna" argument is not provided.');const{methodId:t,klarna:{container:n,onLoad:r}}=e;return yield this.paymentIntegrationService.loadPaymentMethod(t),new Promise(e=>{const o=this.paymentIntegrationService.getState().getPaymentMethod(t);if(!o)throw new l(i.MissingPaymentMethod);if(!this.klarnaCredit||!o.clientToken)throw new u(a.PaymentNotInitialized);this.klarnaCredit.init({client_token:o.clientToken}),this.klarnaCredit.load({container:n},t=>{r&&r(t),e(t)})})})}getUpdateSessionData(e,n){if(!t([...g,...b],e.countryCode))return{};const i={billing_address:this.mapToKlarnaAddress(e,e.email)};return n&&(i.shipping_address=this.mapToKlarnaAddress(n,e.email)),i}needsStateCode(e){return t(b,e)}mapToKlarnaAddress(e,t){const n={street_address:e.address1,city:e.city,country:e.countryCode,given_name:e.firstName,family_name:e.lastName,postal_code:e.postalCode,region:this.needsStateCode(e.countryCode)?e.stateOrProvinceCode:e.stateOrProvince,email:t};return e.address2&&(n.street_address2=e.address2),e.phone&&(n.phone=e.phone),n}authorize(){return new Promise((e,t)=>{const n=this.paymentIntegrationService.getState(),r=n.getBillingAddress(),o=n.getShippingAddress();if(!r)throw new l(i.MissingBillingAddress);if(!this.klarnaCredit)throw new u(a.PaymentNotInitialized);const s=this.getUpdateSessionData(r,o);this.klarnaCredit.authorize(s,n=>n.approved?e(n):n.show_form?t(new h):void t(new y))})}}class w extends s{constructor(e){super(e||"Unable to proceed because the client library of a payment method is not loaded or ready to be used."),this.name="PaymentMethodClientUnavailableError",this.type="payment_method_client_unavailable"}}class P{constructor(e,t=window){this.scriptLoader=e,this.klarnaWindow=t}load(){var e,t,n,i,a,r;return n=this,i=void 0,r=function*(){if((null===(e=this.klarnaWindow.Klarna)||void 0===e?void 0:e.Credit)||(yield this.scriptLoader.loadScript("//credit.klarnacdn.net/lib/v1/api.js")),!(null===(t=this.klarnaWindow.Klarna)||void 0===t?void 0:t.Credit))throw new w;return this.klarnaWindow.Klarna.Credit},new((a=void 0)||(a=Promise))(function(e,t){function o(e){try{d(r.next(e))}catch(e){t(e)}}function s(e){try{d(r.throw(e))}catch(e){t(e)}}function d(t){var n;t.done?e(t.value):(n=t.value,n instanceof a?n:new a(function(e){e(n)})).then(o,s)}d((r=r.apply(n,i||[])).next())})}}const k=o(t=>new f(t,new P(e())),[{id:"klarna"}]);var I=function(e,t,n,i){return new(n||(n=Promise))(function(a,r){function o(e){try{d(i.next(e))}catch(e){r(e)}}function s(e){try{d(i.throw(e))}catch(e){r(e)}}function d(e){var t;e.done?a(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(o,s)}d((i=i.apply(e,t||[])).next())})};class M{constructor(e,t,n){this.paymentIntegrationService=e,this.klarnav2ScriptLoader=t,this.klarnav2TokenUpdater=n}initialize(e){return I(this,void 0,void 0,function*(){this.klarnaPayments=yield this.klarnav2ScriptLoader.load(),this.unsubscribe=this.paymentIntegrationService.subscribe(t=>{t.isPaymentMethodInitialized({methodId:e.methodId,gatewayId:e.gatewayId})&&this.loadPaymentsWidget(e)},e=>{const t=e.getCheckout();return t&&t.outstandingBalance},e=>{const t=e.getCheckout();return t&&t.coupons}),yield this.loadPaymentsWidget(e)})}deinitialize(){return this.unsubscribe&&this.unsubscribe(),Promise.resolve()}execute(e,t){return I(this,void 0,void 0,function*(){if(!e.payment)throw new d('Unable to proceed because "payload.payment" argument is not provided.');const n=function(e,t){var n={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(i=Object.getOwnPropertySymbols(e);a<i.length;a++)t.indexOf(i[a])<0&&Object.prototype.propertyIsEnumerable.call(e,i[a])&&(n[i[a]]=e[i[a]])}return n}(e.payment,[]),{gatewayId:i,methodId:a}=n;if(!i)throw new d('Unable to proceed because "payload.payment.gatewayId" argument is not provided.');const r=this.paymentIntegrationService.getState(),{id:o}=r.getCartOrThrow(),{clientToken:s,initializationData:c}=r.getPaymentMethodOrThrow(a),{klarnaMultipleRadioButton:l}=c||{};yield this.klarnav2TokenUpdater.klarnaOrderInitialization(o,s);const u=this.isKlarnaMultipleRadioButtonEnabled(l,i,a),{authorization_token:h}=yield this.authorizeOrThrow(u,a);yield this.paymentIntegrationService.initializePayment(i,{authorizationToken:h}),yield this.paymentIntegrationService.submitOrder(Object.assign(Object.assign({},e),{payment:n,useStoreCredit:e.useStoreCredit}),t)})}finalize(){return Promise.reject(new c)}loadPaymentsWidget(e){return I(this,void 0,void 0,function*(){if(!e.klarnav2)throw new d('Unable to load widget because "options.klarnav2" argument is not provided.');const{methodId:t,gatewayId:n,klarnav2:{container:r,onLoad:o}}=e;if(!n)throw new d('Unable to proceed because "payload.payment.gatewayId" argument is not provided.');const s=this.paymentIntegrationService.getState(),c={params:s.getCartOrThrow().id};return yield this.klarnav2TokenUpdater.updateClientToken(n,{params:c}).catch(()=>{throw new l(i.MissingPaymentMethod)}),new Promise(e=>{const i=s.getPaymentMethodOrThrow(t),{klarnaMultipleRadioButton:d}=i.initializationData||{};if(!this.klarnaPayments||!i.clientToken)throw new u(a.PaymentNotInitialized);this.klarnaPayments.init({client_token:i.clientToken}),this.klarnaPayments.load({container:r,payment_method_category:this.isKlarnaMultipleRadioButtonEnabled(d,n,t)},t=>{o&&o(t),e(t)})})})}getUpdateSessionData(e,n,i){if(!t([...g,...b],n.countryCode))return{};const a={billing_address:this.mapToKlarnaAddress(e,n,n.email)};return i&&(a.shipping_address=this.mapToKlarnaAddress(e,i,n.email)),a}needsStateCode(e){return t(b,e)}isKlarnaMultipleRadioButtonEnabled(e,t,n){return e?n:t}mapToKlarnaAddress(e,t,n){const i=this.paymentIntegrationService.getState(),{checkoutSettings:a}=i.getStoreConfigOrThrow(),r=i.getPaymentMethodOrThrow(e),{enableBillie:o}=r.initializationData||{},s={street_address:t.address1,city:t.city,country:t.countryCode,given_name:t.firstName,family_name:t.lastName,postal_code:t.postalCode,region:this.needsStateCode(t.countryCode)?t.stateOrProvinceCode:t.stateOrProvince,email:n};return t.address2&&(s.street_address2=t.address2),t.phone&&(s.phone=t.phone),t.company&&o&&function(e,t,n=!0){var i;return null!==(i=e["PI-3915.b2b_payment_session_for_klarna"])&&void 0!==i?i:n}(a.features)&&(s.organization_name=t.company),s}authorizeOrThrow(e,t){return I(this,void 0,void 0,function*(){yield this.paymentIntegrationService.loadCheckout();const n=this.paymentIntegrationService.getState(),i=n.getBillingAddressOrThrow(),r=n.getShippingAddress(),o=this.getUpdateSessionData(t,i,r);return new Promise((t,n)=>{if(!this.klarnaPayments)return n(new u(a.PaymentNotInitialized));this.klarnaPayments.authorize({payment_method_category:e},o,e=>e.approved?t(e):e.show_form?n(new h):void n(new y))})})}}var S;class C{constructor(e,t=window){this.scriptLoader=e,this.klarnaWindow=t}load(){var e,t,n,i,a,r;return n=this,i=void 0,r=function*(){if((null===(e=this.klarnaWindow.Klarna)||void 0===e?void 0:e.Payments)||(yield this.scriptLoader.loadScript("https://x.klarnacdn.net/kp/lib/v1/api.js")),!(null===(t=this.klarnaWindow.Klarna)||void 0===t?void 0:t.Payments))throw new w;return this.klarnaWindow.Klarna.Payments},new((a=void 0)||(a=Promise))(function(e,t){function o(e){try{d(r.next(e))}catch(e){t(e)}}function s(e){try{d(r.throw(e))}catch(e){t(e)}}function d(t){var n;t.done?e(t.value):(n=t.value,n instanceof a?n:new a(function(e){e(n)})).then(o,s)}d((r=r.apply(n,i||[])).next())})}}!function(e){e.Json="application/json",e.JsonV1="application/vnd.bc.v1+json"}(S||(S={}));const O=S,z="This API endpoint is for internal use only and may change in the future",_={"X-Checkout-SDK-Version":"1.904.1"};class T{constructor(e){this.requestSender=e}updateClientToken(e,{timeout:t,params:n}={}){const i=`/api/storefront/payments/${e}`;return this.requestSender.get(i,{timeout:t,headers:Object.assign({Accept:O.JsonV1,"X-API-INTERNAL":z},_),params:n})}klarnaOrderInitialization(e,t){return n=this,i=void 0,r=function*(){const n={headers:Object.assign({Accept:O.JsonV1,"X-API-INTERNAL":z},_),body:{cartId:e,clientToken:t}};yield this.requestSender.put("/api/storefront/initialization/klarna",n)},new((a=void 0)||(a=Promise))(function(e,t){function o(e){try{d(r.next(e))}catch(e){t(e)}}function s(e){try{d(r.throw(e))}catch(e){t(e)}}function d(t){var n;t.done?e(t.value):(n=t.value,n instanceof a?n:new a(function(e){e(n)})).then(o,s)}d((r=r.apply(n,i||[])).next())});var n,i,a,r}}const U=o(t=>{const{getHost:i}=t.getState(),a=n({host:i()});return new M(t,new C(e()),new T(a))},[{gateway:"klarna"}]);export{k as createKlarnaPaymentStrategy,U as createKlarnaV2PaymentStrategy};
|
|
1
|
+
import{getScriptLoader as e}from"@bigcommerce/script-loader";import{includes as t}from"lodash";import{createRequestSender as n}from"@bigcommerce/request-sender";var i,a,r={};function o(e,t){return Object.assign(e,{resolveIds:t})}r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);class s extends Error{constructor(e){var t;super(e||"An unexpected error has occurred."),this.name="StandardError",this.type="standard",t=new.target.prototype,Object.setPrototypeOf?Object.setPrototypeOf(this,t):this.__proto__=t,"function"==typeof Error.captureStackTrace?Error.captureStackTrace(this,new.target):this.stack=new Error(this.message).stack}}class d extends s{constructor(e){super(e||"Invalid arguments have been provided."),this.name="InvalidArgumentError",this.type="invalid_argument"}}class c extends s{constructor(){super("The current order does not need to be finalized at this stage."),this.name="OrderFinalizationNotRequiredError",this.type="order_finalization_not_required"}}!function(e){e[e.MissingBillingAddress=0]="MissingBillingAddress",e[e.MissingCart=1]="MissingCart",e[e.MissingCheckout=2]="MissingCheckout",e[e.MissingConsignments=3]="MissingConsignments",e[e.MissingCustomer=4]="MissingCustomer",e[e.MissingCheckoutConfig=5]="MissingCheckoutConfig",e[e.MissingOrder=6]="MissingOrder",e[e.MissingOrderConfig=7]="MissingOrderConfig",e[e.MissingOrderId=8]="MissingOrderId",e[e.MissingPayment=9]="MissingPayment",e[e.MissingPaymentId=10]="MissingPaymentId",e[e.MissingPaymentInstrument=11]="MissingPaymentInstrument",e[e.MissingPaymentMethod=12]="MissingPaymentMethod",e[e.MissingPaymentRedirectUrl=13]="MissingPaymentRedirectUrl",e[e.MissingPaymentStatus=14]="MissingPaymentStatus",e[e.MissingPaymentToken=15]="MissingPaymentToken",e[e.MissingShippingAddress=16]="MissingShippingAddress"}(i||(i={}));class l extends s{constructor(e){super(function(e){switch(e){case i.MissingBillingAddress:return"Unable to proceed because billing address data is unavailable.";case i.MissingCart:return"Unable to proceed because cart data is unavailable.";case i.MissingConsignments:return"Unable to proceed because consignments data is unavailable.";case i.MissingCheckout:return"Unable to proceed because checkout data is unavailable.";case i.MissingCustomer:return"Unable to proceed because customer data is unavailable.";case i.MissingCheckoutConfig:case i.MissingOrderConfig:return"Unable to proceed because configuration data is unavailable.";case i.MissingOrder:return"Unable to proceed because order data is unavailable.";case i.MissingOrderId:return"Unable to proceed because order ID is unavailable or not generated yet.";case i.MissingPayment:return"Unable to proceed because payment data is unavailable.";case i.MissingPaymentToken:return"Unable to proceed because the token required to submit a payment is missing.";case i.MissingPaymentMethod:return"Unable to proceed because payment method data is unavailable or not properly configured.";case i.MissingShippingAddress:return"Unable to proceed because shipping address data is unavailable.";default:return"Unable to proceed because the required data is unavailable."}}(e)),this.subtype=e,this.name="MissingDataError",this.type="missing_data"}}!function(e){e[e.CheckoutButtonNotInitialized=0]="CheckoutButtonNotInitialized",e[e.CustomerNotInitialized=1]="CustomerNotInitialized",e[e.PaymentNotInitialized=2]="PaymentNotInitialized",e[e.ShippingNotInitialized=3]="ShippingNotInitialized",e[e.SpamProtectionNotInitialized=4]="SpamProtectionNotInitialized"}(a||(a={}));class u extends s{constructor(e){super(function(e){switch(e){case a.CustomerNotInitialized:return"Unable to proceed because the customer step of checkout has not been initialized.";case a.PaymentNotInitialized:return"Unable to proceed because the payment step of checkout has not been initialized.";case a.ShippingNotInitialized:return"Unable to proceed because the shipping step of checkout has not been initialized.";case a.SpamProtectionNotInitialized:return"Unable to proceed because the checkout spam protection has not been initialized.";default:return"Unable to proceed because the required component has not been initialized."}}(e)),this.subtype=e,this.name="NotInitializedError",this.type="not_initialized"}}class h extends s{constructor(e){super(e||"Payment process was cancelled."),this.name="PaymentMethodCancelledError",this.type="payment_cancelled"}}const p={body:{},headers:{},status:0};class m extends s{constructor(e,{message:t,errors:n}={}){const{body:i,headers:a,status:r}=e||p;super(t||"An unexpected error has occurred."),this.name="RequestError",this.type="request",this.body=i,this.headers=a,this.status=r,this.errors=n||[]}}class y extends m{constructor(e){super(e,{message:"There is a problem processing your payment. Please try again later."}),this.name="PaymentMethodInvalidError",this.type="payment_method_invalid"}}const g=["AT","BE","CA","CH","CZ","DE","DK","ES","FI","FR","GB","GR","IE","IT","NL","NO","NZ","PL","PT","SE"],b=["AU"];var v=function(e,t,n,i){return new(n||(n=Promise))(function(a,r){function o(e){try{d(i.next(e))}catch(e){r(e)}}function s(e){try{d(i.throw(e))}catch(e){r(e)}}function d(e){var t;e.done?a(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(o,s)}d((i=i.apply(e,t||[])).next())})};class f{constructor(e,t){this.paymentIntegrationService=e,this.klarnaScriptLoader=t}initialize(e){return v(this,void 0,void 0,function*(){this.klarnaCredit=yield this.klarnaScriptLoader.load(),this.unsubscribe=this.paymentIntegrationService.subscribe(t=>{t.isPaymentMethodInitialized({methodId:e.methodId,gatewayId:e.gatewayId})&&this.loadWidget(e)},e=>{const t=e.getCheckout();return t&&t.outstandingBalance},e=>{const t=e.getCheckout();return t&&t.coupons}),yield this.loadWidget(e)})}deinitialize(){return this.unsubscribe&&this.unsubscribe(),Promise.resolve()}execute(e,t){return v(this,void 0,void 0,function*(){if(!e.payment)throw new d('Unable to proceed because "payload.payment" argument is not provided.');const n=e.payment,{paymentData:i}=n,a=function(e,t){var n={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(i=Object.getOwnPropertySymbols(e);a<i.length;a++)t.indexOf(i[a])<0&&Object.prototype.propertyIsEnumerable.call(e,i[a])&&(n[i[a]]=e[i[a]])}return n}(n,["paymentData"]),{authorization_token:r}=yield this.authorize();yield this.paymentIntegrationService.initializePayment(a.methodId,{authorizationToken:r}),yield this.paymentIntegrationService.submitOrder(Object.assign(Object.assign({},e),{payment:a,useStoreCredit:e.useStoreCredit}),t)})}finalize(){return Promise.reject(new c)}loadWidget(e){return v(this,void 0,void 0,function*(){if(!e.klarna)throw new d('Unable to load widget because "options.klarna" argument is not provided.');const{methodId:t,klarna:{container:n,onLoad:r}}=e;return yield this.paymentIntegrationService.loadPaymentMethod(t),new Promise(e=>{const o=this.paymentIntegrationService.getState().getPaymentMethod(t);if(!o)throw new l(i.MissingPaymentMethod);if(!this.klarnaCredit||!o.clientToken)throw new u(a.PaymentNotInitialized);this.klarnaCredit.init({client_token:o.clientToken}),this.klarnaCredit.load({container:n},t=>{r&&r(t),e(t)})})})}getUpdateSessionData(e,n){if(!t([...g,...b],e.countryCode))return{};const i={billing_address:this.mapToKlarnaAddress(e,e.email)};return n&&(i.shipping_address=this.mapToKlarnaAddress(n,e.email)),i}needsStateCode(e){return t(b,e)}mapToKlarnaAddress(e,t){const n={street_address:e.address1,city:e.city,country:e.countryCode,given_name:e.firstName,family_name:e.lastName,postal_code:e.postalCode,region:this.needsStateCode(e.countryCode)?e.stateOrProvinceCode:e.stateOrProvince,email:t};return e.address2&&(n.street_address2=e.address2),e.phone&&(n.phone=e.phone),n}authorize(){return new Promise((e,t)=>{const n=this.paymentIntegrationService.getState(),r=n.getBillingAddress(),o=n.getShippingAddress();if(!r)throw new l(i.MissingBillingAddress);if(!this.klarnaCredit)throw new u(a.PaymentNotInitialized);const s=this.getUpdateSessionData(r,o);this.klarnaCredit.authorize(s,n=>n.approved?e(n):n.show_form?t(new h):void t(new y))})}}class w extends s{constructor(e){super(e||"Unable to proceed because the client library of a payment method is not loaded or ready to be used."),this.name="PaymentMethodClientUnavailableError",this.type="payment_method_client_unavailable"}}class P{constructor(e,t=window){this.scriptLoader=e,this.klarnaWindow=t}load(){var e,t,n,i,a,r;return n=this,i=void 0,r=function*(){if((null===(e=this.klarnaWindow.Klarna)||void 0===e?void 0:e.Credit)||(yield this.scriptLoader.loadScript("//credit.klarnacdn.net/lib/v1/api.js")),!(null===(t=this.klarnaWindow.Klarna)||void 0===t?void 0:t.Credit))throw new w;return this.klarnaWindow.Klarna.Credit},new((a=void 0)||(a=Promise))(function(e,t){function o(e){try{d(r.next(e))}catch(e){t(e)}}function s(e){try{d(r.throw(e))}catch(e){t(e)}}function d(t){var n;t.done?e(t.value):(n=t.value,n instanceof a?n:new a(function(e){e(n)})).then(o,s)}d((r=r.apply(n,i||[])).next())})}}const k=o(t=>new f(t,new P(e())),[{id:"klarna"}]);var I=function(e,t,n,i){return new(n||(n=Promise))(function(a,r){function o(e){try{d(i.next(e))}catch(e){r(e)}}function s(e){try{d(i.throw(e))}catch(e){r(e)}}function d(e){var t;e.done?a(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(o,s)}d((i=i.apply(e,t||[])).next())})};class M{constructor(e,t,n){this.paymentIntegrationService=e,this.klarnav2ScriptLoader=t,this.klarnav2TokenUpdater=n}initialize(e){return I(this,void 0,void 0,function*(){this.klarnaPayments=yield this.klarnav2ScriptLoader.load(),this.unsubscribe=this.paymentIntegrationService.subscribe(t=>{t.isPaymentMethodInitialized({methodId:e.methodId,gatewayId:e.gatewayId})&&this.loadPaymentsWidget(e)},e=>{const t=e.getCheckout();return t&&t.outstandingBalance},e=>{const t=e.getCheckout();return t&&t.coupons}),yield this.loadPaymentsWidget(e)})}deinitialize(){return this.unsubscribe&&this.unsubscribe(),Promise.resolve()}execute(e,t){return I(this,void 0,void 0,function*(){if(!e.payment)throw new d('Unable to proceed because "payload.payment" argument is not provided.');const n=function(e,t){var n={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(i=Object.getOwnPropertySymbols(e);a<i.length;a++)t.indexOf(i[a])<0&&Object.prototype.propertyIsEnumerable.call(e,i[a])&&(n[i[a]]=e[i[a]])}return n}(e.payment,[]),{gatewayId:i,methodId:a}=n;if(!i)throw new d('Unable to proceed because "payload.payment.gatewayId" argument is not provided.');const r=this.paymentIntegrationService.getState(),{id:o}=r.getCartOrThrow(),{clientToken:s,initializationData:c}=r.getPaymentMethodOrThrow(a),{klarnaMultipleRadioButton:l}=c||{};yield this.klarnav2TokenUpdater.klarnaOrderInitialization(o,s);const u=this.isKlarnaMultipleRadioButtonEnabled(l,i,a),{authorization_token:h}=yield this.authorizeOrThrow(u,a);yield this.paymentIntegrationService.initializePayment(i,{authorizationToken:h}),yield this.paymentIntegrationService.submitOrder(Object.assign(Object.assign({},e),{payment:n,useStoreCredit:e.useStoreCredit}),t)})}finalize(){return Promise.reject(new c)}loadPaymentsWidget(e){return I(this,void 0,void 0,function*(){if(!e.klarnav2)throw new d('Unable to load widget because "options.klarnav2" argument is not provided.');const{methodId:t,gatewayId:n,klarnav2:{container:r,onLoad:o}}=e;if(!n)throw new d('Unable to proceed because "payload.payment.gatewayId" argument is not provided.');const s=this.paymentIntegrationService.getState(),c={params:s.getCartOrThrow().id};return yield this.klarnav2TokenUpdater.updateClientToken(n,{params:c}).catch(()=>{throw new l(i.MissingPaymentMethod)}),new Promise(e=>{const i=s.getPaymentMethodOrThrow(t),{klarnaMultipleRadioButton:d}=i.initializationData||{};if(!this.klarnaPayments||!i.clientToken)throw new u(a.PaymentNotInitialized);this.klarnaPayments.init({client_token:i.clientToken}),this.klarnaPayments.load({container:r,payment_method_category:this.isKlarnaMultipleRadioButtonEnabled(d,n,t)},t=>{o&&o(t),e(t)})})})}getUpdateSessionData(e,n,i){if(!t([...g,...b],n.countryCode))return{};const a={billing_address:this.mapToKlarnaAddress(e,n,n.email)};return i&&(a.shipping_address=this.mapToKlarnaAddress(e,i,n.email)),a}needsStateCode(e){return t(b,e)}isKlarnaMultipleRadioButtonEnabled(e,t,n){return e?n:t}mapToKlarnaAddress(e,t,n){const i=this.paymentIntegrationService.getState(),{checkoutSettings:a}=i.getStoreConfigOrThrow(),r=i.getPaymentMethodOrThrow(e),{enableBillie:o}=r.initializationData||{},s={street_address:t.address1,city:t.city,country:t.countryCode,given_name:t.firstName,family_name:t.lastName,postal_code:t.postalCode,region:this.needsStateCode(t.countryCode)?t.stateOrProvinceCode:t.stateOrProvince,email:n};return t.address2&&(s.street_address2=t.address2),t.phone&&(s.phone=t.phone),t.company&&o&&function(e,t,n=!0){var i;return null!==(i=e["PI-3915.b2b_payment_session_for_klarna"])&&void 0!==i?i:n}(a.features)&&(s.organization_name=t.company),s}authorizeOrThrow(e,t){return I(this,void 0,void 0,function*(){yield this.paymentIntegrationService.loadCheckout();const n=this.paymentIntegrationService.getState(),i=n.getBillingAddressOrThrow(),r=n.getShippingAddress(),o=this.getUpdateSessionData(t,i,r);return new Promise((t,n)=>{if(!this.klarnaPayments)return n(new u(a.PaymentNotInitialized));this.klarnaPayments.authorize({payment_method_category:e},o,e=>e.approved?t(e):e.show_form?n(new h):void n(new y))})})}}var S;class C{constructor(e,t=window){this.scriptLoader=e,this.klarnaWindow=t}load(){var e,t,n,i,a,r;return n=this,i=void 0,r=function*(){if((null===(e=this.klarnaWindow.Klarna)||void 0===e?void 0:e.Payments)||(yield this.scriptLoader.loadScript("https://x.klarnacdn.net/kp/lib/v1/api.js")),!(null===(t=this.klarnaWindow.Klarna)||void 0===t?void 0:t.Payments))throw new w;return this.klarnaWindow.Klarna.Payments},new((a=void 0)||(a=Promise))(function(e,t){function o(e){try{d(r.next(e))}catch(e){t(e)}}function s(e){try{d(r.throw(e))}catch(e){t(e)}}function d(t){var n;t.done?e(t.value):(n=t.value,n instanceof a?n:new a(function(e){e(n)})).then(o,s)}d((r=r.apply(n,i||[])).next())})}}!function(e){e.Json="application/json",e.JsonV1="application/vnd.bc.v1+json"}(S||(S={}));const O=S,z="This API endpoint is for internal use only and may change in the future",_={"X-Checkout-SDK-Version":"1.904.2"};class T{constructor(e){this.requestSender=e}updateClientToken(e,{timeout:t,params:n}={}){const i=`/api/storefront/payments/${e}`;return this.requestSender.get(i,{timeout:t,headers:Object.assign({Accept:O.JsonV1,"X-API-INTERNAL":z},_),params:n})}klarnaOrderInitialization(e,t){return n=this,i=void 0,r=function*(){const n={headers:Object.assign({Accept:O.JsonV1,"X-API-INTERNAL":z},_),body:{cartId:e,clientToken:t}};yield this.requestSender.put("/api/storefront/initialization/klarna",n)},new((a=void 0)||(a=Promise))(function(e,t){function o(e){try{d(r.next(e))}catch(e){t(e)}}function s(e){try{d(r.throw(e))}catch(e){t(e)}}function d(t){var n;t.done?e(t.value):(n=t.value,n instanceof a?n:new a(function(e){e(n)})).then(o,s)}d((r=r.apply(n,i||[])).next())});var n,i,a,r}}const U=o(t=>{const{getHost:i}=t.getState(),a=n({host:i()});return new M(t,new C(e()),new T(a))},[{gateway:"klarna"}]);export{k as createKlarnaPaymentStrategy,U as createKlarnaV2PaymentStrategy};
|
|
2
2
|
//# sourceMappingURL=klarna.js.map
|