Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #87 from Tolfix/dev
Browse files Browse the repository at this point in the history
v2.5
  • Loading branch information
Tolfx authored Mar 7, 2022
2 parents 3ba22b5 + 16e168e commit 5253238
Show file tree
Hide file tree
Showing 30 changed files with 337 additions and 57 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cpg-api",
"version": "v2.4",
"version": "v2.6",
"description": "Central Payment Gateway",
"main": "./build/Main.js",
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Models/Customers/Customer.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Default_Language, MongoDB_URI } from "../../../Config";
import { ICustomer, ICustomerMethods } from "../../../Interfaces/Customer.interface";
import Logger from "../../../Lib/Logger";
import GetText from "../../../Translation/GetText";
import { A_PaymentCurrency } from "../../../Types/PaymentTypes";
import { currencyCodes } from "../../../Types/PaymentTypes";

const CustomerSchema = new Schema
(
Expand Down Expand Up @@ -101,7 +101,7 @@ const CustomerSchema = new Schema

currency: {
type: String,
enum: A_PaymentCurrency,
enum: currencyCodes,
required: true,
},

Expand Down
7 changes: 7 additions & 0 deletions src/Database/Models/Invoices.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Default_Language, MongoDB_URI } from "../../Config";
import { A_InvoiceStatus, IInvoice } from "../../Interfaces/Invoice.interface";
import Logger from "../../Lib/Logger";
import GetText from "../../Translation/GetText";
import { currencyCodes } from "../../Types/PaymentTypes";

const InvoiceSchema = new Schema
(
Expand Down Expand Up @@ -92,6 +93,12 @@ const InvoiceSchema = new Schema
default: false,
},

currency: {
type: String,
enum: currencyCodes,
default: 'USD',
},

}
);

Expand Down
8 changes: 7 additions & 1 deletion src/Database/Models/Orders.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { A_OrderStatus, IOrder } from "../../Interfaces/Orders.interface";
import Logger from "../../Lib/Logger";
import GetText from "../../Translation/GetText";
import { A_CC_Payments, A_RecurringMethod } from "../../Types/PaymentMethod";
import { A_PaymentTypes } from "../../Types/PaymentTypes";
import { A_PaymentTypes, currencyCodes } from "../../Types/PaymentTypes";

const OrderSchema = new Schema
(
Expand Down Expand Up @@ -95,6 +95,12 @@ const OrderSchema = new Schema
require: false
},

currency: {
type: String,
enum: currencyCodes,
default: 'USD',
},

}
);

Expand Down
7 changes: 7 additions & 0 deletions src/Database/Models/Transactions.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IDTransactions } from "../../Interfaces/Transactions.interface";
import Logger from "../../Lib/Logger";
import GetText from "../../Translation/GetText";
import { A_CC_Payments } from "../../Types/PaymentMethod";
import { currencyCodes } from "../../Types/PaymentTypes";

const TransactionsSchema = new Schema
(
Expand Down Expand Up @@ -47,6 +48,12 @@ const TransactionsSchema = new Schema
default: 0,
},

currency: {
type: String,
enum: currencyCodes,
default: 'USD',
},

}
);

Expand Down
7 changes: 4 additions & 3 deletions src/Email/Templates/Invoices/Invoice.template.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { stripIndents } from "common-tags";
import { Company_Currency, Full_Domain } from "../../../Config";
import { Full_Domain } from "../../../Config";
import { ICustomer } from "../../../Interfaces/Customer.interface";
import { IInvoice } from "../../../Interfaces/Invoice.interface";
import getFullName from "../../../Lib/Customers/getFullName";
import { GetCurrencySymbol } from "../../../Types/PaymentTypes";
import GetTableStyle from "../CSS/GetTableStyle";
import UseStyles from "../General/UseStyles";

Expand Down Expand Up @@ -76,7 +77,7 @@ export default async (invoice: IInvoice, customer: ICustomer) => await UseStyles
<tr>
<td>${item.notes}</td>
<td>${item.quantity}</td>
<td>${item.amount} ${!customer.currency ? await Company_Currency() : customer.currency}</td>
<td>${item.amount} ${GetCurrencySymbol(invoice.currency)}</td>
</tr>
`))).join('')}
</tbody>
Expand All @@ -85,7 +86,7 @@ export default async (invoice: IInvoice, customer: ICustomer) => await UseStyles
<strong>
Total:
</strong>
${invoice.amount+invoice.amount*invoice.tax_rate/100} ${!customer.currency ? await Company_Currency() : customer.currency} (${invoice.tax_rate}%)
${invoice.amount+invoice.amount*invoice.tax_rate/100} ${GetCurrencySymbol(invoice.currency)} (${invoice.tax_rate}%)
</p>
<p>
<strong>
Expand Down
7 changes: 4 additions & 3 deletions src/Email/Templates/Invoices/LateInvoice.Template.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { stripIndents } from "common-tags";
import { Company_Currency, Full_Domain } from "../../../Config";
import { Full_Domain } from "../../../Config";
import { ICustomer } from "../../../Interfaces/Customer.interface";
import { IInvoice } from "../../../Interfaces/Invoice.interface";
import getFullName from "../../../Lib/Customers/getFullName";
import { GetCurrencySymbol } from "../../../Types/PaymentTypes";
import GetTableStyle from "../CSS/GetTableStyle";
import UseStyles from "../General/UseStyles";

Expand Down Expand Up @@ -64,7 +65,7 @@ export default async (invoice: IInvoice, customer: ICustomer) => await UseStyles
<tr>
<td>${item.notes}</td>
<td>${item.quantity}</td>
<td>${item.amount} ${!customer.currency ? await Company_Currency() : customer.currency}</td>
<td>${item.amount} ${GetCurrencySymbol(invoice.currency)}</td>
</tr>
`))).join('')}
</tbody>
Expand All @@ -73,7 +74,7 @@ export default async (invoice: IInvoice, customer: ICustomer) => await UseStyles
<strong>
Total:
</strong>
${invoice.amount+invoice.amount*invoice.tax_rate/100} ${!customer.currency ? await Company_Currency() : customer.currency} (${invoice.tax_rate}%)
${invoice.amount+invoice.amount*invoice.tax_rate/100} ${GetCurrencySymbol(invoice.currency)} (${invoice.tax_rate}%)
</p>
<p>
<strong>
Expand Down
8 changes: 4 additions & 4 deletions src/Email/Templates/Orders/NewOrderCreated.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { stripIndents } from "common-tags";
import { Company_Currency } from "../../../Config";
import ConfigurableOptionsModel from "../../../Database/Models/ConfigurableOptions.model";
import { ICustomer } from "../../../Interfaces/Customer.interface";
import { IOrder } from "../../../Interfaces/Orders.interface";
import getFullName from "../../../Lib/Customers/getFullName";
import getProductById from "../../../Lib/Products/getProductById";
import { GetCurrencySymbol } from "../../../Types/PaymentTypes";
import GetTableStyle from "../CSS/GetTableStyle";
import UseStyles from "../General/UseStyles";

Expand Down Expand Up @@ -48,7 +48,7 @@ export default async (order: IOrder, customer: ICustomer) => await UseStyles(str
<tr>
<td>${p?.name}</td>
<td>${product.quantity}</td>
<td>${p?.price} ${!customer.currency ? await Company_Currency() : customer.currency}</td>
<td>${p?.price} ${GetCurrencySymbol(order.currency)}</td>
</tr>`;
if(p_c.length > 0)
Expand All @@ -59,7 +59,7 @@ export default async (order: IOrder, customer: ICustomer) => await UseStyles(str
<tr>
<td>+ ${p?.name} - ${c?.name}</td>
<td>1</td>
<td>${c?.price} ${!customer.currency ? await Company_Currency() : customer.currency}</td>
<td>${c?.price} ${GetCurrencySymbol(order.currency)}</td>
</tr>`
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ export default async (order: IOrder, customer: ICustomer) => await UseStyles(str
total += p_c.reduce((a, b) => a + b);
return total;
}))).reduce((acc, cur) => acc + cur, 0)} ${(!customer.currency ? await Company_Currency() : customer.currency).toLocaleUpperCase()}
}))).reduce((acc, cur) => acc + cur, 0)} ${(order.currency).toLocaleUpperCase()}
</p>
</div>
`);
3 changes: 2 additions & 1 deletion src/Email/Templates/Transaction/NewTransaction.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ICustomer } from "../../../Interfaces/Customer.interface";
import { ITransactions } from "../../../Interfaces/Transactions.interface";
import PrintCompanyInformation from "../../../Lib/Company/PrintCompanyInformation";
import getFullName from "../../../Lib/Customers/getFullName";
import { GetCurrencySymbol, TPaymentCurrency } from "../../../Types/PaymentTypes";
import UseStyles from "../General/UseStyles";

export = async (t: ITransactions, c: ICustomer, charged = false) => UseStyles(stripIndents`
Expand All @@ -25,7 +26,7 @@ export = async (t: ITransactions, c: ICustomer, charged = false) => UseStyles(st
${await PrintCompanyInformation()}
</p>
<p>
Amount: ${t.amount} ${!c.currency ? await Company_Currency() : c.currency}
Amount: ${t.amount} ${GetCurrencySymbol(t.currency)}
</p>
</div>
`);
3 changes: 2 additions & 1 deletion src/Interfaces/Customer.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TPaymentCurrency } from "../Types/PaymentTypes";
import { ICompanyConfig } from "./Admin/Configs.interface";
import { IImage } from "./Images.interface";

Expand Down Expand Up @@ -27,7 +28,7 @@ export interface ICustomer
password: string;
createdAt: Date;
profile_picture: IImage["id"] | null;
currency: string | ICompanyConfig["currency"];
currency: TPaymentCurrency | ICompanyConfig["currency"];
extra: {
[key: string]: any;
};
Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/Invoice.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TPaymentCurrency } from "../Types/PaymentTypes";
import { IConfigurableOptions } from "./ConfigurableOptions.interface";
import { ICustomer } from "./Customer.interface";
import { TOrderStatus } from "./Orders.interface";
Expand Down Expand Up @@ -56,6 +57,7 @@ export interface IInvoice
tax_rate: number;
notes: string;
paid: boolean;
currency: TPaymentCurrency;
notified: boolean;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Interfaces/Orders.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TRecurringMethod } from "../Types/PaymentMethod";
import { TPaymentTypes } from "../Types/PaymentTypes";
import { TPaymentCurrency, TPaymentTypes } from "../Types/PaymentTypes";
import { IConfigurableOptions } from "./ConfigurableOptions.interface";
import { ICustomer } from "./Customer.interface";
import { IInvoice } from "./Invoice.interface";
Expand Down Expand Up @@ -45,6 +45,7 @@ export interface IOrder
price_override?: number;
dates: IOrderDates<IOrder["billing_type"]>;
invoices: Array<IInvoice["uid"]>;
currency: TPaymentCurrency;
promotion_code?: IPromotionsCodes["id"];
}

Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/Transactions.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Document } from "mongoose";
import { TPaymentCurrency } from "../Types/PaymentTypes";
import { ICustomer } from "./Customer.interface";
import { IInvoice } from "./Invoice.interface";

Expand All @@ -18,6 +19,7 @@ export interface ITransactions
date: Date | string;
payment_method: IInvoice["payment_method"];
amount: IInvoice["amount"];
currency: TPaymentCurrency;
fees: number;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lib/Orders/newInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Logger from "../Logger";
import PromotionCodeModel from "../../Database/Models/PromotionsCode.model";
import { sanitizeMongoose } from "../Sanitize";


// Create a method that checks if the order next recycle is within 14 days
export function isWithinNext14Days(date: Date | string): boolean
{
Expand Down Expand Up @@ -103,6 +102,7 @@ export async function createInvoiceFromOrder(order: IOrder)
tax_rate: Products?.reduce((acc, cur) => cur.tax_rate, 0),
notes: "",
paid: false,
currency: order.currency,
notified: false,
})).save();

Expand Down
2 changes: 1 addition & 1 deletion src/Lib/Quotes/CreateQuotePdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
},
"taxNotation": "vat",
"settings": {
"currency": (await Company_Currency()).toUpperCase(),
"currency": (!Customer.currency ? await Company_Currency() : Customer.currency).toUpperCase(),
"margin-top": 25,
"margin-right": 25,
"margin-left": 25,
Expand Down
12 changes: 6 additions & 6 deletions src/Middlewares/EnsureAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ export default function EnsureAdmin(eR = false)
password = login.split(":")[1];
login = login.split(":")[0];
}
eR ? null : Logger.warning(`Authoring admin with username: ${login}`);

!eR ? Logger.warning(`Authoring admin with username: ${login}`) : null;

const match = bcrypt.compare(password, (CacheAdmin.get(getAdminByUsername(login) ?? "ADM_")?.["password"]) ?? "")
if(!match)
{
eR ? null : Logger.warning(`Authorization failed for admin with username: ${login}`);
!eR ? Logger.warning(`Authorization failed for admin with username: ${login}`) : null;
return eR ? Promise.resolve(false) : APIError("Unauthorized admin", 403)(res);
}

Expand All @@ -53,19 +53,19 @@ export default function EnsureAdmin(eR = false)
if(b64auth[0].toLocaleLowerCase() === "bearer")
{
const token = (Buffer.isBuffer(b64auth[1]) ? Buffer.from(b64auth[1], 'base64') : b64auth[1]).toString();
eR ? null : Logger.warning(`Authoring admin with token: ${token}`);
!eR ? Logger.warning(`Authoring admin with token: ${token}`) : null;

try
{
const payload = jwt.verify(token, JWT_Access_Token);

if(!payload)
{
eR ? null : Logger.warning(`Authorization failed for admin with token: ${token}`);
!eR ? Logger.warning(`Authorization failed for admin with token: ${token}`) : null;
return eR ? Promise.resolve(false) : APIError("Unauthorized admin", 403)(res);
}

eR ? null : Logger.warning(`Authorized admin with token: ${token}`);
eR ? Logger.warning(`Authorized admin with token: ${token}`) : null;

return eR ? Promise.resolve(true) : next?.();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Middlewares/EnsureAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function EnsureAuth(eR = false)
//@ts-ignore
req.customer = payload.data;
// @ts-ignore
eR ? null : Logger.api(`Authorizing`, payload.data);
!eR ? Logger.api(`Authorizing`, payload.data) : null;

return eR ? Promise.resolve(true) : next?.();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Payments/Currencies/Paypal.currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const data = {

export const PaypalCurrencies = data.currencies;

export function validCurrencyPaypal(currency: typeof data["currencies"])
export function validCurrencyPaypal(currency: typeof PaypalCurrencies)
{
// @ts-ignore
return data.currencies.includes(currency);
Expand Down
3 changes: 2 additions & 1 deletion src/Payments/Paypal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import paypal from "paypal-rest-sdk";
import { Company_Currency, DebugMode, Domain, Full_Domain, Http_Schema, Paypal_Client_Id, Paypal_Client_Secret, PORT } from "../Config";
import { Company_Currency, DebugMode, Full_Domain, Paypal_Client_Id, Paypal_Client_Secret } from "../Config";
import CustomerModel from "../Database/Models/Customers/Customer.model";
import TransactionsModel from "../Database/Models/Transactions.model";
import { IInvoice } from "../Interfaces/Invoice.interface";
Expand Down Expand Up @@ -136,6 +136,7 @@ export async function retrievePaypalTransaction(payerId: string, paymentId: stri
fees: 0,
invoice_uid: invoice.id,
customer_uid: invoice.customer_uid,
currency: invoice.currency ?? await Company_Currency(),
date: getDate(),
uid: idTransicitons(),
}).save());
Expand Down
2 changes: 2 additions & 0 deletions src/Payments/Stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export const ChargeCustomer = async (invoice_id: IInvoice["id"]) =>
fees: 0,
invoice_uid: invoice.id,
customer_uid: invoice.customer_uid,
currency: invoice.currency ?? await Company_Currency(),
date: getDate(),
uid: idTransicitons(),
}).save());
Expand Down Expand Up @@ -219,6 +220,7 @@ export const markInvoicePaid = async (intent: stripe.Response<stripe.PaymentInte
fees: 0,
invoice_uid: invoice.id,
customer_uid: invoice.customer_uid,
currency: invoice.currency ?? await Company_Currency(),
date: getDate(),
uid: idTransicitons(),
}).save());
Expand Down
6 changes: 2 additions & 4 deletions src/Plugins/PluginHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Logger from "../Lib/Logger";
import { Plugins } from "../Config";
import {Plugins} from "../Config";
import npm from "npm";
import fs from "fs";
import GetText from "../Translation/GetText";
Expand Down Expand Up @@ -68,8 +68,6 @@ export function isPluginInstalled(plugin: string)
export function getPlugins()
{
// get all installed npm packages
const packages = Plugins;
// get plugins starting with cpg-plugin
const plugins = packages.filter(p => p.startsWith("cpg-plugin"));
return plugins;
return Plugins.filter(p => p.startsWith("cpg-plugin"));
}
Loading

0 comments on commit 5253238

Please sign in to comment.