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 🙏

© 2024 – Pkg Stats / Ryan Hefner

latest-nestjs-stripe

v1.0.4

Published

Injectable Stripe client for Nestjs And Create A Futres And Cards Payment flow

Downloads

4

Readme

Nestjs-Stripe

#  latest-nestjs-stripe

 latest-nestjs-stripe is a latest NestJS module for integrating Stripe into your application, and it provides some built-in payment flows for easy implementation.

Instalation

 npm i latest-nestjs-stripe
 yarn add latest-nestjs-stripe

Nest.js and Stripe and express are peer dependencies.

ENUM USE FOR ENDPOINTS

stripe.enum.ts It's not compulsory; you can use your own.

export enum PATHSTRIPE {
    ROOT = 'stripe',
    INTENT = 'intent',
    WEBHOOK = 'webhook',
    WEBHOOK_PATH = 'v1/stripe/webhook'
}

INJECT STRIPE MODULE IN YOUR MODULE

import { Module, RequestMethod } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { StripeEventsExtension, StripeModule } from "latest-nestjs-stripe";
import { StripeController } from "./controllers/stripe.controller";
import { PATHSTRIPE } from "./enums/stripe.enum";

@Module({
    imports: [
        StripeModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (configService: ConfigService) => ({
                apiKey: configService.get('STRIPE_SECRET_KEY'),
                config: { apiVersion: "2024-04-10" }
            }),
            inject: [ConfigService]
        }),
    ],
    controllers: [StripeController]
})
export default class Stripe {}

INJECT USE STRIPE CLIENT EXAMPLE.

We can use the client for creating payment intent. Use all Stripe client features with this.

 constructor(@Inject(StripeService) private readonly stripe: StripeService) { }
 //THIS IS A STTRIPE CLIENT
 this.stripe.client

CONTROLLER FOR CREATE INTENT USING STRIPE CLIENT

import { Controller, HttpCode, HttpStatus, Post } from '@nestjs/common';
import { StripeService } from 'latest-nestjs-stripe';
import { PATHSTRIPE } from '../enums/stripe.enum';

@Controller(PATHSTRIPE.ROOT)
export class StripeController {

    constructor(@Inject(StripeService) private readonly stripe: StripeService) { }

    @Post(PATHSTRIPE.INTENT)
    @HttpCode(HttpStatus.CREATED)
    public create() {
        return this.stripe.client.paymentIntents.create({
            amount: 20 * 100,
            currency: "AED",
            metadata: {
                from: 'sec'
            },
            description: "lorem ipsum"
        })
    }

}

WEBHOOK IMPLEMENTATION ON APPLICATION AND CONTROLLER

There are two methods for using webhooks: one if you use a single middleware module, and the other if you use more than one middleware module.

STEP ONE go to main.ts and active rawBody

    const app = await NestFactory.create(AppModule, {
        rawBody: true
    })

STEP TWO go to your module and extend middleware class StripeEventsExtension from "latest-nestjs-stripe"

FIRST METHOD FOR SINGLE MIDDLEWARE

import { Module, RequestMethod } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { StripeEventsExtension, StripeModule } from "latest-nestjs-stripe";
import { StripeController } from "./controllers/stripe.controller";
import { PATHSTRIPE } from "./enums/stripe.enum";

@Module({
    imports: [
        StripeModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (configService: ConfigService) => ({
                apiKey: configService.get('STRIPE_SECRET_KEY'),
                config: { apiVersion: "2024-04-10" }
            }),
            inject: [ConfigService]
        }),
    ],
    controllers: [StripeController]
})


export default class Stripe extends StripeEventsExtension {
    constructor() {
        const key = process.env.WEBHOOK_KEY //webhook key
        const path = PATHSTRIPE.RAW_WEBHOOK //Webhook path to apply middleware on this path
        //"The RequestMethod.POST parameter applies middleware on this route method."
        super(key, RequestMethod.POST, path) // This parameter is required in the StripeEventsExtension class.
    }
    

}

SECOND METHOD FOR MULTIPLE MIDDLEWARE

import { MiddlewareConsumer, Module, NestModule, RequestMethod } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { RawEventStripeMiddleware, StripeModule } from "latest-nestjs-stripe";
import { StripeController } from "./controllers/stripe.controller";
import { PATHSTRIPE } from "./enums/stripe.enum";

@Module({
    imports: [
        StripeModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (configService: ConfigService) => ({
                apiKey: configService.get('STRIPE_SECRET_KEY'),
                config: { apiVersion: "2024-04-10" }
            }),
            inject: [ConfigService]
        }),
    ],
    controllers: [StripeController]

})

export default class Stripe implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        const key = process.env.WEBHOOK_KEY
        consumer.apply(RawEventStripeMiddleware(key))
            .forRoutes({
                method: RequestMethod.POST,
                path: PATHSTRIPE.RAW_WEBHOOK
            })
        //MULTIPLE MIDDLEWARES INJECTS
        // consumer.apply(RawEventStripeMiddleware(key))
        //     .forRoutes({
        //         method: RequestMethod.POST,
        //         path: PATHSTRIPE.RAW_WEBHOOK
        //     })
    }
}

HANDLE WEBHOOK IN CONTROLLER

import { Controller, HttpCode, HttpStatus, Post, RawBodyRequest, Req } from '@nestjs/common';
import { StripeEventRequestI } from 'latest-nestjs-stripe';
import { PATHSTRIPE } from '../enums/stripe.enum';

@Controller(PATHSTRIPE.ROOT)
export class StripeController {

    // webhook
    @Post(PATHSTRIPE.WEBHOOK)
    @HttpCode(HttpStatus.OK)
    public webhook(@Req() req: RawBodyRequest<StripeEventRequestI>) {
        const event = req.event
        switch (event.type) {
            case 'payment_intent.created':
                console.log(event.data.object.client_secret)
                break;
            case 'payment_intent.succeeded':
                console.log(event.data.object.client_secret)
                break;
            default:
                break;
        }

        return true
    }


}
  //Stripe Client
  const  client  =  this.stripeService.client

BUILT IN FUNCTIONS FOR FUTURE PAYMENTS


__create_customer()
__get_payment_methods()
__get_default_payment_method()
__set_default_payment_method()
__detach_payment_method()
payment_intent() //for futre and non future both
__check_payment_method(payment_method,customer_id)

FUTURE PAYMENTS FLOW DOCS COMING SOON

License

This project is licensed under the MIT License - see the LICENSE.md file for details