import { email } from "https://esm.town/v/std/email?v=11";
import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
import { html } from "https://esm.town/v/stevekrouse/html";
import { thisValURL } from "https://esm.town/v/stevekrouse/thisValURL";
import { renderToString } from "npm:react-dom/server";
import Stripe from "npm:stripe";
const stripe = new Stripe(Deno.env.get("stripe_sk_customer_readonly") as string, {
apiVersion: "2020-08-27",
});
function getStripeCustomer(customerId) {
return stripe.customers.retrieve(customerId);
}
let welcomeEmail = renderToString(
<div style={{ maxWidth: "500px", fontFamily: "sans-serif" }}>
<p>
Thank you for becoming a paying customer of Val Town. It's support like yours that enables us to build the future of coding.
</p>
<p>If I can ever do anything for you, let me know! And if you have any feedback please send it my way.</p>
<p>Best,</p>
<p>Steve</p>
<p>Val Town | CEO</p>
<p>
ps - this email is <a href={thisValURL()}>a val</a>
</p>
</div>,
);
export let newStripeEvent = async (req: Request) => {
if (req.method === "GET") return html(welcomeEmail);
const signature = req.headers.get("Stripe-Signature");
const body = await req.text();
const webhookSecret = Deno.env.get("STRIPE_WEBHOOK_SECRET");
let event;
try {
event = await stripe.webhooks.constructEventAsync(
body,
signature,
webhookSecret,
);
} catch (err) {
return new Response(`Webhook Error: ${err.message}`, { status: 400 });
}
const customer = await getStripeCustomer(event.data.object.customer);
const customerEmail = customer.email;
discordWebhook({
url: Deno.env.get("discordUserEvents"),
content: `<@&1081224342110736394> New Subscription 🥳 ${customerEmail}`,
});
await email({
to: customerEmail,
subject: "Thank you!",
html: welcomeEmail,
from: { name: "Steve Krouse", email: "stevekrouse.company@valtown.email" },
cc: { name: "Team", email: "team@val.town" },
});
return Response.json("ok");
};