|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { headers } from 'next/headers'; |
| 6 | +import Image from 'next/image'; |
| 7 | +import Link from 'next/link'; |
| 8 | +import { notFound, redirect } from 'next/navigation'; |
| 9 | + |
| 10 | +import { SubscriptionParams } from '@fxa/payments/ui'; |
| 11 | +import { determineChurnCancelEligibilityAction } from '@fxa/payments/ui/actions'; |
| 12 | +import { ChurnError, getApp } from '@fxa/payments/ui/server'; |
| 13 | +import { getLocalizedDateString } from '@fxa/shared/l10n'; |
| 14 | +import { auth } from 'apps/payments/next/auth'; |
| 15 | +import { config } from 'apps/payments/next/config'; |
| 16 | + |
| 17 | +export default async function LoyaltyDiscountCancelErrorPage({ |
| 18 | + params, |
| 19 | + searchParams, |
| 20 | +}: { |
| 21 | + params: SubscriptionParams; |
| 22 | + searchParams: Record<string, string> | undefined; |
| 23 | +}) { |
| 24 | + const { locale, subscriptionId } = params; |
| 25 | + |
| 26 | + if (!config.churnInterventionConfig.enabled) { |
| 27 | + redirect(`/${locale}/subscriptions/${subscriptionId}/cancel`); |
| 28 | + } |
| 29 | + |
| 30 | + const acceptLanguage = headers().get('accept-language'); |
| 31 | + const l10n = getApp().getL10n(acceptLanguage, locale); |
| 32 | + |
| 33 | + const session = await auth(); |
| 34 | + if (!session?.user?.id) { |
| 35 | + const redirectToUrl = new URL( |
| 36 | + `${config.paymentsNextHostedUrl}/${locale}/subscriptions/landing` |
| 37 | + ); |
| 38 | + redirectToUrl.search = new URLSearchParams(searchParams).toString(); |
| 39 | + redirect(redirectToUrl.href); |
| 40 | + } |
| 41 | + |
| 42 | + const uid = session.user.id; |
| 43 | + |
| 44 | + const pageContent = await determineChurnCancelEligibilityAction( |
| 45 | + uid, |
| 46 | + subscriptionId, |
| 47 | + acceptLanguage |
| 48 | + ); |
| 49 | + |
| 50 | + if (!pageContent) { |
| 51 | + notFound(); |
| 52 | + } |
| 53 | + |
| 54 | + const { churnCancelContentEligibility } = pageContent; |
| 55 | + if (churnCancelContentEligibility.isEligible) { |
| 56 | + redirect( |
| 57 | + `/${locale}/subscriptions/${subscriptionId}/loyalty-discount/cancel` |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + const { cmsOfferingContent, reason } = churnCancelContentEligibility; |
| 62 | + if (!cmsOfferingContent) { |
| 63 | + notFound(); |
| 64 | + } |
| 65 | + |
| 66 | + const cancelContent = pageContent.cancelContent; |
| 67 | + |
| 68 | + if (cancelContent.flowType !== 'cancel') { |
| 69 | + return ( |
| 70 | + <ChurnError |
| 71 | + cmsOfferingContent={cmsOfferingContent} |
| 72 | + locale={locale} |
| 73 | + reason={reason} |
| 74 | + pageContent={cancelContent} |
| 75 | + subscriptionId={subscriptionId} |
| 76 | + /> |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + if (reason === 'no_churn_intervention_found') { |
| 81 | + const { productName, webIcon } = cmsOfferingContent; |
| 82 | + return ( |
| 83 | + <section |
| 84 | + className="flex justify-center min-h-[calc(100vh_-_4rem)] tablet:items-center tablet:min-h-[calc(100vh_-_5rem)]" |
| 85 | + aria-labelledby="churn-cancel-flow-error-heading" |
| 86 | + > |
| 87 | + <div className="max-w-[480px] p-10 text-grey-600 tablet:bg-white tablet:rounded-xl tablet:border tablet:border-grey-200 tablet:shadow-[0_0_16px_0_rgba(0,0,0,0.08)]"> |
| 88 | + <div className="flex flex-col items-center justify-center gap-4 text-center"> |
| 89 | + <Image src={webIcon} alt={productName} height={64} width={64} /> |
| 90 | + |
| 91 | + <h1 |
| 92 | + id="churn-cancel-flow-error-heading" |
| 93 | + className="font-bold leading-7 text-center text-xl" |
| 94 | + > |
| 95 | + {l10n.getString( |
| 96 | + 'churn-cancel-flow-error-offer-expired-title', |
| 97 | + 'This offer has expired' |
| 98 | + )} |
| 99 | + </h1> |
| 100 | + <div className="leading-6"> |
| 101 | + <p className="my-2"> |
| 102 | + {l10n.getString( |
| 103 | + 'churn-cancel-flow-error-offer-expired-message', |
| 104 | + `There are currently no discounts available for this subscription. You can continue with cancellation if you’d like.` |
| 105 | + )} |
| 106 | + </p> |
| 107 | + </div> |
| 108 | + <div className="flex flex-col gap-3 w-full"> |
| 109 | + <Link |
| 110 | + href={`/${locale}/subscriptions/${subscriptionId}/cancel`} |
| 111 | + className="border box-border flex font-bold font-header h-12 items-center justify-center rounded text-center py-2 px-5 bg-blue-500 border-blue-600 hover:bg-blue-700 text-white" |
| 112 | + > |
| 113 | + {l10n.getString( |
| 114 | + 'churn-cancel-flow-error-button-continue-to-cancel', |
| 115 | + 'Continue to cancel' |
| 116 | + )} |
| 117 | + </Link> |
| 118 | + <Link |
| 119 | + href={`/${locale}/subscriptions/landing`} |
| 120 | + className="border box-border flex font-bold font-header h-12 items-center justify-center rounded text-center py-2 px-5 bg-grey-10 border-grey-200 hover:bg-grey-50" |
| 121 | + > |
| 122 | + {l10n.getString( |
| 123 | + 'churn-cancel-flow-error-page-button-back-to-subscriptions', |
| 124 | + 'Back to subscriptions' |
| 125 | + )} |
| 126 | + </Link> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + </section> |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + if (reason === 'already_canceling_at_period_end') { |
| 135 | + const { productName, webIcon } = cmsOfferingContent; |
| 136 | + const { currentPeriodEnd } = cancelContent; |
| 137 | + const currentPeriodEndLongFallback = getLocalizedDateString( |
| 138 | + currentPeriodEnd, |
| 139 | + false, |
| 140 | + locale |
| 141 | + ); |
| 142 | + return ( |
| 143 | + <section |
| 144 | + className="flex justify-center min-h-[calc(100vh_-_4rem)] tablet:items-center tablet:min-h-[calc(100vh_-_5rem)]" |
| 145 | + aria-labelledby="error-already-canceling-heading" |
| 146 | + > |
| 147 | + <div className="max-w-[480px] p-10 text-grey-600 tablet:bg-white tablet:rounded-xl tablet:border tablet:border-grey-200 tablet:shadow-[0_0_16px_0_rgba(0,0,0,0.08)]"> |
| 148 | + <div className="flex flex-col items-center justify-center gap-4 text-center"> |
| 149 | + <Image src={webIcon} alt={productName} height={64} width={64} /> |
| 150 | + |
| 151 | + <h1 |
| 152 | + id="error-already-canceling-heading" |
| 153 | + className="font-bold leading-7 text-center text-xl" |
| 154 | + > |
| 155 | + {l10n.getString( |
| 156 | + 'churn-cancel-flow-error-already-canceling-title', |
| 157 | + 'Your subscription is set to end' |
| 158 | + )} |
| 159 | + </h1> |
| 160 | + <div className="leading-6"> |
| 161 | + <p className="my-2"> |
| 162 | + {l10n.getString( |
| 163 | + 'churn-cancel-flow-error-already-canceling-message', |
| 164 | + { |
| 165 | + productName, |
| 166 | + currentPeriodEnd: currentPeriodEndLongFallback, |
| 167 | + }, |
| 168 | + `You’ll continue to have access to ${productName} until ${currentPeriodEndLongFallback}.` |
| 169 | + )} |
| 170 | + </p> |
| 171 | + </div> |
| 172 | + <div className="flex flex-col gap-3 w-full"> |
| 173 | + <Link |
| 174 | + href={`/${locale}/subscriptions/landing`} |
| 175 | + className="border box-border flex font-bold font-header h-12 items-center justify-center rounded text-center py-2 px-5 bg-blue-500 border-blue-600 hover:bg-blue-700 text-white" |
| 176 | + > |
| 177 | + {l10n.getString( |
| 178 | + 'churn-cancel-flow-error-page-button-back-to-subscriptions', |
| 179 | + 'Back to subscriptions' |
| 180 | + )} |
| 181 | + </Link> |
| 182 | + <Link |
| 183 | + href={`/${locale}/subscriptions/${subscriptionId}/stay-subscribed`} |
| 184 | + className="border box-border flex font-bold font-header h-12 items-center justify-center rounded text-center py-2 px-5 bg-grey-10 border-grey-200 hover:bg-grey-50" |
| 185 | + > |
| 186 | + {l10n.getString( |
| 187 | + 'churn-cancel-flow-error-page-button-keep-subscription', |
| 188 | + 'Keep subscription' |
| 189 | + )} |
| 190 | + </Link> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + </div> |
| 194 | + </section> |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + return ( |
| 199 | + <ChurnError |
| 200 | + cmsOfferingContent={cmsOfferingContent} |
| 201 | + locale={locale} |
| 202 | + reason={reason} |
| 203 | + pageContent={cancelContent} |
| 204 | + subscriptionId={subscriptionId} |
| 205 | + /> |
| 206 | + ); |
| 207 | +} |
0 commit comments