feat: add server modules (stripe, contract, ai, facebook)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import Stripe from 'stripe';
|
||||
|
||||
function getStripe(): Stripe | null {
|
||||
if (!process.env.STRIPE_SECRET_KEY) return null;
|
||||
return new Stripe(process.env.STRIPE_SECRET_KEY);
|
||||
}
|
||||
|
||||
export async function createDonationCheckout(params: {
|
||||
amount: number;
|
||||
donorEmail?: string;
|
||||
petId?: number;
|
||||
petName?: string;
|
||||
successUrl: string;
|
||||
cancelUrl: string;
|
||||
}): Promise<string | null> {
|
||||
const stripe = getStripe();
|
||||
if (!stripe) return null;
|
||||
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
payment_method_types: ['card'],
|
||||
mode: 'payment',
|
||||
customer_email: params.donorEmail,
|
||||
line_items: [{
|
||||
price_data: {
|
||||
currency: 'usd',
|
||||
product_data: {
|
||||
name: params.petName
|
||||
? `Donation for ${params.petName}`
|
||||
: 'Donation'
|
||||
},
|
||||
unit_amount: params.amount
|
||||
},
|
||||
quantity: 1
|
||||
}],
|
||||
metadata: {
|
||||
type: 'donation',
|
||||
petId: params.petId?.toString() || ''
|
||||
},
|
||||
success_url: params.successUrl,
|
||||
cancel_url: params.cancelUrl
|
||||
});
|
||||
|
||||
return session.url;
|
||||
}
|
||||
|
||||
export async function createAdoptionFeeCheckout(params: {
|
||||
amount: number;
|
||||
petId: number;
|
||||
petName: string;
|
||||
adopterEmail: string;
|
||||
successUrl: string;
|
||||
cancelUrl: string;
|
||||
}): Promise<string | null> {
|
||||
const stripe = getStripe();
|
||||
if (!stripe) return null;
|
||||
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
payment_method_types: ['card'],
|
||||
mode: 'payment',
|
||||
customer_email: params.adopterEmail,
|
||||
line_items: [{
|
||||
price_data: {
|
||||
currency: 'usd',
|
||||
product_data: {
|
||||
name: `Adoption Fee — ${params.petName}`
|
||||
},
|
||||
unit_amount: params.amount
|
||||
},
|
||||
quantity: 1
|
||||
}],
|
||||
metadata: {
|
||||
type: 'adoption_fee',
|
||||
petId: params.petId.toString()
|
||||
},
|
||||
success_url: params.successUrl,
|
||||
cancel_url: params.cancelUrl
|
||||
});
|
||||
|
||||
return session.url;
|
||||
}
|
||||
|
||||
export function constructWebhookEvent(
|
||||
body: string,
|
||||
signature: string
|
||||
): Stripe.Event | null {
|
||||
const stripe = getStripe();
|
||||
if (!stripe || !process.env.STRIPE_WEBHOOK_SECRET) return null;
|
||||
|
||||
return stripe.webhooks.constructEvent(
|
||||
body,
|
||||
signature,
|
||||
process.env.STRIPE_WEBHOOK_SECRET
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user