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> {
|
||||
await deleteCouchDbAuthUser(userId);
|
||||
if (subscriptionId) {
|
||||
console.log("deleting sub id", subscriptionId);
|
||||
stripe.subscriptions.del(subscriptionId);
|
||||
}
|
||||
const user = await updateLingdocsUser(userId, {
|
||||
|
|
|
@ -37,23 +37,17 @@ paymentRouter.post(
|
|||
}
|
||||
}
|
||||
let subscription: Stripe.Subscription;
|
||||
let status: Stripe.Subscription.Status;
|
||||
// Handle the event
|
||||
const userId = event.data.object.metadata.userId as T.UUID;
|
||||
switch (event.type) {
|
||||
case 'customer.subscription.deleted':
|
||||
subscription = event.data.object;
|
||||
status = subscription.status;
|
||||
console.log(`Subscription status is ${status}.`);
|
||||
console.log(`Downgrading user ${userId}.`);
|
||||
await downgradeUser(userId);
|
||||
// Then define and call a method to handle the subscription deleted.
|
||||
// handleSubscriptionDeleted(subscriptionDeleted);
|
||||
break;
|
||||
case 'customer.subscription.created':
|
||||
subscription = event.data.object;
|
||||
status = subscription.status;
|
||||
console.log(`Upgrading user ${userId}.`);
|
||||
await upgradeUser(userId, subscription);
|
||||
// TODO: save subscription to db
|
||||
break;
|
||||
|
@ -81,8 +75,10 @@ paymentRouter.post("/create-checkout-session", async (req, res, next) => {
|
|||
}
|
||||
try {
|
||||
const source = req.query.source;
|
||||
const returnUrl = source === "dictionary"
|
||||
const returnUrl = source === "account"
|
||||
? "https://dictionary.lingdocs.com/account"
|
||||
: source === "wordlist"
|
||||
? "https://dictionary.lingdocs.com"
|
||||
: "https://account.lingdocs.com/user";
|
||||
const price = req.body.priceId;
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
|
|
|
@ -8,15 +8,15 @@ const prices = [
|
|||
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">
|
||||
<h5>Subscription options</h5>
|
||||
<div className="d-flex flex-row flex-wrap my-3" style={{ gap: "1.5rem" }}>
|
||||
{prices.map(({ priceId, label }) => <div key={priceId}>
|
||||
<form
|
||||
action={checkoutPortalUrl}
|
||||
action={`${checkoutPortalUrl}?source=${source}`}
|
||||
method="POST"
|
||||
>
|
||||
<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>Review words with Anki-style spaced repetition</li>
|
||||
</ul>
|
||||
<UpgradePrices />
|
||||
<UpgradePrices source="account" />
|
||||
</Modal.Body>
|
||||
<div className="form-group px-3">
|
||||
<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>Review words with Anki-style spaced repetition</li>
|
||||
</ul>
|
||||
{user ? <UpgradePrices /> : <Link to="/account">Sign In</Link>}
|
||||
{user ? <UpgradePrices source="wordlist" /> : <Link to="/account">Sign In</Link>}
|
||||
</div>
|
||||
</div>
|
||||
</div>;
|
||||
|
|
Loading…
Reference in New Issue