bokoness

Untitled

Apr 26th, 2021
725
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const router = require("express").Router();
  2. const axios = require("axios");
  3. const os = require("os");
  4. const keys = require("../../secret.js");
  5. const ICREDIT_GROUP_PRIVATE_TOKEN =
  6.     os.hostname() === "bokoness"
  7.         ? keys.ICREDIT_GROUP_PRIVATE_TOKEN_TEST
  8.         : keys.ICREDIT_GROUP_PRIVATE_TOKEN;
  9. const Bagel = require("@bageldb/bagel-db");
  10. let db = new Bagel(keys.ADMIN_TOKEN);
  11.  
  12. //5326000000000006
  13. // 4580000000000000
  14. // 123456790
  15. // 0534280844
  16.  
  17. const details = {
  18.     room: {
  19.         collectionName: "reservation",
  20.         description: "Room Subscription",
  21.         price: 80
  22.     },
  23.     conference: {
  24.         collectionName: "functionHall",
  25.         description: "Conference Room Subscription",
  26.         price: 250,
  27.         vip: 225
  28.     }
  29. };
  30.  
  31. const getPaymentUrl = method => {
  32.     const baseUrl =
  33.         os.hostname() === "bokoness"
  34.             ? "https://testicredit.rivhit.co.il/API/PaymentPageRequest.svc/"
  35.             : "https://icredit.rivhit.co.il/API/PaymentPageRequest.svc/";
  36.     switch (method) {
  37.         case "getUrl":
  38.             return baseUrl + "GetUrl";
  39.         case "verify":
  40.             return baseUrl + "Verify";
  41.         case "saleDetails":
  42.             return baseUrl + "SaleDetails";
  43.         default:
  44.             return null;
  45.     }
  46. };
  47.  
  48. router.post("/iCredit", async (req, res) => {
  49.     try {
  50.         let data = {
  51.             GroupPrivateToken: ICREDIT_GROUP_PRIVATE_TOKEN,
  52.             Items: [
  53.                 {
  54.                     Quantity: req.body.event.hours || 1,
  55.                     UnitPrice: details[req.body.item].price,
  56.                     Description: details[req.body.item].description
  57.                 }
  58.             ],
  59.             RedirectURL:"https://wecaremodiin.com/payment/redirect",
  60.             ExemptVAT: true,
  61.             MaxPayments: 1,
  62.             CustomerFirstName: req.body.user ? req.body.user.firstName : "N/A",
  63.             CustomerLastName: req.body.user ? req.body.user.lastName : "N/A"
  64.         };
  65.  
  66.         response = await axios.post(getPaymentUrl("getUrl"), data);
  67.         console.log(response);
  68.         try {
  69.             const icreditKey = await db.collection("icreditKeys").post({
  70.                 private: response.data.PrivateSaleToken,
  71.                 public: response.data.PublicSaleToken,
  72.                 uid: req.body.uid,
  73.                 item: req.body.item
  74.             });
  75.             await db
  76.                 .collection("icreditKeys")
  77.                 .item(icreditKey.data.id)
  78.                 .collection(
  79.                     req.body.item === "room"
  80.                         ? "eventData"
  81.                         : "conferenceRoomData"
  82.                 )
  83.                 .post(req.body.event);
  84.             res.send({ url: response.data.URL, recordId: icreditKey.data.id });
  85.         } catch (error) {
  86.             console.log("problem with bageldb");
  87.             res.status(503).send(error);
  88.         }
  89.     } catch (e) {
  90.         console.log("problem with icredit", e);
  91.         return res.status(500).send(e);
  92.     }
  93. });
  94.  
  95. router.get("/redirect", async (req, res) => {
  96.     try {
  97.         let token = req.query.Token;
  98.         //verify deal
  99.         const response = await axios.post(getPaymentUrl("verify"), {
  100.             GroupPrivateToken: ICREDIT_GROUP_PRIVATE_TOKEN,
  101.             SaleId: token,
  102.             TotalAmount: 55.9
  103.         });
  104.         const status = response.data.Status;
  105.         //TODO: check if payment status is ok
  106.  
  107.         const record = await db
  108.             .collection("icreditKeys")
  109.             .query("public", "=", token)
  110.             .everything()
  111.             .get();
  112.  
  113.         let event =
  114.             record.data[0].item === "room"
  115.                 ? record.data[0].eventData[0]
  116.                 : record.data[0].conferenceRoomData[0];
  117.  
  118.         event.isPayed = true;
  119.         await db
  120.             .collection(details[record.data[0].item].collectionName)
  121.             .post(event);
  122.  
  123.         const saleDetails = await axios.post(getPaymentUrl("saleDetails"), {
  124.             SalePrivateToken: record.data[0].private
  125.         });
  126.  
  127.         await db.collection("transactions").post({
  128.             cardName: saleDetails.data.CardName,
  129.             cardNum: saleDetails.data.CardNum,
  130.             fullName: `${saleDetails.data.CustomerFirstName} ${saleDetails.data.CustomerLastName}`,
  131.             transactionId: saleDetails.data.CustomerTransactionId,
  132.             item: saleDetails.data.Items[0].Description,
  133.             quantity: saleDetails.data.Items[0].Quantity,
  134.             price: saleDetails.data.Items[0].UnitPrice,
  135.             totalPrice:
  136.                 saleDetails.data.Items[0].Quantity *
  137.                 saleDetails.data.Items[0].UnitPrice,
  138.             status,
  139.             uid: record.data[0].uid
  140.         });
  141.  
  142.         await db
  143.             .collection("icreditKeys")
  144.             .item(record.data[0]._id)
  145.             .delete();
  146.  
  147.         //TODO: change that the production url
  148.         res.redirect(
  149.             os.hostname() === "bokoness"
  150.                 ? "http://localhost:3000/schedule"
  151.                 : "https://wecaremodiin.com/schedule"
  152.         );
  153.     } catch (e) {
  154.         console.log(e);
  155.         res.status(500).send("Something went wrong");
  156.     }
  157. });
  158.  
  159. router.post("/package", async (req, res) => {
  160.     let { packageId, event, uid } = req.body;
  161.  
  162.     try {
  163.         const userPackage = await db
  164.             .collection("packagesSubscriptions")
  165.             .item(packageId)
  166.             .get();
  167.         if (userPackage.data.uses < Number(event.hours)) {
  168.             return res.status(403).send("Not enough hours in package");
  169.         }
  170.         await db
  171.             .collection("packagesSubscriptions")
  172.             .item(userPackage.data._id)
  173.             .put({
  174.                 uses: userPackage.data.uses - event.hours
  175.             });
  176.         //create event and update isPayed to true
  177.         const data = { ...event };
  178.         data.uid = [{ itemRefID: uid }];
  179.         data.isPayed = true;
  180.         await db.collection("reservation").post(data);
  181.         return res.sendStatus(200);
  182.     } catch (error) {
  183.         console.log(error);
  184.         return res.sendStatus(500);
  185.     }
  186. });
  187.  
  188. module.exports = router;
  189.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×