2021-09-22 18:36:13 +00:00
|
|
|
import { loadStripe, Stripe } from "@stripe/stripe-js";
|
2021-10-05 22:46:48 +00:00
|
|
|
import { Maybe } from "@trpc/server";
|
2021-09-22 18:36:13 +00:00
|
|
|
import { stringify } from "querystring";
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2021-10-05 22:46:48 +00:00
|
|
|
export function createPaymentLink(opts: {
|
|
|
|
paymentUid: string;
|
|
|
|
name?: Maybe<string>;
|
|
|
|
date?: Maybe<string>;
|
|
|
|
absolute?: boolean;
|
|
|
|
}): string {
|
|
|
|
const { paymentUid, name, date, absolute = true } = opts;
|
2021-09-22 18:36:13 +00:00
|
|
|
let link = "";
|
|
|
|
if (absolute) link = process.env.NEXT_PUBLIC_APP_URL!;
|
|
|
|
const query = stringify({ date, name });
|
|
|
|
return link + `/payment/${paymentUid}?${query}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default getStripe;
|