@tagadapay/plugin-sdk 2.3.12 → 2.3.14

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.
@@ -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
- const response = await apiService.fetch(`/api/v1/checkout-sessions/${options.checkoutSessionId}/order-summary`, {
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
- return response;
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 / 100).toFixed(2),
126
+ type: 'final',
127
+ },
128
+ {
129
+ label: 'Shipping',
130
+ amount: (orderSummary.shippingCost / 100).toFixed(2),
131
+ type: 'final',
132
+ },
133
+ {
134
+ label: 'Tax',
135
+ amount: (orderSummary.totalTaxAmount / 100).toFixed(2),
136
+ type: 'final',
137
+ },
138
+ ];
139
+ // Construct Apple Pay total
140
+ const total = {
141
+ label: 'Total',
142
+ amount: (orderSummary.totalAdjustedAmount / 100).toFixed(2),
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 / 100).toFixed(2),
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 {
@@ -302,6 +341,7 @@ export function useApplePay(options = {}) {
302
341
  }
303
342
  const { lineItems: newLineItems, total: newTotal, shippingMethods: newShippingMethods, } = newOrderSummary;
304
343
  console.log('======= APPLE PAY ON SHIPPING CONTACT SELECTED ======');
344
+ console.log('newOrderSummary', newOrderSummary);
305
345
  console.log(newLineItems, newTotal, newShippingMethods);
306
346
  session.completeShippingContactSelection(window.ApplePaySession.STATUS_SUCCESS, newShippingMethods, newTotal, newLineItems);
307
347
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "2.3.12",
3
+ "version": "2.3.14",
4
4
  "description": "Modern React SDK for building Tagada Pay plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",