Skip to content

Commit

Permalink
feat: added subscriptions invoices endpoints (#7)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
  • Loading branch information
rostyk-kanafotskyy authored Aug 2, 2024
1 parent ff12711 commit 0a1d4a8
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 56 deletions.
59 changes: 59 additions & 0 deletions src/endpoints/subscription-invoices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import RequestFactory from '../factories/request'
import { buildURL } from '../utils/helpers'

class SubscriptionInvoicesEndpoint {
constructor(endpoint) {
const config = { ...endpoint }
this.request = new RequestFactory(config)

this.endpoint = 'subscriptions/invoices'
}

All() {
const { filter, limit, offset } = this

return this.request.send(
buildURL(this.endpoint, {
filter,
limit,
offset
}),
'GET'
)
}

Get(id) {
return this.request.send(`${this.endpoint}/${id}`, 'GET')
}

GetPayments(invoiceId) {
return this.request.send(`${this.endpoint}/${invoiceId}/payments`, 'GET')
}

GetPayment(invoiceId, paymentId) {
return this.request.send(
`${this.endpoint}/${invoiceId}/payments/${paymentId}`,
'GET'
)
}

Filter(filter) {
this.filter = filter

return this
}

Limit(value) {
this.limit = value

return this
}

Offset(value) {
this.offset = value

return this
}
}

export default SubscriptionInvoicesEndpoint
9 changes: 6 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import { SubscriptionSchedulesEndpoint } from './types/subscription-schedules'
import { CustomApisEndpoint } from './types/custom-apis'
import { SubscriptionDunningRulesEndpoint } from './types/subscription-dunning-rules'
import { SubscriptionProrationPoliciesEndpoint } from './types/subscription-proration-policies'
import { SubscriptionInvoicesEndpoint } from './types/subscription-invoices'

export * from './types/config'
export * from './types/storage'
Expand Down Expand Up @@ -144,6 +145,7 @@ export * from './types/subscription-schedules'
export * from './types/custom-apis'
export * from './types/subscription-dunning-rules'
export * from './types/subscription-proration-policies'
export * from './types/subscription-invoices'

// UMD
export as namespace elasticpath
Expand Down Expand Up @@ -205,13 +207,14 @@ export class ElasticPath {
SubscriptionOfferings: SubscriptionOfferingsEndpoint
OneTimePasswordTokenRequest: OneTimePasswordTokenRequestEndpoint
Subscriptions: SubscriptionsEndpoint
RulePromotions : RulePromotionsEndpoint
SubscriptionSubscribers : SubscriptionSubscribersEndpoint
SubscriptionJobs : SubscriptionJobsEndpoint
RulePromotions: RulePromotionsEndpoint
SubscriptionSubscribers: SubscriptionSubscribersEndpoint
SubscriptionJobs: SubscriptionJobsEndpoint
SubscriptionSchedules: SubscriptionSchedulesEndpoint
CustomApis: CustomApisEndpoint
SubscriptionDunningRules: SubscriptionDunningRulesEndpoint
SubscriptionProrationPolicies: SubscriptionProrationPoliciesEndpoint
SubscriptionInvoices: SubscriptionInvoicesEndpoint

Cart(id?: string): CartEndpoint // This optional cart id is super worrying when using the SDK in a node server :/
constructor(config: Config)
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ import SubscriptionOfferingsEndpoint from './endpoints/subscription-offerings'
import SubscriptionsEndpoint from './endpoints/subscriptions'
import RulePromotionsEndpoint from './endpoints/rule-promotions'
import SubscriptionSubscribersEndpoint from './endpoints/subscription-subscribers'
import SubscriptionJobsEndpoint from './endpoints/subscription-jobs'
import SubscriptionJobsEndpoint from './endpoints/subscription-jobs'
import SubscriptionSchedulesEndpoint from './endpoints/subscription-schedules'
import SubscriptionDunningRulesEndpoint from './endpoints/subscription-dunning-rules'
import SubscriptionProrationPoliciesEndpoint from './endpoints/subscription-proration-policies'
import SubscriptionInvoicesEndpoint from './endpoints/subscription-invoices'

import {
cartIdentifier,
Expand Down Expand Up @@ -144,7 +145,9 @@ export default class ElasticPath {
this.SubscriptionSchedules = new SubscriptionSchedulesEndpoint(config)
this.CustomApis = new CustomApisEndpoint(config)
this.SubscriptionDunningRules = new SubscriptionDunningRulesEndpoint(config)
this.SubscriptionProrationPolicies = new SubscriptionProrationPoliciesEndpoint(config)
this.SubscriptionProrationPolicies =
new SubscriptionProrationPoliciesEndpoint(config)
this.SubscriptionInvoices = new SubscriptionInvoicesEndpoint(config)
}

// Expose `Cart` class on ElasticPath class
Expand Down
178 changes: 178 additions & 0 deletions src/types/subscription-invoices.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* Subscription Invoices
* Description: Invoices represent the amount a customer owes for a subscription.
* Elastic Path Subscriptions generates an invoice for every period in a subscription billing cycle.
* DOCS: https://elasticpath.dev/docs/api/subscriptions/invoices
*/
import {
Identifiable,
CrudQueryableResource,
Resource,
ResourceList,
ResourcePage
} from './core'
import { ItemTaxObject } from './cart'
import { Price } from './price'

interface SubscriptionInvoiceItemPrice extends Omit<Price, 'includes_tax'> {
includes_tax?: boolean
}

interface SubscriptionInvoiceItem {
description: string
price: SubscriptionInvoiceItemPrice
product_id?: string
from_time_period?: string
until_time_period?: string
}

/**
* Core Subscription Invoice Base Interface
* For custom flows, extend this interface
* DOCS: https://elasticpath.dev/docs/api/subscriptions/get-invoice#responses
*/

export interface SubscriptionInvoiceBase {
type: 'subscription_invoice'
attributes: {
billing_period: {
start: string
end: string
}
invoice_items: SubscriptionInvoiceItem[]
tax_items?: ItemTaxObject[]
outstanding: boolean
number?: number
tax_required: boolean
payment_retries_limit_reached: boolean
updated_at?: string
created_at?: string
}
}

interface ProrationEvent {
proration_policy_id: string
billing_cost_before_proration: number
refunded_amount_for_unused_plan: number
new_plan_cost: number
prorated_at: string
}

export interface SubscriptionInvoice
extends Identifiable,
SubscriptionInvoiceBase {
meta: {
owner: 'store' | 'organization'
subscription_id?: string
subscriber_id?: string
price?: SubscriptionInvoiceItemPrice
timestamps: {
updated_at: string
created_at: string
taxes_added_at?: string
}
prorated_at: ProrationEvent[]
}
}

/**
* Core Subscription Invoice Payments Base Interface
* DOCS: https://elasticpath.dev/docs/api/subscriptions/get-invoice-payment#responses
*/

export interface SubscriptionInvoicePaymentBase {
type: 'subscription_invoice_payment'
attributes: {
success: boolean
gateway: string
external_payment_id?: string
failure_detail?: {
reason?: string
}
amount: SubscriptionInvoiceItemPrice
}
}

export interface SubscriptionInvoicePayment
extends Identifiable,
SubscriptionInvoicePaymentBase {
meta: {
owner: 'store' | 'organization'
subscription_id: string
invoice_id: string
job_id: string
timestamps: {
updated_at: string
created_at: string
payment_taken_at?: string
}
}
}

/**
* Subscription Invoice Filtering
* DOCS: https://elasticpath.dev/docs/api/subscriptions/list-invoices#filtering
*/
export interface SubscriptionInvoiceFilter {
eq?: {
subscriber_id?: string
subscription_id?: string
outstanding?: string
tax_required?: string
}
}

/**
* Subscription Invoice Endpoints
* DOCS: https://elasticpath.dev/docs/api/subscriptions/list-invoices
*/
export interface SubscriptionInvoicesEndpoint {
endpoint: 'invoices'

/**
* List Invoices
* DOCS: https://elasticpath.dev/docs/api/subscriptions/list-invoices
* @constructor
*/
All(): Promise<ResourcePage<SubscriptionInvoice>>

/**
* Get Invoice
* DOCS: https://elasticpath.dev/docs/api/subscriptions/get-invoice
* @param id - The ID of the invoice.
* @constructor
*/
Get(id: string): Promise<Resource<SubscriptionInvoice>>

/**
* List Invoice Payments
* DOCS: https://elasticpath.dev/docs/api/subscriptions/list-invoice-payments
* @param invoiceId - The ID of the invoice to get the payments for.
* @constructor
*/
GetPayments(
invoiceId: string
): Promise<ResourceList<SubscriptionInvoicePayment>>

/**
* Get Invoice Payment
* DOCS: https://elasticpath.dev/docs/api/subscriptions/get-invoice-payment
* @param invoiceId - The ID of the invoice to get the payment for.
* @param paymentId - The ID of the payment.
* @constructor
*/
GetPayment(
invoiceId: string,
paymentId: string
): Promise<Resource<SubscriptionInvoicePayment>>

/**
* Subscription Invoice Filtering
* DOCS: https://elasticpath.dev/docs/api/subscriptions/list-invoices#filtering
*/
Filter(filter: SubscriptionInvoiceFilter): SubscriptionInvoicesEndpoint

Limit(value: number): SubscriptionInvoicesEndpoint

Offset(value: number): SubscriptionInvoicesEndpoint
}
Loading

0 comments on commit 0a1d4a8

Please sign in to comment.