@@ -7,7 +7,8 @@ class SubscriptionHydration {
7
7
/**
8
8
* @typedef ConstructorOptions
9
9
* @property {import('./paddle/api.js') } api
10
- * @property {import('./subscription-hook-storage.js') hookStorage }
10
+ * @property {import('./subscription-hook-storage.js') } hookStorage
11
+ * @property {import('./subscription-info.js') } subscriptionInfo
11
12
*/
12
13
13
14
/**
@@ -16,10 +17,11 @@ class SubscriptionHydration {
16
17
* @param {ConstructorOptions } options
17
18
*/
18
19
19
- constructor ( storagePath , { api = { } , hookStorage = { } } ) {
20
+ constructor ( storagePath , { api = { } , hookStorage = { } , subscriptionInfo = { } } ) {
20
21
this . _resourceName = 'subscription'
21
22
this . _storage = resource ( { documentPath : storagePath , resourceName : this . _resourceName } )
22
23
this . _api = api
24
+ this . _subscriptionInfo = subscriptionInfo
23
25
this . _hookStorage = hookStorage
24
26
}
25
27
@@ -74,20 +76,12 @@ class SubscriptionHydration {
74
76
* @returns
75
77
*/
76
78
async hydrateSubscriptionCreated ( ids , { subscription_id } , checkoutId ) {
77
- {
78
- const { subscription } = await this . _storage . get ( ids )
79
- if ( subscription ?. status ?. length > 1 ) {
80
- console . log ( `Subscription ${ ids } was already hydrated. Exiting early and quietly.` )
81
- return
82
- }
83
- }
84
-
85
79
const subscriptions = await this . _api . getSubscription ( { subscription_id } )
86
80
if ( ! Array . isArray ( subscriptions ) || subscriptions . length < 1 ) {
87
81
return
88
82
}
89
83
90
- // more sanity checks here
84
+ // checks here
91
85
const subscription = subscriptions . at ( 0 )
92
86
if ( ! subscription . custom_data ?. _pi ?. ids ) {
93
87
console . error ( `Expected "subscription.custom_data._pi.ids" to be an Array. Did you set customData during Checkout? Please see https://developer.paddle.com/changelog/e9a54055ea46e-custom-data-now-available#custom-data-in-webhooks.` )
@@ -106,6 +100,17 @@ class SubscriptionHydration {
106
100
throw new Error ( SubscriptionHydration . HYDRATION_UNAUTHORIZED )
107
101
}
108
102
103
+ // check whether plan is already active
104
+ // because then we already received the webhook
105
+ const { plan_id : plan } = subscription
106
+ {
107
+ const { subscription : localSubscription } = await this . _storage . get ( ids )
108
+ const subscriptionStatus = await this . _subscriptionInfo . getAllSubscriptionsStatus ( localSubscription )
109
+ if ( subscriptionStatus [ plan ] === true ) {
110
+ return
111
+ }
112
+ }
113
+
109
114
const subscriptionCreatedPayload = {
110
115
alert_id : SubscriptionHydration . HYDRATION_SUBSCRIPTION_CREATED ,
111
116
alert_name : SubscriptionHydration . HYDRATION_SUBSCRIPTION_CREATED ,
0 commit comments