|
|
import { |
|
|
camelOrSnakeSlug, |
|
|
getPlan, |
|
|
getTermDuration, |
|
|
GOOGLE_WORKSPACE_BUSINESS_STARTER_YEARLY, |
|
|
GSUITE_EXTRA_LICENSE_SLUG, |
|
|
isBiennially, |
|
|
isBlogger, |
|
|
isBloggerPlan, |
|
|
isBusiness, |
|
|
isConciergeSession, |
|
|
isCustomDesign, |
|
|
isDIFMProduct, |
|
|
isDomainMapping, |
|
|
isDomainMoveInternal, |
|
|
isDomainProduct, |
|
|
isDomainRegistration, |
|
|
isDomainTransfer, |
|
|
isEcommerce, |
|
|
isFreeWordPressComDomain, |
|
|
isGSuiteOrExtraLicenseOrGoogleWorkspace, |
|
|
isGSuiteOrGoogleWorkspace, |
|
|
isJetpackPlan, |
|
|
isJetpackProduct, |
|
|
isMonthlyProduct, |
|
|
isNoAds, |
|
|
isP2Plus, |
|
|
isPersonal, |
|
|
isPlan, |
|
|
isPremium, |
|
|
isPro, |
|
|
isRenewable, |
|
|
isSiteRedirect, |
|
|
isSpaceUpgrade, |
|
|
isStarter, |
|
|
isTriennially, |
|
|
isTitanMail, |
|
|
isUnlimitedSpace, |
|
|
isUnlimitedThemes, |
|
|
isVideoPress, |
|
|
isWpComBloggerPlan, |
|
|
isWpComFreePlan, |
|
|
TITAN_MAIL_MONTHLY_SLUG, |
|
|
TITAN_MAIL_YEARLY_SLUG, |
|
|
isAkismetProduct, |
|
|
isWpcomEnterpriseGridPlan, |
|
|
is100Year, |
|
|
PLAN_FREE, |
|
|
} from '@automattic/calypso-products'; |
|
|
import { isDomainForGravatarFlow, isHundredYearDomainFlow } from '@automattic/onboarding'; |
|
|
import { isWpComProductRenewal as isRenewal } from '@automattic/wpcom-checkout'; |
|
|
import { getTld } from 'calypso/lib/domains'; |
|
|
import { domainProductSlugs } from 'calypso/lib/domains/constants'; |
|
|
import type { WithCamelCaseSlug, WithSnakeCaseSlug } from '@automattic/calypso-products'; |
|
|
import type { SiteDetails } from '@automattic/data-stores'; |
|
|
import type { |
|
|
ResponseCart, |
|
|
ResponseCartProduct, |
|
|
RequestCartProduct, |
|
|
RequestCartProductExtra, |
|
|
GSuiteProductUser, |
|
|
MinimalRequestCartProduct, |
|
|
} from '@automattic/shopping-cart'; |
|
|
|
|
|
export const DOMAIN_PRICE_RULE = { |
|
|
ONE_TIME_PRICE: 'ONE_TIME_PRICE', |
|
|
FREE_DOMAIN: 'FREE_DOMAIN', |
|
|
FREE_FOR_FIRST_YEAR: 'FREE_FOR_FIRST_YEAR', |
|
|
FREE_WITH_PLAN: 'FREE_WITH_PLAN', |
|
|
PRICE: 'PRICE', |
|
|
DOMAIN_MOVE_PRICE: 'DOMAIN_MOVE_PRICE', |
|
|
INCLUDED_IN_HIGHER_PLAN: 'INCLUDED_IN_HIGHER_PLAN', |
|
|
UPGRADE_TO_HIGHER_PLAN_TO_BUY: 'UPGRADE_TO_HIGHER_PLAN_TO_BUY', |
|
|
} as const; |
|
|
|
|
|
type DomainPriceRule = ( typeof DOMAIN_PRICE_RULE )[ keyof typeof DOMAIN_PRICE_RULE ]; |
|
|
|
|
|
export type ObjectWithProducts = Pick< ResponseCart, 'products' >; |
|
|
|
|
|
export function getAllCartItems( cart?: ObjectWithProducts ): ResponseCartProduct[] { |
|
|
return ( cart && cart.products ) || []; |
|
|
} |
|
|
|
|
|
export function getRenewalItems( cart: ObjectWithProducts ): ResponseCartProduct[] { |
|
|
return getAllCartItems( cart ).filter( isRenewal ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasDIFMProduct( cart: ObjectWithProducts ): boolean { |
|
|
return cart && getAllCartItems( cart ).some( isDIFMProduct ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasPlan( cart: ObjectWithProducts ): boolean { |
|
|
return cart && getAllCartItems( cart ).some( isPlan ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasJetpackPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isJetpackPlan ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasP2PlusPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isP2Plus ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasEcommercePlan( cart: ObjectWithProducts ): boolean { |
|
|
return cart && getAllCartItems( cart ).some( isEcommerce ); |
|
|
} |
|
|
|
|
|
export function hasBloggerPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isBlogger ); |
|
|
} |
|
|
|
|
|
export function hasPersonalPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isPersonal ); |
|
|
} |
|
|
|
|
|
export function hasPremiumPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isPremium ); |
|
|
} |
|
|
|
|
|
export function hasProPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isPro ); |
|
|
} |
|
|
|
|
|
export function hasBusinessPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isBusiness ); |
|
|
} |
|
|
|
|
|
export function has100YearPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( is100Year ); |
|
|
} |
|
|
|
|
|
const isHundredYearDomain = ( product: { |
|
|
extra?: RequestCartProductExtra; |
|
|
volume?: number; |
|
|
} ): boolean => { |
|
|
return Boolean( product.extra?.is_hundred_year_domain || product.volume === 100 ); |
|
|
}; |
|
|
|
|
|
export function has100YearDomain( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isHundredYearDomain ); |
|
|
} |
|
|
|
|
|
export function hasStarterPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isStarter ); |
|
|
} |
|
|
|
|
|
export function hasMonthlyCartItem( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isMonthlyProduct ); |
|
|
} |
|
|
|
|
|
export function hasBiennialCartItem( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isBiennially ); |
|
|
} |
|
|
|
|
|
export function hasTriennialCartItem( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isTriennially ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasProduct( cart: ObjectWithProducts, productSlug: string ): boolean { |
|
|
return getAllCartItems( cart ).some( function ( cartItem ) { |
|
|
return cartItem.product_slug === productSlug; |
|
|
} ); |
|
|
} |
|
|
|
|
|
export function hasDomainRegistration( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isDomainRegistration ); |
|
|
} |
|
|
|
|
|
export function hasDomainBeingUsedForPlan( cart: ObjectWithProducts ): boolean { |
|
|
return getDomainRegistrations( cart ).some( ( registration ) => |
|
|
isDomainBeingUsedForPlan( cart, registration.meta ) |
|
|
); |
|
|
} |
|
|
|
|
|
export function hasRenewalItem( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isRenewal ); |
|
|
} |
|
|
|
|
|
export function hasTransferProduct( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isDomainTransfer ); |
|
|
} |
|
|
|
|
|
export function hasFreeCouponTransfersOnly( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).every( ( item ) => { |
|
|
return ( |
|
|
( isDomainTransfer( item ) && |
|
|
item.is_sale_coupon_applied && |
|
|
item.item_subtotal_integer === 0 ) || |
|
|
isPartialCredits( item ) |
|
|
); |
|
|
} ); |
|
|
} |
|
|
|
|
|
export function getDomainTransfers( cart: ObjectWithProducts ): ResponseCartProduct[] { |
|
|
return getAllCartItems( cart ).filter( |
|
|
( product ) => product.product_slug === domainProductSlugs.TRANSFER_IN |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasOnlyRenewalItems( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).every( ( item ) => isRenewal( item ) || isPartialCredits( item ) ); |
|
|
} |
|
|
|
|
|
export function hasConciergeSession( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isConciergeSession ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getCartItemBillPeriod( cartItem: ResponseCartProduct ): number { |
|
|
const billPeriod = cartItem.bill_period ? parseInt( cartItem.bill_period, 10 ) : undefined; |
|
|
if ( ! Number.isInteger( billPeriod ) ) { |
|
|
const plan = getPlan( cartItem.product_slug ); |
|
|
if ( plan ) { |
|
|
return getTermDuration( plan.term ) ?? 0; |
|
|
} |
|
|
} |
|
|
return billPeriod ?? 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasRenewableSubscription( cart: ObjectWithProducts ): boolean { |
|
|
return ( |
|
|
cart.products && |
|
|
getAllCartItems( cart ).some( ( cartItem ) => getCartItemBillPeriod( cartItem ) > 0 ) |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function planItem( productSlug: string ): { product_slug: string } | null { |
|
|
|
|
|
if ( isWpComFreePlan( productSlug ) || isWpcomEnterpriseGridPlan( productSlug ) ) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
return { |
|
|
product_slug: productSlug, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function supportsPrivacyProtectionPurchase( |
|
|
productSlug: string, |
|
|
productsList: { product_slug: string; is_privacy_protection_product_purchase_allowed?: boolean }[] |
|
|
): boolean { |
|
|
const product = Object.values( productsList ).find( |
|
|
( item ) => item.product_slug === productSlug |
|
|
); |
|
|
return product?.is_privacy_protection_product_purchase_allowed ?? false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function domainItem( |
|
|
productSlug: string, |
|
|
domain: string, |
|
|
source?: string |
|
|
): MinimalRequestCartProduct { |
|
|
const extra = source ? { extra: { source: source } } : undefined; |
|
|
|
|
|
return Object.assign( |
|
|
{ |
|
|
product_slug: productSlug, |
|
|
meta: domain, |
|
|
}, |
|
|
extra |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function themeItem( themeSlug: string, source?: string ): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: 'premium_theme', |
|
|
meta: themeSlug, |
|
|
extra: { |
|
|
source: source, |
|
|
}, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function marketplaceThemeProduct( productSlug: string ): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: productSlug, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function domainRegistration( properties: { |
|
|
productSlug: string; |
|
|
domain: string; |
|
|
source?: string; |
|
|
extra?: RequestCartProductExtra; |
|
|
volume?: number; |
|
|
} ): MinimalRequestCartProduct & { is_domain_registration: boolean } { |
|
|
return { |
|
|
...domainItem( properties.productSlug, properties.domain, properties.source ), |
|
|
is_domain_registration: true, |
|
|
...( properties.volume ? { volume: properties.volume } : {} ), |
|
|
...( properties.extra ? { extra: properties.extra } : {} ), |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function domainMapping( properties: { |
|
|
domain: string; |
|
|
source?: string; |
|
|
} ): MinimalRequestCartProduct { |
|
|
return domainItem( 'domain_map', properties.domain, properties.source ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function siteRedirect( properties: { |
|
|
domain?: string; |
|
|
source?: string; |
|
|
} ): MinimalRequestCartProduct { |
|
|
if ( ! properties.domain ) { |
|
|
throw new Error( 'Site redirect product requires a domain' ); |
|
|
} |
|
|
return domainItem( 'offsite_redirect', properties.domain, properties.source ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function domainTransfer( properties: { |
|
|
domain: string; |
|
|
source?: string; |
|
|
extra?: RequestCartProductExtra; |
|
|
volume?: number; |
|
|
} ): MinimalRequestCartProduct { |
|
|
return { |
|
|
...domainItem( domainProductSlugs.TRANSFER_IN, properties.domain, properties.source ), |
|
|
...( properties.extra ? { extra: properties.extra } : {} ), |
|
|
...( properties.volume ? { volume: properties.volume } : {} ), |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getGoogleApps( cart: ObjectWithProducts ): ResponseCartProduct[] { |
|
|
return getAllCartItems( cart ).filter( isGSuiteOrExtraLicenseOrGoogleWorkspace ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function googleApps( |
|
|
properties: { |
|
|
domain?: string; |
|
|
meta?: string; |
|
|
new_quantity?: number; |
|
|
quantity?: number | null; |
|
|
users?: GSuiteProductUser[]; |
|
|
} & ( WithCamelCaseSlug | WithSnakeCaseSlug ) |
|
|
): MinimalRequestCartProduct { |
|
|
const { quantity, new_quantity, users } = properties; |
|
|
|
|
|
const domainName = 'meta' in properties ? properties.meta : properties.domain; |
|
|
if ( ! domainName ) { |
|
|
throw new Error( 'A domain is required for a Google Apps item' ); |
|
|
} |
|
|
|
|
|
const productSlug = camelOrSnakeSlug( properties ) || GOOGLE_WORKSPACE_BUSINESS_STARTER_YEARLY; |
|
|
|
|
|
const extra = { |
|
|
google_apps_users: users, |
|
|
...( new_quantity ? { new_quantity } : {} ), |
|
|
}; |
|
|
|
|
|
return { |
|
|
...domainItem( productSlug, domainName ), |
|
|
...( quantity ? { quantity } : {} ), |
|
|
extra, |
|
|
}; |
|
|
} |
|
|
|
|
|
export function googleAppsExtraLicenses( properties: { |
|
|
domain: string; |
|
|
source?: string; |
|
|
users: GSuiteProductUser[]; |
|
|
} ): MinimalRequestCartProduct { |
|
|
const item = domainItem( GSUITE_EXTRA_LICENSE_SLUG, properties.domain, properties.source ); |
|
|
|
|
|
return { |
|
|
...item, |
|
|
extra: { google_apps_users: properties.users }, |
|
|
}; |
|
|
} |
|
|
|
|
|
export interface TitanProductProps { |
|
|
domain?: string; |
|
|
meta?: string; |
|
|
source?: string; |
|
|
quantity?: number | null; |
|
|
extra?: RequestCartProductExtra; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function titanMailProduct( |
|
|
properties: TitanProductProps, |
|
|
productSlug: string |
|
|
): MinimalRequestCartProduct { |
|
|
const domainName = properties.meta ?? properties.domain; |
|
|
|
|
|
if ( ! domainName ) { |
|
|
throw new Error( 'Titan mail requires a domain' ); |
|
|
} |
|
|
|
|
|
return { |
|
|
...domainItem( productSlug, domainName, properties.source ), |
|
|
quantity: properties.quantity, |
|
|
extra: properties.extra, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function titanMailYearly( properties: TitanProductProps ): MinimalRequestCartProduct { |
|
|
return titanMailProduct( properties, TITAN_MAIL_YEARLY_SLUG ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function titanMailMonthly( properties: TitanProductProps ): MinimalRequestCartProduct { |
|
|
return titanMailProduct( properties, TITAN_MAIL_MONTHLY_SLUG ); |
|
|
} |
|
|
|
|
|
export function hasGoogleApps( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isGSuiteOrExtraLicenseOrGoogleWorkspace ); |
|
|
} |
|
|
|
|
|
export function hasTitanMail( cart: ObjectWithProducts ): boolean { |
|
|
return getAllCartItems( cart ).some( isTitanMail ); |
|
|
} |
|
|
|
|
|
export function customDesignItem(): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: 'custom-design', |
|
|
}; |
|
|
} |
|
|
|
|
|
export function noAdsItem(): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: 'no-adverts/no-adverts.php', |
|
|
}; |
|
|
} |
|
|
|
|
|
export function videoPressItem(): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: 'videopress', |
|
|
}; |
|
|
} |
|
|
|
|
|
export function unlimitedSpaceItem(): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: 'unlimited_space', |
|
|
}; |
|
|
} |
|
|
|
|
|
export function unlimitedThemesItem(): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: 'unlimited_themes', |
|
|
}; |
|
|
} |
|
|
|
|
|
export function spaceUpgradeItem( slug: string ): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: slug, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function jetpackProductItem( slug: string ): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: slug, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function akismetProductItem( slug: string ): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: slug, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function renewableProductItem( slug: string ): MinimalRequestCartProduct { |
|
|
return { |
|
|
product_slug: slug, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDomainRegistrations( cart: ObjectWithProducts ): ResponseCartProduct[] { |
|
|
return getAllCartItems( cart ).filter( ( product ) => product.is_domain_registration === true ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDomainsInCart( cart: ObjectWithProducts ): ResponseCartProduct[] { |
|
|
return getAllCartItems( cart ).filter( |
|
|
( product ) => isDomainRegistration( product ) || isDomainMoveInternal( product ) |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDomainMappings( cart: ObjectWithProducts ): ResponseCartProduct[] { |
|
|
return getAllCartItems( cart ).filter( ( product ) => product.product_slug === 'domain_map' ); |
|
|
} |
|
|
|
|
|
function createRenewalCartItemFromProduct( |
|
|
product: ( WithCamelCaseSlug | WithSnakeCaseSlug ) & { |
|
|
is_domain_registration?: boolean; |
|
|
isDomainRegistration?: boolean; |
|
|
id: string | number; |
|
|
isRenewable?: boolean; |
|
|
} & Partial< RequestCartProduct > & { |
|
|
domain?: string; |
|
|
users?: GSuiteProductUser[]; |
|
|
}, |
|
|
properties: { domain?: string } |
|
|
) { |
|
|
const slug = camelOrSnakeSlug( product ); |
|
|
|
|
|
if ( isSpaceUpgrade( product ) ) { |
|
|
return spaceUpgradeItem( slug ); |
|
|
} |
|
|
|
|
|
if ( isJetpackProduct( product ) ) { |
|
|
return jetpackProductItem( slug ); |
|
|
} |
|
|
|
|
|
if ( isAkismetProduct( product ) ) { |
|
|
return akismetProductItem( slug ); |
|
|
} |
|
|
|
|
|
if ( isUnlimitedThemes( product ) ) { |
|
|
return unlimitedThemesItem(); |
|
|
} |
|
|
|
|
|
if ( isUnlimitedSpace( product ) ) { |
|
|
return unlimitedSpaceItem(); |
|
|
} |
|
|
|
|
|
if ( isVideoPress( product ) ) { |
|
|
return videoPressItem(); |
|
|
} |
|
|
|
|
|
if ( isCustomDesign( product ) ) { |
|
|
return customDesignItem(); |
|
|
} |
|
|
|
|
|
if ( isNoAds( product ) ) { |
|
|
return noAdsItem(); |
|
|
} |
|
|
|
|
|
if ( isSiteRedirect( product ) ) { |
|
|
return siteRedirect( properties ); |
|
|
} |
|
|
|
|
|
if ( isTitanMail( product ) ) { |
|
|
return titanMailProduct( product, slug ); |
|
|
} |
|
|
|
|
|
if ( isGSuiteOrGoogleWorkspace( product ) ) { |
|
|
return googleApps( product ); |
|
|
} |
|
|
|
|
|
if ( isPlan( product ) ) { |
|
|
return planItem( slug ); |
|
|
} |
|
|
|
|
|
if ( isDomainProduct( product ) ) { |
|
|
return domainItem( slug, properties.domain ?? '' ); |
|
|
} |
|
|
|
|
|
if ( isRenewable( product ) ) { |
|
|
return renewableProductItem( slug ); |
|
|
} |
|
|
|
|
|
return undefined; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getRenewalItemFromProduct( |
|
|
product: ( WithCamelCaseSlug | WithSnakeCaseSlug ) & { |
|
|
is_domain_registration?: boolean; |
|
|
isDomainRegistration?: boolean; |
|
|
id: string | number; |
|
|
isRenewable?: boolean; |
|
|
} & Partial< RequestCartProduct > & { |
|
|
domain?: string; |
|
|
users?: GSuiteProductUser[]; |
|
|
}, |
|
|
properties: { domain?: string; isMarketplaceProduct?: boolean } |
|
|
): MinimalRequestCartProduct { |
|
|
const cartItem = createRenewalCartItemFromProduct( product, properties ); |
|
|
if ( ! cartItem ) { |
|
|
throw new Error( 'This product cannot be renewed.' ); |
|
|
} |
|
|
return getRenewalItemFromCartItem( cartItem, product ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getRenewalItemFromCartItem< T extends MinimalRequestCartProduct >( |
|
|
cartItem: T, |
|
|
properties: { id: string | number } |
|
|
): T { |
|
|
return { |
|
|
...cartItem, |
|
|
extra: { |
|
|
...cartItem.extra, |
|
|
purchaseId: properties.id, |
|
|
purchaseType: 'renewal', |
|
|
}, |
|
|
}; |
|
|
} |
|
|
|
|
|
export function hasDomainInCart( cart: ObjectWithProducts, domain: string ): boolean { |
|
|
return getAllCartItems( cart ).some( ( product ) => { |
|
|
return ( |
|
|
product.meta === domain && |
|
|
( isDomainRegistration( product ) || isDomainMoveInternal( product ) ) |
|
|
); |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function updatePrivacyForDomain< T extends MinimalRequestCartProduct >( |
|
|
item: T, |
|
|
value: boolean |
|
|
): T { |
|
|
return { |
|
|
...item, |
|
|
extra: { |
|
|
...item.extra, |
|
|
privacy: value, |
|
|
}, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isPartialCredits( cartItem: ResponseCartProduct ): boolean { |
|
|
return cartItem.product_slug === 'wordpress-com-credits'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isNextDomainFree( cart?: ResponseCart, domain = '' ): boolean { |
|
|
if ( ! cart || ! cart.next_domain_is_free ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
if ( cart.next_domain_condition === 'blog' ) { |
|
|
if ( getTld( domain ) !== 'blog' ) { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
export function isDomainBundledWithPlan( cart?: ResponseCart, domain?: string ): boolean { |
|
|
const bundledDomain = cart?.bundled_domain ?? ''; |
|
|
return '' !== bundledDomain && ( domain ?? '' ).toLowerCase() === bundledDomain.toLowerCase(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isDomainBeingUsedForPlan( cart?: ObjectWithProducts, domain?: string ): boolean { |
|
|
if ( ! cart || ! domain ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
if ( ! hasPlan( cart ) ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
const domainProducts = getDomainRegistrations( cart ).concat( getDomainMappings( cart ) ); |
|
|
const domainProduct = domainProducts.shift() || { meta: '' }; |
|
|
const processedDomainInCart = domain === domainProduct.meta; |
|
|
if ( ! processedDomainInCart ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
if ( hasBloggerPlan( cart ) ) { |
|
|
const tld = domain.split( '.' ).pop(); |
|
|
if ( tld !== 'blog' ) { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
function hasSomeSlug( data: unknown ): data is WithSnakeCaseSlug | WithCamelCaseSlug { |
|
|
return Boolean( |
|
|
( data as WithSnakeCaseSlug ).product_slug || ( data as WithCamelCaseSlug ).productSlug |
|
|
); |
|
|
} |
|
|
|
|
|
export function shouldBundleDomainWithPlan( |
|
|
withPlansOnly: boolean, |
|
|
selectedSite: undefined | SiteDetails, |
|
|
cart: ResponseCart, |
|
|
suggestionOrCartItem: { |
|
|
product_slug?: string; |
|
|
productSlug?: string; |
|
|
is_domain_registration?: boolean; |
|
|
isDomainRegistration?: boolean; |
|
|
domain_name?: string; |
|
|
is_free?: boolean; |
|
|
} |
|
|
): boolean { |
|
|
return Boolean( |
|
|
withPlansOnly && |
|
|
|
|
|
( isDomainRegistration( suggestionOrCartItem ) || |
|
|
( hasSomeSlug( suggestionOrCartItem ) && isDomainMapping( suggestionOrCartItem ) ) || |
|
|
( suggestionOrCartItem.domain_name && |
|
|
! isFreeWordPressComDomain( suggestionOrCartItem ) ) ) && |
|
|
! isDomainBeingUsedForPlan( cart, suggestionOrCartItem.domain_name ) && |
|
|
! isNextDomainFree( cart ) && |
|
|
! hasPlan( cart ) && |
|
|
( ! selectedSite || selectedSite.plan?.product_slug === 'free_plan' ) |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasToUpgradeToPayForADomain( |
|
|
selectedSite: undefined | SiteDetails, |
|
|
cart: ObjectWithProducts, |
|
|
domain?: string |
|
|
): boolean { |
|
|
if ( ! domain || ! getTld( domain ) ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
const sitePlanSlug = selectedSite?.plan?.product_slug; |
|
|
const isDotBlogDomain = 'blog'.startsWith( getTld( domain ) ); |
|
|
|
|
|
if ( sitePlanSlug && isWpComBloggerPlan( sitePlanSlug ) && ! isDotBlogDomain ) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
if ( hasBloggerPlan( cart ) && ! isDotBlogDomain ) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
return false; |
|
|
} |
|
|
|
|
|
export function isDomainMappingFree( selectedSite: SiteDetails | null | undefined ): boolean { |
|
|
return Boolean( |
|
|
selectedSite?.plan && |
|
|
isPlan( selectedSite.plan ) && |
|
|
! isBloggerPlan( selectedSite.plan.product_slug ) |
|
|
); |
|
|
} |
|
|
|
|
|
export function isPaidDomain( domainPriceRule: string ): boolean { |
|
|
return DOMAIN_PRICE_RULE.PRICE === domainPriceRule; |
|
|
} |
|
|
|
|
|
const isMonthlyOrFreeFlow = ( flowName: string | undefined ): boolean => { |
|
|
return Boolean( |
|
|
flowName && |
|
|
[ |
|
|
'free', |
|
|
'personal-monthly', |
|
|
'premium-monthly', |
|
|
'business-monthly', |
|
|
'ecommerce-monthly', |
|
|
].includes( flowName ) |
|
|
); |
|
|
}; |
|
|
|
|
|
export function getDomainPriceRule( |
|
|
withPlansOnly: boolean, |
|
|
selectedSite: undefined | SiteDetails, |
|
|
cart: ResponseCart, |
|
|
suggestion: { |
|
|
product_slug?: string; |
|
|
productSlug?: string; |
|
|
cost?: string; |
|
|
sale_cost?: number; |
|
|
domain_name?: string; |
|
|
is_premium?: boolean; |
|
|
}, |
|
|
isDomainOnly: boolean, |
|
|
flowName: string, |
|
|
domainAndPlanUpsellFlow: boolean |
|
|
): DomainPriceRule { |
|
|
|
|
|
if ( isHundredYearDomainFlow( flowName ) ) { |
|
|
return DOMAIN_PRICE_RULE.ONE_TIME_PRICE; |
|
|
} |
|
|
|
|
|
if ( ! suggestion.product_slug || suggestion.cost === 'Free' ) { |
|
|
return DOMAIN_PRICE_RULE.FREE_DOMAIN; |
|
|
} |
|
|
|
|
|
if ( suggestion?.is_premium ) { |
|
|
return DOMAIN_PRICE_RULE.PRICE; |
|
|
} |
|
|
|
|
|
if ( hasSomeSlug( suggestion ) && isDomainMoveInternal( suggestion ) ) { |
|
|
return DOMAIN_PRICE_RULE.DOMAIN_MOVE_PRICE; |
|
|
} |
|
|
|
|
|
if ( isMonthlyOrFreeFlow( flowName ) ) { |
|
|
return DOMAIN_PRICE_RULE.PRICE; |
|
|
} |
|
|
|
|
|
if ( isDomainForGravatarFlow( flowName ) ) { |
|
|
return suggestion.sale_cost === 0 |
|
|
? DOMAIN_PRICE_RULE.FREE_FOR_FIRST_YEAR |
|
|
: DOMAIN_PRICE_RULE.PRICE; |
|
|
} |
|
|
|
|
|
if ( domainAndPlanUpsellFlow ) { |
|
|
return DOMAIN_PRICE_RULE.FREE_WITH_PLAN; |
|
|
} |
|
|
|
|
|
if ( isDomainBeingUsedForPlan( cart, suggestion.domain_name ) ) { |
|
|
return DOMAIN_PRICE_RULE.FREE_WITH_PLAN; |
|
|
} |
|
|
|
|
|
if ( hasSomeSlug( suggestion ) && isDomainMapping( suggestion ) ) { |
|
|
if ( isDomainMappingFree( selectedSite ) ) { |
|
|
return DOMAIN_PRICE_RULE.FREE_WITH_PLAN; |
|
|
} |
|
|
|
|
|
if ( withPlansOnly ) { |
|
|
return DOMAIN_PRICE_RULE.INCLUDED_IN_HIGHER_PLAN; |
|
|
} |
|
|
|
|
|
return DOMAIN_PRICE_RULE.PRICE; |
|
|
} |
|
|
|
|
|
if ( isNextDomainFree( cart, suggestion.domain_name ) ) { |
|
|
return DOMAIN_PRICE_RULE.FREE_WITH_PLAN; |
|
|
} |
|
|
|
|
|
if ( shouldBundleDomainWithPlan( withPlansOnly, selectedSite, cart, suggestion ) ) { |
|
|
return DOMAIN_PRICE_RULE.INCLUDED_IN_HIGHER_PLAN; |
|
|
} |
|
|
|
|
|
if ( hasToUpgradeToPayForADomain( selectedSite, cart, suggestion.domain_name ) ) { |
|
|
return DOMAIN_PRICE_RULE.UPGRADE_TO_HIGHER_PLAN_TO_BUY; |
|
|
} |
|
|
|
|
|
return DOMAIN_PRICE_RULE.PRICE; |
|
|
} |
|
|
|
|
|
export function getPlanCartItem( cartItems?: MinimalRequestCartProduct[] | null ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cartItems?.find( ( item ) => isPlan( item ) || item.product_slug === PLAN_FREE ) ?? null; |
|
|
} |
|
|
|