Repository navigation
[feature] Enhance the captive-portal upgrade experience #1145 #1147 #1146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
73b7008
a442348
fe33f6e
dd7d661
4461cba
5356460
04b9cc5
2fe11cc
765d828
a5f64ed
191e928
2b1da49
dc31152
ae3a8eb
f198f60
3e96b32
fafcc69
e7b2e94
9f6b7c7
21aae2e
ea19528
3492e3d
8cefe1a
6e82900
1154b8e
44841d9
a949781
0b56799
2dffad6
fded425
6ae1194
8f31b1a
a4f22e4
1daa1a3
edcb8de
10e1950
cb303ba
b3d85be
37909ec
8c3af09
928c3ad
9391300
2a33c37
1b815a0
34a8de6
cc9d312
da623c9
fad50a0
d03788e
ca3468d
871eb07
bc57c12
762c80e
f6a9257
d0d61f2
4d2a2fd
e0f267d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,13 @@ import {Link, Navigate} from "react-router-dom"; | |
| import {toast} from "react-toastify"; | ||
| import {t} from "ttag"; | ||
| import LoadingContext from "../../utils/loading-context"; | ||
| import Loader from "../../utils/loader"; | ||
| import Contact from "../contact-box"; | ||
| import validateToken from "../../utils/validate-token"; | ||
| import handleLogout from "../../utils/handle-logout"; | ||
| import cancelUpgradePlan from "../../utils/cancel-upgrade-plan"; | ||
| import logError from "../../utils/log-error"; | ||
| import {storeValue, clearStoredValue} from "../../utils/synced-storage"; | ||
|
|
||
| export default class PaymentStatus extends React.Component { | ||
| constructor(props) { | ||
|
|
@@ -45,16 +49,19 @@ export default class PaymentStatus extends React.Component { | |
| const {method, is_verified: isVerified} = userData; | ||
| // flag user to repeat login in order to restart session with new radius group | ||
| if (status === "success" && method === "bank_card" && isVerified === true) { | ||
| setUserData({ | ||
| ...userData, | ||
| mustLogin: !settings.payment_requires_internet, | ||
| mustLogout: settings.payment_requires_internet, | ||
| repeatLogin: settings.payment_requires_internet, | ||
| }); | ||
| // Skip logout/login cycle if CoA handles session group updates transparently | ||
| if (!settings.captive_portal_supports_coa) { | ||
| setUserData({ | ||
| ...userData, | ||
| mustLogin: !settings.payment_requires_internet, | ||
| mustLogout: settings.payment_requires_internet, | ||
| repeatLogin: settings.payment_requires_internet, | ||
| }); | ||
| } | ||
| } else if ( | ||
| status === "draft" && | ||
| method === "bank_card" && | ||
| isVerified === false | ||
| // Payment completion needs internet for both new registrations and upgrades | ||
| (userData.in_upgrade || (method === "bank_card" && isVerified === false)) | ||
| ) { | ||
| setUserData({ | ||
| ...userData, | ||
|
|
@@ -63,6 +70,45 @@ export default class PaymentStatus extends React.Component { | |
| } | ||
| } | ||
|
|
||
| // Abandon upgrade: cancel order, clear flags, reconnect under current plan | ||
| backToStatus = async () => { | ||
| const {orgSlug, navigate, setUserData, userData, language, cookies} = | ||
| this.props; | ||
| try { | ||
| const response = await cancelUpgradePlan( | ||
| orgSlug, | ||
| userData.auth_token || userData.key, | ||
| language, | ||
| ); | ||
| setUserData({ | ||
| ...userData, | ||
| ...response, | ||
| auth_token: response.auth_token || response.key || userData.auth_token, | ||
| proceedToPayment: false, | ||
| mustLogin: undefined, | ||
| mustLogout: true, | ||
| captivePortalLogoutOnly: true, | ||
| }); | ||
| } catch (error) { | ||
| if (!error.response || error.response.status !== 404) { | ||
| toast.error(t`ERR_OCCUR`); | ||
| logError(error, "Error while cancelling plan upgrade"); | ||
| } | ||
| setUserData({ | ||
| ...userData, | ||
| in_upgrade: false, | ||
| proceedToPayment: false, | ||
| payment_url: null, | ||
| mustLogin: undefined, | ||
| mustLogout: true, | ||
| captivePortalLogoutOnly: true, | ||
| }); | ||
| } | ||
| // Clear persisted proceedToPayment flag | ||
| clearStoredValue(`${orgSlug}_proceedToPayment`, cookies); | ||
| navigate(`/${orgSlug}/status`); | ||
| }; | ||
|
|
||
| logout = () => { | ||
| const {logout, cookies, orgSlug, setUserData, userData, navigate} = | ||
| this.props; | ||
|
|
@@ -82,33 +128,32 @@ export default class PaymentStatus extends React.Component { | |
| render() { | ||
| const {orgSlug, params, isAuthenticated, userData} = this.props; | ||
| const {status} = params; | ||
| const {method, is_verified: isVerified} = userData; | ||
| const {method, is_verified: isVerified, in_upgrade: inUpgrade} = userData; | ||
| const redirectToStatus = () => <Navigate to={`/${orgSlug}/status`} />; | ||
| const acceptedValues = ["success", "failed", "draft"]; | ||
| const {isTokenValid} = this.state; | ||
|
|
||
| // not registered with bank card flow | ||
| if ( | ||
| (method && method !== "bank_card") || | ||
| !acceptedValues.includes(status) | ||
| ) { | ||
| return redirectToStatus(); | ||
| } | ||
|
|
||
| // likely somebody opening this page by mistake | ||
| if ( | ||
| // Upgraders retain their existing registration state; bypass bank-card redirect guards | ||
| const isFailedUpgrade = Boolean(inUpgrade) && status === "failed"; | ||
| const isDraftUpgrade = Boolean(inUpgrade) && status === "draft"; | ||
| const shouldRedirectToStatus = | ||
| !acceptedValues.includes(status) || | ||
| (!inUpgrade && method && method !== "bank_card") || | ||
| (isAuthenticated === false && status !== "draft") || | ||
| (["failed", "draft"].includes(status) && isVerified === true) || | ||
| (!isFailedUpgrade && | ||
| !isDraftUpgrade && | ||
| ["failed", "draft"].includes(status) && | ||
| isVerified === true) || | ||
| (status === "success" && isVerified === false) || | ||
| isTokenValid === false | ||
| ) { | ||
| isTokenValid === false; | ||
|
|
||
| if (shouldRedirectToStatus) { | ||
| return redirectToStatus(); | ||
| } | ||
|
|
||
| // draft case | ||
| // if (isAuthenticated === false && status === "draft") { | ||
| if (status === "draft") { | ||
| return this.renderDraft(); | ||
| return this.renderDraft(isDraftUpgrade); | ||
| } | ||
|
|
||
| // success case | ||
|
|
@@ -117,30 +162,57 @@ export default class PaymentStatus extends React.Component { | |
| return redirectToStatus(); | ||
| } | ||
|
|
||
| return this.renderFailed(); | ||
| // Show loader while validateToken resolves in_upgrade state on cold reload | ||
| if (status === "failed" && isTokenValid === null) { | ||
| return <Loader />; | ||
| } | ||
|
|
||
| return this.renderFailed(isFailedUpgrade); | ||
| } | ||
|
|
||
| paymentProceedHandler() { | ||
| const {authenticate, setUserData, userData, settings} = this.props; | ||
| const { | ||
| authenticate, | ||
| setUserData, | ||
| userData, | ||
| settings, | ||
| cookies, | ||
| orgSlug, | ||
| captivePortalSyncAuth, | ||
| } = this.props; | ||
| // Payment gateway may require internet access. | ||
| // Since, captive portal login is handled by the Status component, | ||
| // the user is navigated to the "/status" for captive portal login | ||
| // which then redirects the user to the payment gateway. | ||
| if (settings.payment_requires_internet) { | ||
| setUserData({ | ||
| ...userData, | ||
| mustLogin: true, | ||
| proceedToPayment: true, | ||
| }); | ||
| // Persist so it survives page reload during sync captive portal auth | ||
| storeValue(captivePortalSyncAuth, `${orgSlug}_mustLogin`, true, cookies); | ||
| storeValue( | ||
| captivePortalSyncAuth, | ||
| `${orgSlug}_proceedToPayment`, | ||
| "true", | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this storing a string literal?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pandafy I'm not making a code change for this thread. GitHub was unreachable, so the current PR and review thread could not be verified safely. |
||
| cookies, | ||
| ); | ||
| } | ||
| authenticate(true); | ||
| } | ||
|
|
||
| renderDraft() { | ||
| const {orgSlug, page = {}, settings} = this.props; | ||
| const {timeout = 5, max_attempts: maxAttempts = 3} = page; | ||
| const payProceedUrl = settings.payment_requires_internet | ||
| getProceedUrl() { | ||
| const {orgSlug, settings} = this.props; | ||
| return settings.payment_requires_internet | ||
| ? `/${orgSlug}/status` | ||
| : `/${orgSlug}/payment/process`; | ||
| } | ||
|
|
||
| renderDraft(isDraftUpgrade = false) { | ||
| const {page = {}} = this.props; | ||
| const {timeout = 5, max_attempts: maxAttempts = 3} = page; | ||
| const payProceedUrl = this.getProceedUrl(); | ||
|
|
||
| return ( | ||
| <div className="container content"> | ||
|
|
@@ -170,9 +242,9 @@ export default class PaymentStatus extends React.Component { | |
| <button | ||
| type="button" | ||
| className="button full" | ||
| onClick={this.logout} | ||
| onClick={isDraftUpgrade ? this.backToStatus : this.logout} | ||
| > | ||
| {t`PAY_GIVE_UP_BTN`} | ||
| {isDraftUpgrade ? t`PAY_GO_BACK_BTN` : t`PAY_GIVE_UP_BTN`} | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
@@ -182,8 +254,16 @@ export default class PaymentStatus extends React.Component { | |
| ); | ||
| } | ||
|
|
||
| renderFailed() { | ||
| const {orgSlug} = this.props; | ||
| renderFailed(isFailedUpgrade = false) { | ||
| const {orgSlug, userData} = this.props; | ||
| // User might have exhausted the temporary session quota while completing | ||
| // the payment. Thus, we need to login them back in the captive portal | ||
| // before directing them to the payment gateway. | ||
| const retryUrl = isFailedUpgrade | ||
| ? this.getProceedUrl() | ||
| : `/${orgSlug}/payment/draft`; | ||
| // No retry when upgrade payment attempts exhausted (no payment_url) | ||
| const showRetry = !isFailedUpgrade || Boolean(userData.payment_url); | ||
|
pandafy marked this conversation as resolved.
Resolved
|
||
| // failed payment case | ||
| return ( | ||
| <div className="container content"> | ||
|
|
@@ -192,20 +272,28 @@ export default class PaymentStatus extends React.Component { | |
| <div className="inner"> | ||
| <h2 className="row payment-status-row-1">{t`PAY_FAIL`}</h2> | ||
| <div className="row payment-status-row-2">{t`PAY_SUB_H`}</div> | ||
| <div className="row payment-status-row-3"> | ||
| <Link className="button full" to={`/${orgSlug}/payment/draft`}> | ||
| {t`PAY_TRY_AGAIN_BTN`} | ||
| </Link> | ||
| </div> | ||
| {showRetry && ( | ||
| <div className="row payment-status-row-3"> | ||
| <Link | ||
| className="button full" | ||
| to={retryUrl} | ||
| onClick={ | ||
| isFailedUpgrade ? this.paymentProceedHandler : undefined | ||
| } | ||
| > | ||
| {t`PAY_TRY_AGAIN_BTN`} | ||
| </Link> | ||
| </div> | ||
| )} | ||
|
|
||
| <div className="row payment-status-row-4"> | ||
| <p>{t`PAY_GIVE_UP_TXT`}</p> | ||
| <button | ||
| type="button" | ||
| className="button full" | ||
| onClick={this.logout} | ||
| onClick={isFailedUpgrade ? this.backToStatus : this.logout} | ||
| > | ||
| {t`PAY_GIVE_UP_BTN`} | ||
| {isFailedUpgrade ? t`PAY_GO_BACK_BTN` : t`PAY_GIVE_UP_BTN`} | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
@@ -224,12 +312,14 @@ PaymentStatus.propTypes = { | |
| userData: PropTypes.object.isRequired, | ||
| setUserData: PropTypes.func.isRequired, | ||
| isAuthenticated: PropTypes.bool, | ||
| captivePortalSyncAuth: PropTypes.bool, | ||
| authenticate: PropTypes.func.isRequired, | ||
| page: PropTypes.object, | ||
| logout: PropTypes.func.isRequired, | ||
| cookies: PropTypes.instanceOf(Cookies).isRequired, | ||
| settings: PropTypes.shape({ | ||
| payment_requires_internet: PropTypes.bool, | ||
| captive_portal_supports_coa: PropTypes.bool, | ||
| }).isRequired, | ||
| params: PropTypes.shape({ | ||
| status: PropTypes.string, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.