Plugin template for CPG.
Modules can be used for products, perhaps you are selling a game server, and when they purchase the product you want to create a server. Modules make this possible and easier.
You'll need to listen on invoice_paid so you can fetch products ids and check if they have the respective module_name.
If a product has your module_name you can proceed to check if they have the matching attributes in modules to generate whatever you need in that process.
require("dotenv").config();constBuildDir=process.cwd()+"/build";// @ts-ignoreimporttype{ILoggingTypes}from"@cpg/Interfaces/Logging.interface";// @ts-ignoreimporttypemainEventfrom"@cpg/Events/Main.event";// @ts-ignoreimporttype{server}from"@cpg/Server/Server";// @ts-ignoreimporttypecsfrom"@cpg/Database/Models/Customers/Customer.model";// @ts-ignoreimporttypepmfrom"@cpg/Database/Models/Products.model";// @ts-ignoreimporttype{sendEmailassE}from"@cpg/Email/Send"importconfigfrom"../config.json";export=asyncfunctionmain(){constLogger=(awaitimport(`${BuildDir}/Lib/Logger`)).defaultasILoggingTypes;constMainEvent=(awaitimport(`${BuildDir}/Events/Main.event`)).defaultastypeofmainEvent;constServer=(awaitimport(`${BuildDir}/Server/Server`)).serverastypeofserver;constsendEmail=(awaitimport(`${BuildDir}/Email/Send`)).sendEmailastypeofsE;constCustomerModel=(awaitimport(`${BuildDir}/Database/Models/Customers/Customer.model`)).defaultastypeofcs;constProductModel=(awaitimport(`${BuildDir}/Database/Models/Products.model`)).defaultastypeofpm;Logger.info(`Starting ${config.name} plugin with version ${config.version}.`);constmodule_name="cpg-plugin-emails";constmodule_attr={header_text: "Header text file for product",}Server.get(`/modules/${module_name}`,(req,res)=>{res.send(module_attr);});MainEvent.on(`invoice_paid`,async(invoice)=>{constcustomer=awaitCustomerModel.findOne({$or: [{uid: invoice.customer_uid},{id: invoice.customer_uid}]});if(!customer)return;// find all products in the invoice.items..product_idconstproducts=awaitProductModel.find({id: {$in: invoice.items.map(i=>i.product_id)}});Logger.debug(`Found ${products.length} products in invoice.`);// Check if any of the products has module.// Filter themconstmProducts=products.filter(p=>p.module_name.includes(module_name));Logger.debug(`Found ${mProducts.length} products with module.`);if(mProducts.length<=0)return;mProducts.forEach(p=>{// Get header_text from p.modulesconstheader_text=p.modules.find(m=>m.name==="header_text");if(!header_text)return;Logger.debug(`Found header_text: ${header_text.value}`);sendEmail({receiver: customer?.personal.email,subject: `Product specific email`,body: {body: `<h1>${header_text.value}</h1>`,}})});});}If you want to create your own payment method for CPG there is a way.
This might be useful if your plugin has a third party payment gateway that isn't added to CPG officialy yet.
First of come up with a uniqe payment_method name. This name will be used when proceeding on orders/place.
Now you need to create some methods to make this work, and import correct files to ensure it goes through without issues. You might also need to create custom routes, but we won't go through on this.
require("dotenv").config();constBuildDir=process.cwd()+"/build";// @ts-ignoreimporttype{ce_ordersasce_orders_type}from"@cpg/Lib/Orders/PlaceOrder";// @ts-ignoreimporttype{A_CC_PaymentsasA_CC_Payments_type}from"@cpg/Types/PaymentMethod";export=asyncfunctionmain(){constce_orders=(awaitimport(`${BuildDir}/Lib/Orders/PlaceOrder`)).ce_ordersastypeofce_orders_type;constA_CC_Payments=(awaitimport(`${BuildDir}/Types/PaymentMethod`)).A_CC_PaymentsastypeofA_CC_Payments_type;constpayment_method_name="steam"asconst;A_CC_Payments.push(payment_method_name);// @ts-ignorece_orders.set(payment_method_name,(order,invoice,req,res,next)=>{// Handle order/invoice});}Once a name has been decided, proceed to add it in payment_methods (v3/configs/payment_methods) in the API. This will ensure is accepted on checking and won't fail.
Want to contribute? Great! You can contribute by forking this repository, then make changes and make a PR!
Or simple ask on our discord server.
Tolfix is a company focusing about IT, Development and Networking, we drive to help others with their problems when it comes to IT and love contributing to others.
Want to find more information about us you can visit us at https://tolfix.com/.

