0.0.22 • Published 1 month ago

@valor/nestjs-stripe v0.0.22

Weekly downloads
-
License
ISC
Repository
github
Last release
1 month ago

Table Of Contents

About

nestjs-stripe implements a module, StripeModule, which when imported into your nestjs project provides a Stripe client to any class that injects it. This lets Stripe be worked into your dependency injection workflow without having to do any extra work outside of the initial setup.

Installation

npm install --save @valor/nestjs-stripe

Getting Started

To use @valor/nestjs-stripe import StripeModule

import { Module } from '@nestjs-common';
import { StripeModule } from '@valor/nestjs-stripe';

@Module({
  imports: [
    StripeModule.forRoot({
      apiKey: process.env.STRIPE_API_KEY,
      webHookSignature: process.env.STRIPE_WEBHOOK_SIGNATURE,
      successUrl: 'http://localhost:3333/purchase-success',
      cancelUrl: 'http://localhost:3333/card',
      currency: 'usd'
    }, AppAuthGuard)
  ],
})
export class AppModule {}

You can then inject the Stripe client into any of your injectables by using a custom decorator

import { CreateCheckoutSessionDto, StripeService, WebhookEventType, WebhookService } from '@nest/stripe';
import { Injectable, Logger } from '@nestjs/common';
import Stripe from 'stripe';

@Injectable()
export class AppService {
  constructor(
    private readonly stripeWebhookService: WebhookService,
    private readonly stripeService: StripeService
  ) {
    this.stripeWebhookService.subscribeToEvent(WebhookEventType.invoicePaid).subscribe(console.log);
    this.stripeWebhookService.subscribeToEvent(WebhookEventType.customerSubscriptionCreated).subscribe((e) => this.createMeteredUsage(e));
    this.stripeWebhookService.subscribeToEvent(WebhookEventType.customerSubscriptionUpdated).subscribe((e) => this.createMeteredUsage(e));
  }

  async creteCheckoutSession(dto: CreateCheckoutSessionDto) {
    const response = await this.stripeService.createCheckoutSession(dto);
    if (response.success) {
      return response.sessionId
    } else {
      throw new Error(response.errorMessage);
    }
  }

  async createMeteredUsage(evt: Stripe.Event) {
    const stripeSubscription = evt.data.object as Stripe.Subscription;
    const subscriptionRes = await this.stripeService.getSubscriptionById(stripeSubscription.id);
    if (subscriptionRes.success) {
      const subscription = subscriptionRes.data;
      subscription.items.forEach(async si => {
        if (si.plan.usageType === 'metered') {
          const r = await this.stripeService.createUsageRecord(si.id, {
            quantity: si.quantity || 1,
            action: 'increment',
            timestamp: 'now',
          });
          if (!r.success) {
            Logger.error(r.errorMessage, 'Create Usage Record');
          } else {
            Logger.debug(r.usageRecord.id, 'Usage Record')
          }
        }
      })
    } else {
      throw new Error(subscriptionRes.errorMessage);
    }
  }

}

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements

Copyright © 2022 Oleksandr Pavlovskyi

0.0.20

1 month ago

0.0.21

1 month ago

0.0.22

1 month ago

0.0.19

4 months ago

0.0.18

5 months ago

0.0.17

5 months ago

0.0.10

8 months ago

0.0.11

8 months ago

0.0.12

8 months ago

0.0.13

7 months ago

0.0.14

7 months ago

0.0.15

7 months ago

0.0.9

8 months ago

0.0.16

7 months ago

0.0.8

8 months ago

0.0.5

9 months ago

0.0.4

10 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago