@automattic/wpcom-checkout
Version:
Functions and components used by WordPress.com checkout.
197 lines • 8.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIntroductoryOfferIntervalDisplay = getIntroductoryOfferIntervalDisplay;
exports.getPremiumDomainIntroductoryOfferDisplay = getPremiumDomainIntroductoryOfferDisplay;
exports.getItemIntroductoryOfferDisplay = getItemIntroductoryOfferDisplay;
const tslib_1 = require("tslib");
const calypso_products_1 = require("@automattic/calypso-products");
const number_formatters_1 = require("@automattic/number-formatters");
const i18n_calypso_1 = tslib_1.__importDefault(require("i18n-calypso"));
const transformations_1 = require("./transformations");
function getIntroductoryOfferIntervalDisplay({ translate, intervalUnit, intervalCount, isFreeTrial, isPriceIncrease, context, remainingRenewalsUsingOffer = 0, isStreamlinedPrice, }) {
let text = isPriceIncrease
? translate('First billing period', { textOnly: true })
: String(translate('Discount for first period'));
if (isFreeTrial) {
if (intervalUnit === 'month') {
if (intervalCount === 1) {
text = String(translate('First month free'));
}
else {
text = String(translate('First %(numberOfMonths)d months free', {
args: {
numberOfMonths: intervalCount,
},
}));
}
}
if (intervalUnit === 'year') {
if (intervalCount === 1) {
text = String(translate('First year free'));
}
else {
text = String(translate('First %(numberOfYears)d years free', {
args: {
numberOfYears: intervalCount,
},
}));
}
}
}
else {
if (intervalUnit === 'month') {
if (intervalCount === 1) {
text = isPriceIncrease
? translate('Price for first month', { textOnly: true })
: String(translate('Discount for first month'));
}
else {
text = isPriceIncrease
? translate('Price for first %(numberOfMonths)d months', {
textOnly: true,
args: {
numberOfMonths: intervalCount,
},
})
: String(translate('Discount for first %(numberOfMonths)d months', {
args: {
numberOfMonths: intervalCount,
},
}));
}
}
if (intervalUnit === 'year') {
if (intervalCount === 1) {
text = isPriceIncrease
? translate('Price for first year', { textOnly: true })
: String(translate('Discount for first year'));
const isAdditionalDiscountTranslated = i18n_calypso_1.default.getLocaleSlug()?.startsWith('en') ||
i18n_calypso_1.default.hasTranslation('Additional discount for first year');
text =
!isPriceIncrease && isStreamlinedPrice && isAdditionalDiscountTranslated
? translate('Additional discount for first year')
: text;
}
else {
text = isPriceIncrease
? translate('Price for first %(numberOfYears)d years', {
textOnly: true,
args: { numberOfYears: intervalCount },
})
: String(translate('Discount for first %(numberOfYears)d years', {
args: {
numberOfYears: intervalCount,
},
}));
}
}
}
if (remainingRenewalsUsingOffer > 0) {
text += ' - ';
if (context === 'checkout') {
if (remainingRenewalsUsingOffer === 1) {
text += isPriceIncrease
? translate('Applies for one renewal', { textOnly: true })
: translate('The first renewal is also discounted.', { textOnly: true });
}
else {
text += isPriceIncrease
? translate('Applies for %(remainingRenewals)d renewals', {
textOnly: true,
args: {
remainingRenewals: remainingRenewalsUsingOffer,
},
})
: String(translate('The first %(remainingRenewals)d renewal is also discounted.', 'The first %(remainingRenewals)d renewals are also discounted.', {
count: remainingRenewalsUsingOffer,
args: {
remainingRenewals: remainingRenewalsUsingOffer,
},
}));
}
}
else {
text += isPriceIncrease
? translate('Applies for %(remainingRenewals)d renewal', 'Applies for %(remainingRenewals)d renewals', {
textOnly: true,
count: remainingRenewalsUsingOffer,
args: {
remainingRenewals: remainingRenewalsUsingOffer,
},
})
: String(translate('%(remainingRenewals)d discounted renewal remaining.', '%(remainingRenewals)d discounted renewals remaining.', {
count: remainingRenewalsUsingOffer,
args: {
remainingRenewals: remainingRenewalsUsingOffer,
},
}));
}
}
return text;
}
function getPremiumDomainIntroductoryOfferDisplay(translate, product) {
if (!(0, calypso_products_1.isDomainRegistration)(product) ||
!product.extra.premium ||
!product.introductory_offer_terms?.enabled) {
return null;
}
const { interval_unit: intervalUnit, interval_count: intervalCount } = product.introductory_offer_terms;
// Monthly introductory offers are covered, but they shouldn't normally happen.
if (intervalUnit === 'month') {
return null;
}
if (intervalUnit === 'year') {
const renewalPrice = (0, number_formatters_1.formatCurrency)(product.item_original_cost_integer, product.currency, {
isSmallestUnit: true,
stripZeros: true,
});
if (intervalCount === 1) {
return String(translate('Renews for %(renewalPrice)s/year', {
args: {
renewalPrice,
},
}));
}
else if (intervalCount >= 1) {
return String(translate('Renews for %(renewalPrice)s/year after %(numberOfYears)d years', {
args: {
renewalPrice,
numberOfYears: intervalCount,
},
}));
}
}
return null;
}
function getItemIntroductoryOfferDisplay(translate, product, isStreamlinedPrice) {
// Introductory offer manual renewals often have prorated prices that are
// difficult to display as a simple discount so we keep their display
// simple.
if (product.is_renewal) {
return null;
}
if (product.introductory_offer_terms?.reason) {
const text = translate('Order not eligible for introductory discount');
return { enabled: false, text };
}
if (!product.introductory_offer_terms?.enabled) {
return null;
}
const premiumDomainText = getPremiumDomainIntroductoryOfferDisplay(translate, product);
if (premiumDomainText) {
return { enabled: true, text: premiumDomainText };
}
const isFreeTrial = product.item_subtotal_integer === 0;
const text = getIntroductoryOfferIntervalDisplay({
translate,
intervalUnit: product.introductory_offer_terms.interval_unit,
intervalCount: product.introductory_offer_terms.interval_count,
isFreeTrial,
isPriceIncrease: (0, transformations_1.doesIntroductoryOfferHavePriceIncrease)(product),
context: 'checkout',
remainingRenewalsUsingOffer: product.introductory_offer_terms.transition_after_renewal_count,
isStreamlinedPrice,
});
return { enabled: true, text };
}
//# sourceMappingURL=introductory-offer.js.map