Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes made on Wednesdays lesson #10

Open
wants to merge 1 commit into
base: tuesday
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/routes/client/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const app = require('../../loaders/express-handlebars');
const { createOrder } = require('../../services/server/klarna');
const { getItemById } = require('../../services/server/fakeStore');

app.get('/', async function (req, res, next) {
// Replace with HTML snippet from CreateOrder Klarna Request
const klarnaJsonResponse = await createOrder();
const html_snippet = klarnaJsonResponse.html_snippet;
console.log('Order id: ', klarnaJsonResponse.order_id);
res.render('checkout', { klarna_checkout: html_snippet });
app.get('/checkout/:product_id', async function (req, res, next) {
try {
const product_id = req.params.product_id;
const product = await getItemById(product_id);
const klarnaJsonResponse = await createOrder(product);
const html_snippet = klarnaJsonResponse.html_snippet;
console.log('Order id: ', klarnaJsonResponse.order_id);
res.render('checkout', { klarna_checkout: html_snippet });
} catch (error) {
res.send(error.message);
}
});

module.exports = app;
14 changes: 14 additions & 0 deletions src/services/server/fakeStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fetch from 'node-fetch';

async function getItemById(id) {
const response = await fetch(`https://fakestoreapi.com/products/${id}`);
if (response.ok) {
const product = await response.json();
return product;
} else {
console.error('ERROR: ', response);
throw new Error('500 error! Fake store API not available');
}
}

module.exports = { getItemById };
65 changes: 47 additions & 18 deletions src/services/server/klarna.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,41 @@ function getKlarnaAuth() {
return auth;
}

function formatAsOrderLines(currentCart) {
currentCart.forEach((item) => {
item.total_amount = item.quantity * item.unit_price;
item.total_tax_amount = item.total_amount - (item.total_amount * 10000) / (10000 + item.tax_rate);
});
return currentCart;
}

function formatProduct(product) {
return {
type: 'physical', // same
reference: product.id,
name: product.title,
quantity: 1, // As a challenge, this value should be dynamic so that we can buy multiple items
quantity_unit: 'pcs', // same
unit_price: parseInt(product.price) * 100,
tax_rate: 2500, // same
total_discount_amount: 0, // same
image_url: product.image
};
}

// 1. Add async createOrder function that returns Klarna response.json()
async function createOrder() {
async function createOrder(product) {
const formattedProduct = formatProduct(product);
const order_lines = formatAsOrderLines([formattedProduct]);

let order_amount = 0;
let order_tax_amount = 0;

order_lines.forEach((item) => {
order_amount += item.total_amount;
order_tax_amount += item.total_tax_amount;
});

// Sub Parts
const path = '/checkout/v3/orders';
const auth = getKlarnaAuth();
Expand All @@ -26,28 +59,24 @@ async function createOrder() {
purchase_country: 'SE',
purchase_currency: 'SEK',
locale: 'sv-SE',
order_amount: 50000,
order_tax_amount: 4545,
order_lines: [
{
type: 'physical',
reference: '19-402-USA',
name: 'Red T-Shirt',
quantity: 5,
quantity_unit: 'pcs',
unit_price: 10000,
tax_rate: 1000,
total_amount: 50000,
total_discount_amount: 0,
total_tax_amount: 4545
}
],
order_amount: order_amount,
order_tax_amount: order_tax_amount,
order_lines,
merchant_urls: {
terms: 'https://www.example.com/terms.html',
checkout: 'https://www.example.com/checkout.html',
confirmation: `${process.env.CONFIRMATION_URL}/confirmation?order_id={checkout.order.id}`,
push: 'https://www.example.com/api/push'
}
},
shipping_options: [
{
id: 'express_priority',
name: 'Techover EXPRESS 1-2 full moons',
price: 39 * 100,
tax_amount: 0,
tax_rate: 0
}
]
};

const body = JSON.stringify(payload);
Expand Down