@@ -22,15 +22,10 @@ class AppStoreSubscriptionService(private val userAppClient: UserAppClient, priv
22
22
23
23
fun handleInitialPurchase (tenant : String? , subscriptionPurchaseRequest : SubscriptionPurchaseRequest ) : UserAppSubscriptionOrder ? {
24
24
val receiptResponse = appStoreClient(tenant).verifyReceipt(ReceiptRequest (subscriptionPurchaseRequest.receipt))
25
-
26
25
logger.debug { " handleInitialPurchase: ReceiptResponse: $receiptResponse " }
27
-
28
26
if (receiptResponse.isValid()) {
29
-
30
27
val latestReceiptInfo = receiptResponse.latestReceiptInfo!! .stream().findFirst().get()
31
-
32
28
var effectivePrice = subscriptionPurchaseRequest.price
33
-
34
29
// intro offer period purchase
35
30
if (latestReceiptInfo.isInIntroOfferPeriod && subscriptionPurchaseRequest.effectivePrice != null ) {
36
31
effectivePrice = subscriptionPurchaseRequest.effectivePrice
@@ -48,7 +43,8 @@ class AppStoreSubscriptionService(private val userAppClient: UserAppClient, priv
48
43
appMarketplace = AppMarketplace .APP_STORE ,
49
44
expiryTimeMillis = latestReceiptInfo.expiresDateMs,
50
45
discountCode = subscriptionPurchaseRequest.discountCode,
51
- appStoreReceipt = subscriptionPurchaseRequest.receipt
46
+ appStoreReceipt = subscriptionPurchaseRequest.receipt,
47
+ isTrialPeriod = latestReceiptInfo.isTrialPeriod
52
48
)
53
49
54
50
return userAppClient.sendSubscriptionNotification(notification)
@@ -61,15 +57,10 @@ class AppStoreSubscriptionService(private val userAppClient: UserAppClient, priv
61
57
62
58
fun handleAutoRenewal (tenant : String? , subscriptionRenewRequest : SubscriptionRenewRequest ) {
63
59
val receiptResponse = appStoreClient(tenant).verifyReceipt(ReceiptRequest (subscriptionRenewRequest.receipt))
64
-
65
60
logger.debug { " handleAutoRenewal: ReceiptResponse: $receiptResponse " }
66
-
67
61
if (receiptResponse.isValid()) {
68
-
69
62
if (receiptResponse.latestReceiptInfo != null ) {
70
-
71
63
val latestReceiptInfo = receiptResponse.latestReceiptInfo.stream().findFirst().get()
72
-
73
64
val notification = UserAppSubscriptionNotification (
74
65
notificationType = NotificationType .SUBSCRIPTION_RENEWED ,
75
66
description = " Subscription renewal from AppStore" ,
@@ -81,11 +72,11 @@ class AppStoreSubscriptionService(private val userAppClient: UserAppClient, priv
81
72
countryCode = null ,
82
73
currencyCode = null ,
83
74
discountCode = subscriptionRenewRequest.discountCode,
84
- appStoreReceipt = subscriptionRenewRequest.receipt
75
+ appStoreReceipt = subscriptionRenewRequest.receipt,
76
+ isTrialPeriod = latestReceiptInfo.isTrialPeriod
85
77
)
86
78
87
79
val subscriptionOrder = userAppClient.sendSubscriptionNotification(notification);
88
-
89
80
checkArgument(subscriptionOrder != null ) { " Could not process SubscriptionRenewRequest $subscriptionRenewRequest in user app" }
90
81
}
91
82
} else {
@@ -99,7 +90,7 @@ class AppStoreSubscriptionService(private val userAppClient: UserAppClient, priv
99
90
logger.debug { " Processing StatusUpdateNotification: ${statusUpdateNotification.notificationType} " }
100
91
101
92
var notificationType = NotificationType .SUBSCRIPTION_PURCHASED
102
- val latestReceiptInfo = statusUpdateNotification.latestReceiptInfo
93
+ val latestReceiptInfo = statusUpdateNotification.unifiedReceipt. latestReceiptInfo?.maxBy { it.purchaseDateMs } !!
103
94
104
95
when (val appStoreNotificationType = parseAppStoreNotificationTypeEnum(statusUpdateNotification.notificationType)) {
105
96
@@ -119,7 +110,7 @@ class AppStoreSubscriptionService(private val userAppClient: UserAppClient, priv
119
110
}
120
111
121
112
// a customer downgrades
122
- AppStoreNotificationType .DID_CHANGE_RENEWAL_PREF , AppStoreNotificationType . DID_CHANGE_RENEWAL_STATUS -> {
113
+ AppStoreNotificationType .DID_CHANGE_RENEWAL_PREF -> {
123
114
// auto_renewal_product_id - product customer will auto renew at
124
115
// skipping it
125
116
// latest_receipt_info.original_transaction_id
@@ -158,7 +149,7 @@ class AppStoreSubscriptionService(private val userAppClient: UserAppClient, priv
158
149
// latest_receipt_info.expires_date_ms - date when the subscription will expire
159
150
}
160
151
161
- AppStoreNotificationType .DID_RENEW -> {
152
+ AppStoreNotificationType .DID_RENEW , AppStoreNotificationType . DID_CHANGE_RENEWAL_STATUS -> {
162
153
// restore service for a renewed subscription
163
154
// update customer's subscription to active / subscribe
164
155
@@ -186,7 +177,8 @@ class AppStoreSubscriptionService(private val userAppClient: UserAppClient, priv
186
177
countryCode = null ,
187
178
currencyCode = null ,
188
179
discountCode = null ,
189
- appStoreReceipt = statusUpdateNotification.latestReceipt
180
+ appStoreReceipt = statusUpdateNotification.unifiedReceipt.latestReceipt,
181
+ isTrialPeriod = latestReceiptInfo.isTrialPeriod
190
182
)
191
183
192
184
logger.debug {" Sending UserAppSubscriptionNotification: $notification " }
0 commit comments