mirror of
https://github.com/Motion-Games/MotionWebStudio.git
synced 2026-04-21 09:00:53 +02:00
stripe
This commit is contained in:
104
supabase/functions/check-subscriptions/index.ts
Normal file
104
supabase/functions/check-subscriptions/index.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
|
||||
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.39.3'
|
||||
|
||||
declare const Deno: any;
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||||
}
|
||||
|
||||
Deno.serve(async (req: any) => {
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response('ok', { headers: corsHeaders })
|
||||
}
|
||||
|
||||
try {
|
||||
const supabase = createClient(
|
||||
Deno.env.get('SUPABASE_URL') ?? '',
|
||||
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
|
||||
)
|
||||
|
||||
// Fetch active subscriptions
|
||||
const { data: subscriptions, error } = await supabase
|
||||
.from('maintenance_subscriptions')
|
||||
.select('*, order:orders(*)')
|
||||
.eq('status', 'active');
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
const notificationsToSend: any[] = [];
|
||||
const today = new Date();
|
||||
|
||||
// Mock email sending function for now
|
||||
const sendEmailMock = async (payload: any) => {
|
||||
console.log("Mock email sent:", payload);
|
||||
// In production, you would invoke the 'resend' function here
|
||||
};
|
||||
|
||||
if (subscriptions) {
|
||||
for (const sub of subscriptions) {
|
||||
const billingDate = new Date(sub.next_billing_date);
|
||||
const daysUntil = Math.ceil((billingDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
|
||||
|
||||
// Logic: Notify if <= 30 days and not notified recently (e.g. in last 14 days)
|
||||
let shouldNotify = false;
|
||||
|
||||
// If it's already overdue or coming up in 30 days
|
||||
if (daysUntil <= 30) {
|
||||
if (!sub.last_notified_at) {
|
||||
shouldNotify = true;
|
||||
} else {
|
||||
const lastNotified = new Date(sub.last_notified_at);
|
||||
const daysSinceLastNotification = Math.ceil((today.getTime() - lastNotified.getTime()) / (1000 * 60 * 60 * 24));
|
||||
// Only notify if 2 weeks passed since last notification
|
||||
if (daysSinceLastNotification > 14) {
|
||||
shouldNotify = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldNotify) {
|
||||
const payload = {
|
||||
to: sub.client_email,
|
||||
subject: `Emlékeztető: Éves díj esedékes (${daysUntil} nap)`,
|
||||
daysLeft: daysUntil,
|
||||
domain: sub.order?.details?.domainName || 'Weboldal',
|
||||
subscriptionId: sub.id
|
||||
};
|
||||
|
||||
notificationsToSend.push(payload);
|
||||
|
||||
// Itt hívnánk meg a sendEmail függvényt
|
||||
await sendEmailMock(payload);
|
||||
|
||||
// Update last_notified_at to keep data consistent
|
||||
const { error: updateError } = await supabase
|
||||
.from('maintenance_subscriptions')
|
||||
.update({ last_notified_at: new Date().toISOString() })
|
||||
.eq('id', sub.id);
|
||||
|
||||
if (updateError) {
|
||||
console.error(`Failed to update subscription ${sub.id}:`, updateError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({ success: true, notifications: notificationsToSend }),
|
||||
{
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: 200,
|
||||
}
|
||||
)
|
||||
} catch (err: any) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: err.message }),
|
||||
{
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: 400,
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
90
supabase/functions/create-checkout-session/index.ts
Normal file
90
supabase/functions/create-checkout-session/index.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
// @ts-nocheck
|
||||
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
|
||||
import { Stripe } from "https://esm.sh/stripe@12.0.0?target=deno"
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||||
}
|
||||
|
||||
serve(async (req) => {
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response('ok', { headers: corsHeaders })
|
||||
}
|
||||
|
||||
try {
|
||||
const STRIPE_SECRET_KEY = Deno.env.get('STRIPE_SECRET_KEY')
|
||||
if (!STRIPE_SECRET_KEY) {
|
||||
throw new Error('STRIPE_SECRET_KEY not set in environment variables')
|
||||
}
|
||||
|
||||
const stripe = new Stripe(STRIPE_SECRET_KEY, {
|
||||
apiVersion: '2022-11-15',
|
||||
httpClient: Stripe.createFetchHttpClient(),
|
||||
})
|
||||
|
||||
const { order_id, package_name, payment_type, customer_email } = await req.json()
|
||||
|
||||
// Árazási logika (HUF-ban)
|
||||
let priceAmount = 0
|
||||
|
||||
// Landing Page: 190.000 Ft (Előleg: 40.000, Vég: 150.000)
|
||||
if (package_name === 'Landing Page') {
|
||||
if (payment_type === 'deposit') priceAmount = 40000;
|
||||
else if (payment_type === 'final') priceAmount = 150000;
|
||||
}
|
||||
// Pro Web: 350.000 Ft (Előleg: 80.000, Vég: 270.000)
|
||||
else if (package_name === 'Pro Web') {
|
||||
if (payment_type === 'deposit') priceAmount = 80000;
|
||||
else if (payment_type === 'final') priceAmount = 270000;
|
||||
}
|
||||
|
||||
if (priceAmount === 0) {
|
||||
throw new Error('Invalid package or payment type')
|
||||
}
|
||||
|
||||
// Origin meghatározása a visszairányításhoz
|
||||
const origin = req.headers.get('origin') || 'https://motionweb.hu'
|
||||
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
payment_method_types: ['card'],
|
||||
line_items: [
|
||||
{
|
||||
price_data: {
|
||||
currency: 'huf',
|
||||
product_data: {
|
||||
name: `${package_name} - ${payment_type === 'deposit' ? 'Előleg' : 'Fennmaradó összeg'}`,
|
||||
},
|
||||
unit_amount: priceAmount * 100, // Fillérben kell megadni
|
||||
},
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
mode: 'payment',
|
||||
success_url: `${origin}/#/dashboard?payment_success=true&order_id=${order_id}`,
|
||||
cancel_url: `${origin}/#/dashboard?payment_cancelled=true&order_id=${order_id}`,
|
||||
customer_email: customer_email,
|
||||
metadata: {
|
||||
order_id: order_id,
|
||||
payment_type: payment_type
|
||||
},
|
||||
})
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({ url: session.url }),
|
||||
{
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: 200,
|
||||
}
|
||||
)
|
||||
} catch (error: any) {
|
||||
console.error(error)
|
||||
return new Response(
|
||||
JSON.stringify({ error: error.message }),
|
||||
{
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: 400,
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user