@paykit-sdk/comgate 1.0.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/README.md ADDED
@@ -0,0 +1,199 @@
1
+ # @paykit-sdk/comgate
2
+
3
+ Comgate provider for PayKit
4
+
5
+ ## Quick Start
6
+
7
+ ```typescript
8
+ import { createEndpointHandlers, PayKit } from '@paykit-sdk/core';
9
+ import { comgate, createComgate } from '@paykit-sdk/comgate';
10
+
11
+ // Method 1: Using environment variables
12
+ const provider = comgate(); // Ensure COMGATE_MERCHANT, COMGATE_SECRET, COMGATE_SANDBOX environment variables are set
13
+
14
+ // Method 2: Direct configuration
15
+ const provider = createComgate({
16
+ merchant: 'mer_',
17
+ isSandbox: true,
18
+ secret: 'xxx'
19
+ });
20
+
21
+ export const paykit = new PayKit(provider);
22
+ export const endpoints = createEndpointHandlers(paykit);
23
+ ```
24
+
25
+ ### Next.js Catch All API Route (/api/paykit/[...endpoint]/route.ts)
26
+
27
+ ```typescript
28
+ import { paykit } from '@/lib/paykit';
29
+ import { EndpointPath } from '@paykit-sdk/core';
30
+
31
+ export async function POST(request: NextRequest, { params }: { params: { endpoint: string[] } }) {
32
+ try {
33
+ // Construct the endpoint path with full type safety
34
+ const endpoint = ('/' + params.endpoint.join('/')) as EndpointPath;
35
+ const handler = endpoints[endpoint];
36
+
37
+ if (!handler) {
38
+ return NextResponse.json({ message: 'Endpoint not found' }, { status: 404 });
39
+ }
40
+
41
+ // Parse request body
42
+ const body = await request.json();
43
+ const { args } = body;
44
+
45
+ const result = await handler(...args);
46
+
47
+ return NextResponse.json({ result });
48
+ } catch (error) {
49
+ console.error('PayKit API Error:', error);
50
+ return NextResponse.json(
51
+ { message: error instanceof Error ? error.message : 'Internal server error' },
52
+ { status: 500 },
53
+ );
54
+ }
55
+ }
56
+ ```
57
+
58
+ ### Next.js Webhooks (/api/paykit/webhooks/route.ts)
59
+
60
+ ```typescript
61
+ import { paykit } from '@/lib/paykit';
62
+ import { NextRequest } from 'next/server';
63
+
64
+ export async function POST(request: NextRequest) {
65
+ const webhook = paykit.webhooks
66
+ .setup({ webhookSecret: process.env.COMGATE_SECRET })
67
+ .on('customer.created', async event => {
68
+ console.log('Customer created:', event.data);
69
+ });
70
+ .on('subscription.created', async event => {
71
+ console.log('Subscription created:', event.data);
72
+ });
73
+ .on('payment.created', async event => {
74
+ console.log('Payment created:', event.data);
75
+ });
76
+ .on('refund.created', async event => {
77
+ console.log('Refund created:', event.data);
78
+ });
79
+ .on('invoice.generated', async event => {
80
+ console.log('Invoice generated:', event.data);
81
+ });
82
+
83
+ const body = await request.text();
84
+ const headers = Object.fromEntries(request.headers.entries());
85
+ const url = request.url
86
+ await webhook.handle({ body, headers, fullUrl: url });
87
+
88
+ // Return immediately, processing happens in background
89
+ return NextResponse.json({ success: true });
90
+ }
91
+ ```
92
+
93
+ ### Express.js with Webhook Handler
94
+
95
+ ```typescript
96
+ import { paykit, endpoints } from '@/lib/paykit';
97
+ import { createEndpointHandlers, EndpointPath } from '@paykit-sdk/core';
98
+ import express from 'express';
99
+
100
+ const app = express();
101
+
102
+ // IMPORTANT: Webhook route must come BEFORE express.json() middleware
103
+ // This ensures we get the raw body for signature verification
104
+ app.post('/api/webhooks/comgate', express.raw({ type: 'application/json' }), async (req, res) => {
105
+ try {
106
+ const webhookSecret = process.env.COMGATE_SECRET;
107
+
108
+ if (!webhookSecret) {
109
+ return res.status(500).json({ error: 'Webhook secret not configured' });
110
+ }
111
+
112
+ const webhook = paykit.webhooks
113
+ .setup({ webhookSecret })
114
+ .on('customer.created', async event => {
115
+ console.log('Customer created:', event.data);
116
+ })
117
+ .on('subscription.created', async event => {
118
+ console.log('Subscription created:', event.data);
119
+ })
120
+ .on('payment.created', async event => {
121
+ console.log('Payment created:', event.data);
122
+ })
123
+ .on('refund.created', async event => {
124
+ console.log('Refund created:', event.data);
125
+ })
126
+ .on('invoice.generated', async event => {
127
+ console.log('Invoice generated:', event.data);
128
+ });
129
+
130
+ const body = req.body; // Raw buffer from express.raw()
131
+ const headers = req.headers;
132
+ const url = request.url;
133
+ await webhook.handle({ body, headers, fullUrl: url });
134
+
135
+ // Return immediately, processing happens in background
136
+ res.json({ success: true });
137
+ } catch (error) {
138
+ console.error('Webhook error:', error);
139
+ res.status(500).json({
140
+ message: error instanceof Error ? error.message : 'Webhook processing failed',
141
+ });
142
+ }
143
+ });
144
+
145
+ // Regular API routes use JSON middleware
146
+ app.use(express.json());
147
+
148
+ app.post('/api/paykit/*', async (req, res) => {
149
+ try {
150
+ const endpoint = req.path.replace('/api/paykit', '') as EndpointPath;
151
+ const handler = endpoints[endpoint];
152
+
153
+ if (!handler) {
154
+ return res.status(404).json({ message: 'Endpoint not found' });
155
+ }
156
+
157
+ const { args } = req.body;
158
+ const result = await handler(...args);
159
+
160
+ res.json({ result });
161
+ } catch (error) {
162
+ res.status(500).json({
163
+ message: error instanceof Error ? error.message : 'Internal server error',
164
+ });
165
+ }
166
+ });
167
+
168
+ app.listen(3000, () => {
169
+ console.log('Server running on port 3000');
170
+ });
171
+ ```
172
+
173
+ ## Subscription Management
174
+
175
+ ```typescript
176
+ // Update subscription
177
+ await paykit.subscriptions.update('sub_123', {
178
+ metadata: { plan: 'enterprise' },
179
+ });
180
+
181
+ // Cancel subscription
182
+ await paykit.subscriptions.cancel('sub_123');
183
+ ```
184
+
185
+ ## Environment Variables
186
+
187
+ ```bash
188
+ COMGATE_MERCHANT=mer_...
189
+ COMGATE_SECRET=xxx...
190
+ COMGATE_SANDBOX=false
191
+ ```
192
+
193
+ ## Support
194
+
195
+ - [Congate Documentation](https://apidoc.comgate.cz/)
196
+
197
+ ## License
198
+
199
+ GPL-3.0
@@ -0,0 +1,50 @@
1
+ import { PaykitProviderOptions, AbstractPayKitProvider, PayKitProvider, CreateCheckoutSchema, Checkout, UpdateCheckoutSchema, CreateCustomerParams, Customer, UpdateCustomerParams, CreatePaymentSchema, Payment, UpdatePaymentSchema, CapturePaymentSchema, CreateRefundSchema, Refund, CreateSubscriptionSchema, Subscription, UpdateSubscriptionSchema, HandleWebhookParams, WebhookEventPayload } from '@paykit-sdk/core';
2
+
3
+ interface ComgateOptions extends PaykitProviderOptions {
4
+ /**
5
+ * The merchant ID
6
+ */
7
+ merchant: string;
8
+ /**
9
+ * The secret key
10
+ */
11
+ secret: string;
12
+ /**
13
+ * Whether to use the sandbox environment
14
+ */
15
+ isSandbox: boolean;
16
+ }
17
+ declare class ComgateProvider extends AbstractPayKitProvider implements PayKitProvider {
18
+ private readonly opts;
19
+ readonly providerName = "comgate";
20
+ private baseUrl;
21
+ private _client;
22
+ constructor(opts: ComgateOptions);
23
+ private _throwOnError;
24
+ createCheckout: (params: CreateCheckoutSchema) => Promise<Checkout>;
25
+ updateCheckout: (id: string, params: UpdateCheckoutSchema) => Promise<Checkout>;
26
+ deleteCheckout: (id: string) => Promise<null>;
27
+ retrieveCheckout: (id: string) => Promise<Checkout>;
28
+ createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
29
+ retrieveCustomer: (id: string) => Promise<Customer | null>;
30
+ updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
31
+ deleteCustomer: (id: string) => Promise<null>;
32
+ createPayment: (params: CreatePaymentSchema) => Promise<Payment>;
33
+ updatePayment: (_id: string, _params: UpdatePaymentSchema) => Promise<Payment>;
34
+ deletePayment: (id: string) => Promise<null>;
35
+ retrievePayment: (id: string) => Promise<Payment | null>;
36
+ capturePayment: (id: string, params: CapturePaymentSchema) => Promise<Payment>;
37
+ cancelPayment: (id: string) => Promise<Payment>;
38
+ /**
39
+ * Creates a refund for a paid payment
40
+ */
41
+ createRefund: (params: CreateRefundSchema) => Promise<Refund>;
42
+ createSubscription: (params: CreateSubscriptionSchema) => Promise<Subscription>;
43
+ deleteSubscription: (id: string) => Promise<null>;
44
+ updateSubscription: (id: string, params: UpdateSubscriptionSchema) => Promise<Subscription>;
45
+ retrieveSubscription: (id: string) => Promise<Subscription>;
46
+ cancelSubscription: (id: string) => Promise<Subscription>;
47
+ handleWebhook: ({ body: rawBody, headers }: HandleWebhookParams) => Promise<Array<WebhookEventPayload>>;
48
+ }
49
+
50
+ export { type ComgateOptions, ComgateProvider };
@@ -0,0 +1,50 @@
1
+ import { PaykitProviderOptions, AbstractPayKitProvider, PayKitProvider, CreateCheckoutSchema, Checkout, UpdateCheckoutSchema, CreateCustomerParams, Customer, UpdateCustomerParams, CreatePaymentSchema, Payment, UpdatePaymentSchema, CapturePaymentSchema, CreateRefundSchema, Refund, CreateSubscriptionSchema, Subscription, UpdateSubscriptionSchema, HandleWebhookParams, WebhookEventPayload } from '@paykit-sdk/core';
2
+
3
+ interface ComgateOptions extends PaykitProviderOptions {
4
+ /**
5
+ * The merchant ID
6
+ */
7
+ merchant: string;
8
+ /**
9
+ * The secret key
10
+ */
11
+ secret: string;
12
+ /**
13
+ * Whether to use the sandbox environment
14
+ */
15
+ isSandbox: boolean;
16
+ }
17
+ declare class ComgateProvider extends AbstractPayKitProvider implements PayKitProvider {
18
+ private readonly opts;
19
+ readonly providerName = "comgate";
20
+ private baseUrl;
21
+ private _client;
22
+ constructor(opts: ComgateOptions);
23
+ private _throwOnError;
24
+ createCheckout: (params: CreateCheckoutSchema) => Promise<Checkout>;
25
+ updateCheckout: (id: string, params: UpdateCheckoutSchema) => Promise<Checkout>;
26
+ deleteCheckout: (id: string) => Promise<null>;
27
+ retrieveCheckout: (id: string) => Promise<Checkout>;
28
+ createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
29
+ retrieveCustomer: (id: string) => Promise<Customer | null>;
30
+ updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
31
+ deleteCustomer: (id: string) => Promise<null>;
32
+ createPayment: (params: CreatePaymentSchema) => Promise<Payment>;
33
+ updatePayment: (_id: string, _params: UpdatePaymentSchema) => Promise<Payment>;
34
+ deletePayment: (id: string) => Promise<null>;
35
+ retrievePayment: (id: string) => Promise<Payment | null>;
36
+ capturePayment: (id: string, params: CapturePaymentSchema) => Promise<Payment>;
37
+ cancelPayment: (id: string) => Promise<Payment>;
38
+ /**
39
+ * Creates a refund for a paid payment
40
+ */
41
+ createRefund: (params: CreateRefundSchema) => Promise<Refund>;
42
+ createSubscription: (params: CreateSubscriptionSchema) => Promise<Subscription>;
43
+ deleteSubscription: (id: string) => Promise<null>;
44
+ updateSubscription: (id: string, params: UpdateSubscriptionSchema) => Promise<Subscription>;
45
+ retrieveSubscription: (id: string) => Promise<Subscription>;
46
+ cancelSubscription: (id: string) => Promise<Subscription>;
47
+ handleWebhook: ({ body: rawBody, headers }: HandleWebhookParams) => Promise<Array<WebhookEventPayload>>;
48
+ }
49
+
50
+ export { type ComgateOptions, ComgateProvider };