Skip to content

Commit

Permalink
feat: create invoice item
Browse files Browse the repository at this point in the history
  • Loading branch information
StashBank committed Oct 17, 2023
1 parent d853a75 commit 09aede1
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 6 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.15",
"version": "0.0.16",
"type": "commonjs",
"private": false,
"author": "opavlovskyi-valor-software",
Expand Down
10 changes: 8 additions & 2 deletions libs/stripe/src/lib/controllers/invoice.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import {
Param,
Get,
Query,
Logger,
Patch} from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiResponse } from '@nestjs/swagger';
import {
BaseDataResponse,
BaseResponse,
BaseSearchInvoiceDto,
InvoiceDto,
InvoiceFinalizeInvoiceDto,
Expand Down Expand Up @@ -77,4 +75,12 @@ export class InvoiceController {
return this.stripeService.finalizeInvoice(invoiceId, dto);
}

@ApiResponse({ type: BaseDataResponse<InvoiceDto> })
@Patch(':invoiceId/send')
sendInvoice(
@Param('invoiceId') invoiceId: string,
): Promise<BaseDataResponse<InvoiceDto>> {
return this.stripeService.sendInvoice(invoiceId);
}

}
122 changes: 122 additions & 0 deletions libs/stripe/src/lib/dto/create-invoice-item.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { DiscountDto, MetadataDto, PeriodDto } from './shared.dto';

type TaxBehavior = 'exclusive' | 'inclusive' | 'unspecified'
const TaxBehaviors = ['exclusive', 'inclusive', 'unspecified'];

export class InvoiceItemPriceDataDto {
@ApiProperty()
currency: string;

@ApiProperty()
product: string;

@ApiPropertyOptional({
description: 'Only required if a [default tax behavior](https://stripe.com/docs/tax/products-prices-tax-categories-tax-behavior#setting-a-default-tax-behavior-(recommended)) was not provided in the Stripe Tax settings. Specifies whether the price is considered inclusive of taxes or exclusive of taxes. One of `inclusive`, `exclusive`, or `unspecified`. Once specified as either `inclusive` or `exclusive`, it cannot be changed.',
enum: TaxBehaviors
})
taxBehavior?: TaxBehavior;

@ApiPropertyOptional({
description: 'A positive integer in cents (or local equivalent) (or 0 for a free price) representing how much to charge.'
})
unitAmount?: number;

@ApiPropertyOptional({
description: 'Same as `unit_amount`, but accepts a decimal value in cents (or local equivalent) with at most 12 decimal places. Only one of `unit_amount` and `unit_amount_decimal` can be set.'
})
unitAmountDecimal?: string;
}

export class CreateInvoiceItemDto {
@ApiProperty()
customer: string;

@ApiPropertyOptional({
description: 'The integer amount in cents (or local equivalent) of the charge to be applied to the upcoming invoice. Passing in a negative `amount` will reduce the `amount_due` on the invoice.'
})
amount?: number;

@ApiPropertyOptional()
currency?: string;

@ApiPropertyOptional()
description?: string;

@ApiPropertyOptional({
description: 'Controls whether discounts apply to this invoice item. Defaults to false for prorations or negative invoice items, and true for all other invoice items.'
})
discountable?: boolean;

@ApiPropertyOptional({
description: 'The coupons to redeem into discounts for the invoice item or invoice line item.',
isArray: true,
type: DiscountDto
})
discounts?: DiscountDto[];

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

@ApiPropertyOptional({
description: 'The ID of an existing invoice to add this invoice item to. When left blank, the invoice item will be added to the next upcoming scheduled invoice. This is useful when adding invoice items in response to an invoice.created webhook. You can only add invoice items to draft invoices and there is a maximum of 250 items per invoice.'
})
invoice?: 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 period associated with this invoice item. When set to different values, the period will be rendered on the invoice. If you have [Stripe Revenue Recognition](https://stripe.com/docs/revenue-recognition) enabled, the period will be used to recognize and defer revenue. See the [Revenue Recognition documentation](https://stripe.com/docs/revenue-recognition/methodology/subscriptions-and-invoicing) for details.'
})
period?: PeriodDto;

@ApiPropertyOptional()
price?: string;

@ApiPropertyOptional()
priceData?: InvoiceItemPriceDataDto;

@ApiPropertyOptional({
description: 'Non-negative integer. The quantity of units for the invoice item.'
})
quantity?: number;

@ApiPropertyOptional({
description: 'The ID of a subscription to add this invoice item to. When left blank, the invoice item will be be added to the next upcoming scheduled invoice. When set, scheduled invoices for subscriptions other than the specified subscription will ignore the invoice item. Use this when you want to express that an invoice item has been accrued within the context of a particular subscription.'
})
subscription?: string;

@ApiPropertyOptional({
description: 'Only required if a [default tax behavior](https://stripe.com/docs/tax/products-prices-tax-categories-tax-behavior#setting-a-default-tax-behavior-(recommended)) was not provided in the Stripe Tax settings. Specifies whether the price is considered inclusive of taxes or exclusive of taxes. One of `inclusive`, `exclusive`, or `unspecified`. Once specified as either `inclusive` or `exclusive`, it cannot be changed.',
enum: TaxBehaviors
})
taxBehavior?: TaxBehavior;

@ApiPropertyOptional({
description: 'A [tax code](https://stripe.com/docs/tax/tax-categories) ID.'
})
taxCode?:string;

@ApiPropertyOptional({
description: 'The tax rates which apply to the invoice item. When set, the `default_tax_rates` on the invoice do not apply to this invoice item.',
isArray: true,
type: String
})
taxRates?: Array<string>;

@ApiPropertyOptional({
description: 'The integer unit amount in cents (or local equivalent) of the charge to be applied to the upcoming invoice. This `unit_amount` will be multiplied by the quantity to get the full amount. Passing in a negative `unit_amount` will reduce the `amount_due` on the invoice.'
})
unitAmount?: number;

@ApiPropertyOptional({
description: 'Same as `unit_amount`, but accepts a decimal value in cents (or local equivalent) with at most 12 decimal places. Only one of `unit_amount` and `unit_amount_decimal` can be set.'
})
unitAmountDecimal?: string;
}
3 changes: 3 additions & 0 deletions libs/stripe/src/lib/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ export * from './update-customer.dto';
export * from './save-webhook-endpoint.dto';
export * from './save-test-clock.dto';
export * from './update-invoice.dto';
export * from './create-invoice.dto';
export * from './create-invoice-item.dto';

export * from './stripe/customer.dto';
export * from './stripe/subscription-item.dto';
export * from './stripe/subscription.dto';
export * from './subscription-schedule.dto';
export * from './stripe/invoice-line-item.dto';
export * from './stripe/invoice.dto';
export * from './stripe/invoice-item.dto';
export * from './stripe/usage-record.dto';
export * from './stripe/plan.dto';
export * from './stripe/price.dto';
Expand Down
106 changes: 106 additions & 0 deletions libs/stripe/src/lib/dto/stripe/invoice-item.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { BaseDto } from '../base.dto';
import { CustomerDto } from './customer.dto';
import { DiscountDto, PeriodDto } from '../shared.dto';
import { InvoiceDto } from './invoice.dto';
import { PlanDto } from './plan.dto';
import { PriceDto } from './price.dto';

export class InvoiceItemDto extends BaseDto {
@ApiPropertyOptional({
description: 'Amount (in the `currency` specified) of the invoice item. This should always be equal to `unit_amount * quantity`.'
})
amount: number;

@ApiPropertyOptional()
currency: string;

@ApiPropertyOptional()
customerId?: string;

@ApiPropertyOptional()
customer?: CustomerDto;

@ApiPropertyOptional({
description: 'Time at which the object was created. Measured in seconds since the Unix epoch.'
})
date?: Date;

@ApiPropertyOptional()
description: string;

@ApiPropertyOptional({
description: 'If true, discounts will apply to this invoice item. Always false for prorations.'
})
discountable?: boolean;

@ApiPropertyOptional({
isArray: true,
type: String
})
discountIds?: Array<string>;

@ApiPropertyOptional({
description: 'The discounts which apply to the invoice item. Item discounts are applied before invoice discounts. Use `expand[]=discounts` to expand each discount.',
isArray: true,
type: String
})
discounts?: Array<DiscountDto>;

@ApiPropertyOptional()
invoiceId?: string;

@ApiPropertyOptional()
invoice?: InvoiceDto;

@ApiPropertyOptional()
period?: PeriodDto;

@ApiPropertyOptional({
description: 'If the invoice item is a proration, the plan of the subscription that the proration was computed for.'
})
plan?: PlanDto;

@ApiPropertyOptional({
description: 'The price of the invoice item.'
})
price?: PriceDto;

@ApiPropertyOptional({
description: 'Whether the invoice item was created automatically as a proration adjustment when the customer switched plans.'
})
proration?: boolean;

@ApiPropertyOptional({
description: 'Quantity of units for the invoice item. If the invoice item is a proration, the quantity of the subscription that the proration was computed for.'
})
quantity?: number;

@ApiPropertyOptional({
description: ' The subscription that this invoice item has been created for, if any.'
})
subscriptionId?: string;

@ApiPropertyOptional({
description: 'The subscription item that this invoice item has been created for, if any.'
})
subscriptionItemId?: string;

@ApiPropertyOptional({
description: 'The tax rates which apply to the invoice item. When set, the `default_tax_rates` on the invoice do not apply to this invoice item.',
isArray: true,
type: String
})
taxRates?: Array<string>;

@ApiPropertyOptional({
description: 'The integer unit amount in cents (or local equivalent) of the charge to be applied to the upcoming invoice. This `unit_amount` will be multiplied by the quantity to get the full amount. Passing in a negative `unit_amount` will reduce the `amount_due` on the invoice.'
})
unitAmount?: number;

@ApiPropertyOptional({
description: 'Same as `unit_amount`, but accepts a decimal value in cents (or local equivalent) with at most 12 decimal places. Only one of `unit_amount` and `unit_amount_decimal` can be set.'
})
unitAmountDecimal?: string;

}
Loading

0 comments on commit 09aede1

Please sign in to comment.