-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprices.ts
109 lines (100 loc) · 2.64 KB
/
prices.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import type Stripe from 'stripe'
export const reasonResourceWasDeleted = (id: string) =>
`Stripe resource ${id} was deleted.`
export interface Config {
behavior: 'inclusive' | 'exclusive'
price: Stripe.Price
stripe: Stripe
created_at?: string
created_by?: string
}
/**
* Create a new `Stripe.Price` with a defined `tax_behavior`.
*
* In Stripe we can update only `nickname` and `metadata` of a `Price`, so if we
* need to define `tax_behavior` we have to create a new `Price`.
* https://stripe.com/docs/billing/subscriptions/products-and-prices#changing-prices
*/
export const createPriceWithTaxBehavior = async ({
behavior,
price,
stripe,
created_at,
created_by
}: Config) => {
if (price.deleted) {
throw new Error(reasonResourceWasDeleted(price.id))
}
if (!price.active) {
throw new Error(`price ${price.id} is not active`)
}
if (!price.unit_amount) {
throw new Error(`price ${price.id} has no unit_amount`)
}
let product_id: string
if (typeof price.product === 'string') {
product_id = price.product
} else {
if (price.product.deleted) {
throw new Error(reasonResourceWasDeleted(price.product.id))
} else {
product_id = price.product.id
}
}
// recreate the request body that created the original price
const {
created,
id,
livemode,
lookup_key,
nickname,
object,
product,
recurring,
tax_behavior,
tiers,
tiers_mode,
transform_quantity,
unit_amount,
unit_amount_decimal,
type,
...original_price
} = price
let custom_unit_amount = undefined
if (price.custom_unit_amount) {
custom_unit_amount = {
enabled: true,
maximum: price.custom_unit_amount.maximum || undefined,
minimum: price.custom_unit_amount.minimum || undefined,
preset: price.custom_unit_amount.preset || undefined
}
}
let price_recurring = undefined
if (price.recurring) {
const aggregate_usage = price.recurring.aggregate_usage || undefined
const trial_period_days = price.recurring.trial_period_days || undefined
price_recurring = {
...price.recurring,
aggregate_usage,
trial_period_days
}
}
let metadata = original_price.metadata
if (created_at) {
metadata = { ...metadata, created_at }
}
if (created_by) {
metadata = { ...metadata, created_by }
}
const new_price = await stripe.prices.create({
...original_price,
custom_unit_amount,
metadata,
nickname: nickname || `nickname of original price ${price.id}`,
product: product_id,
recurring: price_recurring,
tax_behavior: behavior,
unit_amount: price.unit_amount
})
return new_price
}