2022-01-03 22:50:59 +00:00
|
|
|
import { Stripe } from "@stripe/stripe-js";
|
|
|
|
import { loadStripe } from "@stripe/stripe-js/pure";
|
2021-09-22 18:36:13 +00:00
|
|
|
import { stringify } from "querystring";
|
|
|
|
|
2022-03-09 22:56:05 +00:00
|
|
|
export type Maybe<T> = T | undefined | null;
|
2021-10-14 19:22:01 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
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>;
|
2022-05-25 01:29:29 +00:00
|
|
|
email?: Maybe<string>;
|
2021-10-05 22:46:48 +00:00
|
|
|
absolute?: boolean;
|
|
|
|
}): string {
|
2022-05-25 01:29:29 +00:00
|
|
|
const { paymentUid, name, email, date, absolute = true } = opts;
|
2021-09-22 18:36:13 +00:00
|
|
|
let link = "";
|
2022-03-26 00:39:38 +00:00
|
|
|
if (absolute) link = process.env.NEXT_PUBLIC_WEBSITE_URL!;
|
2022-05-25 01:29:29 +00:00
|
|
|
const query = stringify({ date, name, email });
|
2021-09-22 18:36:13 +00:00
|
|
|
return link + `/payment/${paymentUid}?${query}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default getStripe;
|