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 });
}
}
};
-
Men’s Slim Quartz Watch -
kindergarten small school bag backpack -
Men's Business Mechanical Watches -
Women’s Two-Tone Quartz Watch -
Men’s Digital Alarm Watch with Stopwatch -
Men’s Black-Case Quartz Watch -
Men’s P815 Quartz Watch with Coloured Dial -
Men’s Bold Quartz Watch -
15.6-Inch Laptop Travel Backpack -
Striped Straw Beach Bag with Double Straps -
Soft PU Hobo Bag with Multiple Compartments -
Kids’ Animal Backpack for School -
Fashion Open Ring – 3 Metallic Colours -
Starfish & Shell-Motif Drop Earrings – 1 Pair -
Blue Stone-Effect Square Hoop Earrings – 1 Pair -
Colourful Round Statement Earrings – 5 Colours -
Heart-Accent Stackable Ring Set – 2 Pieces, 9 Styles -
Geometric Hoop Dangle Earrings – Gold-Coloured Pair -
Sparkling Stone-Effect Ear Cuffs – Gold-Coloured Pair -
Camellia Rose Open Ring – Silver-Coloured