diff --git a/functions/lib/appmax/customer.js b/functions/lib/appmax/customer.js new file mode 100644 index 0000000..30dede9 --- /dev/null +++ b/functions/lib/appmax/customer.js @@ -0,0 +1,53 @@ +const axios = require('axios') + +const createOrUpdateCustomer = async (buyer, to, items, browser_ip, utm, token) => { + const { email, fullname } = buyer + const splitedName = fullname.split(' ') + const products = [] + items.forEach(({sku, quantity}) => { + if (quantity > 0) { + products.push({ + product_sku: sku, + product_qty: quantity + }) + } + }); + const body = { + "access-token": token, + "firstname": splitedName[0], + "lastname": splitedName[splitedName.length - 1], + email, + "telephone": buyer.phone.number, + "postcode": to.zip.replace(/\D/g, '').padStart(8, '0'), + "address_street": to.street, + "address_street_number": to.number ? String(to.number) : 'SN', + "address_street_complement": to.complement, + "address_street_district": to.borough, + "address_city": to.city, + "address_state": to.province || to.province_code, + "ip": browser_ip, + "custom_txt": items[0].name, + products, + "tracking": { + "utm_source": utm.source, + "utm_campaign": utm.campaign, + "utm_medium": utm.medium, + "utm_content": utm.content, + "utm_term": utm.content + } + } + + const { data } = await axios({ + url: 'https://homolog.sandboxappmax.com.br/api/v3/customer', + method: 'post', + data: body + }) + console.log('created customer', JSON.stringify(data)) + + if (data) { + return data.customer_id + } + return null +} + +module.exports = createOrUpdateCustomer diff --git a/functions/lib/appmax/order.js b/functions/lib/appmax/order.js new file mode 100644 index 0000000..fb95555 --- /dev/null +++ b/functions/lib/appmax/order.js @@ -0,0 +1,38 @@ +const axios = require('axios') + +const createOrder = async (items, amount, customer_id, token) => { + const { total, discount, freight } = amount + const products = [] + items.forEach(({sku, quantity, name, price, final_price}) => { + if (quantity > 0) { + products.push({ + sku, + name, + qty: quantity, + price: final_price || price + }) + } + }); + const body = { + "access-token": token, + total, + products, + "shipping": freight, + customer_id, + discount +} + + const { data } = await axios({ + url: 'https://homolog.sandboxappmax.com.br/api/v3/order', + method: 'post', + data: body + }) + console.log('created order', JSON.stringify(data)) + + if (data) { + return data.order_id + } + return null +} + +module.exports = createOrder