Example #1
0
    public static validate(req: express.Request) {
        var check = require('validator').check,
            sanitize = require('validator').sanitize;

        check(req.body.pdf).notNull();
        check(req.body.recipientName).notNull();
        check(req.body.recipientAddress1).notNull();
        check(req.body.recipientCity).notNull();
        check(req.body.recipientPostalCode).notNull();
        var iso = req.body.recipientCountryIso;
        var name = req.body.recipientName;
        var company = req.body.recipientCompany;
        var address1 = req.body.recipientAddress1;
        var address2 = req.body.recipientAddress2;
        var city = req.body.recipientCity;
        var state = req.body.recipientState;
        var zip = req.body.recipientPostalCode;

        if (name.length > 50) throw  new Error("The FaxRecipient Name may not be longer than 50 characters");
        if (company != null && typeof company !== 'undefined' && company.length > 50) throw  new Error("The FaxRecipient Company may not be longer than 50 characters");
        if (address1.length > 50) throw  new Error("The FaxRecipient Address Line 1 may not be longer than 50 characters");
        if (address2 != null && typeof address2 !== 'undefined' && address2.length > 50) throw  new Error("The FaxRecipient Address Line 2 may not be longer than 50 characters");
        if (city.length > 50) throw  new Error("The FaxRecipient City may not be longer than 50 characters");
        if (state != null && typeof state !== 'undefined' && state.length > 50) throw  new Error("The FaxRecipient State may not be longer than 50 characters");
        if (zip.length > 50) throw  new Error("The FaxRecipient Zip may not be longer than 50 characters");

        // Sanitize
        req.body.recipientName = sanitize(req.body.recipientName).trim();
        req.body.recipientName = sanitize(req.body.recipientName).escape();
        req.body.recipientName = req.body.recipientName.replace("'", '').replace('"', '');

        req.body.recipientCompany = (typeof req.body.recipientCompany === 'undefined' || req.body.recipientCompany == null) ? undefined : sanitize(req.body.recipientCompany).trim();
        req.body.recipientCompany = (typeof req.body.recipientCompany === 'undefined' || req.body.recipientCompany == null) ? undefined : sanitize(req.body.recipientCompany).escape();
        req.body.recipientCompany = (typeof req.body.recipientCompany === 'undefined' || req.body.recipientCompany == null) ? undefined : req.body.recipientCompany.replace("'", '').replace('"', '');

        req.body.recipientAddress1 = sanitize(req.body.recipientAddress1).trim();
        req.body.recipientAddress1 = sanitize(req.body.recipientAddress1).escape();
        req.body.recipientAddress1 = req.body.recipientAddress1.replace("'", '').replace('"', '');

        req.body.recipientAddress2 = (typeof req.body.recipientAddress2 === 'undefined' || req.body.recipientAddress2 == null) ? undefined : sanitize(req.body.recipientAddress2).trim();
        req.body.recipientAddress2 = (typeof req.body.recipientAddress2 === 'undefined' || req.body.recipientAddress2 == null) ? undefined : sanitize(req.body.recipientAddress2).escape();
        req.body.recipientAddress2 = (typeof req.body.recipientAddress2 === 'undefined' || req.body.recipientAddress2 == null) ? undefined : req.body.recipientAddress2.replace("'", '').replace('"', '');

        req.body.recipientPostalCode = sanitize(req.body.recipientPostalCode).trim();
        req.body.recipientPostalCode = sanitize(req.body.recipientPostalCode).escape();
        req.body.recipientPostalCode = req.body.recipientPostalCode.replace("'", '').replace('"', '');

        req.body.recipientCity = sanitize(req.body.recipientCity).trim();
        req.body.recipientCity = sanitize(req.body.recipientCity).escape();
        req.body.recipientCity = req.body.recipientCity.replace("'", '').replace('"', '');

        req.body.recipientState = (typeof req.body.recipientState === 'undefined' || req.body.recipientState == null) ? undefined : req.body.recipientState;

        req.body.recipientCountryIso = (typeof req.body.recipientCountryIso === 'undefined') ? undefined : sanitize(req.body.recipientCountryIso).escape();
    }
Example #2
0
                MongoManager.getNextSequence("invoicenumber", function (invoiceNumber) {
                    var sanitize = require('validator').sanitize;
                    letter.invoiceNumber = invoiceNumber;
                    letter.issuer.name = sanitize(req.body.address.name).escape();
                    letter.issuer.address1 = sanitize(req.body.address.line1).escape();
                    letter.issuer.address2 = (typeof req.body.address.line2 === 'undefined') ? undefined : sanitize(req.body.address.line2).escape();
                    letter.issuer.postalCode = sanitize(req.body.address.postalCode).escape();
                    letter.issuer.city = sanitize(req.body.address.city).escape();
                    letter.issuer.country = sanitize(req.body.address.country).escape();
                    letter.issuer.email = sanitize(req.body.emailAddress).trim();

                    TaxationHelper.processTaxation(letter); // Set Tax appropriately

                    // Important: Critical Path Begins
                    // The user may not see an error in case of a successful payment
                    var braintreeClient = new BraintreeClient(!Config.isProd());
                    braintreeClient.pay(letter.financialInformation.priceInSettlementCurrency, letter.financialInformation.settlementCurrency, creditCard, function (error: Error, result: any) {
                        if (error) {
                            res.json(502, {error: error.message});
                            return;
                        }
                        letter.payed = true;
                        letter.transactionInformation.sandboxTransaction = braintreeClient.isSandbox();
                        letter.transactionInformation.transactionDate = new Date();
                        letter.transactionInformation.transactionId = result.transaction.id;
                        letter.updatedAt = new Date();
                        collection.update({'_id': letter._id}, letter, {safe: true}, function (err:Error, result:number) {
                            res.send(letter);
                            /*
                            // Try to Dispatch the letter
                            MailManager.transferLetterToPrintProvider(letter, function (error:Error) {
                                status.pdfProcessed = true;
                                conclude(status, letter, res);
                            });

                            // Try to send the bill
                            BillingManager.generateAndSendBillForLetter(letter, function (err:Error) {
                                status.billProcessed = true;
                                conclude(status, letter, res);
                            });
                            */
                        });
                    });
                });