UNPKG

@vbusatta/adonis-stripe

Version:

Provider for AdonisJS 6. Simplifies Stripe integration with webhooks and services.

81 lines (80 loc) 2.73 kB
/** * Stripe Service for AdonisJS 6. * * This module provides a service for interacting with the Stripe API and handling * incoming webhook events. The service is designed to be used as a singleton and * can be registered with the AdonisJS IoC container. * * @see https://stripe.com/docs/api * @see https://stripe.com/docs/webhooks */ import { Stripe as StripeSDK } from 'stripe'; export default class StripeService { #stripe; #logger; #webhookSecret; #defaultApiVersion = '2024-11-20.acacia'; #eventHandlers = {}; constructor(logger, config) { this.#logger = logger; this.#stripe = new StripeSDK(config.apiKey, { apiVersion: config.apiVersion || this.#defaultApiVersion, }); this.#webhookSecret = config.webhookSecret || null; } /** * Provides direct access to the Stripe SDK instance. */ get api() { return this.#stripe; } /** * Processes an incoming webhook request by verifying the signature and handling the event. */ async processWebhook(rawBody, signature) { const event = this.verifyWebhookSignature(rawBody, signature); await this.handleEvent(event); } /** * Verifies the signature of a Stripe webhook request and extracts the event. */ verifyWebhookSignature(rawBody, signature) { if (!this.#webhookSecret) { this.#logger.error('Stripe webhook secret is missing'); throw new Error('Webhook secret not configured'); } try { this.#logger.debug('Verifying Stripe webhook signature'); return this.#stripe.webhooks.constructEvent(rawBody, signature, this.#webhookSecret); } catch (error) { this.#logger.error('Invalid Stripe webhook signature', error); throw new Error('Invalid webhook signature'); } } /** * Processes a Stripe event by invoking the registered handler. */ async handleEvent(event) { this.#logger.debug(`Handling event type: ${event.type}`); const handler = this.#eventHandlers[event.type]; if (handler) { try { await handler(event); } catch (error) { this.#logger.error(`Error while handling event ${event.type}:`, error); } } else { this.#logger.warn(`No handler found for event type: ${event.type}`); } } /** * Registers a handler for a specific Stripe event type. */ onEvent(eventType, handler) { this.#logger.debug(`Registering handler for event type: ${eventType}`); this.#eventHandlers[eventType] = handler; } }