All files / workspace/modules Payment.js

97.67% Statements 84/86
90.48% Branches 19/21
100% Functions 4/4
97.67% Lines 84/86

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 861x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 2x 2x 7x     7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 14x 14x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x
import Is from 'strong-type';
import DominosFormat from './DominosFormat.js';
 
const is=new Is;
 
class Payment extends DominosFormat{
    constructor(parameters) {
        is.string(parameters.number);
        is.string(parameters.expiration);
        is.string(parameters.securityCode);
        is.string(parameters.postalCode);
        
        if(parameters.amount){
            is.number(parameters.amount)
        }
        if(parameters.tipAmount){
            is.number(parameters.tipAmount);
        }
 
        super();
 
        parameters.number=this.#digitsOnly(parameters.number);
 
        this.#validate(parameters.number);
        
        if(this.cardType){
            this.number=parameters.number;
            
            //numbers only
            this.expiration=this.#digitsOnly(parameters.expiration);
            this.securityCode=parameters.securityCode;
            this.postalCode=parameters.postalCode;
            this.amount=parameters.amount||0;
            this.tipAmount=parameters.tipAmount||0;
 
        }
 
        return this;
    }
    
    type='CreditCard'
    amount=0
    tipAmount=0
    number=''
    cardType=''
    expiration=''
    securityCode=''
    postalCode=''   
 
    #digitsOnly(string){
        return string.replace(/\D/g,'');
    }
 
    #validate(number){
        const re = {
            visa        : /^4[0-9]{12}(?:[0-9]{3})?$/,
            mastercard  : /^5[1-5][0-9]{14}$/,
            amex        : /^3[47][0-9]{13}$/,
            diners      : /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/,
            discover    : /^6(?:011|5[0-9]{2})[0-9]{12}$/,
            jcb         : /^(?:2131|1800|35\d{3})\d{11}$/,
            enroute     : /^(?:2014|2149)\d{11}$/
        };
    
        (re.visa.test(number))? this.cardType='VISA':null;
    
        (re.mastercard.test(number))? this.cardType='MASTERCARD':null;
    
        (re.amex.test(number))? this.cardType='AMEX':null;
    
        (re.diners.test(number))? this.cardType='DINERS':null;
    
        (re.discover.test(number))? this.cardType='DISCOVER':null;
    
        (re.jcb.test(number))? this.cardType='JCB':null;
    
        (re.enroute.test(number))? this.cardType='ENROUTE':null;
        
        return this;
    }
}
 
export {
    Payment as default,
    Payment
}