Skip to content

Commit

Permalink
feat: void invoice (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
StashBank authored Oct 4, 2023
1 parent 4f9b01d commit ed1655b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
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.12",
"version": "0.0.13",
"type": "commonjs",
"private": false,
"author": "opavlovskyi-valor-software",
Expand Down
25 changes: 23 additions & 2 deletions libs/stripe/src/lib/controllers/invoice.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import {
Param,
Get,
Query,
Logger} from '@nestjs/common';
Logger,
Patch} from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiResponse } from '@nestjs/swagger';
import {
BaseDataResponse,
BaseSearchInvoiceDto,
InvoiceDto,
InvoiceFinalizeInvoiceDto,
InvoicePreviewDto,
InvoicePreviewResponse
InvoicePreviewResponse,
InvoiceVoidInvoiceDto
} from '../dto';
import { StripeAuthGuard } from '../stripe-auth.guard';
import { StripeService } from '../stripe.service';
Expand Down Expand Up @@ -48,4 +51,22 @@ export class InvoiceController {
return this.stripeService.upcomingInvoicePreview(dto);
}

@ApiResponse({ type: BaseDataResponse<InvoiceDto> })
@Patch(':invoiceId/void')
voidInvoice(
@Param('invoiceId') invoiceId: string,
@Body() dto: InvoiceVoidInvoiceDto
): Promise<BaseDataResponse<InvoiceDto>> {
return this.stripeService.voidInvoice(invoiceId, dto);
}

@ApiResponse({ type: BaseDataResponse<InvoiceDto> })
@Patch(':invoiceId/finalize')
finalizeInvoice(
@Param('invoiceId') invoiceId: string,
@Body() dto: InvoiceFinalizeInvoiceDto
): Promise<BaseDataResponse<InvoiceDto>> {
return this.stripeService.finalizeInvoice(invoiceId, dto);
}

}
1 change: 1 addition & 0 deletions libs/stripe/src/lib/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './update-product.dto';
export * from './update-customer.dto';
export * from './save-webhook-endpoint.dto';
export * from './save-test-clock.dto';
export * from './update-invoice.dto';

export * from './stripe/customer.dto';
export * from './stripe/subscription-item.dto';
Expand Down
14 changes: 14 additions & 0 deletions libs/stripe/src/lib/dto/update-invoice.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiPropertyOptional } from '@nestjs/swagger';

export class InvoiceVoidInvoiceDto {
@ApiPropertyOptional({ isArray: true, type: String })
expand?: Array<string>;
}

export class InvoiceFinalizeInvoiceDto {
@ApiPropertyOptional()
autoAdvance?:boolean;

@ApiPropertyOptional({ isArray: true, type: String })
expand?: Array<string>;
}
31 changes: 31 additions & 0 deletions libs/stripe/src/lib/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import {
ListRequestParamsDto,
TestClockDto,
BaseSearchInvoiceDto,
InvoiceVoidInvoiceDto,
InvoiceFinalizeInvoiceDto,
} from './dto';
import { StripeConfig, STRIPE_CONFIG } from './stripe.config';
import { StripeLogger } from './stripe.logger';
Expand Down Expand Up @@ -1018,6 +1020,35 @@ export class StripeService {
return this.handleError(exception, 'Search Invoices');
}
}

async voidInvoice(id: string, dto: InvoiceVoidInvoiceDto): Promise<BaseDataResponse<InvoiceDto>> {
try {
const invoice = await this.stripe.invoices.voidInvoice(id, {
expand: dto.expand
});
return {
success: true,
data: this.invoiceToDto(invoice)
};
} catch (exception) {
return this.handleError(exception, 'Void Invoice');
}
}

async finalizeInvoice(id: string, dto: InvoiceFinalizeInvoiceDto): Promise<BaseDataResponse<InvoiceDto>> {
try {
const invoice = await this.stripe.invoices.finalizeInvoice(id, {
auto_advance: dto.autoAdvance,
expand: dto.expand
});
return {
success: true,
data: this.invoiceToDto(invoice)
};
} catch (exception) {
return this.handleError(exception, 'Finalize Invoice');
}
}
//#endregion

//#region Quote
Expand Down

0 comments on commit ed1655b

Please sign in to comment.