#!/usr/bin/env node /** * Full order creation test via API * Tests complete checkout flow including order completion */ const SALEOR_API_URL = 'https://api.manoonoils.com/graphql/'; async function saleorFetch(query, variables = {}) { const response = await fetch(SALEOR_API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: query.replace(/\n\s*/g, ' '), variables }), }); const result = await response.json(); if (result.errors) { console.error('GraphQL Error:', JSON.stringify(result.errors, null, 2)); throw new Error(result.errors[0].message); } return result.data; } async function runOrderTest() { console.log('๐Ÿงช FULL ORDER CREATION TEST ON DEV BRANCH\n'); console.log('=' .repeat(60)); try { // STEP 1: Create checkout console.log('\n๐Ÿ“ฆ STEP 1: Create Checkout'); const createResult = await saleorFetch(` mutation CheckoutCreate($input: CheckoutCreateInput!) { checkoutCreate(input: $input) { checkout { id token totalPrice { gross { amount currency } } } errors { field message } } } `, { input: { channel: "default-channel", email: "test-order@example.com", lines: [], languageCode: "SR" } }); const checkoutId = createResult.checkoutCreate.checkout.id; console.log('โœ… Checkout created:', checkoutId); // STEP 2: Get product and add to cart console.log('\n๐Ÿ›’ STEP 2: Add Product'); const productsResult = await saleorFetch(` query { products(channel: "default-channel", first: 1) { edges { node { variants { id name } } } } } `); const variantId = productsResult.products.edges[0].node.variants[0].id; await saleorFetch(` mutation CheckoutLinesAdd($checkoutId: ID!, $lines: [CheckoutLineInput!]!) { checkoutLinesAdd(checkoutId: $checkoutId, lines: $lines) { checkout { id } errors { field message } } } `, { checkoutId: checkoutId, lines: [{ variantId: variantId, quantity: 1 }] }); console.log('โœ… Product added'); // STEP 3: Update email console.log('\n๐Ÿ“ง STEP 3: Update Email'); await saleorFetch(` mutation CheckoutEmailUpdate($checkoutId: ID!, $email: String!) { checkoutEmailUpdate(checkoutId: $checkoutId, email: $email) { checkout { id } errors { field message } } } `, { checkoutId: checkoutId, email: "test-order@example.com" }); console.log('โœ… Email updated'); // STEP 4: Set shipping address console.log('\n๐Ÿ“ STEP 4: Set Shipping Address'); await saleorFetch(` mutation CheckoutShippingAddressUpdate($checkoutId: ID!, $shippingAddress: AddressInput!) { checkoutShippingAddressUpdate(checkoutId: $checkoutId, shippingAddress: $shippingAddress) { checkout { id shippingMethods { id name price { amount } } } errors { field message } } } `, { checkoutId: checkoutId, shippingAddress: { firstName: "Test", lastName: "User", streetAddress1: "123 Test Street", city: "Belgrade", postalCode: "11000", country: "RS", phone: "+38160123456" } }); // Get shipping methods const methodsResult = await saleorFetch(` query GetCheckout($token: UUID!) { checkout(token: $token) { shippingMethods { id name price { amount } } } } `, { token: createResult.checkoutCreate.checkout.token }); const shippingMethodId = methodsResult.checkout.shippingMethods[0].id; console.log('โœ… Address set, shipping method available:', methodsResult.checkout.shippingMethods[0].name); // STEP 5: Set billing address console.log('\n๐Ÿ’ณ STEP 5: Set Billing Address'); await saleorFetch(` mutation CheckoutBillingAddressUpdate($checkoutId: ID!, $billingAddress: AddressInput!) { checkoutBillingAddressUpdate(checkoutId: $checkoutId, billingAddress: $billingAddress) { checkout { id } errors { field message } } } `, { checkoutId: checkoutId, billingAddress: { firstName: "Test", lastName: "User", streetAddress1: "123 Test Street", city: "Belgrade", postalCode: "11000", country: "RS", phone: "+38160123456" } }); console.log('โœ… Billing address set'); // STEP 6: Select shipping method console.log('\n๐Ÿšš STEP 6: Select Shipping Method'); await saleorFetch(` mutation CheckoutShippingMethodUpdate($checkoutId: ID!, $shippingMethodId: ID!) { checkoutShippingMethodUpdate(checkoutId: $checkoutId, shippingMethodId: $shippingMethodId) { checkout { id totalPrice { gross { amount } } subtotalPrice { gross { amount } } shippingPrice { gross { amount } } } errors { field message } } } `, { checkoutId: checkoutId, shippingMethodId: shippingMethodId }); console.log('โœ… Shipping method selected'); // STEP 7: Complete checkout (create order) console.log('\nโœ… STEP 7: Complete Checkout (Create Order)'); console.log('-'.repeat(60)); const completeResult = await saleorFetch(` mutation CheckoutComplete($checkoutId: ID!) { checkoutComplete(checkoutId: $checkoutId) { order { id number status created total { gross { amount currency } } subtotal { gross { amount } } shippingPrice { gross { amount } } } errors { field message } } } `, { checkoutId: checkoutId }); if (completeResult.checkoutComplete.errors?.length > 0) { throw new Error(`Order creation failed: ${completeResult.checkoutComplete.errors[0].message}`); } const order = completeResult.checkoutComplete.order; console.log('โœ… ORDER CREATED SUCCESSFULLY!'); console.log(''); console.log('Order Details:'); console.log(' Order ID:', order.id); console.log(' Order Number:', order.number); console.log(' Status:', order.status); console.log(' Created:', order.created); console.log(''); console.log('Pricing:'); console.log(' Subtotal:', order.subtotal.gross.amount, 'RSD'); console.log(' Shipping:', order.shippingPrice.gross.amount, 'RSD'); console.log(' Total:', order.total.gross.amount, 'RSD'); // Verification const expectedTotal = order.subtotal.gross.amount + order.shippingPrice.gross.amount; console.log(''); console.log('๐Ÿ“Š VERIFICATION:'); if (order.total.gross.amount === expectedTotal) { console.log('โœ… PASS: Order total includes shipping correctly'); console.log(` ${order.subtotal.gross.amount} + ${order.shippingPrice.gross.amount} = ${order.total.gross.amount}`); } else { console.log('โŒ FAIL: Order total does not match expected'); } console.log(''); console.log('๐ŸŽ‰ DEV BRANCH TEST COMPLETE - ALL SYSTEMS GO!'); } catch (error) { console.error('\nโŒ Test failed:', error.message); process.exit(1); } } runOrderTest();