Skip to content
Snippets Groups Projects
Commit b13c09fb authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

fixed price and amount selection

parent 75123c10
No related branches found
No related tags found
No related merge requests found
...@@ -71,11 +71,19 @@ class Crypto { ...@@ -71,11 +71,19 @@ class Crypto {
/** /**
* Creates an hmac hash for purchase data so we can check it later * Creates an hmac hash for purchase data so we can check it later
*/ */
createIntegrityHash(order_id, amount, price, public_key_n, public_key_e) { createIntegrityHash(
order_id,
amount,
unit_size,
price_per_unit,
public_key_n,
public_key_e
) {
let data_to_hash = JSON.stringify({ let data_to_hash = JSON.stringify({
order_id: parseInt(order_id), order_id: parseInt(order_id),
amount: parseInt(amount), amount: parseInt(amount),
price: parseFloat(price), unit_size: parseInt(unit_size),
price_per_unit: parseFloat(price_per_unit),
public_key_n: public_key_n, public_key_n: public_key_n,
public_key_e: public_key_e, public_key_e: public_key_e,
}); });
...@@ -93,14 +101,16 @@ class Crypto { ...@@ -93,14 +101,16 @@ class Crypto {
user_hash, user_hash,
order_id, order_id,
amount, amount,
price, unit_size,
price_per_unit,
public_key_n, public_key_n,
public_key_e public_key_e
) { ) {
let data_to_hash = JSON.stringify({ let data_to_hash = JSON.stringify({
order_id: parseInt(order_id), order_id: parseInt(order_id),
amount: parseInt(amount), amount: parseInt(amount),
price: parseFloat(price), unit_size: parseInt(unit_size),
price_per_unit: parseFloat(price_per_unit),
public_key_n: public_key_n, public_key_n: public_key_n,
public_key_e: public_key_e, public_key_e: public_key_e,
}); });
......
...@@ -13,12 +13,21 @@ class Order { ...@@ -13,12 +13,21 @@ class Order {
return 15; return 15;
} }
static get PRICE_FOR_100() {
return 2;
}
static get PRICE_FOR_250() {
return 4;
}
/** /**
* Data stored in Redis Database * Data stored in Redis Database
*/ */
#order_id; #order_id;
#amount; #amount;
#price; #unit_size;
#price_per_unit;
#encrypted_sales_receipts; #encrypted_sales_receipts;
#payment_completed; #payment_completed;
...@@ -28,10 +37,17 @@ class Order { ...@@ -28,10 +37,17 @@ class Order {
#create_mode; #create_mode;
#redis_client; #redis_client;
constructor(order_id, amount, price, encrypted_sales_receipts) { constructor(
order_id,
amount,
unit_size,
price_per_unit,
encrypted_sales_receipts
) {
this.#order_id = order_id; this.#order_id = order_id;
this.#amount = amount; this.#amount = amount;
this.#price = price; this.#unit_size = unit_size;
this.#price_per_unit = price_per_unit;
this.#encrypted_sales_receipts = encrypted_sales_receipts; this.#encrypted_sales_receipts = encrypted_sales_receipts;
this.#payment_completed = false; this.#payment_completed = false;
...@@ -73,7 +89,8 @@ class Order { ...@@ -73,7 +89,8 @@ class Order {
.hmset(redis_key, { .hmset(redis_key, {
order_id: this.#order_id, order_id: this.#order_id,
amount: this.#amount, amount: this.#amount,
price: this.#price, unit_size: this.#unit_size,
price_per_unit: this.#price_per_unit,
encrypted_sales_receipts: this.#encrypted_sales_receipts, encrypted_sales_receipts: this.#encrypted_sales_receipts,
payment_completed: this.#payment_completed, payment_completed: this.#payment_completed,
}) })
......
const default_searches = 300; const default_searches = 250;
const default_price = 4;
const default_estimate_months = 1; const default_estimate_months = 1;
document.getElementById("amount").addEventListener("input", multiplierChanged); document.getElementById("amount").addEventListener("input", multiplierChanged);
multiplierChanged(); multiplierChanged();
function multiplierChanged() { function multiplierChanged() {
let price_for_250 =
document.querySelector("form.offer.default").dataset.price_for_250;
let multiplier = document.getElementById("amount").value; let multiplier = document.getElementById("amount").value;
let searches_element = document.querySelector( let searches_element = document.querySelector(
"#offers > .offer.default > h1" "#offers > .offer.default > h1"
...@@ -17,6 +18,6 @@ function multiplierChanged() { ...@@ -17,6 +18,6 @@ function multiplierChanged() {
"#offers > .offer.default > button.select" "#offers > .offer.default > button.select"
); );
searches_element.textContent = default_searches * multiplier; searches_element.textContent = default_searches * multiplier;
price_element.textContent = default_price * multiplier + ""; price_element.textContent = price_for_250 * multiplier + "";
estimate_element.textContent = default_estimate_months * multiplier; estimate_element.textContent = default_estimate_months * multiplier;
} }
...@@ -61,7 +61,9 @@ function execute_payment_paypal(e) { ...@@ -61,7 +61,9 @@ function execute_payment_paypal(e) {
body: JSON.stringify({ body: JSON.stringify({
order_id: document.querySelector("input[name=order_id]").value, order_id: document.querySelector("input[name=order_id]").value,
amount: document.querySelector("input[name=amount]").value, amount: document.querySelector("input[name=amount]").value,
price: document.querySelector("input[name=price]").value, unit_size: document.querySelector("input[name=unit_size]"),
price_per_unit:
document.querySelector("input[name=price]").value,
public_key_n: document.querySelector("input[name=public_key_n]") public_key_n: document.querySelector("input[name=public_key_n]")
.value, .value,
public_key_e: document.querySelector("input[name=public_key_e]") public_key_e: document.querySelector("input[name=public_key_e]")
......
...@@ -3,8 +3,6 @@ const Crypto = require("../../app/Crypto.js"); ...@@ -3,8 +3,6 @@ const Crypto = require("../../app/Crypto.js");
const Order = require("../../app/Order.js"); const Order = require("../../app/Order.js");
const config = require("config"); const config = require("config");
const price_for_100 = 1;
var express = require("express"); var express = require("express");
var router = express.Router(); var router = express.Router();
const { query, body, validationResult } = require("express-validator"); const { query, body, validationResult } = require("express-validator");
...@@ -13,8 +11,8 @@ const { query, body, validationResult } = require("express-validator"); ...@@ -13,8 +11,8 @@ const { query, body, validationResult } = require("express-validator");
router.get( router.get(
"/", "/",
query("amount") query("amount")
.isInt({ min: 1, max: 4 }) .isInt({ min: 0, max: 12 })
.withMessage("Amount needs to be between 1 and 4."), .withMessage("Amount needs to be between 0 and 4."),
async function (req, res, next) { async function (req, res, next) {
const errors = validationResult(req); const errors = validationResult(req);
if (!errors.isEmpty()) { if (!errors.isEmpty()) {
...@@ -22,9 +20,10 @@ router.get( ...@@ -22,9 +20,10 @@ router.get(
} }
let params = { let params = {
amount: req.query.amount, amount: req.query.amount === 0 ? 1 : req.query.amount,
unit_size: 100, unit_size: req.query.amount === 0 ? 100 : 250,
price: req.query.amount * price_for_100, price_per_unit:
req.query.amount === 0 ? Order.PRICE_FOR_100 : Order.PRICE_FOR_250,
order_id: await generate_unique_order_id(), order_id: await generate_unique_order_id(),
payments: { payments: {
paypal: { paypal: {
...@@ -45,7 +44,8 @@ router.get( ...@@ -45,7 +44,8 @@ router.get(
params.integrity = crypto.createIntegrityHash( params.integrity = crypto.createIntegrityHash(
params.order_id, params.order_id,
params.amount, params.amount,
params.price, params.unit_size,
params.price_per_unit,
params.crypto.N, params.crypto.N,
params.crypto.E params.crypto.E
); );
...@@ -60,7 +60,8 @@ router.use( ...@@ -60,7 +60,8 @@ router.use(
.isInt({ min: 1, max: 4 }) .isInt({ min: 1, max: 4 })
.withMessage("Invalid amount submitted") .withMessage("Invalid amount submitted")
.toInt(), .toInt(),
body("price") body("unit_size").isIn(["100", "250"]).toInt(),
body("price_per_unit")
.isCurrency({ symbol: "", allow_negatives: false, thousands_separator: "" }) .isCurrency({ symbol: "", allow_negatives: false, thousands_separator: "" })
.withMessage("Invalid Price Value.") .withMessage("Invalid Price Value.")
.toFloat(), .toFloat(),
...@@ -80,7 +81,8 @@ router.use( ...@@ -80,7 +81,8 @@ router.use(
value, value,
req.body.order_id, req.body.order_id,
req.body.amount, req.body.amount,
req.body.price, req.body.unit_size,
req.body.price_per_unit,
req.body.public_key_n, req.body.public_key_n,
req.body.public_key_e req.body.public_key_e
) )
...@@ -121,7 +123,8 @@ router.use( ...@@ -121,7 +123,8 @@ router.use(
let order = new Order( let order = new Order(
req.body.order_id, req.body.order_id,
req.body.amount, req.body.amount,
req.body.price, req.body.unit_size,
req.body.price_per_unit,
req.body.encrypted_sales_receipts req.body.encrypted_sales_receipts
); );
order order
...@@ -143,6 +146,7 @@ router.use( ...@@ -143,6 +146,7 @@ router.use(
); );
var paypalRouter = require("./paypal.js"); var paypalRouter = require("./paypal.js");
const { PRICE_FOR_250 } = require("../../app/Order.js");
router.use("/payment/order/paypal", paypalRouter); router.use("/payment/order/paypal", paypalRouter);
module.exports = router; module.exports = router;
......
...@@ -39,6 +39,13 @@ async function createOrder(req) { ...@@ -39,6 +39,13 @@ async function createOrder(req) {
currency_code: "EUR", currency_code: "EUR",
value: req.body.price.toFixed(2), value: req.body.price.toFixed(2),
}, },
items: [
{
name: "MetaGer Pass: Suchanfragen (100x)",
quantity: 1,
unit_amount: 0,
},
],
}, },
], ],
application_context: { application_context: {
......
var express = require('express'); var express = require("express");
const Order = require("../app/Order.js");
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get("/", function (req, res, next) {
res.render('index', { title: 'Express' }); res.render("index", {
title: "Express",
price_for_100: Order.PRICE_FOR_100,
price_for_250: Order.PRICE_FOR_250,
});
}); });
module.exports = router; module.exports = router;
...@@ -3,12 +3,13 @@ ...@@ -3,12 +3,13 @@
<main> <main>
<input type="hidden" name="order_id" value="<%- order_id %>"> <input type="hidden" name="order_id" value="<%- order_id %>">
<input type="hidden" name="amount" value="<%- amount %>"> <input type="hidden" name="amount" value="<%- amount %>">
<input type="hidden" name="price" value="<%- price %>"> <input type="hidden" name="unit_size" value="<%- unit_size %>">
<input type="hidden" name="price_per_unit" value="<%- price_per_unit %>">
<input type="hidden" name="public_key_e" value="<%- crypto.E %>"> <input type="hidden" name="public_key_e" value="<%- crypto.E %>">
<input type="hidden" name="public_key_n" value="<%- crypto.N %>"> <input type="hidden" name="public_key_n" value="<%- crypto.N %>">
<input type="hidden" name="integrity" value="<%- integrity %>"> <input type="hidden" name="integrity" value="<%- integrity %>">
<div id="payment-container"> <div id="payment-container">
<div id="heading">Ihr Einkauf: <%- amount * unit_size %> Suchanfragen für <%- price %>€</div> <div id="heading">Ihr Einkauf: <%- amount * unit_size %> Suchanfragen für <%- amount * price_per_unit %>€</div>
<div id="generate-sales-receipt" class="step current"> <div id="generate-sales-receipt" class="step current">
<div class="section-heading"> <div class="section-heading">
<div class="status"> <div class="status">
......
...@@ -4,15 +4,15 @@ ...@@ -4,15 +4,15 @@
</p> </p>
<div id="offers"> <div id="offers">
<form class="offer" action="/checkout"> <form class="offer" action="/checkout">
<input type="hidden" name="amount" value="1"> <input type="hidden" name="amount" value="0">
<h1>100</h1> <h1>100</h1>
<div class="spacer"></div> <div class="spacer"></div>
<button type="submit" class="select">1€</button> <button type="submit" class="select"><%- price_for_100 %>€</button>
</form> </form>
<form class="offer default" action="/checkout"> <form class="offer default" action="/checkout" data-price_for_250="<%- price_for_250 %>">
<h1>-</h1> <h1>-</h1>
<p class="hint">reicht üblicherweise für <span>-</span> Monat</p> <p class="hint">reicht üblicherweise für <span>-</span> Monat</p>
<input type="range" name="amount" id="amount" min="1" max="4" value="1"> <input type="range" name="amount" id="amount" min="1" max="12" value="1">
<div class="spacer"></div> <div class="spacer"></div>
<button type="submit" class="select">- €</button> <button type="submit" class="select">- €</button>
</form> </form>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment