@merchi/cart 1.4.4
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/LICENSE +21 -0
- package/README.md +134 -0
- package/esm/Cart.js +62 -0
- package/esm/CartAlert.js +47 -0
- package/esm/CartHeader.js +29 -0
- package/esm/CartProvider.js +1101 -0
- package/esm/CartTotals.js +89 -0
- package/esm/assets/merchi-monster-loader.gif +0 -0
- package/esm/assets/merchi-swipe-outline.png +0 -0
- package/esm/assets/product-not-found.png +0 -0
- package/esm/assets/stripe-payment-secure.png +0 -0
- package/esm/buttons/Button.js +23 -0
- package/esm/buttons/ButtonBack.js +35 -0
- package/esm/buttons/ButtonClearCart.js +17 -0
- package/esm/buttons/ButtonInvoiceDownload.js +88 -0
- package/esm/buttons/ButtonNext.js +39 -0
- package/esm/buttons/ButtonNextDynamic.js +19 -0
- package/esm/buttons/ButtonOpenCartMerchiCartModal.js +47 -0
- package/esm/buttons/ButtonPay.js +26 -0
- package/esm/buttons/ButtonShipmentTabNext.js +28 -0
- package/esm/buttons/index.js +7 -0
- package/esm/components/Alert.js +26 -0
- package/esm/components/CartBody.js +14 -0
- package/esm/components/CartClient.js +62 -0
- package/esm/components/CartFooter.js +12 -0
- package/esm/components/CartItemRow.js +230 -0
- package/esm/components/CartPaymentSettingsInvalid.js +13 -0
- package/esm/components/CartShipment.js +52 -0
- package/esm/components/CartTabContent.js +12 -0
- package/esm/components/CartTabPanel.js +25 -0
- package/esm/components/CartTableContainer.js +12 -0
- package/esm/components/CartTitle.js +26 -0
- package/esm/components/CartTotalsListGroup.js +71 -0
- package/esm/components/Collapse.js +32 -0
- package/esm/components/CustomCartProductForm.js +128 -0
- package/esm/components/CustomerCheckoutMethodTabs.js +94 -0
- package/esm/components/DiscountInputGroup.js +293 -0
- package/esm/components/LoadingTemplate.js +23 -0
- package/esm/components/NoCartItems.js +20 -0
- package/esm/components/ShipmentGroupCard.js +207 -0
- package/esm/components/Table.js +13 -0
- package/esm/components/containers.js +31 -0
- package/esm/components/index.js +18 -0
- package/esm/components/product-form-sdk-externals.js +36 -0
- package/esm/forms/FormNewCustomer.js +243 -0
- package/esm/forms/FormReturningCustomer.js +165 -0
- package/esm/forms/FormShipmentAddressAndNotes.js +195 -0
- package/esm/forms/FormSquarePayment.js +19 -0
- package/esm/forms/FormStripePayment.js +19 -0
- package/esm/forms/FormWhatsappCustomer.js +253 -0
- package/esm/forms/InputAcceptUserTermsAndConditions.js +35 -0
- package/esm/forms/InputError.js +16 -0
- package/esm/forms/InputSelect.js +48 -0
- package/esm/forms/InputText.js +56 -0
- package/esm/forms/InputsAddress.js +476 -0
- package/esm/forms/index.js +8 -0
- package/esm/index.js +13 -0
- package/esm/modals.js +51 -0
- package/esm/panels/PanelCartItems.js +49 -0
- package/esm/panels/PanelCartShipment.js +66 -0
- package/esm/panels/PanelClearCart.js +56 -0
- package/esm/panels/PanelClientCheckout.js +81 -0
- package/esm/panels/PanelEditCartItem.js +199 -0
- package/esm/panels/PanelPaymentSuccess.js +63 -0
- package/esm/panels/index.js +7 -0
- package/esm/square/SquareCard.js +106 -0
- package/esm/stripe/LegacyStripeCardForm.js +157 -0
- package/esm/stripe/StripeCardFields.js +126 -0
- package/esm/stripe/StripeCardForm.js +33 -0
- package/esm/stripe/StripePaymentButtons.js +98 -0
- package/esm/stripe/actions.js +192 -0
- package/esm/stripe/utils.js +20 -0
- package/esm/styles/globals.css +463 -0
- package/esm/styles/globals.css.map +1 -0
- package/esm/tabs/CartNav.js +28 -0
- package/esm/tabs/NavTab.js +31 -0
- package/esm/types/assets.d.js +0 -0
- package/esm/types/index.js +1 -0
- package/esm/types/merchi_product_form/index.d.js +0 -0
- package/esm/types/merchi_sdk_ts/index.d.js +0 -0
- package/esm/types/react-redux/index.d.js +0 -0
- package/esm/utilities/address.js +457 -0
- package/esm/utilities/area.js +297 -0
- package/esm/utilities/cart.js +95 -0
- package/esm/utilities/company.js +6 -0
- package/esm/utilities/cookie.js +156 -0
- package/esm/utilities/currency.js +219 -0
- package/esm/utilities/customForm.js +57 -0
- package/esm/utilities/helpers.js +229 -0
- package/esm/utilities/index.js +14 -0
- package/esm/utilities/invoice.js +112 -0
- package/esm/utilities/job.js +9 -0
- package/esm/utilities/local_storage.js +85 -0
- package/esm/utilities/product.js +13 -0
- package/esm/utilities/shipment.js +11 -0
- package/esm/utilities/tabs.js +51 -0
- package/esm/utilities/user.js +65 -0
- package/esm/utilities/variations.js +58 -0
- package/lib/Cart.d.ts +9 -0
- package/lib/Cart.js +69 -0
- package/lib/CartAlert.d.ts +3 -0
- package/lib/CartAlert.js +53 -0
- package/lib/CartHeader.d.ts +3 -0
- package/lib/CartHeader.js +37 -0
- package/lib/CartProvider.d.ts +236 -0
- package/lib/CartProvider.js +1107 -0
- package/lib/CartTotals.d.ts +7 -0
- package/lib/CartTotals.js +96 -0
- package/lib/assets/merchi-monster-loader.gif +0 -0
- package/lib/assets/merchi-swipe-outline.png +0 -0
- package/lib/assets/product-not-found.png +0 -0
- package/lib/assets/stripe-payment-secure.png +0 -0
- package/lib/buttons/Button.d.ts +11 -0
- package/lib/buttons/Button.js +31 -0
- package/lib/buttons/ButtonBack.d.ts +6 -0
- package/lib/buttons/ButtonBack.js +44 -0
- package/lib/buttons/ButtonClearCart.d.ts +3 -0
- package/lib/buttons/ButtonClearCart.js +24 -0
- package/lib/buttons/ButtonInvoiceDownload.d.ts +3 -0
- package/lib/buttons/ButtonInvoiceDownload.js +96 -0
- package/lib/buttons/ButtonNext.d.ts +3 -0
- package/lib/buttons/ButtonNext.js +48 -0
- package/lib/buttons/ButtonNextDynamic.d.ts +3 -0
- package/lib/buttons/ButtonNextDynamic.js +28 -0
- package/lib/buttons/ButtonOpenCartMerchiCartModal.d.ts +9 -0
- package/lib/buttons/ButtonOpenCartMerchiCartModal.js +57 -0
- package/lib/buttons/ButtonPay.d.ts +7 -0
- package/lib/buttons/ButtonPay.js +35 -0
- package/lib/buttons/ButtonShipmentTabNext.d.ts +2 -0
- package/lib/buttons/ButtonShipmentTabNext.js +37 -0
- package/lib/buttons/index.d.ts +7 -0
- package/lib/buttons/index.js +48 -0
- package/lib/components/Alert.d.ts +7 -0
- package/lib/components/Alert.js +34 -0
- package/lib/components/CartBody.d.ts +7 -0
- package/lib/components/CartBody.js +22 -0
- package/lib/components/CartClient.d.ts +4 -0
- package/lib/components/CartClient.js +71 -0
- package/lib/components/CartFooter.d.ts +6 -0
- package/lib/components/CartFooter.js +20 -0
- package/lib/components/CartItemRow.d.ts +7 -0
- package/lib/components/CartItemRow.js +236 -0
- package/lib/components/CartPaymentSettingsInvalid.d.ts +2 -0
- package/lib/components/CartPaymentSettingsInvalid.js +21 -0
- package/lib/components/CartShipment.d.ts +6 -0
- package/lib/components/CartShipment.js +59 -0
- package/lib/components/CartTabContent.d.ts +6 -0
- package/lib/components/CartTabContent.js +20 -0
- package/lib/components/CartTabPanel.d.ts +7 -0
- package/lib/components/CartTabPanel.js +31 -0
- package/lib/components/CartTableContainer.d.ts +6 -0
- package/lib/components/CartTableContainer.js +20 -0
- package/lib/components/CartTitle.d.ts +2 -0
- package/lib/components/CartTitle.js +34 -0
- package/lib/components/CartTotalsListGroup.d.ts +3 -0
- package/lib/components/CartTotalsListGroup.js +78 -0
- package/lib/components/Collapse.d.ts +3 -0
- package/lib/components/Collapse.js +40 -0
- package/lib/components/CustomCartProductForm.d.ts +18 -0
- package/lib/components/CustomCartProductForm.js +136 -0
- package/lib/components/CustomerCheckoutMethodTabs.d.ts +6 -0
- package/lib/components/CustomerCheckoutMethodTabs.js +103 -0
- package/lib/components/DiscountInputGroup.d.ts +2 -0
- package/lib/components/DiscountInputGroup.js +301 -0
- package/lib/components/LoadingTemplate.d.ts +7 -0
- package/lib/components/LoadingTemplate.js +30 -0
- package/lib/components/NoCartItems.d.ts +3 -0
- package/lib/components/NoCartItems.js +27 -0
- package/lib/components/ShipmentGroupCard.d.ts +7 -0
- package/lib/components/ShipmentGroupCard.js +215 -0
- package/lib/components/Table.d.ts +7 -0
- package/lib/components/Table.js +19 -0
- package/lib/components/containers.d.ts +14 -0
- package/lib/components/containers.js +40 -0
- package/lib/components/index.d.ts +18 -0
- package/lib/components/index.js +133 -0
- package/lib/components/product-form-sdk-externals.d.ts +46 -0
- package/lib/components/product-form-sdk-externals.js +43 -0
- package/lib/forms/FormNewCustomer.d.ts +4 -0
- package/lib/forms/FormNewCustomer.js +252 -0
- package/lib/forms/FormReturningCustomer.d.ts +3 -0
- package/lib/forms/FormReturningCustomer.js +173 -0
- package/lib/forms/FormShipmentAddressAndNotes.d.ts +15 -0
- package/lib/forms/FormShipmentAddressAndNotes.js +203 -0
- package/lib/forms/FormSquarePayment.d.ts +3 -0
- package/lib/forms/FormSquarePayment.js +27 -0
- package/lib/forms/FormStripePayment.d.ts +3 -0
- package/lib/forms/FormStripePayment.js +27 -0
- package/lib/forms/FormWhatsappCustomer.d.ts +3 -0
- package/lib/forms/FormWhatsappCustomer.js +261 -0
- package/lib/forms/InputAcceptUserTermsAndConditions.d.ts +7 -0
- package/lib/forms/InputAcceptUserTermsAndConditions.js +41 -0
- package/lib/forms/InputError.d.ts +8 -0
- package/lib/forms/InputError.js +24 -0
- package/lib/forms/InputSelect.d.ts +14 -0
- package/lib/forms/InputSelect.js +55 -0
- package/lib/forms/InputText.d.ts +12 -0
- package/lib/forms/InputText.js +63 -0
- package/lib/forms/InputsAddress.d.ts +11 -0
- package/lib/forms/InputsAddress.js +485 -0
- package/lib/forms/index.d.ts +8 -0
- package/lib/forms/index.js +63 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +93 -0
- package/lib/modals.d.ts +17 -0
- package/lib/modals.js +59 -0
- package/lib/panels/PanelCartItems.d.ts +3 -0
- package/lib/panels/PanelCartItems.js +56 -0
- package/lib/panels/PanelCartShipment.d.ts +3 -0
- package/lib/panels/PanelCartShipment.js +72 -0
- package/lib/panels/PanelClearCart.d.ts +3 -0
- package/lib/panels/PanelClearCart.js +62 -0
- package/lib/panels/PanelClientCheckout.d.ts +3 -0
- package/lib/panels/PanelClientCheckout.js +88 -0
- package/lib/panels/PanelEditCartItem.d.ts +6 -0
- package/lib/panels/PanelEditCartItem.js +206 -0
- package/lib/panels/PanelPaymentSuccess.d.ts +3 -0
- package/lib/panels/PanelPaymentSuccess.js +69 -0
- package/lib/panels/index.d.ts +7 -0
- package/lib/panels/index.js +48 -0
- package/lib/square/SquareCard.d.ts +3 -0
- package/lib/square/SquareCard.js +114 -0
- package/lib/stripe/LegacyStripeCardForm.d.ts +3 -0
- package/lib/stripe/LegacyStripeCardForm.js +166 -0
- package/lib/stripe/StripeCardFields.d.ts +15 -0
- package/lib/stripe/StripeCardFields.js +135 -0
- package/lib/stripe/StripeCardForm.d.ts +2 -0
- package/lib/stripe/StripeCardForm.js +42 -0
- package/lib/stripe/StripePaymentButtons.d.ts +8 -0
- package/lib/stripe/StripePaymentButtons.js +106 -0
- package/lib/stripe/actions.d.ts +4 -0
- package/lib/stripe/actions.js +200 -0
- package/lib/stripe/utils.d.ts +18 -0
- package/lib/stripe/utils.js +27 -0
- package/lib/styles/globals.css +463 -0
- package/lib/styles/globals.css.map +1 -0
- package/lib/tabs/CartNav.d.ts +3 -0
- package/lib/tabs/CartNav.js +36 -0
- package/lib/tabs/NavTab.d.ts +9 -0
- package/lib/tabs/NavTab.js +39 -0
- package/lib/types/index.d.ts +13 -0
- package/lib/types/index.js +1 -0
- package/lib/utilities/address.d.ts +30 -0
- package/lib/utilities/address.js +468 -0
- package/lib/utilities/area.d.ts +77 -0
- package/lib/utilities/area.js +328 -0
- package/lib/utilities/cart.d.ts +9 -0
- package/lib/utilities/cart.js +108 -0
- package/lib/utilities/company.d.ts +2 -0
- package/lib/utilities/company.js +13 -0
- package/lib/utilities/cookie.d.ts +4 -0
- package/lib/utilities/cookie.js +165 -0
- package/lib/utilities/currency.d.ts +5 -0
- package/lib/utilities/currency.js +229 -0
- package/lib/utilities/customForm.d.ts +17 -0
- package/lib/utilities/customForm.js +64 -0
- package/lib/utilities/helpers.d.ts +24 -0
- package/lib/utilities/helpers.js +240 -0
- package/lib/utilities/index.d.ts +14 -0
- package/lib/utilities/index.js +159 -0
- package/lib/utilities/invoice.d.ts +3 -0
- package/lib/utilities/invoice.js +118 -0
- package/lib/utilities/job.d.ts +1 -0
- package/lib/utilities/job.js +15 -0
- package/lib/utilities/local_storage.d.ts +31 -0
- package/lib/utilities/local_storage.js +96 -0
- package/lib/utilities/product.d.ts +2 -0
- package/lib/utilities/product.js +21 -0
- package/lib/utilities/shipment.d.ts +2 -0
- package/lib/utilities/shipment.js +18 -0
- package/lib/utilities/tabs.d.ts +14 -0
- package/lib/utilities/tabs.js +60 -0
- package/lib/utilities/user.d.ts +5 -0
- package/lib/utilities/user.js +75 -0
- package/lib/utilities/variations.d.ts +18 -0
- package/lib/utilities/variations.js +66 -0
- package/package.json +109 -0
- package/src/Cart.tsx +96 -0
- package/src/CartAlert.tsx +35 -0
- package/src/CartHeader.tsx +28 -0
- package/src/CartProvider.tsx +992 -0
- package/src/CartTotals.tsx +102 -0
- package/src/assets/merchi-monster-loader.gif +0 -0
- package/src/assets/merchi-swipe-outline.png +0 -0
- package/src/assets/product-not-found.png +0 -0
- package/src/assets/stripe-payment-secure.png +0 -0
- package/src/buttons/Button.tsx +35 -0
- package/src/buttons/ButtonBack.tsx +42 -0
- package/src/buttons/ButtonClearCart.tsx +17 -0
- package/src/buttons/ButtonInvoiceDownload.tsx +60 -0
- package/src/buttons/ButtonNext.tsx +35 -0
- package/src/buttons/ButtonNextDynamic.tsx +31 -0
- package/src/buttons/ButtonOpenCartMerchiCartModal.tsx +61 -0
- package/src/buttons/ButtonPay.tsx +32 -0
- package/src/buttons/ButtonShipmentTabNext.tsx +24 -0
- package/src/buttons/index.ts +15 -0
- package/src/components/Alert.tsx +31 -0
- package/src/components/CartBody.tsx +16 -0
- package/src/components/CartClient.tsx +52 -0
- package/src/components/CartFooter.tsx +15 -0
- package/src/components/CartItemRow.tsx +212 -0
- package/src/components/CartPaymentSettingsInvalid.tsx +12 -0
- package/src/components/CartShipment.tsx +56 -0
- package/src/components/CartTabContent.tsx +15 -0
- package/src/components/CartTabPanel.tsx +25 -0
- package/src/components/CartTableContainer.tsx +15 -0
- package/src/components/CartTitle.tsx +22 -0
- package/src/components/CartTotalsListGroup.tsx +58 -0
- package/src/components/Collapse.tsx +23 -0
- package/src/components/CustomCartProductForm.tsx +123 -0
- package/src/components/CustomerCheckoutMethodTabs.tsx +106 -0
- package/src/components/DiscountInputGroup.tsx +160 -0
- package/src/components/LoadingTemplate.tsx +22 -0
- package/src/components/NoCartItems.tsx +20 -0
- package/src/components/ShipmentGroupCard.tsx +170 -0
- package/src/components/Table.tsx +15 -0
- package/src/components/containers.tsx +40 -0
- package/src/components/index.ts +38 -0
- package/src/components/product-form-sdk-externals.ts +67 -0
- package/src/forms/FormNewCustomer.tsx +170 -0
- package/src/forms/FormReturningCustomer.tsx +117 -0
- package/src/forms/FormShipmentAddressAndNotes.tsx +165 -0
- package/src/forms/FormSquarePayment.tsx +19 -0
- package/src/forms/FormStripePayment.tsx +19 -0
- package/src/forms/FormWhatsappCustomer.tsx +179 -0
- package/src/forms/InputAcceptUserTermsAndConditions.tsx +43 -0
- package/src/forms/InputError.tsx +23 -0
- package/src/forms/InputSelect.tsx +58 -0
- package/src/forms/InputText.tsx +56 -0
- package/src/forms/InputsAddress.tsx +438 -0
- package/src/forms/index.ts +18 -0
- package/src/index.ts +31 -0
- package/src/modals.tsx +65 -0
- package/src/panels/PanelCartItems.tsx +56 -0
- package/src/panels/PanelCartShipment.tsx +86 -0
- package/src/panels/PanelClearCart.tsx +50 -0
- package/src/panels/PanelClientCheckout.tsx +102 -0
- package/src/panels/PanelEditCartItem.tsx +172 -0
- package/src/panels/PanelPaymentSuccess.tsx +66 -0
- package/src/panels/index.ts +15 -0
- package/src/square/SquareCard.tsx +84 -0
- package/src/stripe/LegacyStripeCardForm.tsx +91 -0
- package/src/stripe/StripeCardFields.tsx +173 -0
- package/src/stripe/StripeCardForm.tsx +18 -0
- package/src/stripe/StripePaymentButtons.tsx +117 -0
- package/src/stripe/actions.ts +94 -0
- package/src/stripe/utils.ts +22 -0
- package/src/styles/globals.scss +475 -0
- package/src/tabs/CartNav.tsx +18 -0
- package/src/tabs/NavTab.tsx +36 -0
- package/src/types/assets.d.ts +4 -0
- package/src/types/index.ts +15 -0
- package/src/types/merchi_product_form/index.d.ts +1 -0
- package/src/types/merchi_sdk_ts/index.d.ts +1 -0
- package/src/types/react-redux/index.d.ts +0 -0
- package/src/utilities/address.ts +463 -0
- package/src/utilities/area.ts +359 -0
- package/src/utilities/cart.ts +80 -0
- package/src/utilities/company.ts +8 -0
- package/src/utilities/cookie.ts +58 -0
- package/src/utilities/currency.ts +216 -0
- package/src/utilities/customForm.ts +71 -0
- package/src/utilities/helpers.ts +213 -0
- package/src/utilities/index.ts +14 -0
- package/src/utilities/invoice.ts +60 -0
- package/src/utilities/job.ts +13 -0
- package/src/utilities/local_storage.ts +130 -0
- package/src/utilities/product.ts +14 -0
- package/src/utilities/shipment.ts +11 -0
- package/src/utilities/tabs.ts +38 -0
- package/src/utilities/user.ts +33 -0
- package/src/utilities/variations.ts +80 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { ButtonPay } from '../buttons';
|
|
4
|
+
import FormStripeCardFields from './StripeCardFields';
|
|
5
|
+
import {
|
|
6
|
+
stripeCardFormSubmit,
|
|
7
|
+
stripeInitPromise,
|
|
8
|
+
stripePaymentButtonSubmit,
|
|
9
|
+
} from './actions';
|
|
10
|
+
import { PaymentRequestPaymentMethodEvent } from '@stripe/stripe-js';
|
|
11
|
+
import { companyStripePubKeyOrTestPubKey } from './utils';
|
|
12
|
+
import { useCartContext } from '../CartProvider';
|
|
13
|
+
import { tabIdPaymentSuccess } from '../utilities/tabs';
|
|
14
|
+
|
|
15
|
+
function PaymentButton({ loading }: any) {
|
|
16
|
+
const { cart } = useCartContext();
|
|
17
|
+
return (
|
|
18
|
+
<div>
|
|
19
|
+
<ButtonPay cart={cart} loading={loading} />
|
|
20
|
+
</div>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const badgeTestMode = <div style={{color: 'red'}}>Test mode</div>;
|
|
25
|
+
|
|
26
|
+
function StripeCardForm() {
|
|
27
|
+
const { alertError, apiUrl, cart, setActiveTabIndex, setInvoiceJson } = useCartContext();
|
|
28
|
+
const { domain } = cart;
|
|
29
|
+
const [error, setError] = useState(({} as any));
|
|
30
|
+
const company = domain.company;
|
|
31
|
+
const hasCompanyPubKey = Boolean(company.isStripeValid && companyStripePubKeyOrTestPubKey(company));
|
|
32
|
+
const companyPubKey = hasCompanyPubKey ? companyStripePubKeyOrTestPubKey(company) : '';
|
|
33
|
+
const canUseConnect = !!company.stripeAccountId;
|
|
34
|
+
const [loadingStripePayment, setLoadingStripePayment] = useState(false);
|
|
35
|
+
const [loadingStripePaymentButtons, setLoadingStripePaymentButtons] = useState(false);
|
|
36
|
+
const url: string = apiUrl || '';
|
|
37
|
+
function callbackCreditCardPaymentSuccess(invoiceJson: any) {
|
|
38
|
+
setInvoiceJson(invoiceJson);
|
|
39
|
+
setActiveTabIndex(tabIdPaymentSuccess);
|
|
40
|
+
}
|
|
41
|
+
async function doStripePayment(r: any) {
|
|
42
|
+
setLoadingStripePayment(true);
|
|
43
|
+
setError({});
|
|
44
|
+
try {
|
|
45
|
+
const response = await stripeCardFormSubmit(url, r.stripe, r.card, cart);
|
|
46
|
+
callbackCreditCardPaymentSuccess(response);
|
|
47
|
+
setLoadingStripePayment(false);
|
|
48
|
+
} catch (e: any) {
|
|
49
|
+
setLoadingStripePayment(false);
|
|
50
|
+
alertError(e.message);
|
|
51
|
+
setError({message: e.errorMessage || e.message || 'Unable to process card.'});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function doStripePaymentRequestForButton(
|
|
55
|
+
stripe: any, event: PaymentRequestPaymentMethodEvent
|
|
56
|
+
) {
|
|
57
|
+
setLoadingStripePaymentButtons(true);
|
|
58
|
+
setError({});
|
|
59
|
+
try {
|
|
60
|
+
const response = await stripePaymentButtonSubmit(url, stripe, cart, event);
|
|
61
|
+
callbackCreditCardPaymentSuccess(response);
|
|
62
|
+
setLoadingStripePaymentButtons(false);
|
|
63
|
+
} catch (e: any) {
|
|
64
|
+
setLoadingStripePaymentButtons(false);
|
|
65
|
+
alertError(e.message);
|
|
66
|
+
setError({message: e.errorMessage || e.message || 'Unable to process card.'});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const [stripePublicKey, setStripePublicKey] = useState(companyPubKey);
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
if (!stripePublicKey && canUseConnect) stripeInitPromise(url).then(setStripePublicKey)
|
|
72
|
+
}, [stripePublicKey, canUseConnect]);
|
|
73
|
+
return (
|
|
74
|
+
<>
|
|
75
|
+
{company.isTesting && badgeTestMode}
|
|
76
|
+
<FormStripeCardFields
|
|
77
|
+
doStripePayment={doStripePayment}
|
|
78
|
+
doStripePaymentRequestForButton={doStripePaymentRequestForButton}
|
|
79
|
+
cart={cart}
|
|
80
|
+
error={error}
|
|
81
|
+
loadingStripePayment={loadingStripePayment}
|
|
82
|
+
loadingStripePaymentButtons={loadingStripePaymentButtons}
|
|
83
|
+
PaymentButton={PaymentButton}
|
|
84
|
+
setLoadingStripePayment={setLoadingStripePayment}
|
|
85
|
+
stripePubKey={stripePublicKey}
|
|
86
|
+
/>
|
|
87
|
+
</>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export default StripeCardForm;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import StripePaymentButtons from "./StripePaymentButtons";
|
|
3
|
+
import {
|
|
4
|
+
CardElement,
|
|
5
|
+
Elements,
|
|
6
|
+
useStripe,
|
|
7
|
+
useElements,
|
|
8
|
+
} from "@stripe/react-stripe-js";
|
|
9
|
+
import {
|
|
10
|
+
loadStripe,
|
|
11
|
+
PaymentRequestPaymentMethodEvent,
|
|
12
|
+
} from "@stripe/stripe-js";
|
|
13
|
+
import { CARD_ELEMENT_OPTIONS } from "./utils";
|
|
14
|
+
import pngStripe from "../assets/stripe-payment-secure.png";
|
|
15
|
+
|
|
16
|
+
type DoStripePaymentRequestForButton = (
|
|
17
|
+
stripe: any,
|
|
18
|
+
event: PaymentRequestPaymentMethodEvent
|
|
19
|
+
) => void;
|
|
20
|
+
|
|
21
|
+
interface FormProps {
|
|
22
|
+
doStripePayment: (values: any) => void;
|
|
23
|
+
doStripePaymentRequestForButton?: DoStripePaymentRequestForButton;
|
|
24
|
+
cart: any;
|
|
25
|
+
error: any;
|
|
26
|
+
loadingStripePayment: boolean;
|
|
27
|
+
loadingStripePaymentButtons: boolean;
|
|
28
|
+
PaymentButton?: any;
|
|
29
|
+
setLoadingStripePayment: (loading: boolean) => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function InnerForm({
|
|
33
|
+
doStripePayment,
|
|
34
|
+
doStripePaymentRequestForButton,
|
|
35
|
+
cart,
|
|
36
|
+
error,
|
|
37
|
+
loadingStripePayment,
|
|
38
|
+
loadingStripePaymentButtons,
|
|
39
|
+
PaymentButton,
|
|
40
|
+
setLoadingStripePayment,
|
|
41
|
+
}: FormProps) {
|
|
42
|
+
const inputClass = "form-control p-t-8 stripe-form-control";
|
|
43
|
+
const stripe = useStripe();
|
|
44
|
+
const elements = useElements();
|
|
45
|
+
function paymentStart(e: any) {
|
|
46
|
+
const card = (elements as any).getElement(CardElement) as any;
|
|
47
|
+
setLoadingStripePayment(true);
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
if (!stripe || !elements) {
|
|
50
|
+
setLoadingStripePayment(false);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
doStripePayment({ card, cart, stripe });
|
|
54
|
+
}
|
|
55
|
+
return (
|
|
56
|
+
<>
|
|
57
|
+
{loadingStripePaymentButtons ? (
|
|
58
|
+
<div style={{ alignItems: "center" }}>
|
|
59
|
+
<div
|
|
60
|
+
style={{
|
|
61
|
+
alignItems: "center",
|
|
62
|
+
display: "flex",
|
|
63
|
+
justifyContent: "center",
|
|
64
|
+
marginBottom: "1rem",
|
|
65
|
+
textAlign: "center",
|
|
66
|
+
}}
|
|
67
|
+
>
|
|
68
|
+
<div className="spinner-merchi-small" />
|
|
69
|
+
</div>
|
|
70
|
+
<p style={{ fontSize: "14px", fontWeight: 600, textAlign: "center" }}>
|
|
71
|
+
Processing payment
|
|
72
|
+
</p>
|
|
73
|
+
</div>
|
|
74
|
+
) : (
|
|
75
|
+
<>
|
|
76
|
+
{!!(cart && doStripePaymentRequestForButton) && (
|
|
77
|
+
<StripePaymentButtons
|
|
78
|
+
doPaymentRequest={(event: PaymentRequestPaymentMethodEvent) =>
|
|
79
|
+
(
|
|
80
|
+
doStripePaymentRequestForButton as DoStripePaymentRequestForButton
|
|
81
|
+
)(stripe, event)
|
|
82
|
+
}
|
|
83
|
+
cart={cart}
|
|
84
|
+
/>
|
|
85
|
+
)}
|
|
86
|
+
<form onSubmit={paymentStart}>
|
|
87
|
+
<div className="merchi-cart-stripe-input-container">
|
|
88
|
+
<CardElement
|
|
89
|
+
className={inputClass}
|
|
90
|
+
options={CARD_ELEMENT_OPTIONS}
|
|
91
|
+
/>
|
|
92
|
+
{error.message && (
|
|
93
|
+
<div className="merchi-cart-stripe-input-error">
|
|
94
|
+
{error.message}
|
|
95
|
+
</div>
|
|
96
|
+
)}
|
|
97
|
+
<PaymentButton loading={loadingStripePayment} />
|
|
98
|
+
{pngStripe && (
|
|
99
|
+
<img
|
|
100
|
+
src={
|
|
101
|
+
(pngStripe && "src" in pngStripe
|
|
102
|
+
? pngStripe.src
|
|
103
|
+
: pngStripe) || ""
|
|
104
|
+
}
|
|
105
|
+
width={250}
|
|
106
|
+
alt="Secure credit card payments by Stripe"
|
|
107
|
+
/>
|
|
108
|
+
)}
|
|
109
|
+
</div>
|
|
110
|
+
</form>
|
|
111
|
+
</>
|
|
112
|
+
)}
|
|
113
|
+
</>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface Props {
|
|
118
|
+
doStripePayment: (values: any) => void;
|
|
119
|
+
doStripePaymentRequestForButton?: (
|
|
120
|
+
stripe: any,
|
|
121
|
+
event: PaymentRequestPaymentMethodEvent
|
|
122
|
+
) => void;
|
|
123
|
+
cart: any;
|
|
124
|
+
error: any;
|
|
125
|
+
loadingStripePayment: boolean;
|
|
126
|
+
loadingStripePaymentButtons: boolean;
|
|
127
|
+
PaymentButton?: any;
|
|
128
|
+
setLoadingStripePayment: (loading: boolean) => void;
|
|
129
|
+
stripePubKey: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function FormStripeCardFields({
|
|
133
|
+
doStripePayment,
|
|
134
|
+
doStripePaymentRequestForButton,
|
|
135
|
+
cart,
|
|
136
|
+
error,
|
|
137
|
+
loadingStripePayment,
|
|
138
|
+
loadingStripePaymentButtons,
|
|
139
|
+
PaymentButton,
|
|
140
|
+
setLoadingStripePayment,
|
|
141
|
+
stripePubKey,
|
|
142
|
+
}: Props) {
|
|
143
|
+
return stripePubKey ? (
|
|
144
|
+
<Elements stripe={loadStripe(stripePubKey)}>
|
|
145
|
+
<InnerForm
|
|
146
|
+
doStripePayment={doStripePayment}
|
|
147
|
+
doStripePaymentRequestForButton={doStripePaymentRequestForButton}
|
|
148
|
+
cart={cart}
|
|
149
|
+
error={error}
|
|
150
|
+
loadingStripePayment={loadingStripePayment}
|
|
151
|
+
loadingStripePaymentButtons={loadingStripePaymentButtons}
|
|
152
|
+
setLoadingStripePayment={setLoadingStripePayment}
|
|
153
|
+
PaymentButton={PaymentButton}
|
|
154
|
+
/>
|
|
155
|
+
</Elements>
|
|
156
|
+
) : (
|
|
157
|
+
<div style={{ alignItems: "center" }}>
|
|
158
|
+
<div
|
|
159
|
+
style={{
|
|
160
|
+
alignItems: "center",
|
|
161
|
+
display: "flex",
|
|
162
|
+
justifyContent: "center",
|
|
163
|
+
marginBottom: "1rem",
|
|
164
|
+
textAlign: "center",
|
|
165
|
+
}}
|
|
166
|
+
>
|
|
167
|
+
<div className="spinner-merchi-small" />
|
|
168
|
+
</div>
|
|
169
|
+
</div>
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export default FormStripeCardFields;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { StripePaymentForm, StripePaymentGate } from '@merchi/invoice';
|
|
3
|
+
import LegacyStripeCardForm from './LegacyStripeCardForm';
|
|
4
|
+
import { useCartContext } from '../CartProvider';
|
|
5
|
+
import { tabIdPaymentSuccess } from '../utilities/tabs';
|
|
6
|
+
|
|
7
|
+
export default function StripeCardForm() {
|
|
8
|
+
const { alertError, apiUrl, cart, setActiveTabIndex, setInvoiceJson } = useCartContext();
|
|
9
|
+
return <StripePaymentGate engine={cart.stripePaymentEngine} apiUrl={apiUrl || 'https://api.merchi.co/v6/'} resource="cart" resourceId={cart.id}
|
|
10
|
+
resourceToken={cart.token || cart.cartToken} legacy={<LegacyStripeCardForm />}>
|
|
11
|
+
<StripePaymentForm apiUrl={apiUrl || 'https://api.merchi.co/v6/'} resource="cart"
|
|
12
|
+
resourceId={cart.id} resourceToken={cart.token || cart.cartToken} onError={alertError}
|
|
13
|
+
onSuccess={invoice => {
|
|
14
|
+
setInvoiceJson(invoice);
|
|
15
|
+
setActiveTabIndex(tabIdPaymentSuccess);
|
|
16
|
+
}} />
|
|
17
|
+
</StripePaymentGate>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
CanMakePaymentResult,
|
|
5
|
+
PaymentRequest,
|
|
6
|
+
PaymentRequestOptions,
|
|
7
|
+
PaymentRequestPaymentMethodEvent,
|
|
8
|
+
StripePaymentRequestButtonElementOptions
|
|
9
|
+
} from '@stripe/stripe-js';
|
|
10
|
+
import {
|
|
11
|
+
PaymentRequestButtonElement,
|
|
12
|
+
useStripe,
|
|
13
|
+
} from '@stripe/react-stripe-js';
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
function useOptions(paymentRequest: PaymentRequest) {
|
|
17
|
+
const options = useMemo(
|
|
18
|
+
() => ({
|
|
19
|
+
paymentRequest,
|
|
20
|
+
style: {
|
|
21
|
+
paymentRequestButton: {
|
|
22
|
+
theme: 'dark',
|
|
23
|
+
height: '48px',
|
|
24
|
+
type: 'buy'
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}),
|
|
28
|
+
[paymentRequest]
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return options;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface UsePaymentRequest {
|
|
35
|
+
onPaymentMethod: (payload: any) => void;
|
|
36
|
+
options: PaymentRequestOptions;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function usePaymentRequest({ options, onPaymentMethod }: UsePaymentRequest) {
|
|
40
|
+
const stripe = useStripe();
|
|
41
|
+
const [paymentRequest, setPaymentRequest] = useState((null as unknown));
|
|
42
|
+
const [canMakePayment, setCanMakePayment] = useState(false);
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (stripe && paymentRequest === null) {
|
|
46
|
+
const pr = stripe.paymentRequest(options);
|
|
47
|
+
setPaymentRequest((pr as any));
|
|
48
|
+
}
|
|
49
|
+
}, [stripe, options, paymentRequest]);
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
let subscribed = true;
|
|
53
|
+
if (paymentRequest) {
|
|
54
|
+
(paymentRequest as PaymentRequest).canMakePayment().then(
|
|
55
|
+
(res: CanMakePaymentResult | null) => {
|
|
56
|
+
if (res && subscribed) {
|
|
57
|
+
setCanMakePayment(true);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return () => {
|
|
63
|
+
subscribed = false;
|
|
64
|
+
};
|
|
65
|
+
}, [paymentRequest]);
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (paymentRequest) {
|
|
69
|
+
(paymentRequest as any).on('paymentmethod', onPaymentMethod);
|
|
70
|
+
}
|
|
71
|
+
return () => {
|
|
72
|
+
if (paymentRequest) {
|
|
73
|
+
(paymentRequest as any).off('paymentmethod', onPaymentMethod);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}, [paymentRequest, onPaymentMethod]);
|
|
77
|
+
|
|
78
|
+
return canMakePayment ? paymentRequest : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface PropsStripePaymentButtons {
|
|
82
|
+
doPaymentRequest: (event: PaymentRequestPaymentMethodEvent) => void;
|
|
83
|
+
cart: any;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function StripePaymentButtons(
|
|
87
|
+
{
|
|
88
|
+
doPaymentRequest,
|
|
89
|
+
cart,
|
|
90
|
+
}: PropsStripePaymentButtons) {
|
|
91
|
+
const paymentRequest: unknown = usePaymentRequest({
|
|
92
|
+
options: {
|
|
93
|
+
country: cart.domain.company.country,
|
|
94
|
+
currency: String(cart.currency).toLowerCase(),
|
|
95
|
+
total: {
|
|
96
|
+
label: `Merchi Cart #${cart.id}`,
|
|
97
|
+
amount: Math.round(cart.totalCost * 100)
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
onPaymentMethod: doPaymentRequest
|
|
101
|
+
});
|
|
102
|
+
const options = useOptions((paymentRequest as PaymentRequest));
|
|
103
|
+
|
|
104
|
+
if (!paymentRequest) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return (
|
|
108
|
+
<div style={{marginBottom: '2rem', marginTop: '2rem'}}>
|
|
109
|
+
<PaymentRequestButtonElement
|
|
110
|
+
className='PaymentRequestButton'
|
|
111
|
+
options={(options as StripePaymentRequestButtonElementOptions)}
|
|
112
|
+
/>
|
|
113
|
+
</div>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export default StripePaymentButtons;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { PaymentRequestPaymentMethodEvent } from '@stripe/stripe-js';
|
|
2
|
+
|
|
3
|
+
export async function stripeInitPromise(apiUrl: string) {
|
|
4
|
+
return await fetch(
|
|
5
|
+
`${apiUrl}stripe/master_publishable_key/`,
|
|
6
|
+
{ method: 'GET', mode: 'cors' }
|
|
7
|
+
).then((response: any) => response.key);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async function createPaymentIntent(apiUrl: string, cart: any, paymentMethodType?: string) {
|
|
11
|
+
const { id, token } = cart;
|
|
12
|
+
const fetchOptions: any = {
|
|
13
|
+
method: 'GET',
|
|
14
|
+
mode: 'cors',
|
|
15
|
+
};
|
|
16
|
+
const queryParams: any[] = [['cart_token', token]];
|
|
17
|
+
|
|
18
|
+
if (paymentMethodType) {
|
|
19
|
+
queryParams.push(['payment_method_types', paymentMethodType]);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const queryString = new URLSearchParams(queryParams).toString();
|
|
23
|
+
const uri = `stripe/payment_intent/cart/${id}/`
|
|
24
|
+
const url = `${apiUrl}${uri}?${queryString}`;
|
|
25
|
+
|
|
26
|
+
const response = await fetch(url, fetchOptions);
|
|
27
|
+
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
throw new Error("Failed to create payment intent.");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return await response.json();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function checkPaymentIntnetIsComplete(apiUrl: string, cart: any) {
|
|
36
|
+
const url = `stripe/payment_intent/cart/complete/${cart.id}/`;
|
|
37
|
+
const fetchOptions: any = {
|
|
38
|
+
method: 'GET',
|
|
39
|
+
mode: 'cors',
|
|
40
|
+
};
|
|
41
|
+
const queryString = new URLSearchParams({cart_token: cart.token}).toString();
|
|
42
|
+
|
|
43
|
+
const response = await fetch(`${apiUrl}${url}?${queryString}`, fetchOptions);
|
|
44
|
+
|
|
45
|
+
if (!response.ok) {
|
|
46
|
+
throw new Error("Failed to check if payment intent is complete.");
|
|
47
|
+
}
|
|
48
|
+
const i = await response.json();
|
|
49
|
+
return i.invoice;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function stripeCardFormSubmit(apiUrl: string, stripe: any, card: any, cart: any) {
|
|
53
|
+
try {
|
|
54
|
+
const intent = await createPaymentIntent(apiUrl, cart);
|
|
55
|
+
const stripePayment = await stripe.confirmCardPayment(intent.stripeClientSecret, { payment_method: { card } });
|
|
56
|
+
|
|
57
|
+
if (stripePayment.error) {
|
|
58
|
+
throw new Error(stripePayment.error.message || 'Stripe connect unknown error');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return await checkPaymentIntnetIsComplete(apiUrl, cart);
|
|
62
|
+
} catch (e) {
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function stripePaymentButtonSubmit(
|
|
68
|
+
apiUrl: string,
|
|
69
|
+
stripe: any,
|
|
70
|
+
cart: any,
|
|
71
|
+
paymentMethodEvent: PaymentRequestPaymentMethodEvent
|
|
72
|
+
) {
|
|
73
|
+
const { complete, paymentMethod } = paymentMethodEvent;
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const intent = await createPaymentIntent(apiUrl, cart);
|
|
77
|
+
const stripePayment = await stripe.confirmCardPayment(
|
|
78
|
+
intent.stripeClientSecret,
|
|
79
|
+
{ payment_method: paymentMethod.id },
|
|
80
|
+
{ handleActions: false }
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
if (stripePayment.error) {
|
|
84
|
+
throw new Error(stripePayment.error.message || 'Stripe unknown error');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
complete('success');
|
|
88
|
+
return await checkPaymentIntnetIsComplete(apiUrl, cart);
|
|
89
|
+
|
|
90
|
+
} catch (e) {
|
|
91
|
+
complete('fail');
|
|
92
|
+
throw e;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function companyStripePubKeyOrTestPubKey(company: any) {
|
|
2
|
+
return company.isTesting ? company.stripePublishableTestKey :
|
|
3
|
+
company.stripePublishableKey;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export const CARD_ELEMENT_OPTIONS = {
|
|
7
|
+
hidePostalCode: true,
|
|
8
|
+
style: {
|
|
9
|
+
base: {
|
|
10
|
+
'::placeholder': {
|
|
11
|
+
color: '#999',
|
|
12
|
+
fontSize: '16px',
|
|
13
|
+
},
|
|
14
|
+
fontSize: '16px',
|
|
15
|
+
lineHeight: '1.33',
|
|
16
|
+
paddingTop: '4px',
|
|
17
|
+
},
|
|
18
|
+
invalid: {
|
|
19
|
+
color: '#9e2146',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|