/
record-new-purchase.ts
161 lines (141 loc) · 4.26 KB
/
record-new-purchase.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {Stripe} from 'stripe'
import {first, isEmpty} from 'lodash'
import {type Purchase, getSdk} from '@skillrecordings/database'
import {
getStripeSdk,
Context as StripeContext,
} from '@skillrecordings/stripe-sdk'
import {NEW_INDIVIDUAL_PURCHASE} from '@skillrecordings/types'
import {determinePurchaseType, PurchaseType} from './determine-purchase-type'
import {
PaymentProvider,
PurchaseInfo,
} from './providers/default-payment-options'
export const NO_ASSOCIATED_PRODUCT = 'no-associated-product'
export class PurchaseError extends Error {
checkoutSessionId: string
email?: string
productId?: string
constructor(
message: string,
checkoutSessionId: string,
email?: string,
productId?: string,
) {
super(message)
this.name = 'PurchaseError'
this.checkoutSessionId = checkoutSessionId
this.email = email
this.productId = productId
}
}
type StripeDataOptions = {
checkoutSessionId: string
stripeCtx?: StripeContext
}
export async function stripeData(options: StripeDataOptions) {
const {stripeCtx, checkoutSessionId} = options
const {getCheckoutSession} = getStripeSdk({ctx: stripeCtx})
const checkoutSession = await getCheckoutSession(checkoutSessionId)
const {customer, line_items, payment_intent, metadata} = checkoutSession
const {email, name, id: stripeCustomerId} = customer as Stripe.Customer
const lineItem = first(line_items?.data) as Stripe.LineItem
const stripePrice = lineItem.price
const quantity = lineItem.quantity || 1
const stripeProduct = stripePrice?.product as Stripe.Product
const {charges} = payment_intent as Stripe.PaymentIntent
const stripeCharge = first<Stripe.Charge>(charges.data)
const stripeChargeId = stripeCharge?.id as string
const stripeChargeAmount = stripeCharge?.amount || 0
// extract MerchantCoupon identifier if used for purchase
const discount = first(lineItem.discounts)
const stripeCouponId = discount?.discount.coupon.id
return {
stripeCustomerId,
email,
name,
stripeProductId: stripeProduct.id,
stripeProduct,
stripeChargeId,
stripeCouponId,
quantity,
stripeChargeAmount,
metadata,
}
}
export async function recordNewPurchase(
checkoutSessionId: string,
options: {paymentProvider: PaymentProvider},
): Promise<{
user: any
purchase: Purchase
purchaseInfo: PurchaseInfo
purchaseType: PurchaseType
}> {
const {
findOrCreateUser,
findOrCreateMerchantCustomer,
createMerchantChargeAndPurchase,
getMerchantProduct,
} = getSdk()
const purchaseInfo = await options.paymentProvider.getPurchaseInfo(
checkoutSessionId,
)
const {
customerIdentifier,
email,
name,
productIdentifier,
chargeIdentifier,
couponIdentifier,
quantity,
chargeAmount,
metadata,
} = purchaseInfo
if (!email) throw new PurchaseError(`no-email`, checkoutSessionId)
const {user, isNewUser} = await findOrCreateUser(email, name)
const merchantProduct = await getMerchantProduct(productIdentifier)
if (!merchantProduct)
throw new PurchaseError(
NO_ASSOCIATED_PRODUCT,
checkoutSessionId,
email,
productIdentifier,
)
const {id: merchantProductId, productId, merchantAccountId} = merchantProduct
const {id: merchantCustomerId} = await findOrCreateMerchantCustomer({
user: user,
identifier: customerIdentifier,
merchantAccountId,
})
const purchase = await createMerchantChargeAndPurchase({
userId: user.id,
stripeChargeId: chargeIdentifier,
stripeCouponId: couponIdentifier,
merchantAccountId,
merchantProductId,
merchantCustomerId,
productId,
stripeChargeAmount: chargeAmount,
quantity,
bulk: metadata?.bulk === 'true',
country: metadata?.country,
appliedPPPStripeCouponId: metadata?.appliedPPPStripeCouponId,
upgradedFromPurchaseId: metadata?.upgradedFromPurchaseId,
usedCouponId: metadata?.usedCouponId,
checkoutSessionId,
})
let purchaseType = await determinePurchaseType({checkoutSessionId})
if (purchaseType === null) {
// TODO: report that we did not get a valid purchase type for this checkoutSessionId
//
// then fallback to NEW_INDIVIDUAL_PURCHASE
purchaseType = NEW_INDIVIDUAL_PURCHASE
}
return {
purchase,
user,
purchaseInfo,
purchaseType,
}
}