diff --git a/pass/app/Crypto.js b/pass/app/Crypto.js index fdb3389ef19c51a3683b3828ec771d26027c6caf..bb56135fec7cf063b5d3906b12209e1e2faa5ac7 100644 --- a/pass/app/Crypto.js +++ b/pass/app/Crypto.js @@ -71,11 +71,19 @@ class Crypto { /** * 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({ order_id: parseInt(order_id), 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_e: public_key_e, }); @@ -93,14 +101,16 @@ class Crypto { user_hash, order_id, amount, - price, + unit_size, + price_per_unit, public_key_n, public_key_e ) { let data_to_hash = JSON.stringify({ order_id: parseInt(order_id), 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_e: public_key_e, }); diff --git a/pass/app/Order.js b/pass/app/Order.js index 39db87615379033f8c7ba1cc2b64b0d616f3e0d9..8390709f036c77a1e44df1a463681f67b7ab2939 100644 --- a/pass/app/Order.js +++ b/pass/app/Order.js @@ -13,12 +13,21 @@ class Order { return 15; } + static get PRICE_FOR_100() { + return 2; + } + + static get PRICE_FOR_250() { + return 4; + } + /** * Data stored in Redis Database */ #order_id; #amount; - #price; + #unit_size; + #price_per_unit; #encrypted_sales_receipts; #payment_completed; @@ -28,10 +37,17 @@ class Order { #create_mode; #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.#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.#payment_completed = false; @@ -73,7 +89,8 @@ class Order { .hmset(redis_key, { order_id: this.#order_id, 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, payment_completed: this.#payment_completed, }) diff --git a/pass/public/js/index.js b/pass/public/js/index.js index cf14611505d2df9b201ca014dd32b58325af5b9f..6aeab2233a6c61ad63ff0cc4908b2b59899a30ee 100644 --- a/pass/public/js/index.js +++ b/pass/public/js/index.js @@ -1,11 +1,12 @@ -const default_searches = 300; -const default_price = 4; +const default_searches = 250; const default_estimate_months = 1; document.getElementById("amount").addEventListener("input", multiplierChanged); multiplierChanged(); function multiplierChanged() { + let price_for_250 = + document.querySelector("form.offer.default").dataset.price_for_250; let multiplier = document.getElementById("amount").value; let searches_element = document.querySelector( "#offers > .offer.default > h1" @@ -17,6 +18,6 @@ function multiplierChanged() { "#offers > .offer.default > button.select" ); 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; } diff --git a/pass/resources/js/checkout.js b/pass/resources/js/checkout.js index 21c19bafa39e4cbde68e44d12edae73f0324efbe..12266d9c68355fbe8fd25fb9bf3f2f32eca51276 100644 --- a/pass/resources/js/checkout.js +++ b/pass/resources/js/checkout.js @@ -61,7 +61,9 @@ function execute_payment_paypal(e) { body: JSON.stringify({ order_id: document.querySelector("input[name=order_id]").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]") .value, public_key_e: document.querySelector("input[name=public_key_e]") diff --git a/pass/routes/checkout/checkout.js b/pass/routes/checkout/checkout.js index 7498e1f59ead551bec2957858b49c21ce76d735f..6e295ca32854079698f919116839a4ec9f417eb3 100644 --- a/pass/routes/checkout/checkout.js +++ b/pass/routes/checkout/checkout.js @@ -3,8 +3,6 @@ const Crypto = require("../../app/Crypto.js"); const Order = require("../../app/Order.js"); const config = require("config"); -const price_for_100 = 1; - var express = require("express"); var router = express.Router(); const { query, body, validationResult } = require("express-validator"); @@ -13,8 +11,8 @@ const { query, body, validationResult } = require("express-validator"); router.get( "/", query("amount") - .isInt({ min: 1, max: 4 }) - .withMessage("Amount needs to be between 1 and 4."), + .isInt({ min: 0, max: 12 }) + .withMessage("Amount needs to be between 0 and 4."), async function (req, res, next) { const errors = validationResult(req); if (!errors.isEmpty()) { @@ -22,9 +20,10 @@ router.get( } let params = { - amount: req.query.amount, - unit_size: 100, - price: req.query.amount * price_for_100, + amount: req.query.amount === 0 ? 1 : req.query.amount, + unit_size: req.query.amount === 0 ? 100 : 250, + price_per_unit: + req.query.amount === 0 ? Order.PRICE_FOR_100 : Order.PRICE_FOR_250, order_id: await generate_unique_order_id(), payments: { paypal: { @@ -45,7 +44,8 @@ router.get( params.integrity = crypto.createIntegrityHash( params.order_id, params.amount, - params.price, + params.unit_size, + params.price_per_unit, params.crypto.N, params.crypto.E ); @@ -60,7 +60,8 @@ router.use( .isInt({ min: 1, max: 4 }) .withMessage("Invalid amount submitted") .toInt(), - body("price") + body("unit_size").isIn(["100", "250"]).toInt(), + body("price_per_unit") .isCurrency({ symbol: "", allow_negatives: false, thousands_separator: "" }) .withMessage("Invalid Price Value.") .toFloat(), @@ -80,7 +81,8 @@ router.use( value, req.body.order_id, req.body.amount, - req.body.price, + req.body.unit_size, + req.body.price_per_unit, req.body.public_key_n, req.body.public_key_e ) @@ -121,7 +123,8 @@ router.use( let order = new Order( req.body.order_id, req.body.amount, - req.body.price, + req.body.unit_size, + req.body.price_per_unit, req.body.encrypted_sales_receipts ); order @@ -143,6 +146,7 @@ router.use( ); var paypalRouter = require("./paypal.js"); +const { PRICE_FOR_250 } = require("../../app/Order.js"); router.use("/payment/order/paypal", paypalRouter); module.exports = router; diff --git a/pass/routes/checkout/paypal.js b/pass/routes/checkout/paypal.js index 9b1ff56cdf1106d274ca2242de59ae2642c0867d..b2b2451ef10f36837e107329c944bc745c7d2610 100644 --- a/pass/routes/checkout/paypal.js +++ b/pass/routes/checkout/paypal.js @@ -39,6 +39,13 @@ async function createOrder(req) { currency_code: "EUR", value: req.body.price.toFixed(2), }, + items: [ + { + name: "MetaGer Pass: Suchanfragen (100x)", + quantity: 1, + unit_amount: 0, + }, + ], }, ], application_context: { diff --git a/pass/routes/index.js b/pass/routes/index.js index ecca96a56b309a315ddf6399155fd2f953031d3b..bc78fcc140dc44a93637b008fb4ce2178c28ec4c 100644 --- a/pass/routes/index.js +++ b/pass/routes/index.js @@ -1,9 +1,14 @@ -var express = require('express'); +var express = require("express"); +const Order = require("../app/Order.js"); var router = express.Router(); /* GET home page. */ -router.get('/', function(req, res, next) { - res.render('index', { title: 'Express' }); +router.get("/", function (req, res, next) { + res.render("index", { + title: "Express", + price_for_100: Order.PRICE_FOR_100, + price_for_250: Order.PRICE_FOR_250, + }); }); module.exports = router; diff --git a/pass/views/checkout/checkout.ejs b/pass/views/checkout/checkout.ejs index b7c6bdc16c81cc72dccc315c91894e3220a4f137..24e52f41f14c13df1c132d4e3775b60bcff780b1 100644 --- a/pass/views/checkout/checkout.ejs +++ b/pass/views/checkout/checkout.ejs @@ -3,12 +3,13 @@ <main> <input type="hidden" name="order_id" value="<%- order_id %>"> <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_n" value="<%- crypto.N %>"> <input type="hidden" name="integrity" value="<%- integrity %>"> <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 class="section-heading"> <div class="status"> diff --git a/pass/views/index.ejs b/pass/views/index.ejs index e3640ad24e86bfe686d2506fafbe920d3af4812b..fcd1438d4af3de1693a58d1054607211b309abfd 100644 --- a/pass/views/index.ejs +++ b/pass/views/index.ejs @@ -4,15 +4,15 @@ </p> <div id="offers"> <form class="offer" action="/checkout"> - <input type="hidden" name="amount" value="1"> + <input type="hidden" name="amount" value="0"> <h1>100</h1> <div class="spacer"></div> - <button type="submit" class="select">1€</button> + <button type="submit" class="select"><%- price_for_100 %>€</button> </form> - <form class="offer default" action="/checkout"> + <form class="offer default" action="/checkout" data-price_for_250="<%- price_for_250 %>"> <h1>-</h1> <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> <button type="submit" class="select">- €</button> </form>