@waffo/pancake-nextjs 0.1.8 → 0.2.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/CHANGELOG.md +21 -0
- package/README.md +56 -54
- package/dist/index.cjs +62 -48
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +92 -63
- package/dist/index.d.ts +92 -63
- package/dist/index.js +58 -48
- package/dist/index.js.map +1 -1
- package/dist/server.cjs +16 -10
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +25 -15
- package/dist/server.d.ts +25 -15
- package/dist/server.js +14 -10
- package/dist/server.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -13,61 +13,70 @@ type CheckoutActionParams = ({
|
|
|
13
13
|
type CheckoutActionResult = CheckoutSessionResult | AuthenticatedCheckoutResult;
|
|
14
14
|
/** Server action signature for checkout */
|
|
15
15
|
type CheckoutAction = (params: CheckoutActionParams) => Promise<CheckoutActionResult>;
|
|
16
|
-
/** Server action signature for issuing
|
|
17
|
-
type
|
|
18
|
-
/**
|
|
19
|
-
type
|
|
20
|
-
/** Server action signature for
|
|
21
|
-
type
|
|
16
|
+
/** Server action signature for issuing customer tokens */
|
|
17
|
+
type CustomerTokenAction = (params: IssueSessionTokenParams) => Promise<SessionToken>;
|
|
18
|
+
/** Customer action types */
|
|
19
|
+
type CustomerSessionActionType = "cancelSubscription" | "cancelOnetimeOrder" | "reactivateSubscription" | "createRefundTicket" | "resubmitRefundTicket" | "query";
|
|
20
|
+
/** Server action signature for customer session operations */
|
|
21
|
+
type CustomerSessionAction = (token: string, actionType: CustomerSessionActionType, params: unknown) => Promise<unknown>;
|
|
22
22
|
/** Server action signature for merchant GraphQL queries */
|
|
23
23
|
type MerchantQueryAction = (params: GraphQLParams) => Promise<GraphQLResponse>;
|
|
24
|
+
/** @deprecated Use {@link CustomerTokenAction} instead. */
|
|
25
|
+
type BuyerTokenAction = CustomerTokenAction;
|
|
26
|
+
/** @deprecated Use {@link CustomerSessionAction} instead. */
|
|
27
|
+
type BuyerSessionAction = CustomerSessionAction;
|
|
24
28
|
|
|
25
|
-
/**
|
|
26
|
-
interface
|
|
27
|
-
/**
|
|
29
|
+
/** Customer configuration for automatic token management */
|
|
30
|
+
interface CustomerConfig {
|
|
31
|
+
/** Customer identity (email or merchant-provided identifier) */
|
|
28
32
|
identity: string;
|
|
29
33
|
/** Store ID (optional when `productId` is provided) */
|
|
30
34
|
storeId?: string;
|
|
31
35
|
/** Product ID — used to derive the store when `storeId` is omitted */
|
|
32
36
|
productId?: string;
|
|
33
|
-
/** Server action for issuing tokens — from `
|
|
34
|
-
issueToken:
|
|
35
|
-
/** Server action for
|
|
36
|
-
sessionAction:
|
|
37
|
+
/** Server action for issuing tokens — from `createCustomerTokenAction()` */
|
|
38
|
+
issueToken: CustomerTokenAction;
|
|
39
|
+
/** Server action for customer operations — from `createCustomerSessionAction()` */
|
|
40
|
+
sessionAction: CustomerSessionAction;
|
|
37
41
|
}
|
|
42
|
+
/** @deprecated Use {@link CustomerConfig} instead. */
|
|
43
|
+
type BuyerConfig = CustomerConfig;
|
|
38
44
|
interface WaffoPancakeProviderProps {
|
|
39
|
-
/**
|
|
40
|
-
|
|
45
|
+
/** Customer configuration for automatic token management */
|
|
46
|
+
customer?: CustomerConfig;
|
|
47
|
+
/** @deprecated Use `customer` instead. */
|
|
48
|
+
buyer?: CustomerConfig;
|
|
41
49
|
children: React.ReactNode;
|
|
42
50
|
}
|
|
43
51
|
/**
|
|
44
|
-
* Provider that manages
|
|
52
|
+
* Provider that manages customer token lifecycle via server actions.
|
|
45
53
|
*
|
|
46
54
|
* Auto-issues session tokens on mount and refreshes before expiry.
|
|
47
|
-
* All
|
|
55
|
+
* All customer hooks (`useCustomer`, `useCustomerOrders`, etc.) read from context.
|
|
48
56
|
*
|
|
49
|
-
* The private key never leaves the server — token issuance and
|
|
57
|
+
* The private key never leaves the server — token issuance and customer
|
|
50
58
|
* operations are delegated to server actions.
|
|
51
59
|
*
|
|
52
60
|
* @param props - Provider configuration
|
|
53
|
-
* @param props.
|
|
61
|
+
* @param props.customer - Customer identity and server actions
|
|
62
|
+
* @param props.buyer - Deprecated alias of `customer`
|
|
54
63
|
* @param props.children - React children
|
|
55
64
|
*
|
|
56
65
|
* @example
|
|
57
66
|
* ```tsx
|
|
58
67
|
* // identity must match what you passed as `buyerIdentity` at checkout time —
|
|
59
|
-
* //
|
|
60
|
-
* <WaffoPancakeProvider
|
|
68
|
+
* // customer-portal lookups are keyed by merchant_provided_buyer_identity
|
|
69
|
+
* <WaffoPancakeProvider customer={{
|
|
61
70
|
* identity: user.id,
|
|
62
71
|
* storeId: "STO_xxx",
|
|
63
|
-
* issueToken, // from
|
|
64
|
-
* sessionAction, // from
|
|
72
|
+
* issueToken, // from createCustomerTokenAction()
|
|
73
|
+
* sessionAction, // from createCustomerSessionAction()
|
|
65
74
|
* }}>
|
|
66
75
|
* <App />
|
|
67
76
|
* </WaffoPancakeProvider>
|
|
68
77
|
* ```
|
|
69
78
|
*/
|
|
70
|
-
declare function WaffoPancakeProvider({ buyer, children }: WaffoPancakeProviderProps): react_jsx_runtime.JSX.Element;
|
|
79
|
+
declare function WaffoPancakeProvider({ customer, buyer, children }: WaffoPancakeProviderProps): react_jsx_runtime.JSX.Element;
|
|
71
80
|
|
|
72
81
|
/** Redirect mode for checkout navigation */
|
|
73
82
|
type CheckoutMode = "redirect" | "popup";
|
|
@@ -92,7 +101,7 @@ interface LinkCheckoutProps extends CheckoutBaseOptions {
|
|
|
92
101
|
productId: string;
|
|
93
102
|
/** Currency code (ISO 4217). If omitted, product page auto-detects. */
|
|
94
103
|
currency?: string;
|
|
95
|
-
/** Pre-fill
|
|
104
|
+
/** Pre-fill customer email */
|
|
96
105
|
email?: string;
|
|
97
106
|
/** Redirect URL after successful payment */
|
|
98
107
|
successUrl?: string;
|
|
@@ -221,8 +230,8 @@ declare function CheckoutButton(props: CheckoutButtonProps): react_jsx_runtime.J
|
|
|
221
230
|
*/
|
|
222
231
|
declare function useCheckout(args: CheckoutProps): UseCheckoutReturn;
|
|
223
232
|
|
|
224
|
-
/** State of an async
|
|
225
|
-
interface
|
|
233
|
+
/** State of an async customer action */
|
|
234
|
+
interface CustomerActionState<T = unknown> {
|
|
226
235
|
/** Execute the action */
|
|
227
236
|
execute: (params: T) => Promise<void>;
|
|
228
237
|
/** Whether the action is in progress */
|
|
@@ -230,52 +239,58 @@ interface BuyerActionState<T = unknown> {
|
|
|
230
239
|
/** Error from the last attempt */
|
|
231
240
|
error: Error | null;
|
|
232
241
|
}
|
|
233
|
-
/** Return type of
|
|
234
|
-
interface
|
|
242
|
+
/** Return type of useCustomer hook */
|
|
243
|
+
interface UseCustomerReturn {
|
|
235
244
|
/** Cancel a subscription order */
|
|
236
|
-
cancelSubscription:
|
|
245
|
+
cancelSubscription: CustomerActionState<CancelSubscriptionParams> & {
|
|
237
246
|
data: CancelSubscriptionResult | null;
|
|
238
247
|
};
|
|
239
248
|
/** Cancel a one-time order */
|
|
240
|
-
cancelOnetimeOrder:
|
|
249
|
+
cancelOnetimeOrder: CustomerActionState<CancelOnetimeOrderParams> & {
|
|
241
250
|
data: CancelOnetimeOrderResult | null;
|
|
242
251
|
};
|
|
243
252
|
/** Reactivate a canceling subscription */
|
|
244
|
-
reactivateSubscription:
|
|
253
|
+
reactivateSubscription: CustomerActionState<ReactivateSubscriptionParams> & {
|
|
245
254
|
data: ReactivateSubscriptionResult | null;
|
|
246
255
|
};
|
|
247
256
|
/** Create a refund ticket */
|
|
248
|
-
createRefundTicket:
|
|
257
|
+
createRefundTicket: CustomerActionState<CreateRefundTicketParams> & {
|
|
249
258
|
data: RefundTicket | null;
|
|
250
259
|
};
|
|
251
260
|
/** Resubmit a rejected refund ticket */
|
|
252
|
-
resubmitRefundTicket:
|
|
261
|
+
resubmitRefundTicket: CustomerActionState<ResubmitRefundTicketParams> & {
|
|
253
262
|
data: RefundTicket | null;
|
|
254
263
|
};
|
|
255
264
|
/** Execute a GraphQL query */
|
|
256
265
|
query: <T = Record<string, unknown>>(params: GraphQLParams) => Promise<GraphQLResponse<T>>;
|
|
257
266
|
}
|
|
258
267
|
/**
|
|
259
|
-
* React hook for
|
|
268
|
+
* React hook for customer self-service actions.
|
|
260
269
|
*
|
|
261
270
|
* Must be used within `<WaffoPancakeProvider>`. All operations are executed
|
|
262
271
|
* via server actions — the private key never leaves the server.
|
|
263
272
|
*
|
|
264
|
-
* @returns
|
|
273
|
+
* @returns Customer action handlers with loading/error states
|
|
265
274
|
*
|
|
266
275
|
* @example
|
|
267
276
|
* ```tsx
|
|
268
277
|
* function AccountPage() {
|
|
269
|
-
* const
|
|
278
|
+
* const customer = useCustomer();
|
|
270
279
|
* return (
|
|
271
|
-
* <button onClick={() =>
|
|
280
|
+
* <button onClick={() => customer.cancelSubscription.execute({ orderId: "ORD_xxx" })}>
|
|
272
281
|
* Cancel
|
|
273
282
|
* </button>
|
|
274
283
|
* );
|
|
275
284
|
* }
|
|
276
285
|
* ```
|
|
277
286
|
*/
|
|
278
|
-
declare function
|
|
287
|
+
declare function useCustomer(): UseCustomerReturn;
|
|
288
|
+
/** @deprecated Use {@link CustomerActionState} instead. */
|
|
289
|
+
type BuyerActionState<T = unknown> = CustomerActionState<T>;
|
|
290
|
+
/** @deprecated Use {@link UseCustomerReturn} instead. */
|
|
291
|
+
type UseBuyerReturn = UseCustomerReturn;
|
|
292
|
+
/** @deprecated Use {@link useCustomer} instead. */
|
|
293
|
+
declare const useBuyer: typeof useCustomer;
|
|
279
294
|
|
|
280
295
|
/** Query state with typed data */
|
|
281
296
|
interface QueryState<T> {
|
|
@@ -285,8 +300,8 @@ interface QueryState<T> {
|
|
|
285
300
|
refetch: () => void;
|
|
286
301
|
}
|
|
287
302
|
|
|
288
|
-
/** A
|
|
289
|
-
interface
|
|
303
|
+
/** A customer's one-time order */
|
|
304
|
+
interface CustomerOnetimeOrder {
|
|
290
305
|
id: string;
|
|
291
306
|
status: string;
|
|
292
307
|
currency: string;
|
|
@@ -304,8 +319,8 @@ interface BuyerOnetimeOrder {
|
|
|
304
319
|
}>;
|
|
305
320
|
createdAt: string;
|
|
306
321
|
}
|
|
307
|
-
/** A
|
|
308
|
-
interface
|
|
322
|
+
/** A customer's subscription order */
|
|
323
|
+
interface CustomerSubscriptionOrder {
|
|
309
324
|
id: string;
|
|
310
325
|
status: string;
|
|
311
326
|
currency: string;
|
|
@@ -327,8 +342,8 @@ interface BuyerSubscriptionOrder {
|
|
|
327
342
|
}>;
|
|
328
343
|
createdAt: string;
|
|
329
344
|
}
|
|
330
|
-
/** A
|
|
331
|
-
interface
|
|
345
|
+
/** A customer's payment record */
|
|
346
|
+
interface CustomerPayment {
|
|
332
347
|
id: string;
|
|
333
348
|
orderId: string;
|
|
334
349
|
status: string;
|
|
@@ -338,27 +353,27 @@ interface BuyerPayment {
|
|
|
338
353
|
createdAt: string;
|
|
339
354
|
}
|
|
340
355
|
/**
|
|
341
|
-
* A
|
|
356
|
+
* A customer's refund ticket.
|
|
342
357
|
*
|
|
343
358
|
* Ticket-level fields are flat; per-version fields (`reason`, `requestedAmount`)
|
|
344
|
-
* live under `versionData` because the
|
|
359
|
+
* live under `versionData` because the customer can resubmit a rejected ticket and
|
|
345
360
|
* each submission is a versioned record. `versionData` reflects the current
|
|
346
361
|
* (latest) version. The `versionData` shape is shared with `@waffo/pancake-ts`'s
|
|
347
362
|
* `RefundTicketVersionData`.
|
|
348
363
|
*/
|
|
349
|
-
interface
|
|
364
|
+
interface CustomerRefundTicket {
|
|
350
365
|
id: string;
|
|
351
366
|
status: string;
|
|
352
367
|
versionNumber: number | null;
|
|
353
368
|
versionData: RefundTicketVersionData | null;
|
|
354
369
|
createdAt: string;
|
|
355
370
|
}
|
|
356
|
-
interface
|
|
357
|
-
onetimeOrders:
|
|
358
|
-
subscriptionOrders:
|
|
371
|
+
interface CustomerOrdersData {
|
|
372
|
+
onetimeOrders: CustomerOnetimeOrder[];
|
|
373
|
+
subscriptionOrders: CustomerSubscriptionOrder[];
|
|
359
374
|
}
|
|
360
375
|
/**
|
|
361
|
-
* Fetch the
|
|
376
|
+
* Fetch the customer's order history (one-time + subscription).
|
|
362
377
|
*
|
|
363
378
|
* Must be used within `<WaffoPancakeProvider>`. Token is auto-managed.
|
|
364
379
|
*
|
|
@@ -366,13 +381,13 @@ interface BuyerOrdersData {
|
|
|
366
381
|
*
|
|
367
382
|
* @example
|
|
368
383
|
* ```tsx
|
|
369
|
-
* const { data, isLoading, refetch } =
|
|
384
|
+
* const { data, isLoading, refetch } = useCustomerOrders();
|
|
370
385
|
* // data.onetimeOrders + data.subscriptionOrders
|
|
371
386
|
* ```
|
|
372
387
|
*/
|
|
373
|
-
declare function
|
|
388
|
+
declare function useCustomerOrders(): QueryState<CustomerOrdersData>;
|
|
374
389
|
/**
|
|
375
|
-
* Fetch the
|
|
390
|
+
* Fetch the customer's payment history.
|
|
376
391
|
*
|
|
377
392
|
* Must be used within `<WaffoPancakeProvider>`. Token is auto-managed.
|
|
378
393
|
*
|
|
@@ -380,12 +395,12 @@ declare function useBuyerOrders(): QueryState<BuyerOrdersData>;
|
|
|
380
395
|
*
|
|
381
396
|
* @example
|
|
382
397
|
* ```tsx
|
|
383
|
-
* const { data: payments, isLoading } =
|
|
398
|
+
* const { data: payments, isLoading } = useCustomerPayments();
|
|
384
399
|
* ```
|
|
385
400
|
*/
|
|
386
|
-
declare function
|
|
401
|
+
declare function useCustomerPayments(): QueryState<CustomerPayment[]>;
|
|
387
402
|
/**
|
|
388
|
-
* Fetch the
|
|
403
|
+
* Fetch the customer's refund tickets.
|
|
389
404
|
*
|
|
390
405
|
* Must be used within `<WaffoPancakeProvider>`. Token is auto-managed.
|
|
391
406
|
*
|
|
@@ -393,10 +408,24 @@ declare function useBuyerPayments(): QueryState<BuyerPayment[]>;
|
|
|
393
408
|
*
|
|
394
409
|
* @example
|
|
395
410
|
* ```tsx
|
|
396
|
-
* const { data: tickets, isLoading } =
|
|
411
|
+
* const { data: tickets, isLoading } = useCustomerRefundTickets();
|
|
397
412
|
* ```
|
|
398
413
|
*/
|
|
399
|
-
declare function
|
|
414
|
+
declare function useCustomerRefundTickets(): QueryState<CustomerRefundTicket[]>;
|
|
415
|
+
/** @deprecated Use {@link CustomerOnetimeOrder} instead. */
|
|
416
|
+
type BuyerOnetimeOrder = CustomerOnetimeOrder;
|
|
417
|
+
/** @deprecated Use {@link CustomerSubscriptionOrder} instead. */
|
|
418
|
+
type BuyerSubscriptionOrder = CustomerSubscriptionOrder;
|
|
419
|
+
/** @deprecated Use {@link CustomerPayment} instead. */
|
|
420
|
+
type BuyerPayment = CustomerPayment;
|
|
421
|
+
/** @deprecated Use {@link CustomerRefundTicket} instead. */
|
|
422
|
+
type BuyerRefundTicket = CustomerRefundTicket;
|
|
423
|
+
/** @deprecated Use {@link useCustomerOrders} instead. */
|
|
424
|
+
declare const useBuyerOrders: typeof useCustomerOrders;
|
|
425
|
+
/** @deprecated Use {@link useCustomerPayments} instead. */
|
|
426
|
+
declare const useBuyerPayments: typeof useCustomerPayments;
|
|
427
|
+
/** @deprecated Use {@link useCustomerRefundTickets} instead. */
|
|
428
|
+
declare const useBuyerRefundTickets: typeof useCustomerRefundTickets;
|
|
400
429
|
|
|
401
430
|
/** A merchant's recent order (one-time or subscription) */
|
|
402
431
|
interface MerchantOrder {
|
|
@@ -527,9 +556,9 @@ interface WebhookConfig {
|
|
|
527
556
|
onSubscriptionActivated?: EventHandler;
|
|
528
557
|
/** Subscription renewal payment succeeded */
|
|
529
558
|
onSubscriptionPaymentSucceeded?: EventHandler;
|
|
530
|
-
/**
|
|
559
|
+
/** Customer initiated cancellation (expires at end of current period) */
|
|
531
560
|
onSubscriptionCanceling?: EventHandler;
|
|
532
|
-
/**
|
|
561
|
+
/** Customer withdrew cancellation (subscription restored) */
|
|
533
562
|
onSubscriptionUncanceled?: EventHandler;
|
|
534
563
|
/** Subscription product changed (upgrade/downgrade) */
|
|
535
564
|
onSubscriptionUpdated?: EventHandler;
|
|
@@ -574,4 +603,4 @@ interface WebhookConfig {
|
|
|
574
603
|
*/
|
|
575
604
|
declare function Webhook(config: WebhookConfig): (request: Request) => Promise<Response>;
|
|
576
605
|
|
|
577
|
-
export { type AnonymousCheckoutButtonProps, type AnonymousCheckoutProps, type AuthenticatedCheckoutButtonProps, type AuthenticatedCheckoutProps, type BuyerActionState, type BuyerConfig, type BuyerOnetimeOrder, type BuyerPayment, type BuyerRefundTicket, type BuyerSessionAction, type BuyerSubscriptionOrder, type BuyerTokenAction, type CheckoutAction, type CheckoutActionParams, type CheckoutActionResult, type CheckoutBaseOptions, CheckoutButton, type CheckoutButtonProps, type CheckoutMode, type CheckoutProps, type LinkCheckoutButtonProps, type LinkCheckoutProps, type MerchantOrder, type MerchantOrdersOptions, type MerchantQueryAction, type MerchantSubscription, type QueryState, type SalesOverview, type SubscriptionOverview, type UseBuyerReturn, type UseCheckoutReturn, WaffoPancakeProvider, type WaffoPancakeProviderProps, Webhook, type WebhookConfig, useBuyer, useBuyerOrders, useBuyerPayments, useBuyerRefundTickets, useCheckout, useMerchantOrders, useMerchantSales, useMerchantSubscriptions };
|
|
606
|
+
export { type AnonymousCheckoutButtonProps, type AnonymousCheckoutProps, type AuthenticatedCheckoutButtonProps, type AuthenticatedCheckoutProps, type BuyerActionState, type BuyerConfig, type BuyerOnetimeOrder, type BuyerPayment, type BuyerRefundTicket, type BuyerSessionAction, type BuyerSubscriptionOrder, type BuyerTokenAction, type CheckoutAction, type CheckoutActionParams, type CheckoutActionResult, type CheckoutBaseOptions, CheckoutButton, type CheckoutButtonProps, type CheckoutMode, type CheckoutProps, type CustomerActionState, type CustomerConfig, type CustomerOnetimeOrder, type CustomerPayment, type CustomerRefundTicket, type CustomerSessionAction, type CustomerSubscriptionOrder, type CustomerTokenAction, type LinkCheckoutButtonProps, type LinkCheckoutProps, type MerchantOrder, type MerchantOrdersOptions, type MerchantQueryAction, type MerchantSubscription, type QueryState, type SalesOverview, type SubscriptionOverview, type UseBuyerReturn, type UseCheckoutReturn, type UseCustomerReturn, WaffoPancakeProvider, type WaffoPancakeProviderProps, Webhook, type WebhookConfig, useBuyer, useBuyerOrders, useBuyerPayments, useBuyerRefundTickets, useCheckout, useCustomer, useCustomerOrders, useCustomerPayments, useCustomerRefundTickets, useMerchantOrders, useMerchantSales, useMerchantSubscriptions };
|
package/dist/index.js
CHANGED
|
@@ -5,23 +5,25 @@ import { createContext, useCallback, useContext, useEffect, useMemo, useRef, use
|
|
|
5
5
|
import { jsx } from "react/jsx-runtime";
|
|
6
6
|
var PancakeContext = createContext(null);
|
|
7
7
|
var REFRESH_BUFFER_MS = 3e4;
|
|
8
|
-
function WaffoPancakeProvider({ buyer, children }) {
|
|
8
|
+
function WaffoPancakeProvider({ customer, buyer, children }) {
|
|
9
|
+
const config = customer ?? buyer;
|
|
10
|
+
if (!config) throw new Error("WaffoPancakeProvider: the `customer` prop is required");
|
|
9
11
|
const tokenRef = useRef(null);
|
|
10
12
|
const refreshPromiseRef = useRef(null);
|
|
11
|
-
const [
|
|
13
|
+
const [isCustomerReady, setIsCustomerReady] = useState(false);
|
|
12
14
|
const refreshToken = useCallback(async () => {
|
|
13
|
-
const result = await
|
|
14
|
-
buyerIdentity:
|
|
15
|
-
storeId:
|
|
16
|
-
productId:
|
|
15
|
+
const result = await config.issueToken({
|
|
16
|
+
buyerIdentity: config.identity,
|
|
17
|
+
storeId: config.storeId,
|
|
18
|
+
productId: config.productId
|
|
17
19
|
});
|
|
18
20
|
tokenRef.current = {
|
|
19
21
|
token: result.token,
|
|
20
22
|
expiresAt: new Date(result.expiresAt).getTime()
|
|
21
23
|
};
|
|
22
24
|
return result.token;
|
|
23
|
-
}, [
|
|
24
|
-
const
|
|
25
|
+
}, [config]);
|
|
26
|
+
const getCustomerToken = useCallback(async () => {
|
|
25
27
|
const current = tokenRef.current;
|
|
26
28
|
if (current && Date.now() < current.expiresAt - REFRESH_BUFFER_MS) {
|
|
27
29
|
return current.token;
|
|
@@ -34,16 +36,16 @@ function WaffoPancakeProvider({ buyer, children }) {
|
|
|
34
36
|
return refreshPromiseRef.current;
|
|
35
37
|
}, [refreshToken]);
|
|
36
38
|
useEffect(() => {
|
|
37
|
-
refreshToken().then(() =>
|
|
39
|
+
refreshToken().then(() => setIsCustomerReady(true)).catch(() => setIsCustomerReady(true));
|
|
38
40
|
}, [refreshToken]);
|
|
39
41
|
const value = useMemo(
|
|
40
42
|
() => ({
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
getCustomerToken,
|
|
44
|
+
customerSessionAction: config.sessionAction,
|
|
45
|
+
hasCustomer: true,
|
|
46
|
+
isCustomerReady
|
|
45
47
|
}),
|
|
46
|
-
[
|
|
48
|
+
[getCustomerToken, config.sessionAction, isCustomerReady]
|
|
47
49
|
);
|
|
48
50
|
return /* @__PURE__ */ jsx(PancakeContext.Provider, { value, children });
|
|
49
51
|
}
|
|
@@ -127,9 +129,9 @@ function CheckoutButton(props) {
|
|
|
127
129
|
return /* @__PURE__ */ jsx2("button", { type: "button", onClick: checkout, disabled: disabled || isLoading, className, style, children: isLoading ? loadingChildren ?? children : children });
|
|
128
130
|
}
|
|
129
131
|
|
|
130
|
-
// src/use-
|
|
132
|
+
// src/use-customer.ts
|
|
131
133
|
import { useCallback as useCallback3, useContext as useContext2, useMemo as useMemo2, useState as useState3 } from "react";
|
|
132
|
-
function
|
|
134
|
+
function useCustomerAction(actionFn) {
|
|
133
135
|
const [isLoading, setIsLoading] = useState3(false);
|
|
134
136
|
const [error, setError] = useState3(null);
|
|
135
137
|
const [data, setData] = useState3(null);
|
|
@@ -151,34 +153,34 @@ function useBuyerAction(actionFn) {
|
|
|
151
153
|
);
|
|
152
154
|
return { execute, isLoading, error, data };
|
|
153
155
|
}
|
|
154
|
-
function
|
|
156
|
+
function useCustomer() {
|
|
155
157
|
const ctx = useContext2(PancakeContext);
|
|
156
|
-
if (!ctx) throw new Error("
|
|
157
|
-
const {
|
|
158
|
+
if (!ctx) throw new Error("useCustomer: must be used within <WaffoPancakeProvider>");
|
|
159
|
+
const { getCustomerToken, customerSessionAction } = ctx;
|
|
158
160
|
const callAction = useCallback3(
|
|
159
161
|
async (actionType, params) => {
|
|
160
|
-
const token = await
|
|
161
|
-
return
|
|
162
|
+
const token = await getCustomerToken();
|
|
163
|
+
return customerSessionAction(token, actionType, params);
|
|
162
164
|
},
|
|
163
|
-
[
|
|
165
|
+
[getCustomerToken, customerSessionAction]
|
|
164
166
|
);
|
|
165
|
-
const cancelSubscription =
|
|
167
|
+
const cancelSubscription = useCustomerAction(
|
|
166
168
|
useCallback3((params) => callAction("cancelSubscription", params), [callAction])
|
|
167
169
|
);
|
|
168
|
-
const cancelOnetimeOrder =
|
|
170
|
+
const cancelOnetimeOrder = useCustomerAction(
|
|
169
171
|
useCallback3((params) => callAction("cancelOnetimeOrder", params), [callAction])
|
|
170
172
|
);
|
|
171
|
-
const reactivateSubscription =
|
|
173
|
+
const reactivateSubscription = useCustomerAction(
|
|
172
174
|
useCallback3((params) => callAction("reactivateSubscription", params), [callAction])
|
|
173
175
|
);
|
|
174
|
-
const createRefundTicket =
|
|
176
|
+
const createRefundTicket = useCustomerAction(
|
|
175
177
|
useCallback3((params) => callAction("createRefundTicket", params), [callAction])
|
|
176
178
|
);
|
|
177
179
|
const createRefundTicketMapped = useMemo2(
|
|
178
180
|
() => ({ ...createRefundTicket, data: createRefundTicket.data?.ticket ?? null }),
|
|
179
181
|
[createRefundTicket]
|
|
180
182
|
);
|
|
181
|
-
const resubmitRefundTicket =
|
|
183
|
+
const resubmitRefundTicket = useCustomerAction(
|
|
182
184
|
useCallback3((params) => callAction("resubmitRefundTicket", params), [callAction])
|
|
183
185
|
);
|
|
184
186
|
const resubmitRefundTicketMapped = useMemo2(
|
|
@@ -187,10 +189,10 @@ function useBuyer() {
|
|
|
187
189
|
);
|
|
188
190
|
const query = useCallback3(
|
|
189
191
|
async (params) => {
|
|
190
|
-
const token = await
|
|
191
|
-
return
|
|
192
|
+
const token = await getCustomerToken();
|
|
193
|
+
return customerSessionAction(token, "query", params);
|
|
192
194
|
},
|
|
193
|
-
[
|
|
195
|
+
[getCustomerToken, customerSessionAction]
|
|
194
196
|
);
|
|
195
197
|
return {
|
|
196
198
|
cancelSubscription,
|
|
@@ -201,8 +203,9 @@ function useBuyer() {
|
|
|
201
203
|
query
|
|
202
204
|
};
|
|
203
205
|
}
|
|
206
|
+
var useBuyer = useCustomer;
|
|
204
207
|
|
|
205
|
-
// src/use-
|
|
208
|
+
// src/use-customer-data.ts
|
|
206
209
|
import { useCallback as useCallback5, useContext as useContext3 } from "react";
|
|
207
210
|
|
|
208
211
|
// src/use-query.ts
|
|
@@ -223,8 +226,8 @@ function useQuery(queryFn, ready) {
|
|
|
223
226
|
return { data, isLoading, error, refetch: fetch };
|
|
224
227
|
}
|
|
225
228
|
|
|
226
|
-
// src/use-
|
|
227
|
-
var
|
|
229
|
+
// src/use-customer-data.ts
|
|
230
|
+
var CUSTOMER_ORDERS_QUERY = `query {
|
|
228
231
|
onetimeOrders(limit: 50) {
|
|
229
232
|
id status currency buyerEmail createdAt
|
|
230
233
|
product { id name }
|
|
@@ -236,12 +239,12 @@ var BUYER_ORDERS_QUERY = `query {
|
|
|
236
239
|
payments { id status snapshotDisplayAmount snapshotDisplayCurrency createdAt }
|
|
237
240
|
}
|
|
238
241
|
}`;
|
|
239
|
-
var
|
|
242
|
+
var CUSTOMER_PAYMENTS_QUERY = `query {
|
|
240
243
|
payments(limit: 50) {
|
|
241
244
|
id orderId status snapshotDisplayAmount snapshotDisplayCurrency failureReason createdAt
|
|
242
245
|
}
|
|
243
246
|
}`;
|
|
244
|
-
var
|
|
247
|
+
var CUSTOMER_REFUND_TICKETS_QUERY = `query {
|
|
245
248
|
refundTickets(limit: 50) {
|
|
246
249
|
id status versionNumber
|
|
247
250
|
versionData {
|
|
@@ -251,28 +254,31 @@ var BUYER_REFUND_TICKETS_QUERY = `query {
|
|
|
251
254
|
createdAt
|
|
252
255
|
}
|
|
253
256
|
}`;
|
|
254
|
-
function
|
|
257
|
+
function useCustomerQuery(query) {
|
|
255
258
|
const ctx = useContext3(PancakeContext);
|
|
256
|
-
if (!ctx) throw new Error("
|
|
257
|
-
const {
|
|
259
|
+
if (!ctx) throw new Error("Customer data hook: must be used within <WaffoPancakeProvider>");
|
|
260
|
+
const { getCustomerToken, customerSessionAction, isCustomerReady } = ctx;
|
|
258
261
|
const queryFn = useCallback5(async () => {
|
|
259
|
-
const token = await
|
|
260
|
-
const result = await
|
|
262
|
+
const token = await getCustomerToken();
|
|
263
|
+
const result = await customerSessionAction(token, "query", { query });
|
|
261
264
|
return result.data;
|
|
262
|
-
}, [
|
|
263
|
-
return useQuery(queryFn,
|
|
265
|
+
}, [getCustomerToken, customerSessionAction, query]);
|
|
266
|
+
return useQuery(queryFn, isCustomerReady);
|
|
264
267
|
}
|
|
265
|
-
function
|
|
266
|
-
return
|
|
268
|
+
function useCustomerOrders() {
|
|
269
|
+
return useCustomerQuery(CUSTOMER_ORDERS_QUERY);
|
|
267
270
|
}
|
|
268
|
-
function
|
|
269
|
-
const result =
|
|
271
|
+
function useCustomerPayments() {
|
|
272
|
+
const result = useCustomerQuery(CUSTOMER_PAYMENTS_QUERY);
|
|
270
273
|
return { ...result, data: result.data?.payments ?? null };
|
|
271
274
|
}
|
|
272
|
-
function
|
|
273
|
-
const result =
|
|
275
|
+
function useCustomerRefundTickets() {
|
|
276
|
+
const result = useCustomerQuery(CUSTOMER_REFUND_TICKETS_QUERY);
|
|
274
277
|
return { ...result, data: result.data?.refundTickets ?? null };
|
|
275
278
|
}
|
|
279
|
+
var useBuyerOrders = useCustomerOrders;
|
|
280
|
+
var useBuyerPayments = useCustomerPayments;
|
|
281
|
+
var useBuyerRefundTickets = useCustomerRefundTickets;
|
|
276
282
|
|
|
277
283
|
// src/use-merchant-data.ts
|
|
278
284
|
import { useCallback as useCallback6 } from "react";
|
|
@@ -425,6 +431,10 @@ export {
|
|
|
425
431
|
useBuyerPayments,
|
|
426
432
|
useBuyerRefundTickets,
|
|
427
433
|
useCheckout,
|
|
434
|
+
useCustomer,
|
|
435
|
+
useCustomerOrders,
|
|
436
|
+
useCustomerPayments,
|
|
437
|
+
useCustomerRefundTickets,
|
|
428
438
|
useMerchantOrders,
|
|
429
439
|
useMerchantSales,
|
|
430
440
|
useMerchantSubscriptions
|