@paykit-sdk/react 1.0.1-alpha.6
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/README.md +134 -0
- package/dist/index.d.mts +65 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +168 -0
- package/dist/index.mjs +127 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# @paykit-sdk/react
|
|
2
|
+
|
|
3
|
+
React hooks and components for PayKit SDK - Universal payment processing with Stripe, Polar, and more.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @paykit-sdk/react @tanstack/react-query
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### 1. Setup Provider
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { PaykitProvider } from '@paykit-sdk/react';
|
|
17
|
+
import { stripe } from '@paykit-sdk/stripe';
|
|
18
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
19
|
+
|
|
20
|
+
// or import { local } from '@paykit-sdk/local';
|
|
21
|
+
// or import { polar } from '@paykit-sdk/polar';
|
|
22
|
+
|
|
23
|
+
const queryClient = new QueryClient();
|
|
24
|
+
const provider = stripe();
|
|
25
|
+
|
|
26
|
+
function App() {
|
|
27
|
+
return (
|
|
28
|
+
<QueryClientProvider client={queryClient}>
|
|
29
|
+
<PaykitProvider provider={provider}>
|
|
30
|
+
<YourApp />
|
|
31
|
+
</PaykitProvider>
|
|
32
|
+
</QueryClientProvider>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. Use Hooks
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import { useCustomer, useSubscription, useCheckout } from '@paykit-sdk/react';
|
|
41
|
+
|
|
42
|
+
function CustomerDashboard() {
|
|
43
|
+
const { customer, create, update } = useCustomer({ customerId: 'cus_123' });
|
|
44
|
+
const { subscriptions, cancel } = useSubscription('sub_456');
|
|
45
|
+
const { checkout, create: createCheckout } = useCheckout();
|
|
46
|
+
|
|
47
|
+
const handleCreateCheckout = async () => {
|
|
48
|
+
const newCheckout = await createCheckout.mutate({
|
|
49
|
+
customer_id: 'cus_123',
|
|
50
|
+
item_id: 'price_123',
|
|
51
|
+
session_type: 'one_time',
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Redirect to payment URL
|
|
55
|
+
window.location.href = newCheckout.payment_url;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<div>
|
|
60
|
+
<h1>Welcome, {customer?.name}</h1>
|
|
61
|
+
<button onClick={handleCreateCheckout}>Buy Now</button>
|
|
62
|
+
</div>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Hooks API
|
|
68
|
+
|
|
69
|
+
### `useCustomer(options)`
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
const { customer, create, update } = useCustomer({ customerId: 'cus_123' });
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**Returns:**
|
|
76
|
+
|
|
77
|
+
- `customer` - Customer data
|
|
78
|
+
- `isLoading` - Loading state
|
|
79
|
+
- `error` - Error state
|
|
80
|
+
- `create` - Create customer mutation
|
|
81
|
+
- `update` - Update customer mutation
|
|
82
|
+
|
|
83
|
+
### `useSubscription(subscriptionId)`
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
const { subscription, update, cancel } = useSubscription('sub_123');
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Returns:**
|
|
90
|
+
|
|
91
|
+
- `subscription` - Subscription data
|
|
92
|
+
- `isLoading` - Loading state
|
|
93
|
+
- `error` - Error state
|
|
94
|
+
- `update` - Update subscription mutation
|
|
95
|
+
- `cancel` - Cancel subscription mutation
|
|
96
|
+
|
|
97
|
+
### `useCheckout(checkoutId?)`
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
const { checkout, create } = useCheckout();
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**Returns:**
|
|
104
|
+
|
|
105
|
+
- `checkout` - Checkout data (if ID provided)
|
|
106
|
+
- `isLoading` - Loading state
|
|
107
|
+
- `error` - Error state
|
|
108
|
+
- `create` - Create checkout mutation
|
|
109
|
+
|
|
110
|
+
## Providers
|
|
111
|
+
|
|
112
|
+
Works with any PayKit provider:
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
// Stripe
|
|
116
|
+
import { stripe } from '@paykit-sdk/stripe';
|
|
117
|
+
const provider = stripe();
|
|
118
|
+
|
|
119
|
+
// Local (for development)
|
|
120
|
+
import { local } from '@paykit-sdk/local';
|
|
121
|
+
const provider = local();
|
|
122
|
+
|
|
123
|
+
// Polar
|
|
124
|
+
import { polar } from '@paykit-sdk/polar';
|
|
125
|
+
const provider = polar();
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## TypeScript Support
|
|
129
|
+
|
|
130
|
+
Full TypeScript support included with proper type definitions.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
2
|
+
import { QueryClient } from '@tanstack/react-query';
|
|
3
|
+
import * as _paykit_sdk_core from '@paykit-sdk/core';
|
|
4
|
+
import { CreateCustomerParams, UpdateCustomerParams, PayKitProvider, UpdateSubscriptionParams, CreateCheckoutParams } from '@paykit-sdk/core';
|
|
5
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
|
|
8
|
+
declare const useCustomer: ({ customerId }: {
|
|
9
|
+
customerId: string;
|
|
10
|
+
}) => {
|
|
11
|
+
customer: _paykit_sdk_core.Customer | null | undefined;
|
|
12
|
+
isLoading: boolean;
|
|
13
|
+
error: Error | null;
|
|
14
|
+
create: {
|
|
15
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Customer, Error, CreateCustomerParams, unknown>;
|
|
16
|
+
isLoading: boolean;
|
|
17
|
+
error: Error | null;
|
|
18
|
+
};
|
|
19
|
+
update: {
|
|
20
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Customer, Error, {
|
|
21
|
+
id: string;
|
|
22
|
+
params: UpdateCustomerParams;
|
|
23
|
+
}, unknown>;
|
|
24
|
+
isLoading: boolean;
|
|
25
|
+
error: Error | null;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type PaykitContextValue = {
|
|
30
|
+
provider: PayKitProvider;
|
|
31
|
+
queryClient?: QueryClient;
|
|
32
|
+
};
|
|
33
|
+
declare const PaykitProvider: ({ provider, queryClient, children }: React.PropsWithChildren<PaykitContextValue>) => react_jsx_runtime.JSX.Element;
|
|
34
|
+
declare const usePaykitContext: () => PaykitContextValue;
|
|
35
|
+
|
|
36
|
+
declare const useSubscription: ({ subscriptionId }: {
|
|
37
|
+
subscriptionId: string;
|
|
38
|
+
}) => {
|
|
39
|
+
subscription: _paykit_sdk_core.Subscription | undefined;
|
|
40
|
+
isLoading: boolean;
|
|
41
|
+
error: Error | null;
|
|
42
|
+
update: {
|
|
43
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Subscription, Error, UpdateSubscriptionParams, unknown>;
|
|
44
|
+
isLoading: boolean;
|
|
45
|
+
error: Error | null;
|
|
46
|
+
};
|
|
47
|
+
cancel: {
|
|
48
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Subscription, Error, void, unknown>;
|
|
49
|
+
isLoading: boolean;
|
|
50
|
+
error: Error | null;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
declare const useCheckout: (checkoutId?: string) => {
|
|
55
|
+
checkout: _paykit_sdk_core.Checkout | undefined;
|
|
56
|
+
isLoading: boolean;
|
|
57
|
+
error: Error | null;
|
|
58
|
+
create: {
|
|
59
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Checkout, Error, CreateCheckoutParams, unknown>;
|
|
60
|
+
isLoading: boolean;
|
|
61
|
+
error: Error | null;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export { PaykitProvider, useCheckout, useCustomer, usePaykitContext, useSubscription };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
2
|
+
import { QueryClient } from '@tanstack/react-query';
|
|
3
|
+
import * as _paykit_sdk_core from '@paykit-sdk/core';
|
|
4
|
+
import { CreateCustomerParams, UpdateCustomerParams, PayKitProvider, UpdateSubscriptionParams, CreateCheckoutParams } from '@paykit-sdk/core';
|
|
5
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
|
|
8
|
+
declare const useCustomer: ({ customerId }: {
|
|
9
|
+
customerId: string;
|
|
10
|
+
}) => {
|
|
11
|
+
customer: _paykit_sdk_core.Customer | null | undefined;
|
|
12
|
+
isLoading: boolean;
|
|
13
|
+
error: Error | null;
|
|
14
|
+
create: {
|
|
15
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Customer, Error, CreateCustomerParams, unknown>;
|
|
16
|
+
isLoading: boolean;
|
|
17
|
+
error: Error | null;
|
|
18
|
+
};
|
|
19
|
+
update: {
|
|
20
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Customer, Error, {
|
|
21
|
+
id: string;
|
|
22
|
+
params: UpdateCustomerParams;
|
|
23
|
+
}, unknown>;
|
|
24
|
+
isLoading: boolean;
|
|
25
|
+
error: Error | null;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type PaykitContextValue = {
|
|
30
|
+
provider: PayKitProvider;
|
|
31
|
+
queryClient?: QueryClient;
|
|
32
|
+
};
|
|
33
|
+
declare const PaykitProvider: ({ provider, queryClient, children }: React.PropsWithChildren<PaykitContextValue>) => react_jsx_runtime.JSX.Element;
|
|
34
|
+
declare const usePaykitContext: () => PaykitContextValue;
|
|
35
|
+
|
|
36
|
+
declare const useSubscription: ({ subscriptionId }: {
|
|
37
|
+
subscriptionId: string;
|
|
38
|
+
}) => {
|
|
39
|
+
subscription: _paykit_sdk_core.Subscription | undefined;
|
|
40
|
+
isLoading: boolean;
|
|
41
|
+
error: Error | null;
|
|
42
|
+
update: {
|
|
43
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Subscription, Error, UpdateSubscriptionParams, unknown>;
|
|
44
|
+
isLoading: boolean;
|
|
45
|
+
error: Error | null;
|
|
46
|
+
};
|
|
47
|
+
cancel: {
|
|
48
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Subscription, Error, void, unknown>;
|
|
49
|
+
isLoading: boolean;
|
|
50
|
+
error: Error | null;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
declare const useCheckout: (checkoutId?: string) => {
|
|
55
|
+
checkout: _paykit_sdk_core.Checkout | undefined;
|
|
56
|
+
isLoading: boolean;
|
|
57
|
+
error: Error | null;
|
|
58
|
+
create: {
|
|
59
|
+
mutate: _tanstack_react_query.UseMutateAsyncFunction<_paykit_sdk_core.Checkout, Error, CreateCheckoutParams, unknown>;
|
|
60
|
+
isLoading: boolean;
|
|
61
|
+
error: Error | null;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export { PaykitProvider, useCheckout, useCustomer, usePaykitContext, useSubscription };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
PaykitProvider: () => PaykitProvider,
|
|
34
|
+
useCheckout: () => useCheckout,
|
|
35
|
+
useCustomer: () => useCustomer,
|
|
36
|
+
usePaykitContext: () => usePaykitContext,
|
|
37
|
+
useSubscription: () => useSubscription
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/customer.ts
|
|
42
|
+
var import_react_query2 = require("@tanstack/react-query");
|
|
43
|
+
|
|
44
|
+
// src/paykit.tsx
|
|
45
|
+
var React = __toESM(require("react"));
|
|
46
|
+
var import_react_query = require("@tanstack/react-query");
|
|
47
|
+
|
|
48
|
+
// src/util.ts
|
|
49
|
+
var parseElementContext = (ctx, name) => {
|
|
50
|
+
if (!ctx) throw new Error(`${name}Context must be used within the ${name}Provider`);
|
|
51
|
+
return ctx;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/paykit.tsx
|
|
55
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
56
|
+
var PaykitContext = React.createContext({});
|
|
57
|
+
var PaykitProvider = ({ provider, queryClient, children }) => {
|
|
58
|
+
const queryClient$1 = queryClient != null ? queryClient : new import_react_query.QueryClient();
|
|
59
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PaykitContext.Provider, { value: { provider, queryClient: queryClient$1 }, children });
|
|
60
|
+
};
|
|
61
|
+
var usePaykitContext = () => parseElementContext(React.useContext(PaykitContext), "PaykitContext");
|
|
62
|
+
|
|
63
|
+
// src/customer.ts
|
|
64
|
+
var useCustomer = ({ customerId }) => {
|
|
65
|
+
const { provider } = usePaykitContext();
|
|
66
|
+
const {
|
|
67
|
+
data: customer,
|
|
68
|
+
isLoading,
|
|
69
|
+
error
|
|
70
|
+
} = (0, import_react_query2.useQuery)({
|
|
71
|
+
queryKey: ["paykit", "customer", customerId],
|
|
72
|
+
queryFn: () => provider.retrieveCustomer(customerId),
|
|
73
|
+
enabled: !!customerId
|
|
74
|
+
});
|
|
75
|
+
const {
|
|
76
|
+
mutateAsync: createCustomer,
|
|
77
|
+
isPending: isCreating,
|
|
78
|
+
error: createError
|
|
79
|
+
} = (0, import_react_query2.useMutation)({
|
|
80
|
+
mutationFn: (params) => provider.createCustomer(params)
|
|
81
|
+
});
|
|
82
|
+
const {
|
|
83
|
+
mutateAsync: updateCustomer,
|
|
84
|
+
isPending: isUpdating,
|
|
85
|
+
error: updateError
|
|
86
|
+
} = (0, import_react_query2.useMutation)({
|
|
87
|
+
mutationFn: ({ id, params }) => provider.updateCustomer(id, params)
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
customer,
|
|
91
|
+
isLoading,
|
|
92
|
+
error,
|
|
93
|
+
create: { mutate: createCustomer, isLoading: isCreating, error: createError },
|
|
94
|
+
update: { mutate: updateCustomer, isLoading: isUpdating, error: updateError }
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/subscription.tsx
|
|
99
|
+
var import_react_query3 = require("@tanstack/react-query");
|
|
100
|
+
var useSubscription = ({ subscriptionId }) => {
|
|
101
|
+
const { provider } = usePaykitContext();
|
|
102
|
+
const {
|
|
103
|
+
data: subscription,
|
|
104
|
+
isLoading,
|
|
105
|
+
error
|
|
106
|
+
} = (0, import_react_query3.useQuery)({
|
|
107
|
+
queryKey: ["paykit", "subscription", subscriptionId],
|
|
108
|
+
queryFn: () => provider.retrieveSubscription(subscriptionId),
|
|
109
|
+
enabled: !!subscriptionId
|
|
110
|
+
});
|
|
111
|
+
const {
|
|
112
|
+
mutateAsync: updateSubscription,
|
|
113
|
+
isPending: isUpdating,
|
|
114
|
+
error: updateError
|
|
115
|
+
} = (0, import_react_query3.useMutation)({
|
|
116
|
+
mutationFn: (params) => provider.updateSubscription(subscriptionId, params)
|
|
117
|
+
});
|
|
118
|
+
const {
|
|
119
|
+
mutateAsync: cancelSubscription,
|
|
120
|
+
isPending: isCancelling,
|
|
121
|
+
error: cancelError
|
|
122
|
+
} = (0, import_react_query3.useMutation)({
|
|
123
|
+
mutationFn: () => provider.cancelSubscription(subscriptionId)
|
|
124
|
+
});
|
|
125
|
+
return {
|
|
126
|
+
subscription,
|
|
127
|
+
isLoading,
|
|
128
|
+
error,
|
|
129
|
+
update: { mutate: updateSubscription, isLoading: isUpdating, error: updateError },
|
|
130
|
+
cancel: { mutate: cancelSubscription, isLoading: isCancelling, error: cancelError }
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// src/checkout.ts
|
|
135
|
+
var import_react_query4 = require("@tanstack/react-query");
|
|
136
|
+
var useCheckout = (checkoutId) => {
|
|
137
|
+
const { provider } = usePaykitContext();
|
|
138
|
+
const {
|
|
139
|
+
data: checkout,
|
|
140
|
+
isLoading,
|
|
141
|
+
error
|
|
142
|
+
} = (0, import_react_query4.useQuery)({
|
|
143
|
+
queryKey: ["paykit", "checkout", checkoutId],
|
|
144
|
+
queryFn: () => provider.retrieveCheckout(checkoutId),
|
|
145
|
+
enabled: !!checkoutId
|
|
146
|
+
});
|
|
147
|
+
const {
|
|
148
|
+
mutateAsync: createCheckout,
|
|
149
|
+
isPending: isCreating,
|
|
150
|
+
error: createError
|
|
151
|
+
} = (0, import_react_query4.useMutation)({
|
|
152
|
+
mutationFn: (params) => provider.createCheckout(params)
|
|
153
|
+
});
|
|
154
|
+
return {
|
|
155
|
+
checkout,
|
|
156
|
+
isLoading,
|
|
157
|
+
error,
|
|
158
|
+
create: { mutate: createCheckout, isLoading: isCreating, error: createError }
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
162
|
+
0 && (module.exports = {
|
|
163
|
+
PaykitProvider,
|
|
164
|
+
useCheckout,
|
|
165
|
+
useCustomer,
|
|
166
|
+
usePaykitContext,
|
|
167
|
+
useSubscription
|
|
168
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// src/customer.ts
|
|
2
|
+
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
3
|
+
|
|
4
|
+
// src/paykit.tsx
|
|
5
|
+
import * as React from "react";
|
|
6
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
7
|
+
|
|
8
|
+
// src/util.ts
|
|
9
|
+
var parseElementContext = (ctx, name) => {
|
|
10
|
+
if (!ctx) throw new Error(`${name}Context must be used within the ${name}Provider`);
|
|
11
|
+
return ctx;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// src/paykit.tsx
|
|
15
|
+
import { jsx } from "react/jsx-runtime";
|
|
16
|
+
var PaykitContext = React.createContext({});
|
|
17
|
+
var PaykitProvider = ({ provider, queryClient, children }) => {
|
|
18
|
+
const queryClient$1 = queryClient != null ? queryClient : new QueryClient();
|
|
19
|
+
return /* @__PURE__ */ jsx(PaykitContext.Provider, { value: { provider, queryClient: queryClient$1 }, children });
|
|
20
|
+
};
|
|
21
|
+
var usePaykitContext = () => parseElementContext(React.useContext(PaykitContext), "PaykitContext");
|
|
22
|
+
|
|
23
|
+
// src/customer.ts
|
|
24
|
+
var useCustomer = ({ customerId }) => {
|
|
25
|
+
const { provider } = usePaykitContext();
|
|
26
|
+
const {
|
|
27
|
+
data: customer,
|
|
28
|
+
isLoading,
|
|
29
|
+
error
|
|
30
|
+
} = useQuery({
|
|
31
|
+
queryKey: ["paykit", "customer", customerId],
|
|
32
|
+
queryFn: () => provider.retrieveCustomer(customerId),
|
|
33
|
+
enabled: !!customerId
|
|
34
|
+
});
|
|
35
|
+
const {
|
|
36
|
+
mutateAsync: createCustomer,
|
|
37
|
+
isPending: isCreating,
|
|
38
|
+
error: createError
|
|
39
|
+
} = useMutation({
|
|
40
|
+
mutationFn: (params) => provider.createCustomer(params)
|
|
41
|
+
});
|
|
42
|
+
const {
|
|
43
|
+
mutateAsync: updateCustomer,
|
|
44
|
+
isPending: isUpdating,
|
|
45
|
+
error: updateError
|
|
46
|
+
} = useMutation({
|
|
47
|
+
mutationFn: ({ id, params }) => provider.updateCustomer(id, params)
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
customer,
|
|
51
|
+
isLoading,
|
|
52
|
+
error,
|
|
53
|
+
create: { mutate: createCustomer, isLoading: isCreating, error: createError },
|
|
54
|
+
update: { mutate: updateCustomer, isLoading: isUpdating, error: updateError }
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/subscription.tsx
|
|
59
|
+
import { useMutation as useMutation2, useQuery as useQuery2 } from "@tanstack/react-query";
|
|
60
|
+
var useSubscription = ({ subscriptionId }) => {
|
|
61
|
+
const { provider } = usePaykitContext();
|
|
62
|
+
const {
|
|
63
|
+
data: subscription,
|
|
64
|
+
isLoading,
|
|
65
|
+
error
|
|
66
|
+
} = useQuery2({
|
|
67
|
+
queryKey: ["paykit", "subscription", subscriptionId],
|
|
68
|
+
queryFn: () => provider.retrieveSubscription(subscriptionId),
|
|
69
|
+
enabled: !!subscriptionId
|
|
70
|
+
});
|
|
71
|
+
const {
|
|
72
|
+
mutateAsync: updateSubscription,
|
|
73
|
+
isPending: isUpdating,
|
|
74
|
+
error: updateError
|
|
75
|
+
} = useMutation2({
|
|
76
|
+
mutationFn: (params) => provider.updateSubscription(subscriptionId, params)
|
|
77
|
+
});
|
|
78
|
+
const {
|
|
79
|
+
mutateAsync: cancelSubscription,
|
|
80
|
+
isPending: isCancelling,
|
|
81
|
+
error: cancelError
|
|
82
|
+
} = useMutation2({
|
|
83
|
+
mutationFn: () => provider.cancelSubscription(subscriptionId)
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
subscription,
|
|
87
|
+
isLoading,
|
|
88
|
+
error,
|
|
89
|
+
update: { mutate: updateSubscription, isLoading: isUpdating, error: updateError },
|
|
90
|
+
cancel: { mutate: cancelSubscription, isLoading: isCancelling, error: cancelError }
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// src/checkout.ts
|
|
95
|
+
import { useMutation as useMutation3, useQuery as useQuery3 } from "@tanstack/react-query";
|
|
96
|
+
var useCheckout = (checkoutId) => {
|
|
97
|
+
const { provider } = usePaykitContext();
|
|
98
|
+
const {
|
|
99
|
+
data: checkout,
|
|
100
|
+
isLoading,
|
|
101
|
+
error
|
|
102
|
+
} = useQuery3({
|
|
103
|
+
queryKey: ["paykit", "checkout", checkoutId],
|
|
104
|
+
queryFn: () => provider.retrieveCheckout(checkoutId),
|
|
105
|
+
enabled: !!checkoutId
|
|
106
|
+
});
|
|
107
|
+
const {
|
|
108
|
+
mutateAsync: createCheckout,
|
|
109
|
+
isPending: isCreating,
|
|
110
|
+
error: createError
|
|
111
|
+
} = useMutation3({
|
|
112
|
+
mutationFn: (params) => provider.createCheckout(params)
|
|
113
|
+
});
|
|
114
|
+
return {
|
|
115
|
+
checkout,
|
|
116
|
+
isLoading,
|
|
117
|
+
error,
|
|
118
|
+
create: { mutate: createCheckout, isLoading: isCreating, error: createError }
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
export {
|
|
122
|
+
PaykitProvider,
|
|
123
|
+
useCheckout,
|
|
124
|
+
useCustomer,
|
|
125
|
+
usePaykitContext,
|
|
126
|
+
useSubscription
|
|
127
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@paykit-sdk/react",
|
|
3
|
+
"version": "1.0.1-alpha.6",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"module": "dist/index.esm.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
12
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
13
|
+
"prepublishOnly": "npm run build",
|
|
14
|
+
"typecheck": "tsc --noEmit"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@paykit-sdk/core": "workspace:*"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@tanstack/react-query": "^5.80.5",
|
|
24
|
+
"react": ">=16.8.0",
|
|
25
|
+
"react-dom": ">=16.8.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@swc/core": "^1.12.9",
|
|
29
|
+
"@tanstack/react-query": "^5.80.5",
|
|
30
|
+
"@types/react": "^18.0.0",
|
|
31
|
+
"@types/react-dom": "^18.0.0",
|
|
32
|
+
"react": "^18.0.0",
|
|
33
|
+
"react-dom": "^18.0.0",
|
|
34
|
+
"tsup": "^8.0.0",
|
|
35
|
+
"typescript": "^5.0.0"
|
|
36
|
+
},
|
|
37
|
+
"description": "React hooks and components for PayKit SDK - Universal payment processing with Stripe, Polar, and more",
|
|
38
|
+
"keywords": [
|
|
39
|
+
"paykit",
|
|
40
|
+
"react",
|
|
41
|
+
"hooks",
|
|
42
|
+
"payments",
|
|
43
|
+
"stripe",
|
|
44
|
+
"polar",
|
|
45
|
+
"subscriptions",
|
|
46
|
+
"checkout"
|
|
47
|
+
],
|
|
48
|
+
"author": "Emmanuel Odii",
|
|
49
|
+
"license": "ISC",
|
|
50
|
+
"homepage": "https://paykit-web.vercel.app"
|
|
51
|
+
}
|