File size: 26,632 Bytes
1e92f2d |
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 |
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 );
}
/**
* Determines whether there is a DIFM (Do it for me) product in the shopping cart.
*/
export function hasDIFMProduct( cart: ObjectWithProducts ): boolean {
return cart && getAllCartItems( cart ).some( isDIFMProduct );
}
/**
* Determines whether there is any kind of plan (e.g. Premium or Business) in the shopping cart.
*/
export function hasPlan( cart: ObjectWithProducts ): boolean {
return cart && getAllCartItems( cart ).some( isPlan );
}
/**
* Determines whether there is a Jetpack plan in the shopping cart.
*/
export function hasJetpackPlan( cart: ObjectWithProducts ): boolean {
return getAllCartItems( cart ).some( isJetpackPlan );
}
/**
* Determines whether there is a P2+ plan in the shopping cart.
*/
export function hasP2PlusPlan( cart: ObjectWithProducts ): boolean {
return getAllCartItems( cart ).some( isP2Plus );
}
/**
* Determines whether there is an ecommerce plan in the shopping cart.
*/
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 );
}
/**
* Determines whether there is at least one item of a given product in the specified shopping cart.
*/
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
);
}
/**
* Determines whether all items are renewal items in the specified shopping cart.
*
* Ignores partial credits, which aren't really a line item in this sense.
*/
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 );
}
/**
* Returns a bill period of given cartItem
*/
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;
}
/**
* Determines whether any product in the specified shopping cart is a renewable subscription.
*
* Will return false if the cart is empty.
*/
export function hasRenewableSubscription( cart: ObjectWithProducts ): boolean {
return (
cart.products &&
getAllCartItems( cart ).some( ( cartItem ) => getCartItemBillPeriod( cartItem ) > 0 )
);
}
/**
* Creates a new shopping cart item for a plan.
*/
export function planItem( productSlug: string ): { product_slug: string } | null {
// Free and Enterprise plans don't have shopping cart.
if ( isWpComFreePlan( productSlug ) || isWpcomEnterpriseGridPlan( productSlug ) ) {
return null;
}
return {
product_slug: productSlug,
};
}
/**
* Determines whether a domain Item supports purchasing a privacy subscription
* @param {string} productSlug - e.g. domain_reg, dotblog_domain
* @param {{product_slug: string, is_privacy_protection_product_purchase_allowed?: boolean}[]} productsList - The list of products retrieved using getProductsList from state/products-list/selectors
* @returns {boolean} true if the domainItem supports privacy protection purchase
*/
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;
}
/**
* Creates a new shopping cart item for a domain.
* @param {string} productSlug - the unique string that identifies the product
* @param {string} domain - domain name
* @param {string|undefined} [source] - optional source for the domain item, e.g. `getdotblog`.
* @returns {MinimalRequestCartProduct} the new item
*/
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
);
}
/**
* Creates a new shopping cart item for a premium theme.
* @param {string} themeSlug - the unique string that identifies the product
* @param {string} [source] - optional source for the domain item, e.g. `getdotblog`.
* @returns {MinimalRequestCartProduct} the new item
*/
export function themeItem( themeSlug: string, source?: string ): MinimalRequestCartProduct {
return {
product_slug: 'premium_theme',
meta: themeSlug,
extra: {
source: source,
},
};
}
/**
* Creates a new shopping cart item for a marketplace theme subscription.
* @param productSlug the unique string that identifies the product
* @returns {MinimalRequestCartProduct} the new item
*/
export function marketplaceThemeProduct( productSlug: string ): MinimalRequestCartProduct {
return {
product_slug: productSlug,
};
}
/**
* Creates a new shopping cart item for a domain registration.
*/
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 } : {} ),
};
}
/**
* Creates a new shopping cart item for a domain mapping.
*/
export function domainMapping( properties: {
domain: string;
source?: string;
} ): MinimalRequestCartProduct {
return domainItem( 'domain_map', properties.domain, properties.source );
}
/**
* Creates a new shopping cart item for Site Redirect.
*/
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 );
}
/**
* Creates a new shopping cart item for an incoming domain transfer.
*/
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 } : {} ),
};
}
/**
* Retrieves all the items in the specified shopping cart for G Suite or Google Workspace.
*/
export function getGoogleApps( cart: ObjectWithProducts ): ResponseCartProduct[] {
return getAllCartItems( cart ).filter( isGSuiteOrExtraLicenseOrGoogleWorkspace );
}
/**
* Creates a new shopping cart item for G Suite or Google Workspace.
*/
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;
}
/**
* Creates a new shopping cart item for Titan Mail.
*/
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,
};
}
/**
* Creates a new shopping cart item for Titan Mail Yearly.
*/
export function titanMailYearly( properties: TitanProductProps ): MinimalRequestCartProduct {
return titanMailProduct( properties, TITAN_MAIL_YEARLY_SLUG );
}
/**
* Creates a new shopping cart item for Titan Mail Monthly.
*/
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,
};
}
/**
* Creates a new shopping cart item for a jetpack product.
*/
export function jetpackProductItem( slug: string ): MinimalRequestCartProduct {
return {
product_slug: slug,
};
}
/**
* Creates a new shopping cart item for an Akismet product.
*/
export function akismetProductItem( slug: string ): MinimalRequestCartProduct {
return {
product_slug: slug,
};
}
/**
* Creates a new shopping cart item for a renewable product.
*/
export function renewableProductItem( slug: string ): MinimalRequestCartProduct {
return {
product_slug: slug,
};
}
/**
* Retrieves all the domain registration items in the specified shopping cart.
*/
export function getDomainRegistrations( cart: ObjectWithProducts ): ResponseCartProduct[] {
return getAllCartItems( cart ).filter( ( product ) => product.is_domain_registration === true );
}
/**
* Retrieves all the domain registration items in the specified shopping cart.
*/
export function getDomainsInCart( cart: ObjectWithProducts ): ResponseCartProduct[] {
return getAllCartItems( cart ).filter(
( product ) => isDomainRegistration( product ) || isDomainMoveInternal( product )
);
}
/**
* Retrieves all the domain mapping items in the specified shopping cart.
*/
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;
}
/**
* Returns a renewal CartItem object with the given properties and product slug.
*/
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 );
}
/**
* Returns a renewal CartItem object from the given cartItem and properties.
*/
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 ) )
);
} );
}
/**
* Changes presence of a privacy protection for the given domain cart item.
*/
export function updatePrivacyForDomain< T extends MinimalRequestCartProduct >(
item: T,
value: boolean
): T {
return {
...item,
extra: {
...item.extra,
privacy: value,
},
};
}
/**
* Determines whether a cart item is partial credits
*/
function isPartialCredits( cartItem: ResponseCartProduct ): boolean {
return cartItem.product_slug === 'wordpress-com-credits';
}
/**
* Returns true if, according to cart attributes, a `domain` should be free
*/
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();
}
/**
* Returns true if cart contains a plan and also a domain that comes for free with that plan
*/
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 &&
// not free or a cart item
( isDomainRegistration( suggestionOrCartItem ) ||
( hasSomeSlug( suggestionOrCartItem ) && isDomainMapping( suggestionOrCartItem ) ) ||
( suggestionOrCartItem.domain_name &&
! isFreeWordPressComDomain( suggestionOrCartItem ) ) ) &&
! isDomainBeingUsedForPlan( cart, suggestionOrCartItem.domain_name ) && // a plan in cart
! isNextDomainFree( cart ) && // domain credit
! hasPlan( cart ) && // already a plan in cart
( ! selectedSite || selectedSite.plan?.product_slug === 'free_plan' )
); // site has a plan
}
/**
* Sites on a blogger plan are not allowed to get an additional domain - they need to buy an upgrade to do that.
* This function checks tells if user has to upgrade just to be able to pay for a domain.
*/
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 {
// We'll show a fixed, one time price in the 100-year domain flow
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 ) {
/**
* A null planCartItem corresponds to a free plan. It seems like this is case throughout the signup/plans
* onboarding codebase. There are, however, tests in client/signup/steps/plans/test/index.jsx that
* represent a free plan as a non null product.
*
* Additionally, free plans are, in fact, represented as products with product slugs elsewhere in the
* codebase. This is why we check for both cases here. When we conduct a more thorough investigation and
* determine that PLAN_FREE is no longer, in fact, used to represent free plans in signup/onboarding, we
* can remove PLAN_FREE check. p4TIVU-aLF-p2
*/
return cartItems?.find( ( item ) => isPlan( item ) || item.product_slug === PLAN_FREE ) ?? null;
}
|