@@ -2,24 +2,40 @@ import type Stripe from 'stripe'
2
2
import type { SubscriptionModel } from '../../../../orm/src/models/Subscription'
3
3
import type { UserModel } from '../../../../orm/src/models/User'
4
4
5
- import { stripe } from '..'
5
+ import { manageCustomer , managePrice , stripe } from '..'
6
6
import Subscription from '../../../../orm/src/models/Subscription'
7
7
8
8
export interface SubscriptionManager {
9
- create : ( user : UserModel , type : string , params : Stripe . SubscriptionCreateParams ) => Promise < Stripe . Response < Stripe . Subscription > >
9
+ create : ( user : UserModel , type : string , priceId : string , params : Partial < Stripe . SubscriptionCreateParams > ) => Promise < Stripe . Response < Stripe . Subscription > >
10
10
isValid : ( user : UserModel , type : string ) => Promise < boolean >
11
+ isIncomplete : ( user : UserModel , type : string ) => Promise < boolean >
11
12
}
12
13
13
14
export const manageSubscription : SubscriptionManager = ( ( ) => {
14
- async function create ( user : UserModel , type : string , params : Stripe . SubscriptionCreateParams ) : Promise < Stripe . Response < Stripe . Subscription > > {
15
- if ( ! user . hasStripeId ( ) ) {
16
- throw new Error ( 'Customer does not exist in Stripe' )
17
- }
15
+ async function create ( user : UserModel , type : string , priceId : string , params : Partial < Stripe . SubscriptionCreateParams > ) : Promise < Stripe . Response < Stripe . Subscription > > {
16
+ const price = await managePrice . retrieveByLookupKey ( priceId )
17
+
18
+ if ( ! price )
19
+ throw new Error ( 'Price does not exist in stripe' )
20
+
21
+ const subscriptionItems = [ {
22
+ price : price . id ,
23
+ quantity : 1 ,
24
+ } ]
25
+
26
+ const customerId = await manageCustomer . createOrGetStripeUser ( user , { } ) . then ( ( customer ) => {
27
+ if ( ! customer || ! customer . id ) {
28
+ throw new Error ( 'Customer does not exist in Stripe' )
29
+ }
30
+
31
+ return customer . id
32
+ } )
18
33
19
- const defaultParams : Partial < Stripe . SubscriptionCreateParams > = {
20
- customer : user . stripeId ( ) ,
34
+ const defaultParams : Stripe . SubscriptionCreateParams = {
35
+ customer : customerId ,
21
36
payment_behavior : 'default_incomplete' ,
22
37
expand : [ 'latest_invoice.payment_intent' ] ,
38
+ items : subscriptionItems ,
23
39
}
24
40
25
41
const mergedParams = { ...defaultParams , ...params }
@@ -35,14 +51,21 @@ export const manageSubscription: SubscriptionManager = (() => {
35
51
return subscription . stripe_status === 'active'
36
52
}
37
53
38
- async function isIncomplete ( subscription : SubscriptionModel ) : Promise < boolean > {
39
- return subscription . stripe_status === 'incomplete'
40
- }
41
-
42
54
async function isTrial ( subscription : SubscriptionModel ) : Promise < boolean > {
43
55
return subscription . stripe_status === 'trialing'
44
56
}
45
57
58
+ async function isIncomplete ( user : UserModel , type : string ) : Promise < boolean > {
59
+ const subscription = await Subscription . where ( 'user_id' , user . id )
60
+ . where ( 'type' , type )
61
+ . first ( )
62
+
63
+ if ( ! subscription )
64
+ return false
65
+
66
+ return subscription . stripe_status === 'incomplete'
67
+ }
68
+
46
69
async function isValid ( user : UserModel , type : string ) : Promise < boolean > {
47
70
const subscription = await Subscription . where ( 'user_id' , user . id )
48
71
. where ( 'type' , type )
0 commit comments