@tagadapay/plugin-sdk 2.3.13 → 2.4.0
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/react/components/ApplePayButton.d.ts +16 -0
- package/dist/react/components/ApplePayButton.js +53 -0
- package/dist/react/components/Button.d.ts +8 -0
- package/dist/react/components/Button.js +18 -0
- package/dist/react/components/index.d.ts +2 -0
- package/dist/react/components/index.js +2 -0
- package/dist/react/hooks/useApplePay.d.ts +7 -1
- package/dist/react/hooks/useApplePay.js +53 -14
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { CheckoutData } from '../hooks/useCheckout';
|
|
3
|
+
interface ApplePayButtonProps {
|
|
4
|
+
className?: string;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
onSuccess?: (payment: any) => void;
|
|
7
|
+
onError?: (error: string) => void;
|
|
8
|
+
onCancel?: () => void;
|
|
9
|
+
storeName?: string;
|
|
10
|
+
currencyCode?: string;
|
|
11
|
+
variant?: 'default' | 'outline' | 'ghost';
|
|
12
|
+
size?: 'sm' | 'md' | 'lg';
|
|
13
|
+
checkout: CheckoutData | null;
|
|
14
|
+
}
|
|
15
|
+
export declare const ApplePayButton: React.FC<ApplePayButtonProps>;
|
|
16
|
+
export default ApplePayButton;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useApplePay } from '../hooks/useApplePay';
|
|
3
|
+
import { Button } from './Button';
|
|
4
|
+
export const ApplePayButton = ({ className = '', disabled = false, onSuccess, onError, onCancel, storeName, currencyCode = 'USD', variant = 'outline', size = 'lg', checkout, }) => {
|
|
5
|
+
// Use the Apple Pay hook
|
|
6
|
+
const { handleApplePayClick, processingPayment, applePayError, isApplePayAvailable } = useApplePay({
|
|
7
|
+
onSuccess,
|
|
8
|
+
onError,
|
|
9
|
+
onCancel,
|
|
10
|
+
checkoutSessionId: checkout?.checkoutSession?.id,
|
|
11
|
+
customerId: checkout?.checkoutSession?.customerId,
|
|
12
|
+
});
|
|
13
|
+
// Apple Pay button click handler
|
|
14
|
+
const handleClick = () => {
|
|
15
|
+
if (processingPayment || !isApplePayAvailable || !checkout)
|
|
16
|
+
return;
|
|
17
|
+
const { summary, checkoutSession } = checkout;
|
|
18
|
+
// Build line items from checkout summary
|
|
19
|
+
const lineItems = summary.items?.map((item) => ({
|
|
20
|
+
label: item.product?.name || item.variant?.name || 'Item',
|
|
21
|
+
amount: (item.adjustedAmount / 100).toFixed(2), // Convert from minor units
|
|
22
|
+
type: 'final',
|
|
23
|
+
})) || [];
|
|
24
|
+
// Add shipping as a line item if present
|
|
25
|
+
if (checkoutSession.shippingRate && checkoutSession.shippingRate.amount > 0) {
|
|
26
|
+
lineItems.push({
|
|
27
|
+
label: checkoutSession.shippingRate.shippingRateName || 'Shipping',
|
|
28
|
+
amount: (checkoutSession.shippingRate.amount / 100).toFixed(2),
|
|
29
|
+
type: 'final',
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
// Build total
|
|
33
|
+
const total = {
|
|
34
|
+
label: storeName || checkoutSession.store.name || 'Total',
|
|
35
|
+
amount: (summary.totalAdjustedAmount / 100).toFixed(2),
|
|
36
|
+
type: 'final',
|
|
37
|
+
};
|
|
38
|
+
// Apple Pay configuration
|
|
39
|
+
const config = {
|
|
40
|
+
countryCode: 'US',
|
|
41
|
+
supportedNetworks: ['visa', 'masterCard', 'amex', 'discover'],
|
|
42
|
+
merchantCapabilities: ['supports3DS'],
|
|
43
|
+
};
|
|
44
|
+
// Call the Apple Pay handler
|
|
45
|
+
handleApplePayClick(checkoutSession.id, lineItems, total, config, storeName || checkoutSession.store.name, currencyCode || summary.currency, []);
|
|
46
|
+
};
|
|
47
|
+
// Don't render the button if Apple Pay is not available
|
|
48
|
+
if (!isApplePayAvailable) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return (_jsxs("div", { className: "w-full", children: [_jsx(Button, { type: "button", variant: variant, size: size, className: `w-full shadow-sm ${className}`, onClick: handleClick, disabled: disabled || processingPayment, children: _jsxs("div", { className: "flex items-center justify-center gap-3", children: [processingPayment ? (_jsx("div", { className: "h-5 w-5 animate-spin rounded-full border-2 border-white border-t-transparent" })) : (_jsx("img", { src: "/brandnetwork/applepay.svg", alt: "Apple Pay", className: "h-6 w-auto" })), _jsx("span", { className: "font-medium", children: processingPayment ? 'Processing...' : 'Pay with Apple Pay' })] }) }), applePayError && (_jsx("div", { className: "mt-2 rounded border border-red-200 bg-red-50 p-2 text-sm text-red-600", children: applePayError }))] }));
|
|
52
|
+
};
|
|
53
|
+
export default ApplePayButton;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
3
|
+
variant?: 'default' | 'outline' | 'ghost';
|
|
4
|
+
size?: 'sm' | 'md' | 'lg';
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
export declare const Button: React.FC<ButtonProps>;
|
|
8
|
+
export default Button;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
export const Button = ({ variant = 'default', size = 'md', className = '', children, ...props }) => {
|
|
3
|
+
// Determine button size classes
|
|
4
|
+
const sizeClasses = {
|
|
5
|
+
sm: 'h-10 px-3 text-sm',
|
|
6
|
+
md: 'h-11 px-4 text-sm',
|
|
7
|
+
lg: 'h-12 px-6 text-base',
|
|
8
|
+
};
|
|
9
|
+
// Determine button variant classes
|
|
10
|
+
const variantClasses = {
|
|
11
|
+
default: 'bg-blue-600 text-white border-blue-600 hover:bg-blue-700 hover:border-blue-700',
|
|
12
|
+
outline: 'border border-gray-300 bg-white text-gray-700 hover:bg-gray-50 hover:border-gray-400',
|
|
13
|
+
ghost: 'bg-transparent text-gray-700 hover:bg-gray-100 border-transparent',
|
|
14
|
+
};
|
|
15
|
+
const baseClasses = 'inline-flex items-center justify-center rounded-md font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none';
|
|
16
|
+
return (_jsx("button", { className: `${baseClasses} ${sizeClasses[size]} ${variantClasses[variant]} ${className}`, ...props, children: children }));
|
|
17
|
+
};
|
|
18
|
+
export default Button;
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
import { UseApplePayOptions, UseApplePayResult } from '../types/apple-pay';
|
|
1
|
+
import { ApplePayLineItem, UseApplePayOptions, UseApplePayResult } from '../types/apple-pay';
|
|
2
|
+
import { OrderSummaryData } from './useOrderSummary';
|
|
3
|
+
export interface ApplePayOrderSummary extends OrderSummaryData {
|
|
4
|
+
lineItems: ApplePayLineItem[];
|
|
5
|
+
total: ApplePayLineItem;
|
|
6
|
+
shippingMethods: any[];
|
|
7
|
+
}
|
|
2
8
|
export declare function useApplePay(options?: UseApplePayOptions): UseApplePayResult;
|
|
@@ -94,34 +94,73 @@ export function useApplePay(options = {}) {
|
|
|
94
94
|
throw error;
|
|
95
95
|
}
|
|
96
96
|
}, [apiService, options.customerId]);
|
|
97
|
+
// Get shipping rates for the checkout session
|
|
98
|
+
const getShippingRates = useCallback(async () => {
|
|
99
|
+
try {
|
|
100
|
+
const response = await apiService.fetch(`/api/v1/checkout-sessions/${options.checkoutSessionId}/shipping-rates`);
|
|
101
|
+
return response;
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
console.error('Failed to get shipping rates:', error);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}, [apiService, options.checkoutSessionId]);
|
|
97
108
|
// Recompute order summary after address/shipping changes
|
|
98
109
|
const reComputeOrderSummary = useCallback(async () => {
|
|
99
110
|
try {
|
|
100
|
-
|
|
111
|
+
// Get the order summary data
|
|
112
|
+
const orderSummary = await apiService.fetch(`/api/v1/checkout-sessions/${options.checkoutSessionId}/order-summary`, {
|
|
101
113
|
method: 'POST',
|
|
102
114
|
headers: {
|
|
103
115
|
'Content-Type': 'application/json',
|
|
104
116
|
},
|
|
105
117
|
body: { checkoutSessionId: options.checkoutSessionId }
|
|
106
118
|
});
|
|
107
|
-
|
|
119
|
+
// Get shipping rates
|
|
120
|
+
const shippingRates = await getShippingRates();
|
|
121
|
+
// Construct Apple Pay line items from order summary
|
|
122
|
+
const lineItems = [
|
|
123
|
+
{
|
|
124
|
+
label: 'Subtotal',
|
|
125
|
+
amount: orderSummary.subtotalAdjustedAmount > 0 ? (orderSummary.subtotalAdjustedAmount / 100).toFixed(2) : '0.00',
|
|
126
|
+
type: 'final',
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
label: 'Shipping',
|
|
130
|
+
amount: orderSummary.shippingCost > 0 ? (orderSummary.shippingCost / 100).toFixed(2) : '0.00',
|
|
131
|
+
type: 'final',
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
label: 'Tax',
|
|
135
|
+
amount: orderSummary.totalTaxAmount > 0 ? (orderSummary.totalTaxAmount / 100).toFixed(2) : '0.00',
|
|
136
|
+
type: 'final',
|
|
137
|
+
},
|
|
138
|
+
];
|
|
139
|
+
// Construct Apple Pay total
|
|
140
|
+
const total = {
|
|
141
|
+
label: 'Total',
|
|
142
|
+
amount: orderSummary.totalAdjustedAmount > 0 ? (orderSummary.totalAdjustedAmount / 100).toFixed(2) : '0.00',
|
|
143
|
+
type: 'final',
|
|
144
|
+
};
|
|
145
|
+
// Construct Apple Pay shipping methods from shipping rates
|
|
146
|
+
const shippingMethods = shippingRates?.rates?.map((rate) => ({
|
|
147
|
+
label: rate.shippingRateName,
|
|
148
|
+
amount: rate.amount > 0 ? (rate.amount / 100).toFixed(2) : '0.00',
|
|
149
|
+
identifier: rate.id,
|
|
150
|
+
detail: rate.description || '',
|
|
151
|
+
})) || [];
|
|
152
|
+
return {
|
|
153
|
+
...orderSummary,
|
|
154
|
+
lineItems,
|
|
155
|
+
total,
|
|
156
|
+
shippingMethods,
|
|
157
|
+
};
|
|
108
158
|
}
|
|
109
159
|
catch (error) {
|
|
110
160
|
console.error('Failed to recompute order summary:', error);
|
|
111
161
|
throw error;
|
|
112
162
|
}
|
|
113
|
-
}, [apiService, options.checkoutSessionId]);
|
|
114
|
-
// Get shipping rates for the checkout session
|
|
115
|
-
const getShippingRates = useCallback(async () => {
|
|
116
|
-
try {
|
|
117
|
-
const response = await apiService.fetch(`/api/v1/checkout-sessions/${options.checkoutSessionId}/shipping-rates`);
|
|
118
|
-
return response;
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
console.error('Failed to get shipping rates:', error);
|
|
122
|
-
throw error;
|
|
123
|
-
}
|
|
124
|
-
}, [apiService, options.checkoutSessionId]);
|
|
163
|
+
}, [apiService, options.checkoutSessionId, getShippingRates]);
|
|
125
164
|
// Set shipping rate
|
|
126
165
|
const setShippingRate = useCallback(async (shippingRateId) => {
|
|
127
166
|
try {
|
package/dist/react/index.d.ts
CHANGED
|
@@ -41,4 +41,5 @@ export type { Payment, PaymentPollingHook, PollingOptions } from './hooks/usePay
|
|
|
41
41
|
export type { PaymentInstrument, ThreedsChallenge, ThreedsHook, ThreedsOptions, ThreedsProvider, ThreedsSession } from './hooks/useThreeds';
|
|
42
42
|
export type { ApplePayToken, CardPaymentMethod, PaymentHook, PaymentInstrumentResponse, PaymentOptions, PaymentResponse } from './hooks/usePayment';
|
|
43
43
|
export type { ApplePayAddress, ApplePayConfig, ApplePayLineItem, ApplePayPaymentAuthorizedEvent, ApplePayPaymentRequest, ApplePayPaymentToken, ApplePayValidateMerchantEvent, BasisTheorySessionRequest, BasisTheoryTokenizeRequest, PayToken, UseApplePayOptions, UseApplePayResult } from './types/apple-pay';
|
|
44
|
+
export { Button, ApplePayButton } from './components';
|
|
44
45
|
export { convertCurrency, formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits } from './utils/money';
|
package/dist/react/index.js
CHANGED
|
@@ -36,6 +36,6 @@ export { useThreedsModal } from './hooks/useThreedsModal';
|
|
|
36
36
|
// Apple Pay hooks exports
|
|
37
37
|
export { useApplePay } from './hooks/useApplePay';
|
|
38
38
|
// Component exports
|
|
39
|
-
|
|
39
|
+
export { Button, ApplePayButton } from './components';
|
|
40
40
|
// Utility exports
|
|
41
41
|
export { convertCurrency, formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits } from './utils/money';
|