From 502b10837dd4a7a83c9d3f59d8d2dede7f2aa334 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlovskyi Date: Wed, 4 Oct 2023 13:16:58 +0300 Subject: [PATCH 1/2] feat: void invoice --- libs/stripe/package.json | 2 +- .../src/lib/controllers/invoice.controller.ts | 25 +++++++++++++-- libs/stripe/src/lib/dto/index.ts | 1 + libs/stripe/src/lib/dto/update-invoice.dto.ts | 14 +++++++++ libs/stripe/src/lib/stripe.service.ts | 31 +++++++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 libs/stripe/src/lib/dto/update-invoice.dto.ts 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 From d4ba3e8ee6004006acd6d6183c7ba1d3a418f036 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlovskyi Date: Mon, 16 Oct 2023 17:27:13 +0300 Subject: [PATCH 2/2] feat: create invoice --- libs/stripe/package.json | 2 +- .../src/lib/controllers/invoice.controller.ts | 8 ++ libs/stripe/src/lib/dto/create-invoice.dto.ts | 114 ++++++++++++++++++ libs/stripe/src/lib/dto/shared.dto.ts | 16 +++ libs/stripe/src/lib/stripe.service.ts | 31 +++++ 5 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 libs/stripe/src/lib/dto/create-invoice.dto.ts diff --git a/libs/stripe/package.json b/libs/stripe/package.json index 809bb15..41b528a 100644 --- a/libs/stripe/package.json +++ b/libs/stripe/package.json @@ -1,6 +1,6 @@ { "name": "@valor/nestjs-stripe", - "version": "0.0.13", + "version": "0.0.14", "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 e6946e5..c6f9fe1 100644 --- a/libs/stripe/src/lib/controllers/invoice.controller.ts +++ b/libs/stripe/src/lib/controllers/invoice.controller.ts @@ -13,6 +13,7 @@ import { import { ApiBearerAuth, ApiTags, ApiResponse } from '@nestjs/swagger'; import { BaseDataResponse, + BaseResponse, BaseSearchInvoiceDto, InvoiceDto, InvoiceFinalizeInvoiceDto, @@ -22,6 +23,7 @@ import { } from '../dto'; import { StripeAuthGuard } from '../stripe-auth.guard'; import { StripeService } from '../stripe.service'; +import { CreateInvoiceDto } from '../dto/create-invoice.dto'; @ApiBearerAuth() @ApiTags('Stripe: Invoice') @@ -51,6 +53,12 @@ export class InvoiceController { return this.stripeService.upcomingInvoicePreview(dto); } + @ApiResponse({ type: BaseDataResponse }) + @Post('') + createInvoice(@Body() dto: CreateInvoiceDto): Promise> { + return this.stripeService.createInvoice(dto); + } + @ApiResponse({ type: BaseDataResponse }) @Patch(':invoiceId/void') voidInvoice( diff --git a/libs/stripe/src/lib/dto/create-invoice.dto.ts b/libs/stripe/src/lib/dto/create-invoice.dto.ts new file mode 100644 index 0000000..666fe28 --- /dev/null +++ b/libs/stripe/src/lib/dto/create-invoice.dto.ts @@ -0,0 +1,114 @@ +import { ApiPropertyOptional } from '@nestjs/swagger'; +import { CustomFieldDto, DiscountDto, MetadataDto } from './shared.dto'; + +export class CreateInvoiceDto { + @ApiPropertyOptional({ + description: 'The account tax IDs associated with the invoice. Only editable when the invoice is a draft.', + isArray: true, + type: String + }) + accountTaxIds?: Array; + + @ApiPropertyOptional({ + description: 'A fee in cents (or local equivalent) that will be applied to the invoice and transferred to the application owner\'s Stripe account. The request must be made with an OAuth key or the Stripe-Account header in order to take an application fee. For more information, see the application fees [documentation](https://stripe.com/docs/billing/invoices/connect#collecting-fees).' + }) + applicationFeeAmount?: number; + + @ApiPropertyOptional({ + description: 'Controls whether Stripe will perform [automatic collection](https://stripe.com/docs/billing/invoices/workflow/#auto_advance) of the invoice. When `false`, the invoice\'s state will not automatically advance without an explicit action.' + }) + autoAdvance?: boolean; + + @ApiPropertyOptional({ + description: 'Settings for automatic tax lookup for this invoice.\nWhether Stripe automatically computes tax on this invoice. Note that incompatible invoice items (invoice items with manually specified [tax rates](https://stripe.com/docs/api/tax_rates), negative amounts, or `tax_behavior=unspecified`) cannot be added to automatic tax invoices.' + }) + automaticTax?: boolean; + + @ApiPropertyOptional({ + description: 'Either `charge_automatically`, or `send_invoice`. When charging automatically, Stripe will attempt to pay this invoice using the default source attached to the customer. When sending an invoice, Stripe will email this invoice to the customer with payment instructions. Defaults to `charge_automatically`.', + enum: ['charge_automatically', 'send_invoice'] + }) + collectionMethod?: 'charge_automatically' | 'send_invoice'; + + @ApiPropertyOptional({ description: 'The currency to create this invoice in. Defaults to that of `customer` if not specified.'}) + currency?: string; + + @ApiPropertyOptional({ + description: 'A list of up to 4 custom fields to be displayed on the invoice.', + isArray: true, + type: CustomFieldDto + }) + customFields?: Array; + + @ApiPropertyOptional() + customer?: string; + + @ApiPropertyOptional({ + description: 'The number of days from when the invoice is created until it is due. Valid only for invoices where `collection_method=send_invoice`.' + }) + daysUntilDue?: number; + + @ApiPropertyOptional({ + description: 'ID of the default payment method for the invoice. It must belong to the customer associated with the invoice. If not set, defaults to the subscription\'s default payment method, if any, or to the default payment method in the customer\'s invoice settings.' + }) + defaultPaymentMethodId?: string; + + @ApiPropertyOptional({ + description: 'ID of the default payment source for the invoice. It must belong to the customer associated with the invoice and be in a chargeable state. If not set, defaults to the subscription\'s default source, if any, or to the customer\'s default source.' + }) + defaultSource?: string; + + @ApiPropertyOptional({ + description: 'The tax rates that will apply to any line item that does not have `tax_rates` set.', + isArray: true, + type: String, + }) + defaultTaxRates?: Array; + + @ApiPropertyOptional() + description?: string; + + @ApiPropertyOptional({ + description: 'The coupons to redeem into discounts for the invoice. If not specified, inherits the discount from the invoice\'s customer. Pass an empty string to avoid inheriting any discounts.', + isArray: true, + type: DiscountDto + }) + discounts?: Array; + + @ApiPropertyOptional({ + description: ' The date on which payment for this invoice is due. Valid only for invoices where `collection_method=send_invoice`.' + }) + dueDate?: number; + + @ApiPropertyOptional({ isArray: true, type: String}) + expand?: Array; + + @ApiPropertyOptional() + footer?: string; + + @ApiPropertyOptional({ + description: 'Revise an existing invoice. The new invoice will be created in `status=draft`. See the [revision documentation](https://stripe.com/docs/invoicing/invoice-revisions) for more details.' + }) + fromInvoice?: string; + + @ApiPropertyOptional({ + description: 'Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to `metadata`.' + }) + metadata?: MetadataDto; + + @ApiPropertyOptional({ + description: 'The account (if any) for which the funds of the invoice payment are intended. If set, the invoice will be presented with the branding and support information of the specified account. See the [Invoices with Connect](https://stripe.com/docs/billing/invoices/connect) documentation for details.' + }) + onBehalfOf?: string; + + @ApiPropertyOptional({ + description: 'How to handle pending invoice items on invoice creation. One of `include` or `exclude`. `include` will include any pending invoice items, and will create an empty draft invoice if no pending invoice items exist. `exclude` will always create an empty invoice draft regardless if there are pending invoice items or not. Defaults to `exclude` if the parameter is omitted.', + enum: ['exclude', 'include', 'include_and_require'] + }) + pendingInvoiceItemsBehavior?: 'exclude' | 'include' | 'include_and_require'; + + @ApiPropertyOptional({ + description: 'The ID of the subscription to invoice, if any. If set, the created invoice will only include pending invoice items for that subscription. The subscription\'s billing cycle and regular subscription events won\'t be affected.' + }) + subscription?: string; +} \ No newline at end of file diff --git a/libs/stripe/src/lib/dto/shared.dto.ts b/libs/stripe/src/lib/dto/shared.dto.ts index 1be8830..547c454 100644 --- a/libs/stripe/src/lib/dto/shared.dto.ts +++ b/libs/stripe/src/lib/dto/shared.dto.ts @@ -189,3 +189,19 @@ export class DobDto { @ApiProperty() year: number | null; } + +export class DiscountDto { + @ApiPropertyOptional({ + description: 'ID of the coupon to create a new discount for.' + }) + coupon?: string; + + @ApiProperty({ + description: 'ID of an existing discount on the object (or one of its ancestors) to reuse.' + }) + discount?: string; +} + +export class MetadataDto { + [name: string]: string | number | null; +} \ No newline at end of file diff --git a/libs/stripe/src/lib/stripe.service.ts b/libs/stripe/src/lib/stripe.service.ts index bc09c7d..4767bf5 100644 --- a/libs/stripe/src/lib/stripe.service.ts +++ b/libs/stripe/src/lib/stripe.service.ts @@ -64,6 +64,7 @@ import { } from './dto'; import { StripeConfig, STRIPE_CONFIG } from './stripe.config'; import { StripeLogger } from './stripe.logger'; +import { CreateInvoiceDto } from './dto/create-invoice.dto'; @Injectable() export class StripeService { @@ -994,6 +995,36 @@ export class StripeService { } } + async createInvoice(dto: CreateInvoiceDto): Promise> { + try { + const invoice = await this.stripe.invoices.create({ + account_tax_ids: dto.accountTaxIds, + application_fee_amount: dto.applicationFeeAmount, + auto_advance: dto.autoAdvance, + automatic_tax: dto.automaticTax != null ? { enabled: dto.automaticTax } : undefined, + collection_method: dto.collectionMethod, + currency: dto.currency, + custom_fields: dto.customFields, + customer: dto.customer, + days_until_due: dto.daysUntilDue, + default_payment_method: dto.defaultPaymentMethodId, + default_source: dto.defaultSource, + default_tax_rates: dto.defaultTaxRates, + description: dto.description, + discounts: dto.discounts, + due_date: dto.dueDate, + pending_invoice_items_behavior: dto.pendingInvoiceItemsBehavior, + subscription: dto.subscription, + }); + return { + success: true, + data: this.invoiceToDto(invoice) + } + } catch (exception) { + return this.handleError(exception, 'Invoice Create'); + } + } + async getInvoiceById(id: string): Promise> { try { const invoice = await this.stripe.invoices.retrieve(id);