npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@paykit-sdk/comgate

v1.0.5

Published

Comgate provider for PayKit

Downloads

13

Readme

@paykit-sdk/comgate

Comgate provider for PayKit

Quick Start

import { createEndpointHandlers, PayKit } from '@paykit-sdk/core';
import { comgate, createComgate } from '@paykit-sdk/comgate';

// Method 1: Using environment variables
const provider = comgate(); // Ensure COMGATE_MERCHANT, COMGATE_SECRET, COMGATE_SANDBOX environment variables are set

// Method 2: Direct configuration
const provider = createComgate({
  merchant: 'mer_',
  isSandbox: true,
  secret: 'xxx'
});

export const paykit = new PayKit(provider);
export const endpoints = createEndpointHandlers(paykit);

Next.js Catch All API Route (/api/paykit/[...endpoint]/route.ts)

import { paykit } from '@/lib/paykit';
import { EndpointPath } from '@paykit-sdk/core';

export async function POST(
  request: NextRequest,
  { params }: { params: { endpoint: string[] } },
) {
  try {
    // Construct the endpoint path with full type safety
    const endpoint = ('/' + params.endpoint.join('/')) as EndpointPath;
    const handler = endpoints[endpoint];

    if (!handler) {
      return NextResponse.json({ message: 'Endpoint not found' }, { status: 404 });
    }

    // Parse request body
    const body = await request.json();
    const { args } = body;

    const result = await handler(...args);

    return NextResponse.json({ result });
  } catch (error) {
    console.error('PayKit API Error:', error);
    return NextResponse.json(
      { message: error instanceof Error ? error.message : 'Internal server error' },
      { status: 500 },
    );
  }
}

Next.js Webhooks (/api/paykit/webhooks/route.ts)

import { paykit } from '@/lib/paykit';
import { NextRequest } from 'next/server';

export async function POST(request: NextRequest) {
  const webhook = paykit.webhooks
    .setup({ webhookSecret: process.env.COMGATE_SECRET })
    .on('customer.created', async event => {
      console.log('Customer created:', event.data);
    });
    .on('subscription.created', async event => {
      console.log('Subscription created:', event.data);
    });
    .on('payment.created', async event => {
      console.log('Payment created:', event.data);
    });
    .on('refund.created', async event => {
     console.log('Refund created:', event.data);
    });
    .on('invoice.generated', async event => {
      console.log('Invoice generated:', event.data);
    });

  const body = await request.text();
  const headers = Object.fromEntries(request.headers.entries());
  const url = request.url
  await webhook.handle({ body, headers, fullUrl: url });

  // Return immediately, processing happens in background
  return NextResponse.json({ success: true });
}

Express.js with Webhook Handler

import { paykit, endpoints } from '@/lib/paykit';
import { createEndpointHandlers, EndpointPath } from '@paykit-sdk/core';
import express from 'express';

const app = express();

// IMPORTANT: Webhook route must come BEFORE express.json() middleware
// This ensures we get the raw body for signature verification
app.post(
  '/api/webhooks/comgate',
  express.raw({ type: 'application/json' }),
  async (req, res) => {
    try {
      const webhookSecret = process.env.COMGATE_SECRET;

      if (!webhookSecret) {
        return res.status(500).json({ error: 'Webhook secret not configured' });
      }

      const webhook = paykit.webhooks
        .setup({ webhookSecret })
        .on('customer.created', async event => {
          console.log('Customer created:', event.data);
        })
        .on('subscription.created', async event => {
          console.log('Subscription created:', event.data);
        })
        .on('payment.created', async event => {
          console.log('Payment created:', event.data);
        })
        .on('refund.created', async event => {
          console.log('Refund created:', event.data);
        })
        .on('invoice.generated', async event => {
          console.log('Invoice generated:', event.data);
        });

      const body = req.body; // Raw buffer from express.raw()
      const headers = req.headers;
      const url = request.url;
      await webhook.handle({ body, headers, fullUrl: url });

      // Return immediately, processing happens in background
      res.json({ success: true });
    } catch (error) {
      console.error('Webhook error:', error);
      res.status(500).json({
        message: error instanceof Error ? error.message : 'Webhook processing failed',
      });
    }
  },
);

// Regular API routes use JSON middleware
app.use(express.json());

app.post('/api/paykit/*', async (req, res) => {
  try {
    const endpoint = req.path.replace('/api/paykit', '') as EndpointPath;
    const handler = endpoints[endpoint];

    if (!handler) {
      return res.status(404).json({ message: 'Endpoint not found' });
    }

    const { args } = req.body;
    const result = await handler(...args);

    res.json({ result });
  } catch (error) {
    res.status(500).json({
      message: error instanceof Error ? error.message : 'Internal server error',
    });
  }
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Subscription Management

// Update subscription
await paykit.subscriptions.update('sub_123', {
  metadata: { plan: 'enterprise' },
});

// Cancel subscription
await paykit.subscriptions.cancel('sub_123');

Environment Variables

COMGATE_MERCHANT=mer_...
COMGATE_SECRET=xxx...
COMGATE_SANDBOX=false

Support

License

ISC