1
\$\begingroup\$

This was an assignment and I am new to Node.js. You can find the complete project here. The code worked fine but was rejected, and wasn't given any reason even after asked.

Statement:

  1. Create a payment gateway library, that can handle payments with paypal and braintree
  2. create simple form for making payment, Form contains

    2.1 order section - price, currency, customer fullname
    2.2 payment section - card holdername, card number, expiration, ccv

    conditions:

    • After submitting form, had to use different gateway based on these rules,
    • if credit card type is AMEX, then use Paypal.
    • if currency is USD, EUR, or AUD, then use Paypal. Otherwise use Braintree.
    • if currency is not USD and credit card is AMEX, return error message, that AMEX is possible to use only for USD
  3. Save orderdata + response from payment gateway to database table
  4. Implement "single payment" with credit card.

I am concerned about different things:

1. After form submission, I did this:

$(".submit").click(function(){
    var holderName, cardNumber, cvv, price, expiryMonth, expiryYear, fullName, cardType, currency, error=false;
    var visa = /^4[0-9]{12}(?:[0-9]{3})?$/;
    var mastercard = /^5[1-5][0-9]{14}$/;
    var amex = /^3[47][0-9]{13}$/;
    var discover = /^6(?:011|5[0-9]{2})[0-9]{12}$/;
    var cvv = /^[0-9]{3,4}/;

    $("#myAlert").alert('close');

    fullName = $(".fullName").val();
    holderName = $(".cardHolderName").val();
    cardNumber = $(".cardNumber").val();
    cvv = parseFloat($(".cvv").val());
    price = parseInt($(".price").val());
    expiryMonth = parseInt($(".expiryMonth").val());
    expiryYear = parseInt($(".expiryYear").val());
    currency = $(".currencyDropDown").text();

    if(cardNumber.match(amex)){
        cardType = "amex";
    } else if(cardNumber.match(visa)){
        cardType = "visa";
    } else if(cardNumber.match(mastercard)){
        cardType = "mastercard";
    } else if(cardNumber.match(discover)){
        cardType = "discover";
    } else {
        cardType = "invalid";
    }

    if(cardType === "invalid"){
        error = true;
        $(".cardNumber").addClass("invalid");
        $(".cardNumber").popover({
            container: 'body',
            trigger: 'focus',
            content : "please check the Card Number"
        });
        $(".cardNumber").popover('show');
    }

    if(expiryYear <= 14 || isNaN(expiryYear)){
        error = true;
        $(".expiryYear").addClass("invalid");
        $(".expiryYear").popover({
            container: 'body',
            trigger: 'focus',
            content : "please check the Year"
        });
        $(".expiryYear").popover('show');
    }

    if(expiryMonth === 0 || expiryMonth >= 13 || isNaN(expiryMonth)){
        error = true;
        $(".expiryMonth").addClass("invalid");
        $(".expiryMonth").popover({
            container: 'body',
            trigger: 'focus',
            placement: 'left',
            content : "please check the Month"
        });
        $(".expiryMonth").popover('show');
    }

    if(isNaN(cvv)){
        error = true;
        $(".cvv").addClass("invalid");
    }

    if(isNaN(price)){
        error = true;
        $(".price").addClass("invalid");
        $(".price").popover({
            container: 'body',
            trigger: 'focus',
            placement: 'left',
            content : "please enter the amount"
        });
        $(".price").popover('show');
    }

    if(holderName === ""){
        error = true;
        $(".cardHolderName").addClass("invalid");
    }

    if(fullName === ""){
        error = true;
        $(".fullName").addClass("invalid");
    }

    if(currency.length > 4){
        error = true;
        $(".currencyDropDown").addClass("invalid");
        $(".currencyDropDown").popover({
            container: 'body',
            trigger: 'focus',
            placement: 'right',
            content : "please check the Currency"
        });
        $(".currencyDropDown").popover('show');
    }

    if(!error){
        var card_data = {
            type: cardType,
            number: cardNumber,
            expire_month: expiryMonth,
            expire_year: expiryYear,
            cvv2: cvv,
            name: holderName,
            price : price, 
            currency : currency
        };

        var transaction_data = {
            Price : price, 
            Currency : currency,
            CustomerFullname : fullName
        };

        var paymentPath, proceedToPayment = false;

        if(cardType == "amex"){
            paymentPath = "payment_paypal";
            if(currency == "USD"){
                proceedToPayment = true;
                paymentPath = "payment_paypal";
            } else {
                proceedToPayment = false;
                $("body").prepend("<div id='myAlert' class='alert alert-danger'><p class='close closeAlert'>&times;</p><strong>Error </strong> AMEX is possible to use only for USD </div>");
            }
        } else if(currency === "USD" || currency === "EUR" || currency === "AUD"){
            proceedToPayment = true;
            paymentPath = "payment_paypal";
        } else {
            proceedToPayment = true;
            paymentPath = "payment_braintree";
        }

        if(proceedToPayment){
            $.ajax({
                method: "GET",
                url: "http://localhost:3000/"+paymentPath,
                data: card_data
            }).done(function(message){
                console.log(message);
                $("body").prepend("<div id='myAlert' class='alert alert-success'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button><strong>Success : </strong>"+ message +"</div>");
            }).fail(function(data, message){
                console.log(message);
                $("body").prepend("<div id='myAlert' class='alert alert-danger'><p class='close closeAlert'>&times;</p><strong>Error </strong>"+ message +"</div>");
            });
        }
    } else {
        // Do nothing
    }

});

2. In the server.js node file I did this:

payments_paypal = require("./payments/paypal"),
payments_braintree = require("./payments/braintree");

app.get("/payment_paypal", payments_paypal.paypal);
app.get("/payment_braintree", payments_braintree.braintree);

Was this way of coding wrong w.r.t the given problem statement? How would an experienced Node developer approach? I want to improve my coding skills.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.