Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update customer dto #8

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions apps/nest-stripe-test/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { AppService } from './app.service';
import { AppAuthGuard } from './auth.guard';

@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)],
imports: [StripeModule.forRootAsync({
useFactory: () => ({
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)],
controllers: [AppController],
providers: [AppService],
})
Expand Down
9 changes: 8 additions & 1 deletion apps/nest-stripe-test/src/app/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Observable, of } from 'rxjs';

export const STRIPE_AUTH_GUARD = 'STRIPE_AUTH_GUARD';

@Injectable()
export class AppAuthGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
const v = Date.now() % 2
if (v === 0) {
return Promise.resolve(true)
}
if (v === 2) {
return of(true)
}
return true;
}
}
2 changes: 1 addition & 1 deletion libs/stripe/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@valor/nestjs-stripe",
"version": "0.0.2",
"version": "0.0.4",
"type": "commonjs",
"private": false,
"author": "opavlovskyi-valor-software",
Expand Down
32 changes: 32 additions & 0 deletions libs/stripe/src/lib/controllers/checkout-session.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
UseGuards,
UsePipes,
ValidationPipe,
Controller,
Post,
Body
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiResponse } from '@nestjs/swagger';
import {
CheckoutSessionResponse,
CreateCheckoutSessionDto
} from '../dto';
import { StripeAuthGuard } from '../stripe-auth.guard';
import { StripeService } from '../stripe.service';

@ApiBearerAuth()
@ApiTags('Stripe: Checkout Session')
@UseGuards(StripeAuthGuard)
@UsePipes(new ValidationPipe())
@Controller('stripe/checkout-session')
export class CheckoutSessionController {
constructor(private stripeService: StripeService) {}

@ApiResponse({ type: CheckoutSessionResponse })
@Post('create')
createCheckoutSession(
@Body() dto: CreateCheckoutSessionDto
): Promise<CheckoutSessionResponse> {
return this.stripeService.createCheckoutSession(dto);
}
}
108 changes: 108 additions & 0 deletions libs/stripe/src/lib/controllers/customer.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
UseGuards,
UsePipes,
ValidationPipe,
Controller,
Post,
Body,
Get,
Param,
Query
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiResponse, ApiQuery } from '@nestjs/swagger';
import {
BaseDataResponse,
CreateCustomerDto,
CustomerDto,
CustomerResponse,
PaymentMethodDto,
PaymentMethodTypes,
SubscriptionsResponse,
UpdateCustomerDto
} from '../dto';
import { StripeAuthGuard } from '../stripe-auth.guard';
import { StripeService } from '../stripe.service';
import Stripe from 'stripe';

@ApiBearerAuth()
@ApiTags('Stripe: Customer')
@UseGuards(StripeAuthGuard)
@UsePipes(new ValidationPipe())
@Controller('stripe/customer')
export class CustomerController {
constructor(private stripeService: StripeService) {}

@ApiResponse({ type: CustomerResponse })
@Post('create')
createCustomer(@Body() dto: CreateCustomerDto): Promise<CustomerResponse> {
return this.stripeService.createCustomer(dto);
}

@ApiResponse({ type: CustomerResponse })
@Post(':customerId/update')
updateCustomer(
@Param('customerId') customerId: string,
@Body() dto: UpdateCustomerDto
): Promise<CustomerResponse> {
return this.stripeService.updateCustomer(customerId, dto);
}

@ApiResponse({ type: CustomerResponse })
@ApiQuery({
name: 'useAsDefault',
type: Boolean,
required: false
})
@Post(':customerId/attach-payment-method/:paymentMethodId')
attachPaymentMethod(
@Param('customerId') customerId: string,
@Param('paymentMethodId') paymentMethodId: string,
@Query('useAsDefault') useAsDefault?: string
): Promise<CustomerResponse> {
return this.stripeService.attachPaymentMethod(paymentMethodId, customerId, Boolean(useAsDefault));
}

@ApiResponse({ type: BaseDataResponse })
@Get(':customerId')
getCustomer(@Param('customerId') customerId: string): Promise<BaseDataResponse<CustomerDto>> {
return this.stripeService.getCustomer(customerId);
}

@ApiResponse({ type: BaseDataResponse })
@Get(':email/by-email')
getCustomerByEmail(@Param('email') email: string): Promise<BaseDataResponse<CustomerDto[]>> {
return this.stripeService.getCustomersByEmail(email);
}

@ApiResponse({ type: SubscriptionsResponse })
@Get(':customerId/subscriptions')
customerSubscriptions(@Param('customerId') customerId: string): Promise<SubscriptionsResponse> {
return this.stripeService.customerSubscriptions(customerId);
}

@ApiResponse({ type: BaseDataResponse })
@ApiQuery({
name: 'type',
enum: PaymentMethodTypes
})
@Get(':customerId/payment-method-list')
customerPaymentMethodList(
@Param('customerId') customerId: string,
@Query('type') type: Stripe.PaymentMethodListParams.Type): Promise<BaseDataResponse<PaymentMethodDto[]>> {
return this.stripeService.customerPaymentMethodList(customerId, type);
}

@ApiResponse({ type: SubscriptionsResponse })
@ApiQuery({
name: 'status',
enum: ['accepted', 'canceled', 'draft', 'open'],
required: false
})
@Get(':customerId/quotes')
customerQuotes(
@Param('customerId') customerId: string,
@Query('status') status?: Stripe.Quote.Status
): Promise<SubscriptionsResponse> {
return this.stripeService.customerQuoteList(customerId, status);
}
}
11 changes: 11 additions & 0 deletions libs/stripe/src/lib/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export * from './payment-intent.controller';
export * from './checkout-session.controller';
export * from './customer.controller';
export * from './payment-method.controller';
export * from './price.controller';
export * from './product.controller';
export * from './subscription.controller';
export * from './subscription-schedule.controller';
export * from './invoice.controller';
export * from './usage-record.controller';
export * from './quotes.controller';
41 changes: 41 additions & 0 deletions libs/stripe/src/lib/controllers/invoice.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
UseGuards,
UsePipes,
ValidationPipe,
Controller,
Post,
Body,
Param,
Get} from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiResponse } from '@nestjs/swagger';
import {
BaseDataResponse,
InvoiceDto,
InvoicePreviewDto,
InvoicePreviewResponse
} from '../dto';
import { StripeAuthGuard } from '../stripe-auth.guard';
import { StripeService } from '../stripe.service';

@ApiBearerAuth()
@ApiTags('Stripe: Invoice')
@UseGuards(StripeAuthGuard)
@UsePipes(new ValidationPipe())
@Controller('stripe/invoice')
export class InvoiceController {
constructor(private stripeService: StripeService) {}

@ApiResponse({ type: BaseDataResponse<InvoiceDto> })
@Get(':invoiceId')
getInvoiceById(@Param('invoiceId') invoiceId: string): Promise<BaseDataResponse<InvoiceDto>> {
return this.stripeService.getInvoiceById(invoiceId);
}

@ApiResponse({ type: InvoicePreviewResponse })
@ApiTags('Stripe: Invoice')
@Post('retrieve-upcoming')
retrieveUpcomingInvoice(@Body() dto: InvoicePreviewDto): Promise<InvoicePreviewResponse> {
return this.stripeService.upcomingInvoicePreview(dto);
}

}
44 changes: 44 additions & 0 deletions libs/stripe/src/lib/controllers/payment-intent.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
UseGuards,
UsePipes,
ValidationPipe,
Controller,
Post,
Body,
Get,
Param
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiResponse } from '@nestjs/swagger';
import {
PaymentIntentResponse,
CreatePaymentIntentDto,
BaseDataResponse,
PaymentIntentDto
} from '../dto';
import { StripeAuthGuard } from '../stripe-auth.guard';
import { StripeService } from '../stripe.service';

@ApiBearerAuth()
@ApiTags('Stripe: Payment Intent')
@UseGuards(StripeAuthGuard)
@UsePipes(new ValidationPipe())
@Controller('stripe/payment-intent')
export class PaymentIntentController {
constructor(private stripeService: StripeService) {}

@ApiResponse({ type: PaymentIntentResponse })
@Post('create')
createPaymentIntent(
@Body() dto: CreatePaymentIntentDto
): Promise<PaymentIntentResponse> {
return this.stripeService.createPaymentIntent(dto);
}

@ApiResponse({ type: BaseDataResponse })
@Get(':paymentIntentId')
getPaymentIntentById(
@Param('paymentIntentId') paymentIntentId: string
): Promise<BaseDataResponse<PaymentIntentDto>> {
return this.stripeService.getPaymentIntentById(paymentIntentId);
}
}
42 changes: 42 additions & 0 deletions libs/stripe/src/lib/controllers/payment-method.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
UseGuards,
UsePipes,
ValidationPipe,
Controller,
Post,
Body,
Param
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiResponse } from '@nestjs/swagger';
import {
CreatePaymentMethodResponse,
CreatePaymentMethodDto,
CustomerResponse
} from '../dto';
import { StripeAuthGuard } from '../stripe-auth.guard';
import { StripeService } from '../stripe.service';

@ApiBearerAuth()
@ApiTags('Stripe: Payment Method')
@UseGuards(StripeAuthGuard)
@UsePipes(new ValidationPipe())
@Controller('stripe/payment-method')
export class PaymentMethodController {
constructor(private stripeService: StripeService) {}

@ApiResponse({ type: CreatePaymentMethodResponse })
@Post('create')
createPaymentMethod(
@Body() dto: CreatePaymentMethodDto
): Promise<CreatePaymentMethodResponse> {
return this.stripeService.createPaymentMethod(dto);
}

@ApiTags('Stripe: Payment Method')
@Post(':paymentMethodId/detach')
detachPaymentMethod(
@Param('paymentMethodId') paymentMethodId: string
): Promise<CustomerResponse> {
return this.stripeService.detachPaymentMethod(paymentMethodId);
}
}
57 changes: 57 additions & 0 deletions libs/stripe/src/lib/controllers/price.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
UseGuards,
UsePipes,
ValidationPipe,
Controller,
Post,
Body,
Param,
Get
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiResponse } from '@nestjs/swagger';
import {
BaseDataResponse,
CreatePriceDto,
PriceDto,
PriceResponse,
UpdatePriceDto
} from '../dto';
import { StripeAuthGuard } from '../stripe-auth.guard';
import { StripeService } from '../stripe.service';

@ApiBearerAuth()
@ApiTags('Stripe: Price')
@UseGuards(StripeAuthGuard)
@UsePipes(new ValidationPipe())
@Controller('stripe/price')
export class PriceController {
constructor(private stripeService: StripeService) {}

@ApiResponse({ type: PriceResponse })
@Post('create')
createPrice(@Body() dto: CreatePriceDto): Promise<PriceResponse> {
return this.stripeService.createPrice(dto);
}

@ApiResponse({ type: PriceResponse })
@Post(':priceId/update')
updatePrice(
@Param('priceId') priceId: string,
@Body() dto: UpdatePriceDto
): Promise<PriceResponse> {
return this.stripeService.updatePrice(priceId, dto);
}

@ApiResponse({ type: BaseDataResponse })
@Get('')
priceList(): Promise<BaseDataResponse<PriceDto[]>> {
return this.stripeService.getPriceList();
}

@ApiResponse({ type: BaseDataResponse<PriceDto> })
@Get(':priceId')
priceById(@Param('priceId') priceId: string): Promise<BaseDataResponse<PriceDto>> {
return this.stripeService.getPriceById(priceId);
}

}
Loading