-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.js
237 lines (199 loc) · 5.56 KB
/
index.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* External dependencies
*/
import find from 'lodash/collection/find';
import includes from 'lodash/collection/includes';
import moment from 'moment';
/**
* Internal dependencies
*/
import i18n from 'lib/mixins/i18n';
import { isDomainMapping, isDomainRegistration, isTheme, isPlan } from 'lib/products-values';
/**
* Returns an array of sites objects, each of which contains an array of purchases.
*
* @param {array} purchases An array of purchase objects.
* @param {array} sites An array of site objects
* @return {array} An array of sites with purchases attached.
*/
function getPurchasesBySite( purchases, sites ) {
return purchases.reduce( ( result, currentValue ) => {
const site = find( result, { id: currentValue.siteId } );
if ( site ) {
site.purchases = site.purchases.concat( currentValue );
} else {
const siteObject = find( sites, { ID: currentValue.siteId } );
result = result.concat( {
domain: currentValue.domain,
id: currentValue.siteId,
name: currentValue.siteName,
/* if the purchase is attached to a deleted site,
* there will be no site with this ID in `sites`, so
* we fall back on the domain. */
slug: siteObject ? siteObject.slug : currentValue.domain,
title: currentValue.siteName ? currentValue.siteName : currentValue.domain,
purchases: [ currentValue ]
} );
}
return result;
}, [] );
}
function getName( purchase ) {
if ( isDomainRegistration( purchase ) ) {
return purchase.meta;
}
return purchase.productName;
}
function getSubscriptionEndDate( purchase ) {
return purchase.expiryMoment.format( 'LL' );
}
function hasPaymentMethod( purchase ) {
return isPaidWithPaypal( purchase ) || isPaidWithCreditCard( purchase );
}
function hasPrivateRegistration( purchase ) {
return purchase.hasPrivateRegistration;
}
/**
* Checks if a purchase can be cancelled.
* Returns true for purchases that aren't expired
* Also returns true for purchases whether or not they are after the refund period.
* Purchases included with a plan can't be cancelled.
*
* @param {Object} purchase
* @return {boolean}
*/
function isCancelable( purchase ) {
if ( isIncludedWithPlan( purchase ) ) {
return false;
}
if ( isExpired( purchase ) ) {
return false;
}
if ( isRefundable( purchase ) ) {
return true;
}
return purchase.canDisableAutoRenew;
}
function isExpired( purchase ) {
return 'expired' === purchase.expiryStatus;
}
function isExpiring( purchase ) {
return includes( [
'cardExpired',
'cardExpiring',
'manualRenew',
'expiring'
], purchase.expiryStatus );
}
function isIncludedWithPlan( purchase ) {
return 'included' === purchase.expiryStatus;
}
function isOneTimePurchase( purchase ) {
return 'oneTimePurchase' === purchase.expiryStatus;
}
function isPaidWithPaypal( purchase ) {
return 'paypal' === purchase.payment.type;
}
function isRedeemable( purchase ) {
return purchase.isRedeemable;
}
/**
* Checks if a purchase can be canceled and refunded.
* Purchases usually can be refunded up to 30 days after purchase.
* Domains and domain mappings can be refunded up to 48 hours.
* Purchases included with plan can't be refunded.
*
* @param {Object} purchase
* @return {boolean}
*/
function isRefundable( purchase ) {
return purchase.isRefundable;
}
/**
* Checks if an expired purchase can be removed from a user account.
* Only domains and domain mappings can be removed.
* Purchases included with plan can't be removed.
*
* @param {Object} purchase
* @return {boolean}
*/
function isRemovable( purchase ) {
if ( isIncludedWithPlan( purchase ) ) {
return false;
}
return (
( isDomainRegistration( purchase ) || isDomainMapping( purchase ) ) &&
isExpired( purchase )
);
}
function isRenewable( purchase ) {
return purchase.isRenewable;
}
function isRenewing( purchase ) {
return includes( [ 'active', 'autoRenewing' ], purchase.expiryStatus );
}
function isPaidWithCreditCard( purchase ) {
return 'credit_card' === purchase.payment.type && hasCreditCardData( purchase );
}
function hasCreditCardData( purchase ) {
return Boolean( purchase.payment.creditCard.expiryMoment );
}
function creditCardExpiresBeforeSubscription( purchase ) {
return isPaidWithCreditCard( purchase ) && hasCreditCardData( purchase ) && purchase.payment.creditCard.expiryMoment.diff( purchase.expiryMoment, 'months' ) < 0;
}
function monthsUntilCardExpires( purchase ) {
return purchase.payment.creditCard.expiryMoment.diff( moment(), 'months' );
}
function paymentLogoType( purchase ) {
if ( isPaidWithCreditCard( purchase ) ) {
return purchase.payment.creditCard.type;
}
if ( isPaidWithPaypal( purchase ) ) {
return 'paypal';
}
return null;
}
function purchaseType( purchase ) {
if ( isTheme( purchase ) ) {
return i18n.translate( 'Premium Theme' );
}
if ( isPlan( purchase ) ) {
return i18n.translate( 'Site Plan' );
}
if ( isDomainRegistration( purchase ) ) {
return purchase.productName;
}
if ( purchase.meta ) {
return purchase.meta;
}
return null;
}
function showCreditCardExpiringWarning( purchase ) {
return ! isIncludedWithPlan( purchase ) &&
isPaidWithCreditCard( purchase ) &&
creditCardExpiresBeforeSubscription( purchase ) &&
monthsUntilCardExpires( purchase ) < 3;
}
export {
creditCardExpiresBeforeSubscription,
getName,
getPurchasesBySite,
getSubscriptionEndDate,
hasPaymentMethod,
hasPrivateRegistration,
isCancelable,
isPaidWithCreditCard,
isPaidWithPaypal,
isExpired,
isExpiring,
isIncludedWithPlan,
isOneTimePurchase,
isRedeemable,
isRefundable,
isRemovable,
isRenewable,
isRenewing,
paymentLogoType,
purchaseType,
showCreditCardExpiringWarning,
}