diff --git a/libs/stripe/package.json b/libs/stripe/package.json index 84aec99..809bb15 100644 --- a/libs/stripe/package.json +++ b/libs/stripe/package.json @@ -1,6 +1,6 @@ { "name": "@valor/nestjs-stripe", - "version": "0.0.11", + "version": "0.0.13", "type": "commonjs", "private": false, "author": "opavlovskyi-valor-software", diff --git a/libs/stripe/src/lib/controllers/invoice.controller.ts b/libs/stripe/src/lib/controllers/invoice.controller.ts index 066f289..e6946e5 100644 --- a/libs/stripe/src/lib/controllers/invoice.controller.ts +++ b/libs/stripe/src/lib/controllers/invoice.controller.ts @@ -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'; @@ -48,4 +51,22 @@ export class InvoiceController { return this.stripeService.upcomingInvoicePreview(dto); } + @ApiResponse({ type: BaseDataResponse }) + @Patch(':invoiceId/void') + voidInvoice( + @Param('invoiceId') invoiceId: string, + @Body() dto: InvoiceVoidInvoiceDto + ): Promise> { + return this.stripeService.voidInvoice(invoiceId, dto); + } + + @ApiResponse({ type: BaseDataResponse }) + @Patch(':invoiceId/finalize') + finalizeInvoice( + @Param('invoiceId') invoiceId: string, + @Body() dto: InvoiceFinalizeInvoiceDto + ): Promise> { + return this.stripeService.finalizeInvoice(invoiceId, dto); + } + } \ No newline at end of file diff --git a/libs/stripe/src/lib/dto/index.ts b/libs/stripe/src/lib/dto/index.ts index 81873bb..e61ea12 100644 --- a/libs/stripe/src/lib/dto/index.ts +++ b/libs/stripe/src/lib/dto/index.ts @@ -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'; diff --git a/libs/stripe/src/lib/dto/update-invoice.dto.ts b/libs/stripe/src/lib/dto/update-invoice.dto.ts new file mode 100644 index 0000000..63bf909 --- /dev/null +++ b/libs/stripe/src/lib/dto/update-invoice.dto.ts @@ -0,0 +1,14 @@ +import { ApiPropertyOptional } from '@nestjs/swagger'; + +export class InvoiceVoidInvoiceDto { + @ApiPropertyOptional({ isArray: true, type: String }) + expand?: Array; +} + +export class InvoiceFinalizeInvoiceDto { + @ApiPropertyOptional() + autoAdvance?:boolean; + + @ApiPropertyOptional({ isArray: true, type: String }) + expand?: Array; +} diff --git a/libs/stripe/src/lib/stripe.service.ts b/libs/stripe/src/lib/stripe.service.ts index 2468ecc..bc09c7d 100644 --- a/libs/stripe/src/lib/stripe.service.ts +++ b/libs/stripe/src/lib/stripe.service.ts @@ -59,6 +59,8 @@ import { ListRequestParamsDto, TestClockDto, BaseSearchInvoiceDto, + InvoiceVoidInvoiceDto, + InvoiceFinalizeInvoiceDto, } from './dto'; import { StripeConfig, STRIPE_CONFIG } from './stripe.config'; import { StripeLogger } from './stripe.logger'; @@ -1016,6 +1018,35 @@ export class StripeService { return this.handleError(exception, 'Search Invoices'); } } + + async voidInvoice(id: string, dto: InvoiceVoidInvoiceDto): Promise> { + 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> { + 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