UNPKG

@commercelayer/react-components

Version:
2 lines 3.18 kB
"use client"; "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BraintreeGateway=BraintreeGateway;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),BraintreePayment_1=tslib_1.__importDefault(require("../payment_source/BraintreePayment")),CustomerContext_1=tslib_1.__importDefault(require("../../context/CustomerContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),PaymentMethodChildrenContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodChildrenContext")),PaymentMethodContext_1=tslib_1.__importDefault(require("../../context/PaymentMethodContext")),PaymentSourceContext_1=tslib_1.__importDefault(require("../../context/PaymentSourceContext")),getCardDetails_1=tslib_1.__importDefault(require("../../utils/getCardDetails")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),react_1=require("react"),PaymentCardsTemplate_1=tslib_1.__importDefault(require("../utils/PaymentCardsTemplate")),getPaymentAttributes_1=require("../../utils/getPaymentAttributes");function BraintreeGateway(props){const{readonly,showCard,handleEditClick,children,templateCustomerCards,loading,loaderComponent,templateCustomerSaveToWallet}=props,p=tslib_1.__rest(props,["readonly","showCard","handleEditClick","children","templateCustomerCards","loading","loaderComponent","templateCustomerSaveToWallet"]),{order}=(0,react_1.useContext)(OrderContext_1.default),{payment}=(0,react_1.useContext)(PaymentMethodChildrenContext_1.default),{payments,isGuest}=(0,react_1.useContext)(CustomerContext_1.default),{currentPaymentMethodId,config,paymentSource}=(0,react_1.useContext)(PaymentMethodContext_1.default),paymentResource="braintree_payments",locale=order?.language_code;if(!readonly&&payment?.id!==currentPaymentMethodId)return null;const authorization=paymentSource?.client_token,braintreeConfig=(0,getPaymentAttributes_1.getPaymentAttributes)({resource:paymentResource,config:config??{},keys:["braintree_payments"]}),paymentConfig=braintreeConfig?.braintreePayment,customerPayments=!(0,isEmpty_1.default)(payments)&&payments?payments.filter(customerPayment=>{var _a;return((_a=customerPayment.payment_source)===null||_a===void 0?void 0:_a.type)==="braintree_payments"}):[];if(readonly||showCard){const card=(0,getCardDetails_1.default)({customerPayment:{payment_source:paymentSource},paymentType:paymentResource}),value=Object.assign(Object.assign({},card),{showCard,handleEditClick,readonly});return(0,isEmpty_1.default)(card)?null:(0,jsx_runtime_1.jsx)(PaymentSourceContext_1.default.Provider,{value,children})}return!isGuest&&templateCustomerCards?(0,jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment,{children:[(0,isEmpty_1.default)(customerPayments)?null:(0,jsx_runtime_1.jsx)("div",{className:p.className,children:(0,jsx_runtime_1.jsx)(PaymentCardsTemplate_1.default,{paymentResource,customerPayments,children:templateCustomerCards})}),(0,jsx_runtime_1.jsx)(BraintreePayment_1.default,{templateCustomerSaveToWallet,authorization,locale,config:paymentConfig})]}):(0,jsx_runtime_1.jsx)(BraintreePayment_1.default,{locale,authorization,config:paymentConfig})}exports.default=BraintreeGateway;