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 });
}
}
};
-
Large Cotton-Linen Storage Basket with Carry Handles -
Interactive Rope Tug Toy for Dog -
USB Rechargeable Handheld Personal Fan -
Portable USB Air Cooler Fan with Misting -
Mini Quick-Cool Ice Cup -
Smart Anti-Bark Collar for Dogs – IP67 Waterproof -
Color-Changing Mushroom LED Lamp – 15 cm, 30 cm or Set -
800 ml Portable Dog Water Bottle with Foldable Bowl -
1.2 L Insulated Stainless Steel Tumbler with Straw -
Rechargeable Mini Food Chopper – 250 ml -
1.2 L Christmas Insulated Tumbler with Lid & Straw -
Crystal-Effect Resin Decorative Mug – 400 ml -
Bohemian Underglaze Ceramic Tableware – Multiple Style -
USB Rechargeable Portable Blender Cup -
USB Rechargeable Electric Shaker Bottle -
Portable Stainless Steel Mini Egg Boiler -
4-Egg Microwave Egg Cooker with Ventilated Lid -
Battery-Operated Motion Sensor LED Night Light -
Wall Touch-Up Paint Roller – 500 g -
Adjustable Washing Machine Stand with Locking Wheels -
360° Rotating Faucet Extender with 4 Water Modes