Skip to content

Instantly share code, notes, and snippets.

@GiovanniCavallari
Created February 10, 2021 19:12
Show Gist options
  • Save GiovanniCavallari/07a66efcc2ccb46c8264a3d6e24704c6 to your computer and use it in GitHub Desktop.
Save GiovanniCavallari/07a66efcc2ccb46c8264a3d6e24704c6 to your computer and use it in GitHub Desktop.
Create shopify order with Rest API
const selectShippingMethod: ShippingMethodsDto = shippingMethods.find(
shippingRate => shippingRate.id === shippingLine?.id,
);
if ((shippingMethods.length > 0 && !selectShippingMethod) || (shippingMethods.length === 0 && shippingLine)) {
throw new InvalidShippingRatingError();
}
if (selectShippingMethod) {
shippingLine.price = Number(selectShippingMethod?.price || 0.0);
}
const totalTaxes = checkout.total_tax;
const taxLines: ITaxLines[] = checkout.tax_lines.map(tax => {
return {
title: tax.title,
price: tax.price,
rate: tax.rate,
};
});
const lineItems: ILineItem[] = checkout.line_items.map((item: ICheckoutItems) => {
const lineItem: ILineItem = {
id: item.id,
title: item.title,
['variant_id']: item.variant_id,
name: item.name,
quantity: item.quantity,
price: item.price,
['product_id']: item.product_id,
['requires_shipping']: item.requires_shipping,
sku: item.sku,
properties:
typeof item.properties === 'object'
? Object.keys(item.properties).map((key: string) => ({ name: key, value: item.properties[key] }))
: item.properties,
};
return this.discountService.applyDiscountToLineItem(lineItem, item);
});
const buyerPhone = buyer.phone ? `+55${buyer.phone.areaCode}${buyer.phone.number}` : null;
const buyerAddress = this.createAddressString(
buyer.address.streetName,
buyer.address.streetNumber,
buyer.address.neighborhood,
buyer.address.apartment,
);
const payerPhone = payer.phone ? `+55${payer.phone.areaCode}${payer.phone.number}` : null;
const payerAddress = this.createAddressString(
payer.address.streetName,
payer.address.streetNumber,
payer.address.neighborhood,
payer.address.apartment,
);
const status = 'pending';
const orderData: IShopifyOrder = {
order: {
email: buyer.email,
phone: buyerPhone,
send_fulfillment_receipt: true,
send_receipt: true,
inventory_behaviour: 'decrement_obeying_policy',
customer: {
email: buyer.email,
phone: buyerPhone,
first_name: buyer.firstName,
last_name: buyer.lastName,
},
line_items: lineItems,
subtotal_price: lineItems.map((item: ILineItem) => item.price * item.quantity).reduce((acc, cur) => acc + cur),
total_discounts: 0,
financial_status: status,
shipping_lines: [shippingLine],
tax_lines: taxLines,
total_tax: totalTaxes,
shipping_address: {
first_name: buyer.firstName,
last_name: buyer.lastName,
phone: buyerPhone,
country: 'Brazil',
address1: buyerAddress,
city: buyer.address.cityName,
province: buyer.address.stateName,
zip: buyer.address.zipCode,
},
billing_address: {
first_name: payer.firstName,
last_name: payer.lastName,
phone: payerPhone,
country: 'Brazil',
address1: payerAddress,
city: payer.address.cityName,
province: payer.address.stateName,
zip: payer.address.zipCode,
},
discount_codes: [],
},
};
this.discountService.prepareCheckoutDiscounts(checkout, orderData);
try {
const order: OrderResponseDto = await this.shopifyOrderService.createOrder(url, orderData, seller);
this.shopifyOrderService.putDocumentInOrder(order.id, payer.identification.number, seller);
return order;
} catch (e) {
if ((e as AxiosError).isAxiosError === true && e.response?.data?.errors) {
throw new ShopifyOrderCreationError(e.response.data.errors);
}
throw new ShopifyOrderCreationError();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment