export default {
async fetch(req, env) {
const cors = {
'Access-Control-Allow-Origin': '*', // replace with your store domain
'Access-Control-Allow-Headers': 'Content-Type',
'Content-Type': 'application/json'
};
if (req.method === 'OPTIONS') return new Response(null, { headers: cors });
try {
const d = await req.json();
if (!/^[6-9]\d{9}$/.test(d.phone) || !/^\d{6}$/.test(d.pincode))
throw new Error('Invalid phone or pincode');
const [first, ...rest] = d.name.trim().split(' ');
const address = {
first_name: first, last_name: rest.join(' ') || '-',
address1: d.address, city: d.city, province: d.state,
zip: d.pincode, country: 'India', phone: d.phone
};
const res = await fetch(`https://${env.SHOP}/admin/api/2024-10/orders.json`, {
method: 'POST',
headers: { 'X-Shopify-Access-Token': env.TOKEN, 'Content-Type': 'application/json' },
body: JSON.stringify({
order: {
line_items: [{ variant_id: Number(d.variant_id), quantity: Number(d.quantity) || 1 }],
customer: { first_name: first, last_name: address.last_name, phone: '+91' + d.phone },
shipping_address: address,
billing_address: address,
phone: '+91' + d.phone,
financial_status: 'pending',
gateway: 'Cash on Delivery (COD)',
tags: 'COD',
note: 'COD form order'
}
})
});
const j = await res.json();
if (!res.ok) throw new Error(JSON.stringify(j.errors));
return new Response(JSON.stringify({ ok: true, order: j.order.name }), { headers: cors });
} catch (e) {
return new Response(JSON.stringify({ ok: false, error: e.message }), { status: 400, headers: cors });
}
}
};
-
Soft PU Hobo Bag with Multiple Compartments -
25 cm Animal Plush Toy – 11 Designs -
5-in-1 Wireless Charging Station 15W with Clock -
PHOFAY Lipstick – 3.5 g, 8 Colours -
Geometric Hoop Dangle Earrings – Gold-Coloured Pair -
Color-Changing Mushroom LED Lamp – 15 cm, 30 cm or Set -
5D Diamond Painting Canvas – 30 × 30 cm, Assorted Designs -
Magnetic 3-in-1 Wireless Charging Station -
Matte Lip Colour Collection – Styles A–F -
Sakyamuni Buddha Resin Decorative Statue