smoother flow
This commit is contained in:
parent
2b194a1609
commit
bcf6714d8c
|
@ -82,7 +82,6 @@ export async function upgradeUser(userId: T.UUID, subscription?: T.StripeSubscri
|
||||||
export async function downgradeUser(userId: T.UUID, subscriptionId?: string): Promise<T.DowngradeUserResponse> {
|
export async function downgradeUser(userId: T.UUID, subscriptionId?: string): Promise<T.DowngradeUserResponse> {
|
||||||
await deleteCouchDbAuthUser(userId);
|
await deleteCouchDbAuthUser(userId);
|
||||||
if (subscriptionId) {
|
if (subscriptionId) {
|
||||||
console.log("deleting sub id", subscriptionId);
|
|
||||||
stripe.subscriptions.del(subscriptionId);
|
stripe.subscriptions.del(subscriptionId);
|
||||||
}
|
}
|
||||||
const user = await updateLingdocsUser(userId, {
|
const user = await updateLingdocsUser(userId, {
|
||||||
|
|
|
@ -37,23 +37,17 @@ paymentRouter.post(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let subscription: Stripe.Subscription;
|
let subscription: Stripe.Subscription;
|
||||||
let status: Stripe.Subscription.Status;
|
|
||||||
// Handle the event
|
// Handle the event
|
||||||
const userId = event.data.object.metadata.userId as T.UUID;
|
const userId = event.data.object.metadata.userId as T.UUID;
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case 'customer.subscription.deleted':
|
case 'customer.subscription.deleted':
|
||||||
subscription = event.data.object;
|
subscription = event.data.object;
|
||||||
status = subscription.status;
|
|
||||||
console.log(`Subscription status is ${status}.`);
|
|
||||||
console.log(`Downgrading user ${userId}.`);
|
|
||||||
await downgradeUser(userId);
|
await downgradeUser(userId);
|
||||||
// Then define and call a method to handle the subscription deleted.
|
// Then define and call a method to handle the subscription deleted.
|
||||||
// handleSubscriptionDeleted(subscriptionDeleted);
|
// handleSubscriptionDeleted(subscriptionDeleted);
|
||||||
break;
|
break;
|
||||||
case 'customer.subscription.created':
|
case 'customer.subscription.created':
|
||||||
subscription = event.data.object;
|
subscription = event.data.object;
|
||||||
status = subscription.status;
|
|
||||||
console.log(`Upgrading user ${userId}.`);
|
|
||||||
await upgradeUser(userId, subscription);
|
await upgradeUser(userId, subscription);
|
||||||
// TODO: save subscription to db
|
// TODO: save subscription to db
|
||||||
break;
|
break;
|
||||||
|
@ -81,8 +75,10 @@ paymentRouter.post("/create-checkout-session", async (req, res, next) => {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const source = req.query.source;
|
const source = req.query.source;
|
||||||
const returnUrl = source === "dictionary"
|
const returnUrl = source === "account"
|
||||||
? "https://dictionary.lingdocs.com/account"
|
? "https://dictionary.lingdocs.com/account"
|
||||||
|
: source === "wordlist"
|
||||||
|
? "https://dictionary.lingdocs.com"
|
||||||
: "https://account.lingdocs.com/user";
|
: "https://account.lingdocs.com/user";
|
||||||
const price = req.body.priceId;
|
const price = req.body.priceId;
|
||||||
const session = await stripe.checkout.sessions.create({
|
const session = await stripe.checkout.sessions.create({
|
||||||
|
|
|
@ -8,15 +8,15 @@ const prices = [
|
||||||
priceId: "price_1Lt8NqJnpCQCjf9p4FAEIOMw",
|
priceId: "price_1Lt8NqJnpCQCjf9p4FAEIOMw",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const checkoutPortalUrl = "https://account.lingdocs.com/payment/create-checkout-session/?source=dictionary";
|
const checkoutPortalUrl = "https://account.lingdocs.com/payment/create-checkout-session/";
|
||||||
|
|
||||||
function UpgradePrices() {
|
function UpgradePrices({ source }: { source: "account" | "wordlist" }) {
|
||||||
return <div className="my-4">
|
return <div className="my-4">
|
||||||
<h5>Subscription options</h5>
|
<h5>Subscription options</h5>
|
||||||
<div className="d-flex flex-row flex-wrap my-3" style={{ gap: "1.5rem" }}>
|
<div className="d-flex flex-row flex-wrap my-3" style={{ gap: "1.5rem" }}>
|
||||||
{prices.map(({ priceId, label }) => <div key={priceId}>
|
{prices.map(({ priceId, label }) => <div key={priceId}>
|
||||||
<form
|
<form
|
||||||
action={checkoutPortalUrl}
|
action={`${checkoutPortalUrl}?source=${source}`}
|
||||||
method="POST"
|
method="POST"
|
||||||
>
|
>
|
||||||
<input type="hidden" name="priceId" value={priceId} />
|
<input type="hidden" name="priceId" value={priceId} />
|
||||||
|
|
|
@ -191,7 +191,7 @@ const Account = ({ user, loadUser }: { user: AT.LingdocsUser | undefined, loadUs
|
||||||
<li>Save text, audio, or visual context for words</li>
|
<li>Save text, audio, or visual context for words</li>
|
||||||
<li>Review words with Anki-style spaced repetition</li>
|
<li>Review words with Anki-style spaced repetition</li>
|
||||||
</ul>
|
</ul>
|
||||||
<UpgradePrices />
|
<UpgradePrices source="account" />
|
||||||
</Modal.Body>
|
</Modal.Body>
|
||||||
<div className="form-group px-3">
|
<div className="form-group px-3">
|
||||||
<label htmlFor="upgradePasswordForm">Or enter upgrade password:</label>
|
<label htmlFor="upgradePasswordForm">Or enter upgrade password:</label>
|
||||||
|
|
|
@ -276,7 +276,7 @@ function Wordlist({ options, wordlist, isolateEntry, optionsDispatch, user }: {
|
||||||
<li>Save text, audio, or visual context for words</li>
|
<li>Save text, audio, or visual context for words</li>
|
||||||
<li>Review words with Anki-style spaced repetition</li>
|
<li>Review words with Anki-style spaced repetition</li>
|
||||||
</ul>
|
</ul>
|
||||||
{user ? <UpgradePrices /> : <Link to="/account">Sign In</Link>}
|
{user ? <UpgradePrices source="wordlist" /> : <Link to="/account">Sign In</Link>}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>;
|
</div>;
|
||||||
|
|
Loading…
Reference in New Issue