cal/packages/stripe/client.ts
Omar López 5625cf226b
Stripe to monorepo (#2063)
* downgrade func

* fix security hole lol

* fix query conditions

* - set to trial not free
- auto create stripe customer if missing
- fix production check

* Extracts downgrade logic to script, fixes ts-node conflicts with prisma

* Adds trialEndsAt field to users

* Updates trial/downgrade logic

* Typo

* Legibility fixes

* Update team-billing.ts

* Legibility improvements

* Updates illegal logic

* WIP

* WIP migrating stripe to package

* Update website

* Import fixes

* Import fixes

* Fixes to downgrade script

* Check for premium usernames before downgrading

* Fixed formatting

* Delete deploy-env.sh

* Locks dayjs to 1.10.6

* Type fixes

* Seems like we're stuck with dayjs 1.10.4

* Script fixes

* Adds first name to dump

* Loop fix

Co-authored-by: Jamie <ijamespine@me.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-03-09 15:56:05 -07:00

38 lines
1.0 KiB
TypeScript

import { Stripe } from "@stripe/stripe-js";
import { loadStripe } from "@stripe/stripe-js/pure";
import { stringify } from "querystring";
export type Maybe<T> = T | undefined | null;
const stripePublicKey = process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY!;
let stripePromise: Promise<Stripe | null>;
/**
* This is a singleton to ensure we only instantiate Stripe once.
*/
const getStripe = (userPublicKey?: string) => {
if (!stripePromise) {
stripePromise = loadStripe(
userPublicKey || stripePublicKey /* , {
locale: "es-419" TODO: Handle multiple locales,
} */
);
}
return stripePromise;
};
export function createPaymentLink(opts: {
paymentUid: string;
name?: Maybe<string>;
date?: Maybe<string>;
absolute?: boolean;
}): string {
const { paymentUid, name, date, absolute = true } = opts;
let link = "";
if (absolute) link = process.env.NEXT_PUBLIC_APP_URL!;
const query = stringify({ date, name });
return link + `/payment/${paymentUid}?${query}`;
}
export default getStripe;