From abe2e50c20be390ac130364cca0aa0db36027cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 29 Sep 2016 19:30:09 -0300 Subject: [PATCH 1/2] Use class to set name for inputs instead of id --- clickbus-payments-min.js | 6 +- clickbus-payments.js | 204 +++++++++++++++++++++++---------------- demo/index.html | 40 ++++---- src/gateways/payzen.js | 40 ++++++++ src/main.js | 163 +++++++++++++++---------------- 5 files changed, 269 insertions(+), 184 deletions(-) create mode 100644 src/gateways/payzen.js diff --git a/clickbus-payments-min.js b/clickbus-payments-min.js index f80ce0e..c671e8b 100644 --- a/clickbus-payments-min.js +++ b/clickbus-payments-min.js @@ -1 +1,5 @@ -"use strict";function ClickPromise(t,e){this.callable=t,this.clickbusPayments=e,this.errorPromises=0,this.successPromises=0,this.totalPromises=0,this.callbackSuccess=function(){},this.callbackFail=function(){}}function ClickBusPayments(){this.options={paymentFormId:"payment_form",creditcardFieldId:"credit_card",securityCodeFieldId:"security_code",expirationMonthFieldId:"expiration_month",expirationYearFieldId:"expiration_year",holderNameFieldId:"holder_name",docTypeFieldId:"doc_type",docNumberFieldId:"doc_number",amountFieldId:"amount"},this.attributeNames={creditcardFieldId:"cardNumber",securityCodeFieldId:"securityCode",expirationMonthFieldId:"cardExpirationMonth",expirationYearFieldId:"cardExpirationYear",holderNameFieldId:"cardholderName",docTypeFieldId:"docType",docNumberFieldId:"docNumber"},this.optionalValues={amountFieldId:!1,installmentFieldId:!1},this.gateways=[],this.gatewayType=null,this.personalizedOptions=arguments,this.clickPromise=[],this.successResponse={},this.errorResponse={},this.updateForm()}function addEvent(t,e,i){t.addEventListener?t.addEventListener(e,i):t.attachEvent("on"+e,function(){i.call(t)})}function loadScript(t,e){var i=document.getElementsByTagName("head")[0],s=document.createElement("script");s.type="text/javascript",s.src=t,s.onreadystatechange=e,s.onload=e,i.appendChild(s)}function merge(t,e){if(!(arguments[0]&&arguments[0]instanceof Object))return t;for(var i in e)t[i]=e[i];return t}function MercadoPago(t){this.type="credit_card",this.name="mercadoPago",this.publicKey=t,this.gatewayUrl="https://secure.mlstatic.com/sdk/javascript/v1/mercadopago.js?nocache="+10*Math.random()}function MundiPagg(t,e){this.type="credit_card",this.name="mundipagg",this.publicKey=t}function Paypal(t,e){this.type="paypal",this.name="paypal",this.publicKey=t}ClickPromise.prototype.success=function(t){return this.callbackSuccess=t,this},ClickPromise.prototype.fail=function(t){return this.callbackFail=t,this},ClickPromise.prototype.call=function(){this.callable()},ClickPromise.prototype.finish=function(t,e){try{201==t||200==t?(this.successPromises++,this.clickbusPayments.successResponse.token[e.name]=e.content):(this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=e.cause)}catch(t){this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=[t]}finally{if(this.errorPromises==this.totalPromises)return void this.callbackFail(this.clickbusPayments.errorResponse);this.successPromises+this.errorPromises==this.totalPromises&&this.callbackSuccess(this.clickbusPayments.successResponse)}},ClickBusPayments.prototype.init=function(){this.start(),this.successResponse.token={}},ClickBusPayments.prototype.setPaymentFormId=function(t){return this.options.paymentFormId=t,this.personalizedOptions[0].paymentFormId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setCreditcardFieldId=function(t){return this.options.creditcardFieldId=t,this.personalizedOptions[0].creditcardFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setSecurityCodeFieldId=function(t){return this.options.securityCodeFieldId=t,this.personalizedOptions[0].securityCodeFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationMonthFieldId=function(t){return this.options.expirationMonthFieldId=t,this.personalizedOptions[0].expirationMonthFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationYearFieldId=function(t){return this.options.expirationYearFieldId=t,this.personalizedOptions[0].expirationYearFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setHolderNameFieldId=function(t){return this.options.holderNameFieldId=t,this.personalizedOptions[0].holderNameFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocTypeFieldId=function(t){return this.options.docTypeFieldId=t,this.personalizedOptions[0].docTypeFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocNumberFieldId=function(t){return this.options.docNumberFieldId=t,this.personalizedOptions[0].docNumberFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setAmountFieldId=function(t){return this.options.amountFieldId=t,this.personalizedOptions[0].amountFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setInstallmentFieldId=function(t){return this.options.installmentFieldId=t,this.personalizedOptions[0].installmentFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.subscribe=function(t){this.gateways.push(t)},ClickBusPayments.prototype.start=function(){this.gateways.forEach(function(t){t.start()})},ClickBusPayments.prototype.updateForm=function(){this.options=merge(this.options,this.personalizedOptions[0]);for(var t in this.options)if(!this.optionalValues.hasOwnProperty(t)){var e=document.getElementById(this.options[t]);if(!e){var i=this.options[t]+" is required";throw new Error(i)}this.attributeNames[t]&&e.setAttribute("data-checkout",this.attributeNames[t])}},ClickBusPayments.prototype.generateToken=function(t){var e=document.getElementById(this.options.paymentFormId);return"credit_card"==t&&(this.successResponse.brand=this.getCardBrand()),this.gatewayType=t,this.clickPromise=new ClickPromise(function(){var i=this.clickbusPayments.gateways;i.forEach(function(i){i.type==t&&(this.totalPromises++,i.createToken(e,this))},this)},this),this.clickPromise},ClickBusPayments.prototype.getCardBrand=function(){var t=[{name:"visa",pattern:/^4/},{name:"mastercard",pattern:/^5[1-5][0-9][0-9]/},{name:"amex",pattern:/^3[47]/},{name:"diners",pattern:/^3(?:0[0-5]|[68][0-9])/},{name:"elo",pattern:/^401178|^401179|^431274|^438935|^451416|^457393|^457631|^457632|^504175|^627780|^636297|^636368|^(506699|5067[0-6]\d|50677[0-8])|^(50900\d|5090[1-9]\d|509[1-9]\d{2})|^65003[1-3]|^(65003[5-9]|65004\d|65005[0-1])|^(65040[5-9]|6504[1-3]\d)|^(65048[5-9]|65049\d|6505[0-2]\d|65053 [0-8])|^(65054[1-9]|6505[5-8]\d|65059[0-8])|^(65070\d|65071[0-8])|^65072[0-7]|^(65090[1-9]|65091\d|650920)|^(65165[2-9]|6516[6-7]\d)|^(65500\d|65501\d)|^(65502[1-9]|6550[3-4]\d|65505[0-8])/},{name:"hipercard",pattern:/^3841[046]0|^60/}],e=this.getCreditCard();for(var i in t){var s=t[i];if(e.match(s.pattern))return s.name}return null},ClickBusPayments.prototype.getAmount=function(){var t=document.getElementById(this.options.amountFieldId);if(!t)throw new Error("amountFieldId is required");return t.value.replace(/[ .]/g,"")},ClickBusPayments.prototype.getInstallment=function(){var t=document.getElementById(this.options.installmentFieldId);if(!t)throw new Error("installmentFieldId is required");return t.value},ClickBusPayments.prototype.getHolderName=function(){var t=document.getElementById(this.options.holderNameFieldId);if(!t)throw new Error("holderNameFieldId is required");return t.value},ClickBusPayments.prototype.getCreditCard=function(){var t=document.getElementById(this.options.creditcardFieldId);if(!t)throw new Error("creditcardFieldId is required");return t.value.replace(/[ .-]/g,"")},ClickBusPayments.prototype.getExpirationMonth=function(){var t=document.getElementById(this.options.expirationMonthFieldId);if(!t)throw new Error("expirationMonthFieldId is required");return t.value},ClickBusPayments.prototype.getExpirationYear=function(){var t=document.getElementById(this.options.expirationYearFieldId);if(!t)throw new Error("expirationYearFieldId is required");return t.value},ClickBusPayments.prototype.getSecurityCode=function(){var t=document.getElementById(this.options.securityCodeFieldId);if(!t)throw new Error("securityCodeFieldId is required");return t.value},MercadoPago.prototype.start=function(){loadScript(this.gatewayUrl,function(){Mercadopago.setPublishableKey(this.publicKey)}.bind(this))},MercadoPago.prototype.createToken=function(t,e){var i=e.clickbusPayments.successResponse.token;i.hasOwnProperty(this.name)&&this.clearSession(),Mercadopago.createToken(t,function(t,i){i.name=this.name,i.content=i.id,e.finish(t,i)}.bind(this))},MercadoPago.prototype.clearSession=function(){Mercadopago.clearSession()},MundiPagg.prototype.start=function(){},MundiPagg.prototype.clearSession=function(){},MundiPagg.prototype.createToken=function(t,e){var i=new XMLHttpRequest;i.open("POST","/payment/token/mundipagg"),i.onload=function(){var t=JSON.parse(i.response);return 201==i.status?void e.finish(i.status,{content:t.token,name:this.name}):void e.finish(i.status,{name:this.name,cause:t.ErrorReport})}.bind(this),i.onerror=function(){},i.send(JSON.stringify(this.formatRequest(e.clickbusPayments)))},MundiPagg.prototype.formatRequest=function(t){return{CreditCardBrand:t.getCardBrand(),CreditCardNumber:t.getCreditCard(),ExpMonth:t.getExpirationMonth(),ExpYear:t.getExpirationYear(),HolderName:t.getHolderName(),SecurityCode:t.getSecurityCode(),IsOneDollarAuthEnabled:!1}},Paypal.prototype.start=function(){},Paypal.prototype.clearSession=function(){},Paypal.prototype.createToken=function(t,e){var i=new XMLHttpRequest;i.open("GET","/payment/token/paypal"),i.onload=function(){var t=JSON.parse(i.response);return 200==i.status?void e.finish(i.status,{content:t,name:this.name}):void e.finish(i.status,{name:this.name,cause:t.ErrorReport})}.bind(this),i.onerror=function(){console.log(i)},i.send()}; \ No newline at end of file +<<<<<<< 7c288c94dfc4adeb7483186b5188b00ca68ef8a4 +"use strict";function ClickPromise(t,e){this.callable=t,this.clickbusPayments=e,this.errorPromises=0,this.successPromises=0,this.totalPromises=0,this.callbackSuccess=function(){},this.callbackFail=function(){}}function ClickBusPayments(){this.options={paymentFormId:"payment_form",creditcardFieldId:"credit_card",securityCodeFieldId:"security_code",expirationMonthFieldId:"expiration_month",expirationYearFieldId:"expiration_year",holderNameFieldId:"holder_name",docTypeFieldId:"doc_type",docNumberFieldId:"doc_number",amountFieldId:"amount"},this.attributeNames={creditcardFieldId:"cardNumber",securityCodeFieldId:"securityCode",expirationMonthFieldId:"cardExpirationMonth",expirationYearFieldId:"cardExpirationYear",holderNameFieldId:"cardholderName",docTypeFieldId:"docType",docNumberFieldId:"docNumber"},this.optionalValues={amountFieldId:!1,installmentFieldId:!1},this.gateways=[],this.gatewayType=null,this.personalizedOptions=arguments,this.clickPromise=[],this.successResponse={},this.errorResponse={},this.updateForm()}function addEvent(t,e,i){t.addEventListener?t.addEventListener(e,i):t.attachEvent("on"+e,function(){i.call(t)})}function loadScript(t,e){var i=document.getElementsByTagName("head")[0],s=document.createElement("script");s.type="text/javascript",s.src=t,s.onreadystatechange=e,s.onload=e,i.appendChild(s)}function merge(t,e){if(!(arguments[0]&&arguments[0]instanceof Object))return t;for(var i in e)t[i]=e[i];return t}function MercadoPago(t){this.type="credit_card",this.name="mercadoPago",this.publicKey=t,this.gatewayUrl="https://secure.mlstatic.com/sdk/javascript/v1/mercadopago.js?nocache="+10*Math.random()}function MundiPagg(t,e){this.type="credit_card",this.name="mundipagg",this.publicKey=t}function Paypal(t,e){this.type="paypal",this.name="paypal",this.publicKey=t}ClickPromise.prototype.success=function(t){return this.callbackSuccess=t,this},ClickPromise.prototype.fail=function(t){return this.callbackFail=t,this},ClickPromise.prototype.call=function(){this.callable()},ClickPromise.prototype.finish=function(t,e){try{201==t||200==t?(this.successPromises++,this.clickbusPayments.successResponse.token[e.name]=e.content):(this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=e.cause)}catch(t){this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=[t]}finally{if(this.errorPromises==this.totalPromises)return void this.callbackFail(this.clickbusPayments.errorResponse);this.successPromises+this.errorPromises==this.totalPromises&&this.callbackSuccess(this.clickbusPayments.successResponse)}},ClickBusPayments.prototype.init=function(){this.start(),this.successResponse.token={}},ClickBusPayments.prototype.setPaymentFormId=function(t){return this.options.paymentFormId=t,this.personalizedOptions[0].paymentFormId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setCreditcardFieldId=function(t){return this.options.creditcardFieldId=t,this.personalizedOptions[0].creditcardFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setSecurityCodeFieldId=function(t){return this.options.securityCodeFieldId=t,this.personalizedOptions[0].securityCodeFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationMonthFieldId=function(t){return this.options.expirationMonthFieldId=t,this.personalizedOptions[0].expirationMonthFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationYearFieldId=function(t){return this.options.expirationYearFieldId=t,this.personalizedOptions[0].expirationYearFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setHolderNameFieldId=function(t){return this.options.holderNameFieldId=t,this.personalizedOptions[0].holderNameFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocTypeFieldId=function(t){return this.options.docTypeFieldId=t,this.personalizedOptions[0].docTypeFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocNumberFieldId=function(t){return this.options.docNumberFieldId=t,this.personalizedOptions[0].docNumberFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setAmountFieldId=function(t){return this.options.amountFieldId=t,this.personalizedOptions[0].amountFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setInstallmentFieldId=function(t){return this.options.installmentFieldId=t,this.personalizedOptions[0].installmentFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.subscribe=function(t){this.gateways.push(t)},ClickBusPayments.prototype.start=function(){this.gateways.forEach(function(t){t.start()})},ClickBusPayments.prototype.updateForm=function(){this.options=merge(this.options,this.personalizedOptions[0]);for(var t in this.options)if(!this.optionalValues.hasOwnProperty(t)){var e=document.getElementById(this.options[t]);if(!e){var i=this.options[t]+" is required";throw new Error(i)}this.attributeNames[t]&&e.setAttribute("data-checkout",this.attributeNames[t])}},ClickBusPayments.prototype.generateToken=function(t){var e=document.getElementById(this.options.paymentFormId);return"credit_card"==t&&(this.successResponse.brand=this.getCardBrand()),this.gatewayType=t,this.clickPromise=new ClickPromise(function(){var i=this.clickbusPayments.gateways;i.forEach(function(i){i.type==t&&(this.totalPromises++,i.createToken(e,this))},this)},this),this.clickPromise},ClickBusPayments.prototype.getCardBrand=function(){var t=[{name:"visa",pattern:/^4/},{name:"mastercard",pattern:/^5[1-5][0-9][0-9]/},{name:"amex",pattern:/^3[47]/},{name:"diners",pattern:/^3(?:0[0-5]|[68][0-9])/},{name:"elo",pattern:/^401178|^401179|^431274|^438935|^451416|^457393|^457631|^457632|^504175|^627780|^636297|^636368|^(506699|5067[0-6]\d|50677[0-8])|^(50900\d|5090[1-9]\d|509[1-9]\d{2})|^65003[1-3]|^(65003[5-9]|65004\d|65005[0-1])|^(65040[5-9]|6504[1-3]\d)|^(65048[5-9]|65049\d|6505[0-2]\d|65053 [0-8])|^(65054[1-9]|6505[5-8]\d|65059[0-8])|^(65070\d|65071[0-8])|^65072[0-7]|^(65090[1-9]|65091\d|650920)|^(65165[2-9]|6516[6-7]\d)|^(65500\d|65501\d)|^(65502[1-9]|6550[3-4]\d|65505[0-8])/},{name:"hipercard",pattern:/^3841[046]0|^60/}],e=this.getCreditCard();for(var i in t){var s=t[i];if(e.match(s.pattern))return s.name}return null},ClickBusPayments.prototype.getAmount=function(){var t=document.getElementById(this.options.amountFieldId);if(!t)throw new Error("amountFieldId is required");return t.value.replace(/[ .]/g,"")},ClickBusPayments.prototype.getInstallment=function(){var t=document.getElementById(this.options.installmentFieldId);if(!t)throw new Error("installmentFieldId is required");return t.value},ClickBusPayments.prototype.getHolderName=function(){var t=document.getElementById(this.options.holderNameFieldId);if(!t)throw new Error("holderNameFieldId is required");return t.value},ClickBusPayments.prototype.getCreditCard=function(){var t=document.getElementById(this.options.creditcardFieldId);if(!t)throw new Error("creditcardFieldId is required");return t.value.replace(/[ .-]/g,"")},ClickBusPayments.prototype.getExpirationMonth=function(){var t=document.getElementById(this.options.expirationMonthFieldId);if(!t)throw new Error("expirationMonthFieldId is required");return t.value},ClickBusPayments.prototype.getExpirationYear=function(){var t=document.getElementById(this.options.expirationYearFieldId);if(!t)throw new Error("expirationYearFieldId is required");return t.value},ClickBusPayments.prototype.getSecurityCode=function(){var t=document.getElementById(this.options.securityCodeFieldId);if(!t)throw new Error("securityCodeFieldId is required");return t.value},MercadoPago.prototype.start=function(){loadScript(this.gatewayUrl,function(){Mercadopago.setPublishableKey(this.publicKey)}.bind(this))},MercadoPago.prototype.createToken=function(t,e){var i=e.clickbusPayments.successResponse.token;i.hasOwnProperty(this.name)&&this.clearSession(),Mercadopago.createToken(t,function(t,i){i.name=this.name,i.content=i.id,e.finish(t,i)}.bind(this))},MercadoPago.prototype.clearSession=function(){Mercadopago.clearSession()},MundiPagg.prototype.start=function(){},MundiPagg.prototype.clearSession=function(){},MundiPagg.prototype.createToken=function(t,e){var i=new XMLHttpRequest;i.open("POST","/payment/token/mundipagg"),i.onload=function(){var t=JSON.parse(i.response);return 201==i.status?void e.finish(i.status,{content:t.token,name:this.name}):void e.finish(i.status,{name:this.name,cause:t.ErrorReport})}.bind(this),i.onerror=function(){},i.send(JSON.stringify(this.formatRequest(e.clickbusPayments)))},MundiPagg.prototype.formatRequest=function(t){return{CreditCardBrand:t.getCardBrand(),CreditCardNumber:t.getCreditCard(),ExpMonth:t.getExpirationMonth(),ExpYear:t.getExpirationYear(),HolderName:t.getHolderName(),SecurityCode:t.getSecurityCode(),IsOneDollarAuthEnabled:!1}},Paypal.prototype.start=function(){},Paypal.prototype.clearSession=function(){},Paypal.prototype.createToken=function(t,e){var i=new XMLHttpRequest;i.open("GET","/payment/token/paypal"),i.onload=function(){var t=JSON.parse(i.response);return 200==i.status?void e.finish(i.status,{content:t,name:this.name}):void e.finish(i.status,{name:this.name,cause:t.ErrorReport})}.bind(this),i.onerror=function(){console.log(i)},i.send()}; +======= +"use strict";function ClickPromise(t,e){this.callable=t,this.clickbusPayments=e,this.errorPromises=0,this.successPromises=0,this.totalPromises=0,this.callbackSuccess=function(){},this.callbackFail=function(){}}function ClickBusPayments(){this.options={paymentFormId:"payment_form",creditcardFieldClass:"credit_card",securityCodeFieldClass:"security_code",expirationMonthFieldClass:"expiration_month",expirationYearFieldClass:"expiration_year",holderNameFieldClass:"holder_name",docTypeFieldClass:"doc_type",docNumberFieldClass:"doc_number",amountFieldClass:"amount"},this.optionalValues={amountFieldClass:!1,installmentFieldClass:!1},this.gateways=[],this.gatewayType=null,this.personalizedOptions=arguments,this.clickPromise=[],this.successResponse={},this.errorResponse={},this.updateForm()}function addEvent(t,e,s){t.addEventListener?t.addEventListener(e,s):t.attachEvent("on"+e,function(){s.call(t)})}function loadScript(t,e){var s=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.src=t,i.onreadystatechange=e,i.onload=e,s.appendChild(i)}function merge(t,e){if(!(arguments[0]&&arguments[0]instanceof Object))return t;for(var s in e)t[s]=e[s];return t}function MercadoPago(t){this.type="credit_card",this.name="mercadoPago",this.publicKey=t,this.gatewayUrl="https://secure.mlstatic.com/sdk/javascript/v1/mercadopago.js?nocache="+10*Math.random()}function MundiPagg(t,e){this.type="credit_card",this.name="mundipagg",this.publicKey=t}function Paypal(t,e){this.type="paypal",this.name="paypal",this.publicKey=t}function PayZen(){this.type="debit_card",this.name="payzen"}ClickPromise.prototype.success=function(t){return this.callbackSuccess=t,this},ClickPromise.prototype.fail=function(t){return this.callbackFail=t,this},ClickPromise.prototype.call=function(){this.callable()},ClickPromise.prototype.finish=function(t,e){try{201==t||200==t?(this.successPromises++,this.clickbusPayments.successResponse.token[e.name]=e.content):(this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=e.cause)}catch(t){this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=[t]}finally{if(this.errorPromises==this.totalPromises)return void this.callbackFail(this.clickbusPayments.errorResponse);this.successPromises+this.errorPromises==this.totalPromises&&this.callbackSuccess(this.clickbusPayments.successResponse)}},ClickBusPayments.prototype.init=function(){this.start(),this.successResponse.token={}},ClickBusPayments.prototype.setPaymentFormId=function(t){return this.options.paymentFormId=t,this.personalizedOptions[0].paymentFormId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setCreditcardFieldClass=function(t){return this.options.creditcardFieldClass=t,this.personalizedOptions[0].creditcardFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setSecurityCodeFieldClass=function(t){return this.options.securityCodeFieldClass=t,this.personalizedOptions[0].securityCodeFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationMonthFieldClass=function(t){return this.options.expirationMonthFieldClass=t,this.personalizedOptions[0].expirationMonthFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationYearFieldClass=function(t){return this.options.expirationYearFieldClass=t,this.personalizedOptions[0].expirationYearFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setHolderNameFieldClass=function(t){return this.options.holderNameFieldClass=t,this.personalizedOptions[0].holderNameFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocTypeFieldClass=function(t){return this.options.docTypeFieldClass=t,this.personalizedOptions[0].docTypeFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocNumberFieldClass=function(t){return this.options.docNumberFieldClass=t,this.personalizedOptions[0].docNumberFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setAmountFieldClass=function(t){return this.options.amountFieldClass=t,this.personalizedOptions[0].amountFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setInstallmentFieldClass=function(t){return this.options.installmentFieldClass=t,this.personalizedOptions[0].installmentFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.subscribe=function(t){this.gateways.push(t)},ClickBusPayments.prototype.start=function(){this.gateways.forEach(function(t){t.start()})},ClickBusPayments.prototype.updateForm=function(){this.options=merge(this.options,this.personalizedOptions[0]);for(var t in this.options)if(!this.optionalValues.hasOwnProperty(t)){var e=document.getElementsByClassName(this.options[t]);if(!e){var s=this.options[t]+" is required";throw new Error(s)}}},ClickBusPayments.prototype.generateToken=function(t){var e=document.getElementById(this.options.paymentFormId);return"credit_card"!=t&&"debit_card"!=t||(this.successResponse.brand=this.getCardBrand()),this.gatewayType=t,this.clickPromise=new ClickPromise(function(){var s=this.clickbusPayments.gateways;s.forEach(function(s){s.type==t&&(this.totalPromises++,s.createToken(e,this))},this)},this),this.clickPromise},ClickBusPayments.prototype.getElement=function(t){var e=document.getElementsByClassName(t);if(0==e.length)return!1;for(var s in e){var i=e[s];if(i.offsetWidth>0&&i.offsetHeight)return i}},ClickBusPayments.prototype.getCardBrand=function(){var t=[{name:"visa",pattern:/^4/},{name:"mastercard",pattern:/^5[0-5][0-9][0-9]/},{name:"amex",pattern:/^3[47]/},{name:"diners",pattern:/^3(?:0[0-5]|[68][0-9])/},{name:"elo",pattern:/^401178|^401179|^431274|^438935|^451416|^457393|^457631|^457632|^504175|^627780|^636297|^636368|^(506699|5067[0-6]\d|50677[0-8])|^(50900\d|5090[1-9]\d|509[1-9]\d{2})|^65003[1-3]|^(65003[5-9]|65004\d|65005[0-1])|^(65040[5-9]|6504[1-3]\d)|^(65048[5-9]|65049\d|6505[0-2]\d|65053 [0-8])|^(65054[1-9]|6505[5-8]\d|65059[0-8])|^(65070\d|65071[0-8])|^65072[0-7]|^(65090[1-9]|65091\d|650920)|^(65165[2-9]|6516[6-7]\d)|^(65500\d|65501\d)|^(65502[1-9]|6550[3-4]\d|65505[0-8])/},{name:"hipercard",pattern:/^3841[046]0|^60/}],e=this.getCreditCard();for(var s in t){var i=t[s];if(e.match(i.pattern))return i.name}return null},ClickBusPayments.prototype.getAmount=function(){var t=this.getElement(this.options.amountFieldClass);if(!t)throw new Error("amountFieldClass is required");return t.value.replace(/[ .]/g,"")},ClickBusPayments.prototype.getInstallment=function(){var t=this.getElement(this.options.installmentFieldClass);if(!t)throw new Error("installmentFieldClass is required");return t.value},ClickBusPayments.prototype.getHolderName=function(){var t=this.getElement(this.options.holderNameFieldClass);if(!t)throw new Error("holderNameFieldClass is required");return t.value},ClickBusPayments.prototype.getCreditCard=function(){var t=this.getElement(this.options.creditcardFieldClass);if(!t)throw new Error("creditcardFieldClass is required");return t.value.replace(/[ .-]/g,"")},ClickBusPayments.prototype.getExpirationMonth=function(){var t=this.getElement(this.options.expirationMonthFieldClass);if(!t)throw new Error("expirationMonthFieldClass is required");return t.value},ClickBusPayments.prototype.getExpirationYear=function(){var t=this.getElement(this.options.expirationYearFieldClass);if(!t)throw new Error("expirationYearFieldClass is required");return t.value},ClickBusPayments.prototype.getSecurityCode=function(){var t=this.getElement(this.options.securityCodeFieldClass);if(!t)throw new Error("securityCodeFieldClass is required");return t.value},MercadoPago.prototype.start=function(){loadScript(this.gatewayUrl,function(){Mercadopago.setPublishableKey(this.publicKey)}.bind(this))},MercadoPago.prototype.createToken=function(t,e){var s=e.clickbusPayments.successResponse.token;s.hasOwnProperty(this.name)&&this.clearSession(),Mercadopago.createToken(t,function(t,s){s.name=this.name,s.content=s.id,e.finish(t,s)}.bind(this))},MercadoPago.prototype.clearSession=function(){Mercadopago.clearSession()},MundiPagg.prototype.start=function(){},MundiPagg.prototype.clearSession=function(){},MundiPagg.prototype.createToken=function(t,e){var s=new XMLHttpRequest;s.open("POST","/payment/token/mundipagg"),s.onload=function(){var t=JSON.parse(s.response);return 201==s.status?void e.finish(s.status,{content:t.token,name:this.name}):void e.finish(s.status,{name:this.name,cause:t.ErrorReport})}.bind(this),s.onerror=function(){},s.send(JSON.stringify(this.formatRequest(e.clickbusPayments)))},MundiPagg.prototype.formatRequest=function(t){return{CreditCardTransactionCollection:[{AmountInCents:t.getAmount(),CreditCard:{CreditCardBrand:t.getCardBrand(),CreditCardNumber:t.getCreditCard(),ExpMonth:t.getExpirationMonth(),ExpYear:t.getExpirationYear(),HolderName:t.getHolderName(),SecurityCode:t.getSecurityCode()},CreditCardOperation:"AuthOnly",InstallmentCount:t.getInstallment()}]}},Paypal.prototype.start=function(){},Paypal.prototype.clearSession=function(){},Paypal.prototype.createToken=function(t,e){var s=new XMLHttpRequest;s.open("GET","/payment/token/paypal"),s.onload=function(){var t=JSON.parse(s.response);return 200==s.status?void e.finish(s.status,{content:t,name:this.name}):void e.finish(s.status,{name:this.name,cause:t.ErrorReport})}.bind(this),s.onerror=function(){console.log(s)},s.send()},PayZen.prototype.start=function(){},PayZen.prototype.clearSession=function(){},PayZen.prototype.createToken=function(t,e){var s=new XMLHttpRequest;s.open("POST","/payment/token/debit_card"),s.onload=function(){var t=JSON.parse(s.response);return 200==s.status?void e.finish(s.status,{content:t,name:this.name}):void e.finish(s.status,{name:this.name,cause:t.ErrorReport})}.bind(this),s.onerror=function(){},s.send(JSON.stringify(this.formatRequest(e.clickbusPayments)))},PayZen.prototype.formatRequest=function(t){return{paymentName:this.name,cardBrand:t.getCardBrand(),cardNumber:t.getCreditCard(),cardExpirationMonth:t.getExpirationMonth(),cardExpirationYear:parseInt(t.getExpirationYear(),10)+2e3,cardSecurityCode:t.getSecurityCode()}}; +>>>>>>> Use class to set name for inputs instead of id diff --git a/clickbus-payments.js b/clickbus-payments.js index 3c241f9..5099568 100644 --- a/clickbus-payments.js +++ b/clickbus-payments.js @@ -65,15 +65,15 @@ ClickPromise.prototype.finish = function(status, response) { * Not Required: * * - `paymentFormId` HTML form id - * - `creditcardFieldId` HTML field id for creditcard number, default credit_card - * - `securityCodeFieldId` HTML field id for creditcard security code number, default security_code - * - `expirationMonthFieldId` HTML field id for creditcard expiration month, default expiration_month - * - `expirationYearFieldId` HTML field id for creditcard expiration year, default expiration_year - * - `holderNameFieldId` HTML field id for creditcard holder name, default holder_name - * - `docTypeFieldId` HTML field id for creditcard holder document type, default doc_type - * - `docNumberFieldId` HTML field id for creditcard holder document number, default doc_number - * - `amountFieldId` HTML field id for amount value, default amount - * - `installmentFieldId` HTML field id for install value, default installment + * - `creditcardFieldClass` HTML field clas for creditcard number, default credit_card + * - `securityCodeFieldClass` HTML field clas for creditcard security code number, default security_code + * - `expirationMonthFieldClass` HTML field clas for creditcard expiration month, default expiration_month + * - `expirationYearFieldClass` HTML field clas for creditcard expiration year, default expiration_year + * - `holderNameFieldClass` HTML field clas for creditcard holder name, default holder_name + * - `docTypeFieldClass` HTML field clas for creditcard holder document type, default doc_type + * - `docNumberFieldClass` HTML field clas for creditcard holder document number, default doc_number + * - `amountFieldClass` HTML field clas for amount value, default amount + * - `installmentFieldClass` HTML field id for install value, default installment * * @param {Object} options * @api public @@ -81,29 +81,19 @@ ClickPromise.prototype.finish = function(status, response) { function ClickBusPayments() { this.options = { paymentFormId: "payment_form", - creditcardFieldId: "credit_card", - securityCodeFieldId: "security_code", - expirationMonthFieldId: "expiration_month", - expirationYearFieldId: "expiration_year", - holderNameFieldId: "holder_name", - docTypeFieldId: "doc_type", - docNumberFieldId: "doc_number", - amountFieldId: "amount" - }; - - this.attributeNames = { - creditcardFieldId: "cardNumber", - securityCodeFieldId: "securityCode", - expirationMonthFieldId: "cardExpirationMonth", - expirationYearFieldId: "cardExpirationYear", - holderNameFieldId: "cardholderName", - docTypeFieldId: "docType", - docNumberFieldId: "docNumber" + creditcardFieldClass: "credit_card", + securityCodeFieldClass: "security_code", + expirationMonthFieldClass: "expiration_month", + expirationYearFieldClass: "expiration_year", + holderNameFieldClass: "holder_name", + docTypeFieldClass: "doc_type", + docNumberFieldClass: "doc_number", + amountFieldClass: "amount" }; this.optionalValues = { - amountFieldId: false, - installmentFieldId: false + amountFieldClass: false, + installmentFieldClass: false }; this.gateways = []; @@ -132,73 +122,73 @@ ClickBusPayments.prototype.setPaymentFormId = function(paymentFormId) { return this; }; -ClickBusPayments.prototype.setCreditcardFieldId = function(creditcardFieldId) { - this.options.creditcardFieldId = creditcardFieldId; - this.personalizedOptions[0].creditcardFieldId = creditcardFieldId; +ClickBusPayments.prototype.setCreditcardFieldClass = function(creditcardFieldClass) { + this.options.creditcardFieldClass = creditcardFieldClass; + this.personalizedOptions[0].creditcardFieldClass = creditcardFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setSecurityCodeFieldId = function(securityCodeFieldId) { - this.options.securityCodeFieldId = securityCodeFieldId; - this.personalizedOptions[0].securityCodeFieldId = securityCodeFieldId; +ClickBusPayments.prototype.setSecurityCodeFieldClass = function(securityCodeFieldClass) { + this.options.securityCodeFieldClass = securityCodeFieldClass; + this.personalizedOptions[0].securityCodeFieldClass = securityCodeFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setExpirationMonthFieldId = function(expirationMonthFieldId) { - this.options.expirationMonthFieldId = expirationMonthFieldId; - this.personalizedOptions[0].expirationMonthFieldId = expirationMonthFieldId; +ClickBusPayments.prototype.setExpirationMonthFieldClass = function(expirationMonthFieldClass) { + this.options.expirationMonthFieldClass = expirationMonthFieldClass; + this.personalizedOptions[0].expirationMonthFieldClass = expirationMonthFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setExpirationYearFieldId = function(expirationYearFieldId) { - this.options.expirationYearFieldId = expirationYearFieldId; - this.personalizedOptions[0].expirationYearFieldId = expirationYearFieldId; +ClickBusPayments.prototype.setExpirationYearFieldClass = function(expirationYearFieldClass) { + this.options.expirationYearFieldClass = expirationYearFieldClass; + this.personalizedOptions[0].expirationYearFieldClass = expirationYearFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setHolderNameFieldId = function(holderNameFieldId) { - this.options.holderNameFieldId = holderNameFieldId; - this.personalizedOptions[0].holderNameFieldId = holderNameFieldId; +ClickBusPayments.prototype.setHolderNameFieldClass = function(holderNameFieldClass) { + this.options.holderNameFieldClass = holderNameFieldClass; + this.personalizedOptions[0].holderNameFieldClass = holderNameFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setDocTypeFieldId = function(docTypeFieldId) { - this.options.docTypeFieldId = docTypeFieldId; - this.personalizedOptions[0].docTypeFieldId = docTypeFieldId; +ClickBusPayments.prototype.setDocTypeFieldClass = function(docTypeFieldClass) { + this.options.docTypeFieldClass = docTypeFieldClass; + this.personalizedOptions[0].docTypeFieldClass = docTypeFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setDocNumberFieldId = function(docNumberFieldId) { - this.options.docNumberFieldId = docNumberFieldId; - this.personalizedOptions[0].docNumberFieldId = docNumberFieldId; +ClickBusPayments.prototype.setDocNumberFieldClass = function(docNumberFieldClass) { + this.options.docNumberFieldClass = docNumberFieldClass; + this.personalizedOptions[0].docNumberFieldClass = docNumberFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setAmountFieldId = function(amountFieldId) { - this.options.amountFieldId = amountFieldId; - this.personalizedOptions[0].amountFieldId = amountFieldId; +ClickBusPayments.prototype.setAmountFieldClass = function(amountFieldClass) { + this.options.amountFieldClass = amountFieldClass; + this.personalizedOptions[0].amountFieldClass = amountFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setInstallmentFieldId = function(installmentFieldId) { - this.options.installmentFieldId = installmentFieldId; - this.personalizedOptions[0].installmentFieldId = installmentFieldId; +ClickBusPayments.prototype.setInstallmentFieldClass = function(installmentFieldClass) { + this.options.installmentFieldClass = installmentFieldClass; + this.personalizedOptions[0].installmentFieldClass = installmentFieldClass; this.updateForm(); this.start(); return this; @@ -216,27 +206,23 @@ ClickBusPayments.prototype.start = function() { ClickBusPayments.prototype.updateForm = function() { this.options = merge(this.options, this.personalizedOptions[0]); - for (var fieldId in this.options) { - if (this.optionalValues.hasOwnProperty(fieldId)) { + for (var fieldClass in this.options) { + if (this.optionalValues.hasOwnProperty(fieldClass)) { continue; } - var element = document.getElementById(this.options[fieldId]); - if (!element) { - var errorMessage = this.options[fieldId] + ' is required'; + var elements = document.getElementsByClassName(this.options[fieldClass]); + if (!elements) { + var errorMessage = this.options[fieldClass] + ' is required'; throw new Error(errorMessage); } - - if (this.attributeNames[fieldId]) { - element.setAttribute('data-checkout', this.attributeNames[fieldId]); - } } }; ClickBusPayments.prototype.generateToken = function(gatewayType) { var form = document.getElementById(this.options['paymentFormId']); - if (gatewayType == 'credit_card') { + if (gatewayType == 'credit_card' || gatewayType == 'debit_card') { this.successResponse.brand = this.getCardBrand(); } @@ -258,13 +244,28 @@ ClickBusPayments.prototype.generateToken = function(gatewayType) { return this.clickPromise; }; +ClickBusPayments.prototype.getElement = function(className) { + var elements = document.getElementsByClassName(className); + + if (elements.length == 0) { + return false; + } + + for (var index in elements) { + var element = elements[index]; + if (element.offsetWidth > 0 && element.offsetHeight) { + return element; + } + } +}; + ClickBusPayments.prototype.getCardBrand = function() { var brands = [{ name: 'visa', pattern: /^4/ }, { name: 'mastercard', - pattern: /^5[1-5][0-9][0-9]/ + pattern: /^5[0-5][0-9][0-9]/ }, { name: 'amex', pattern: /^3[47]/ @@ -292,70 +293,70 @@ ClickBusPayments.prototype.getCardBrand = function() { }; ClickBusPayments.prototype.getAmount = function() { - var amount = document.getElementById(this.options.amountFieldId); + var amount = this.getElement(this.options.amountFieldClass); if (!amount) { - throw new Error('amountFieldId is required'); + throw new Error('amountFieldClass is required'); } return amount.value.replace(/[ .]/g, ''); }; ClickBusPayments.prototype.getInstallment = function() { - var installment = document.getElementById(this.options.installmentFieldId); + var installment = this.getElement(this.options.installmentFieldClass); if (!installment) { - throw new Error('installmentFieldId is required'); + throw new Error('installmentFieldClass is required'); } return installment.value; }; ClickBusPayments.prototype.getHolderName = function() { - var holderName = document.getElementById(this.options.holderNameFieldId); + var holderName = this.getElement(this.options.holderNameFieldClass); if (!holderName) { - throw new Error('holderNameFieldId is required'); + throw new Error('holderNameFieldClass is required'); } return holderName.value; }; ClickBusPayments.prototype.getCreditCard = function() { - var ccNumber = document.getElementById(this.options.creditcardFieldId); + var ccNumber = this.getElement(this.options.creditcardFieldClass); if (!ccNumber) { - throw new Error('creditcardFieldId is required'); + throw new Error('creditcardFieldClass is required'); } return ccNumber.value.replace(/[ .-]/g, ''); }; ClickBusPayments.prototype.getExpirationMonth = function() { - var expirationMonth = document.getElementById(this.options.expirationMonthFieldId); + var expirationMonth = this.getElement(this.options.expirationMonthFieldClass); if (!expirationMonth) { - throw new Error('expirationMonthFieldId is required'); + throw new Error('expirationMonthFieldClass is required'); } return expirationMonth.value; }; ClickBusPayments.prototype.getExpirationYear = function() { - var expirationYear = document.getElementById(this.options.expirationYearFieldId); + var expirationYear = this.getElement(this.options.expirationYearFieldClass); if (!expirationYear) { - throw new Error('expirationYearFieldId is required'); + throw new Error('expirationYearFieldClass is required'); } return expirationYear.value; }; ClickBusPayments.prototype.getSecurityCode = function() { - var securityCode = document.getElementById(this.options.securityCodeFieldId); + var securityCode = this.getElement(this.options.securityCodeFieldClass); if (!securityCode) { - throw new Error('securityCodeFieldId is required'); + throw new Error('securityCodeFieldClass is required'); } return securityCode.value; @@ -517,3 +518,44 @@ Paypal.prototype.createToken = function(form, clickPromise) { request.send(); }; + +"use strict"; + +function PayZen() { + this.type = 'debit_card'; + this.name = 'payzen'; +} + +PayZen.prototype.start = function() { }; +PayZen.prototype.clearSession = function() { }; + +PayZen.prototype.createToken = function(form, clickPromise) { + var request = new XMLHttpRequest(); + request.open('POST', '/payment/token/debit_card'); + request.onload = function() { + var response = JSON.parse(request.response); + if (request.status == 200) { + clickPromise.finish(request.status, {content: response, name: this.name}); + return; + } + + clickPromise.finish(request.status, {name: this.name, cause: response.ErrorReport}); + }.bind(this); + + request.onerror = function() { + + }; + + request.send(JSON.stringify(this.formatRequest(clickPromise.clickbusPayments))); +}; + +PayZen.prototype.formatRequest = function(clickbusPayments) { + return { + paymentName: this.name, + cardBrand: clickbusPayments.getCardBrand(), + cardNumber: clickbusPayments.getCreditCard(), + cardExpirationMonth: clickbusPayments.getExpirationMonth(), + cardExpirationYear: parseInt(clickbusPayments.getExpirationYear(), 10) + 2000, + cardSecurityCode: clickbusPayments.getSecurityCode() + } +}; diff --git a/demo/index.html b/demo/index.html index 3d9d46c..67011fb 100644 --- a/demo/index.html +++ b/demo/index.html @@ -22,39 +22,39 @@
- +
- +
- +
- +
- +
- +
- +
- +
- +
@@ -95,20 +95,18 @@

ERROR MESSAGE

window.onload = function() { clickbus = new ClickBusPayments({ paymentFormId: 'payment_form', - creditcardFieldId: 'credit_card', - securityCodeFieldId: 'security_code_other_id', - expirationMonthFieldId: 'expiration_month', - expirationYearFieldId: 'expiration_year', - holderNameFieldId: 'holder_name', - docTypeFieldId: 'doc_type', - docNumberFieldId: 'doc_number', - amountFieldId: 'ammount', - installmentFieldId: 'installment', + creditcardFieldClass: 'credit_card', + securityCodeFieldClass: 'security_code_other_id', + expirationMonthFieldClass: 'expiration_month', + expirationYearFieldClass: 'expiration_year', + holderNameFieldClass: 'holder_name', + docTypeFieldClass: 'doc_type', + docNumberFieldClass: 'doc_number', + amountFieldClass: 'ammount', + installmentFieldClass: 'installment', }); clickbus.subscribe(new MercadoPago('TEST-c24e58b1-a4e0-40f6-adb4-a6ca7cf60209')); - clickbus.subscribe(new MundiPagg('7ac3cf57-7163-446f-aa92-812369516d08')); - clickbus.subscribe(new Paypal()); clickbus.init(); }; @@ -121,7 +119,7 @@

ERROR MESSAGE

document.getElementById('code').innerHTML = ''; document.getElementById('message').innerHTML = ''; - clickbus.generateToken('paypal').success(function(response) { + clickbus.generateToken('credit_card').success(function(response) { console.log(response); for(var key in response.token) { diff --git a/src/gateways/payzen.js b/src/gateways/payzen.js new file mode 100644 index 0000000..384586e --- /dev/null +++ b/src/gateways/payzen.js @@ -0,0 +1,40 @@ +"use strict"; + +function PayZen() { + this.type = 'debit_card'; + this.name = 'payzen'; +} + +PayZen.prototype.start = function() { }; +PayZen.prototype.clearSession = function() { }; + +PayZen.prototype.createToken = function(form, clickPromise) { + var request = new XMLHttpRequest(); + request.open('POST', '/payment/token/debit_card'); + request.onload = function() { + var response = JSON.parse(request.response); + if (request.status == 200) { + clickPromise.finish(request.status, {content: response, name: this.name}); + return; + } + + clickPromise.finish(request.status, {name: this.name, cause: response.ErrorReport}); + }.bind(this); + + request.onerror = function() { + + }; + + request.send(JSON.stringify(this.formatRequest(clickPromise.clickbusPayments))); +}; + +PayZen.prototype.formatRequest = function(clickbusPayments) { + return { + paymentName: this.name, + cardBrand: clickbusPayments.getCardBrand(), + cardNumber: clickbusPayments.getCreditCard(), + cardExpirationMonth: clickbusPayments.getExpirationMonth(), + cardExpirationYear: parseInt(clickbusPayments.getExpirationYear(), 10) + 2000, + cardSecurityCode: clickbusPayments.getSecurityCode() + } +}; diff --git a/src/main.js b/src/main.js index 8cd5de9..e9a0bc0 100644 --- a/src/main.js +++ b/src/main.js @@ -10,15 +10,15 @@ * Not Required: * * - `paymentFormId` HTML form id - * - `creditcardFieldId` HTML field id for creditcard number, default credit_card - * - `securityCodeFieldId` HTML field id for creditcard security code number, default security_code - * - `expirationMonthFieldId` HTML field id for creditcard expiration month, default expiration_month - * - `expirationYearFieldId` HTML field id for creditcard expiration year, default expiration_year - * - `holderNameFieldId` HTML field id for creditcard holder name, default holder_name - * - `docTypeFieldId` HTML field id for creditcard holder document type, default doc_type - * - `docNumberFieldId` HTML field id for creditcard holder document number, default doc_number - * - `amountFieldId` HTML field id for amount value, default amount - * - `installmentFieldId` HTML field id for install value, default installment + * - `creditcardFieldClass` HTML field clas for creditcard number, default credit_card + * - `securityCodeFieldClass` HTML field clas for creditcard security code number, default security_code + * - `expirationMonthFieldClass` HTML field clas for creditcard expiration month, default expiration_month + * - `expirationYearFieldClass` HTML field clas for creditcard expiration year, default expiration_year + * - `holderNameFieldClass` HTML field clas for creditcard holder name, default holder_name + * - `docTypeFieldClass` HTML field clas for creditcard holder document type, default doc_type + * - `docNumberFieldClass` HTML field clas for creditcard holder document number, default doc_number + * - `amountFieldClass` HTML field clas for amount value, default amount + * - `installmentFieldClass` HTML field id for install value, default installment * * @param {Object} options * @api public @@ -26,29 +26,19 @@ function ClickBusPayments() { this.options = { paymentFormId: "payment_form", - creditcardFieldId: "credit_card", - securityCodeFieldId: "security_code", - expirationMonthFieldId: "expiration_month", - expirationYearFieldId: "expiration_year", - holderNameFieldId: "holder_name", - docTypeFieldId: "doc_type", - docNumberFieldId: "doc_number", - amountFieldId: "amount" - }; - - this.attributeNames = { - creditcardFieldId: "cardNumber", - securityCodeFieldId: "securityCode", - expirationMonthFieldId: "cardExpirationMonth", - expirationYearFieldId: "cardExpirationYear", - holderNameFieldId: "cardholderName", - docTypeFieldId: "docType", - docNumberFieldId: "docNumber" + creditcardFieldClass: "credit_card", + securityCodeFieldClass: "security_code", + expirationMonthFieldClass: "expiration_month", + expirationYearFieldClass: "expiration_year", + holderNameFieldClass: "holder_name", + docTypeFieldClass: "doc_type", + docNumberFieldClass: "doc_number", + amountFieldClass: "amount" }; this.optionalValues = { - amountFieldId: false, - installmentFieldId: false + amountFieldClass: false, + installmentFieldClass: false }; this.gateways = []; @@ -77,73 +67,73 @@ ClickBusPayments.prototype.setPaymentFormId = function(paymentFormId) { return this; }; -ClickBusPayments.prototype.setCreditcardFieldId = function(creditcardFieldId) { - this.options.creditcardFieldId = creditcardFieldId; - this.personalizedOptions[0].creditcardFieldId = creditcardFieldId; +ClickBusPayments.prototype.setCreditcardFieldClass = function(creditcardFieldClass) { + this.options.creditcardFieldClass = creditcardFieldClass; + this.personalizedOptions[0].creditcardFieldClass = creditcardFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setSecurityCodeFieldId = function(securityCodeFieldId) { - this.options.securityCodeFieldId = securityCodeFieldId; - this.personalizedOptions[0].securityCodeFieldId = securityCodeFieldId; +ClickBusPayments.prototype.setSecurityCodeFieldClass = function(securityCodeFieldClass) { + this.options.securityCodeFieldClass = securityCodeFieldClass; + this.personalizedOptions[0].securityCodeFieldClass = securityCodeFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setExpirationMonthFieldId = function(expirationMonthFieldId) { - this.options.expirationMonthFieldId = expirationMonthFieldId; - this.personalizedOptions[0].expirationMonthFieldId = expirationMonthFieldId; +ClickBusPayments.prototype.setExpirationMonthFieldClass = function(expirationMonthFieldClass) { + this.options.expirationMonthFieldClass = expirationMonthFieldClass; + this.personalizedOptions[0].expirationMonthFieldClass = expirationMonthFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setExpirationYearFieldId = function(expirationYearFieldId) { - this.options.expirationYearFieldId = expirationYearFieldId; - this.personalizedOptions[0].expirationYearFieldId = expirationYearFieldId; +ClickBusPayments.prototype.setExpirationYearFieldClass = function(expirationYearFieldClass) { + this.options.expirationYearFieldClass = expirationYearFieldClass; + this.personalizedOptions[0].expirationYearFieldClass = expirationYearFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setHolderNameFieldId = function(holderNameFieldId) { - this.options.holderNameFieldId = holderNameFieldId; - this.personalizedOptions[0].holderNameFieldId = holderNameFieldId; +ClickBusPayments.prototype.setHolderNameFieldClass = function(holderNameFieldClass) { + this.options.holderNameFieldClass = holderNameFieldClass; + this.personalizedOptions[0].holderNameFieldClass = holderNameFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setDocTypeFieldId = function(docTypeFieldId) { - this.options.docTypeFieldId = docTypeFieldId; - this.personalizedOptions[0].docTypeFieldId = docTypeFieldId; +ClickBusPayments.prototype.setDocTypeFieldClass = function(docTypeFieldClass) { + this.options.docTypeFieldClass = docTypeFieldClass; + this.personalizedOptions[0].docTypeFieldClass = docTypeFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setDocNumberFieldId = function(docNumberFieldId) { - this.options.docNumberFieldId = docNumberFieldId; - this.personalizedOptions[0].docNumberFieldId = docNumberFieldId; +ClickBusPayments.prototype.setDocNumberFieldClass = function(docNumberFieldClass) { + this.options.docNumberFieldClass = docNumberFieldClass; + this.personalizedOptions[0].docNumberFieldClass = docNumberFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setAmountFieldId = function(amountFieldId) { - this.options.amountFieldId = amountFieldId; - this.personalizedOptions[0].amountFieldId = amountFieldId; +ClickBusPayments.prototype.setAmountFieldClass = function(amountFieldClass) { + this.options.amountFieldClass = amountFieldClass; + this.personalizedOptions[0].amountFieldClass = amountFieldClass; this.updateForm(); this.start(); return this; }; -ClickBusPayments.prototype.setInstallmentFieldId = function(installmentFieldId) { - this.options.installmentFieldId = installmentFieldId; - this.personalizedOptions[0].installmentFieldId = installmentFieldId; +ClickBusPayments.prototype.setInstallmentFieldClass = function(installmentFieldClass) { + this.options.installmentFieldClass = installmentFieldClass; + this.personalizedOptions[0].installmentFieldClass = installmentFieldClass; this.updateForm(); this.start(); return this; @@ -161,27 +151,23 @@ ClickBusPayments.prototype.start = function() { ClickBusPayments.prototype.updateForm = function() { this.options = merge(this.options, this.personalizedOptions[0]); - for (var fieldId in this.options) { - if (this.optionalValues.hasOwnProperty(fieldId)) { + for (var fieldClass in this.options) { + if (this.optionalValues.hasOwnProperty(fieldClass)) { continue; } - var element = document.getElementById(this.options[fieldId]); - if (!element) { - var errorMessage = this.options[fieldId] + ' is required'; + var elements = document.getElementsByClassName(this.options[fieldClass]); + if (!elements) { + var errorMessage = this.options[fieldClass] + ' is required'; throw new Error(errorMessage); } - - if (this.attributeNames[fieldId]) { - element.setAttribute('data-checkout', this.attributeNames[fieldId]); - } } }; ClickBusPayments.prototype.generateToken = function(gatewayType) { var form = document.getElementById(this.options['paymentFormId']); - if (gatewayType == 'credit_card') { + if (gatewayType == 'credit_card' || gatewayType == 'debit_card') { this.successResponse.brand = this.getCardBrand(); } @@ -203,13 +189,28 @@ ClickBusPayments.prototype.generateToken = function(gatewayType) { return this.clickPromise; }; +ClickBusPayments.prototype.getElement = function(className) { + var elements = document.getElementsByClassName(className); + + if (elements.length == 0) { + return false; + } + + for (var index in elements) { + var element = elements[index]; + if (element.offsetWidth > 0 && element.offsetHeight) { + return element; + } + } +}; + ClickBusPayments.prototype.getCardBrand = function() { var brands = [{ name: 'visa', pattern: /^4/ }, { name: 'mastercard', - pattern: /^5[1-5][0-9][0-9]/ + pattern: /^5[0-5][0-9][0-9]/ }, { name: 'amex', pattern: /^3[47]/ @@ -237,70 +238,70 @@ ClickBusPayments.prototype.getCardBrand = function() { }; ClickBusPayments.prototype.getAmount = function() { - var amount = document.getElementById(this.options.amountFieldId); + var amount = this.getElement(this.options.amountFieldClass); if (!amount) { - throw new Error('amountFieldId is required'); + throw new Error('amountFieldClass is required'); } return amount.value.replace(/[ .]/g, ''); }; ClickBusPayments.prototype.getInstallment = function() { - var installment = document.getElementById(this.options.installmentFieldId); + var installment = this.getElement(this.options.installmentFieldClass); if (!installment) { - throw new Error('installmentFieldId is required'); + throw new Error('installmentFieldClass is required'); } return installment.value; }; ClickBusPayments.prototype.getHolderName = function() { - var holderName = document.getElementById(this.options.holderNameFieldId); + var holderName = this.getElement(this.options.holderNameFieldClass); if (!holderName) { - throw new Error('holderNameFieldId is required'); + throw new Error('holderNameFieldClass is required'); } return holderName.value; }; ClickBusPayments.prototype.getCreditCard = function() { - var ccNumber = document.getElementById(this.options.creditcardFieldId); + var ccNumber = this.getElement(this.options.creditcardFieldClass); if (!ccNumber) { - throw new Error('creditcardFieldId is required'); + throw new Error('creditcardFieldClass is required'); } return ccNumber.value.replace(/[ .-]/g, ''); }; ClickBusPayments.prototype.getExpirationMonth = function() { - var expirationMonth = document.getElementById(this.options.expirationMonthFieldId); + var expirationMonth = this.getElement(this.options.expirationMonthFieldClass); if (!expirationMonth) { - throw new Error('expirationMonthFieldId is required'); + throw new Error('expirationMonthFieldClass is required'); } return expirationMonth.value; }; ClickBusPayments.prototype.getExpirationYear = function() { - var expirationYear = document.getElementById(this.options.expirationYearFieldId); + var expirationYear = this.getElement(this.options.expirationYearFieldClass); if (!expirationYear) { - throw new Error('expirationYearFieldId is required'); + throw new Error('expirationYearFieldClass is required'); } return expirationYear.value; }; ClickBusPayments.prototype.getSecurityCode = function() { - var securityCode = document.getElementById(this.options.securityCodeFieldId); + var securityCode = this.getElement(this.options.securityCodeFieldClass); if (!securityCode) { - throw new Error('securityCodeFieldId is required'); + throw new Error('securityCodeFieldClass is required'); } return securityCode.value; From c3824ef0cc02c077ce905974786ecd6bd5822400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Mon, 3 Oct 2016 13:20:07 -0300 Subject: [PATCH 2/2] Add new range for mastercard --- clickbus-payments-min.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/clickbus-payments-min.js b/clickbus-payments-min.js index c671e8b..2875f08 100644 --- a/clickbus-payments-min.js +++ b/clickbus-payments-min.js @@ -1,5 +1 @@ -<<<<<<< 7c288c94dfc4adeb7483186b5188b00ca68ef8a4 -"use strict";function ClickPromise(t,e){this.callable=t,this.clickbusPayments=e,this.errorPromises=0,this.successPromises=0,this.totalPromises=0,this.callbackSuccess=function(){},this.callbackFail=function(){}}function ClickBusPayments(){this.options={paymentFormId:"payment_form",creditcardFieldId:"credit_card",securityCodeFieldId:"security_code",expirationMonthFieldId:"expiration_month",expirationYearFieldId:"expiration_year",holderNameFieldId:"holder_name",docTypeFieldId:"doc_type",docNumberFieldId:"doc_number",amountFieldId:"amount"},this.attributeNames={creditcardFieldId:"cardNumber",securityCodeFieldId:"securityCode",expirationMonthFieldId:"cardExpirationMonth",expirationYearFieldId:"cardExpirationYear",holderNameFieldId:"cardholderName",docTypeFieldId:"docType",docNumberFieldId:"docNumber"},this.optionalValues={amountFieldId:!1,installmentFieldId:!1},this.gateways=[],this.gatewayType=null,this.personalizedOptions=arguments,this.clickPromise=[],this.successResponse={},this.errorResponse={},this.updateForm()}function addEvent(t,e,i){t.addEventListener?t.addEventListener(e,i):t.attachEvent("on"+e,function(){i.call(t)})}function loadScript(t,e){var i=document.getElementsByTagName("head")[0],s=document.createElement("script");s.type="text/javascript",s.src=t,s.onreadystatechange=e,s.onload=e,i.appendChild(s)}function merge(t,e){if(!(arguments[0]&&arguments[0]instanceof Object))return t;for(var i in e)t[i]=e[i];return t}function MercadoPago(t){this.type="credit_card",this.name="mercadoPago",this.publicKey=t,this.gatewayUrl="https://secure.mlstatic.com/sdk/javascript/v1/mercadopago.js?nocache="+10*Math.random()}function MundiPagg(t,e){this.type="credit_card",this.name="mundipagg",this.publicKey=t}function Paypal(t,e){this.type="paypal",this.name="paypal",this.publicKey=t}ClickPromise.prototype.success=function(t){return this.callbackSuccess=t,this},ClickPromise.prototype.fail=function(t){return this.callbackFail=t,this},ClickPromise.prototype.call=function(){this.callable()},ClickPromise.prototype.finish=function(t,e){try{201==t||200==t?(this.successPromises++,this.clickbusPayments.successResponse.token[e.name]=e.content):(this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=e.cause)}catch(t){this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=[t]}finally{if(this.errorPromises==this.totalPromises)return void this.callbackFail(this.clickbusPayments.errorResponse);this.successPromises+this.errorPromises==this.totalPromises&&this.callbackSuccess(this.clickbusPayments.successResponse)}},ClickBusPayments.prototype.init=function(){this.start(),this.successResponse.token={}},ClickBusPayments.prototype.setPaymentFormId=function(t){return this.options.paymentFormId=t,this.personalizedOptions[0].paymentFormId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setCreditcardFieldId=function(t){return this.options.creditcardFieldId=t,this.personalizedOptions[0].creditcardFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setSecurityCodeFieldId=function(t){return this.options.securityCodeFieldId=t,this.personalizedOptions[0].securityCodeFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationMonthFieldId=function(t){return this.options.expirationMonthFieldId=t,this.personalizedOptions[0].expirationMonthFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationYearFieldId=function(t){return this.options.expirationYearFieldId=t,this.personalizedOptions[0].expirationYearFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setHolderNameFieldId=function(t){return this.options.holderNameFieldId=t,this.personalizedOptions[0].holderNameFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocTypeFieldId=function(t){return this.options.docTypeFieldId=t,this.personalizedOptions[0].docTypeFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocNumberFieldId=function(t){return this.options.docNumberFieldId=t,this.personalizedOptions[0].docNumberFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setAmountFieldId=function(t){return this.options.amountFieldId=t,this.personalizedOptions[0].amountFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setInstallmentFieldId=function(t){return this.options.installmentFieldId=t,this.personalizedOptions[0].installmentFieldId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.subscribe=function(t){this.gateways.push(t)},ClickBusPayments.prototype.start=function(){this.gateways.forEach(function(t){t.start()})},ClickBusPayments.prototype.updateForm=function(){this.options=merge(this.options,this.personalizedOptions[0]);for(var t in this.options)if(!this.optionalValues.hasOwnProperty(t)){var e=document.getElementById(this.options[t]);if(!e){var i=this.options[t]+" is required";throw new Error(i)}this.attributeNames[t]&&e.setAttribute("data-checkout",this.attributeNames[t])}},ClickBusPayments.prototype.generateToken=function(t){var e=document.getElementById(this.options.paymentFormId);return"credit_card"==t&&(this.successResponse.brand=this.getCardBrand()),this.gatewayType=t,this.clickPromise=new ClickPromise(function(){var i=this.clickbusPayments.gateways;i.forEach(function(i){i.type==t&&(this.totalPromises++,i.createToken(e,this))},this)},this),this.clickPromise},ClickBusPayments.prototype.getCardBrand=function(){var t=[{name:"visa",pattern:/^4/},{name:"mastercard",pattern:/^5[1-5][0-9][0-9]/},{name:"amex",pattern:/^3[47]/},{name:"diners",pattern:/^3(?:0[0-5]|[68][0-9])/},{name:"elo",pattern:/^401178|^401179|^431274|^438935|^451416|^457393|^457631|^457632|^504175|^627780|^636297|^636368|^(506699|5067[0-6]\d|50677[0-8])|^(50900\d|5090[1-9]\d|509[1-9]\d{2})|^65003[1-3]|^(65003[5-9]|65004\d|65005[0-1])|^(65040[5-9]|6504[1-3]\d)|^(65048[5-9]|65049\d|6505[0-2]\d|65053 [0-8])|^(65054[1-9]|6505[5-8]\d|65059[0-8])|^(65070\d|65071[0-8])|^65072[0-7]|^(65090[1-9]|65091\d|650920)|^(65165[2-9]|6516[6-7]\d)|^(65500\d|65501\d)|^(65502[1-9]|6550[3-4]\d|65505[0-8])/},{name:"hipercard",pattern:/^3841[046]0|^60/}],e=this.getCreditCard();for(var i in t){var s=t[i];if(e.match(s.pattern))return s.name}return null},ClickBusPayments.prototype.getAmount=function(){var t=document.getElementById(this.options.amountFieldId);if(!t)throw new Error("amountFieldId is required");return t.value.replace(/[ .]/g,"")},ClickBusPayments.prototype.getInstallment=function(){var t=document.getElementById(this.options.installmentFieldId);if(!t)throw new Error("installmentFieldId is required");return t.value},ClickBusPayments.prototype.getHolderName=function(){var t=document.getElementById(this.options.holderNameFieldId);if(!t)throw new Error("holderNameFieldId is required");return t.value},ClickBusPayments.prototype.getCreditCard=function(){var t=document.getElementById(this.options.creditcardFieldId);if(!t)throw new Error("creditcardFieldId is required");return t.value.replace(/[ .-]/g,"")},ClickBusPayments.prototype.getExpirationMonth=function(){var t=document.getElementById(this.options.expirationMonthFieldId);if(!t)throw new Error("expirationMonthFieldId is required");return t.value},ClickBusPayments.prototype.getExpirationYear=function(){var t=document.getElementById(this.options.expirationYearFieldId);if(!t)throw new Error("expirationYearFieldId is required");return t.value},ClickBusPayments.prototype.getSecurityCode=function(){var t=document.getElementById(this.options.securityCodeFieldId);if(!t)throw new Error("securityCodeFieldId is required");return t.value},MercadoPago.prototype.start=function(){loadScript(this.gatewayUrl,function(){Mercadopago.setPublishableKey(this.publicKey)}.bind(this))},MercadoPago.prototype.createToken=function(t,e){var i=e.clickbusPayments.successResponse.token;i.hasOwnProperty(this.name)&&this.clearSession(),Mercadopago.createToken(t,function(t,i){i.name=this.name,i.content=i.id,e.finish(t,i)}.bind(this))},MercadoPago.prototype.clearSession=function(){Mercadopago.clearSession()},MundiPagg.prototype.start=function(){},MundiPagg.prototype.clearSession=function(){},MundiPagg.prototype.createToken=function(t,e){var i=new XMLHttpRequest;i.open("POST","/payment/token/mundipagg"),i.onload=function(){var t=JSON.parse(i.response);return 201==i.status?void e.finish(i.status,{content:t.token,name:this.name}):void e.finish(i.status,{name:this.name,cause:t.ErrorReport})}.bind(this),i.onerror=function(){},i.send(JSON.stringify(this.formatRequest(e.clickbusPayments)))},MundiPagg.prototype.formatRequest=function(t){return{CreditCardBrand:t.getCardBrand(),CreditCardNumber:t.getCreditCard(),ExpMonth:t.getExpirationMonth(),ExpYear:t.getExpirationYear(),HolderName:t.getHolderName(),SecurityCode:t.getSecurityCode(),IsOneDollarAuthEnabled:!1}},Paypal.prototype.start=function(){},Paypal.prototype.clearSession=function(){},Paypal.prototype.createToken=function(t,e){var i=new XMLHttpRequest;i.open("GET","/payment/token/paypal"),i.onload=function(){var t=JSON.parse(i.response);return 200==i.status?void e.finish(i.status,{content:t,name:this.name}):void e.finish(i.status,{name:this.name,cause:t.ErrorReport})}.bind(this),i.onerror=function(){console.log(i)},i.send()}; -======= -"use strict";function ClickPromise(t,e){this.callable=t,this.clickbusPayments=e,this.errorPromises=0,this.successPromises=0,this.totalPromises=0,this.callbackSuccess=function(){},this.callbackFail=function(){}}function ClickBusPayments(){this.options={paymentFormId:"payment_form",creditcardFieldClass:"credit_card",securityCodeFieldClass:"security_code",expirationMonthFieldClass:"expiration_month",expirationYearFieldClass:"expiration_year",holderNameFieldClass:"holder_name",docTypeFieldClass:"doc_type",docNumberFieldClass:"doc_number",amountFieldClass:"amount"},this.optionalValues={amountFieldClass:!1,installmentFieldClass:!1},this.gateways=[],this.gatewayType=null,this.personalizedOptions=arguments,this.clickPromise=[],this.successResponse={},this.errorResponse={},this.updateForm()}function addEvent(t,e,s){t.addEventListener?t.addEventListener(e,s):t.attachEvent("on"+e,function(){s.call(t)})}function loadScript(t,e){var s=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.src=t,i.onreadystatechange=e,i.onload=e,s.appendChild(i)}function merge(t,e){if(!(arguments[0]&&arguments[0]instanceof Object))return t;for(var s in e)t[s]=e[s];return t}function MercadoPago(t){this.type="credit_card",this.name="mercadoPago",this.publicKey=t,this.gatewayUrl="https://secure.mlstatic.com/sdk/javascript/v1/mercadopago.js?nocache="+10*Math.random()}function MundiPagg(t,e){this.type="credit_card",this.name="mundipagg",this.publicKey=t}function Paypal(t,e){this.type="paypal",this.name="paypal",this.publicKey=t}function PayZen(){this.type="debit_card",this.name="payzen"}ClickPromise.prototype.success=function(t){return this.callbackSuccess=t,this},ClickPromise.prototype.fail=function(t){return this.callbackFail=t,this},ClickPromise.prototype.call=function(){this.callable()},ClickPromise.prototype.finish=function(t,e){try{201==t||200==t?(this.successPromises++,this.clickbusPayments.successResponse.token[e.name]=e.content):(this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=e.cause)}catch(t){this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=[t]}finally{if(this.errorPromises==this.totalPromises)return void this.callbackFail(this.clickbusPayments.errorResponse);this.successPromises+this.errorPromises==this.totalPromises&&this.callbackSuccess(this.clickbusPayments.successResponse)}},ClickBusPayments.prototype.init=function(){this.start(),this.successResponse.token={}},ClickBusPayments.prototype.setPaymentFormId=function(t){return this.options.paymentFormId=t,this.personalizedOptions[0].paymentFormId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setCreditcardFieldClass=function(t){return this.options.creditcardFieldClass=t,this.personalizedOptions[0].creditcardFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setSecurityCodeFieldClass=function(t){return this.options.securityCodeFieldClass=t,this.personalizedOptions[0].securityCodeFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationMonthFieldClass=function(t){return this.options.expirationMonthFieldClass=t,this.personalizedOptions[0].expirationMonthFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationYearFieldClass=function(t){return this.options.expirationYearFieldClass=t,this.personalizedOptions[0].expirationYearFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setHolderNameFieldClass=function(t){return this.options.holderNameFieldClass=t,this.personalizedOptions[0].holderNameFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocTypeFieldClass=function(t){return this.options.docTypeFieldClass=t,this.personalizedOptions[0].docTypeFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocNumberFieldClass=function(t){return this.options.docNumberFieldClass=t,this.personalizedOptions[0].docNumberFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setAmountFieldClass=function(t){return this.options.amountFieldClass=t,this.personalizedOptions[0].amountFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setInstallmentFieldClass=function(t){return this.options.installmentFieldClass=t,this.personalizedOptions[0].installmentFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.subscribe=function(t){this.gateways.push(t)},ClickBusPayments.prototype.start=function(){this.gateways.forEach(function(t){t.start()})},ClickBusPayments.prototype.updateForm=function(){this.options=merge(this.options,this.personalizedOptions[0]);for(var t in this.options)if(!this.optionalValues.hasOwnProperty(t)){var e=document.getElementsByClassName(this.options[t]);if(!e){var s=this.options[t]+" is required";throw new Error(s)}}},ClickBusPayments.prototype.generateToken=function(t){var e=document.getElementById(this.options.paymentFormId);return"credit_card"!=t&&"debit_card"!=t||(this.successResponse.brand=this.getCardBrand()),this.gatewayType=t,this.clickPromise=new ClickPromise(function(){var s=this.clickbusPayments.gateways;s.forEach(function(s){s.type==t&&(this.totalPromises++,s.createToken(e,this))},this)},this),this.clickPromise},ClickBusPayments.prototype.getElement=function(t){var e=document.getElementsByClassName(t);if(0==e.length)return!1;for(var s in e){var i=e[s];if(i.offsetWidth>0&&i.offsetHeight)return i}},ClickBusPayments.prototype.getCardBrand=function(){var t=[{name:"visa",pattern:/^4/},{name:"mastercard",pattern:/^5[0-5][0-9][0-9]/},{name:"amex",pattern:/^3[47]/},{name:"diners",pattern:/^3(?:0[0-5]|[68][0-9])/},{name:"elo",pattern:/^401178|^401179|^431274|^438935|^451416|^457393|^457631|^457632|^504175|^627780|^636297|^636368|^(506699|5067[0-6]\d|50677[0-8])|^(50900\d|5090[1-9]\d|509[1-9]\d{2})|^65003[1-3]|^(65003[5-9]|65004\d|65005[0-1])|^(65040[5-9]|6504[1-3]\d)|^(65048[5-9]|65049\d|6505[0-2]\d|65053 [0-8])|^(65054[1-9]|6505[5-8]\d|65059[0-8])|^(65070\d|65071[0-8])|^65072[0-7]|^(65090[1-9]|65091\d|650920)|^(65165[2-9]|6516[6-7]\d)|^(65500\d|65501\d)|^(65502[1-9]|6550[3-4]\d|65505[0-8])/},{name:"hipercard",pattern:/^3841[046]0|^60/}],e=this.getCreditCard();for(var s in t){var i=t[s];if(e.match(i.pattern))return i.name}return null},ClickBusPayments.prototype.getAmount=function(){var t=this.getElement(this.options.amountFieldClass);if(!t)throw new Error("amountFieldClass is required");return t.value.replace(/[ .]/g,"")},ClickBusPayments.prototype.getInstallment=function(){var t=this.getElement(this.options.installmentFieldClass);if(!t)throw new Error("installmentFieldClass is required");return t.value},ClickBusPayments.prototype.getHolderName=function(){var t=this.getElement(this.options.holderNameFieldClass);if(!t)throw new Error("holderNameFieldClass is required");return t.value},ClickBusPayments.prototype.getCreditCard=function(){var t=this.getElement(this.options.creditcardFieldClass);if(!t)throw new Error("creditcardFieldClass is required");return t.value.replace(/[ .-]/g,"")},ClickBusPayments.prototype.getExpirationMonth=function(){var t=this.getElement(this.options.expirationMonthFieldClass);if(!t)throw new Error("expirationMonthFieldClass is required");return t.value},ClickBusPayments.prototype.getExpirationYear=function(){var t=this.getElement(this.options.expirationYearFieldClass);if(!t)throw new Error("expirationYearFieldClass is required");return t.value},ClickBusPayments.prototype.getSecurityCode=function(){var t=this.getElement(this.options.securityCodeFieldClass);if(!t)throw new Error("securityCodeFieldClass is required");return t.value},MercadoPago.prototype.start=function(){loadScript(this.gatewayUrl,function(){Mercadopago.setPublishableKey(this.publicKey)}.bind(this))},MercadoPago.prototype.createToken=function(t,e){var s=e.clickbusPayments.successResponse.token;s.hasOwnProperty(this.name)&&this.clearSession(),Mercadopago.createToken(t,function(t,s){s.name=this.name,s.content=s.id,e.finish(t,s)}.bind(this))},MercadoPago.prototype.clearSession=function(){Mercadopago.clearSession()},MundiPagg.prototype.start=function(){},MundiPagg.prototype.clearSession=function(){},MundiPagg.prototype.createToken=function(t,e){var s=new XMLHttpRequest;s.open("POST","/payment/token/mundipagg"),s.onload=function(){var t=JSON.parse(s.response);return 201==s.status?void e.finish(s.status,{content:t.token,name:this.name}):void e.finish(s.status,{name:this.name,cause:t.ErrorReport})}.bind(this),s.onerror=function(){},s.send(JSON.stringify(this.formatRequest(e.clickbusPayments)))},MundiPagg.prototype.formatRequest=function(t){return{CreditCardTransactionCollection:[{AmountInCents:t.getAmount(),CreditCard:{CreditCardBrand:t.getCardBrand(),CreditCardNumber:t.getCreditCard(),ExpMonth:t.getExpirationMonth(),ExpYear:t.getExpirationYear(),HolderName:t.getHolderName(),SecurityCode:t.getSecurityCode()},CreditCardOperation:"AuthOnly",InstallmentCount:t.getInstallment()}]}},Paypal.prototype.start=function(){},Paypal.prototype.clearSession=function(){},Paypal.prototype.createToken=function(t,e){var s=new XMLHttpRequest;s.open("GET","/payment/token/paypal"),s.onload=function(){var t=JSON.parse(s.response);return 200==s.status?void e.finish(s.status,{content:t,name:this.name}):void e.finish(s.status,{name:this.name,cause:t.ErrorReport})}.bind(this),s.onerror=function(){console.log(s)},s.send()},PayZen.prototype.start=function(){},PayZen.prototype.clearSession=function(){},PayZen.prototype.createToken=function(t,e){var s=new XMLHttpRequest;s.open("POST","/payment/token/debit_card"),s.onload=function(){var t=JSON.parse(s.response);return 200==s.status?void e.finish(s.status,{content:t,name:this.name}):void e.finish(s.status,{name:this.name,cause:t.ErrorReport})}.bind(this),s.onerror=function(){},s.send(JSON.stringify(this.formatRequest(e.clickbusPayments)))},PayZen.prototype.formatRequest=function(t){return{paymentName:this.name,cardBrand:t.getCardBrand(),cardNumber:t.getCreditCard(),cardExpirationMonth:t.getExpirationMonth(),cardExpirationYear:parseInt(t.getExpirationYear(),10)+2e3,cardSecurityCode:t.getSecurityCode()}}; ->>>>>>> Use class to set name for inputs instead of id +"use strict";function ClickPromise(t,e){this.callable=t,this.clickbusPayments=e,this.errorPromises=0,this.successPromises=0,this.totalPromises=0,this.callbackSuccess=function(){},this.callbackFail=function(){}}function ClickBusPayments(){this.options={paymentFormId:"payment_form",creditcardFieldClass:"credit_card",securityCodeFieldClass:"security_code",expirationMonthFieldClass:"expiration_month",expirationYearFieldClass:"expiration_year",holderNameFieldClass:"holder_name",docTypeFieldClass:"doc_type",docNumberFieldClass:"doc_number",amountFieldClass:"amount"},this.optionalValues={amountFieldClass:!1,installmentFieldClass:!1},this.gateways=[],this.gatewayType=null,this.personalizedOptions=arguments,this.clickPromise=[],this.successResponse={},this.errorResponse={},this.updateForm()}function addEvent(t,e,s){t.addEventListener?t.addEventListener(e,s):t.attachEvent("on"+e,function(){s.call(t)})}function loadScript(t,e){var s=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.src=t,i.onreadystatechange=e,i.onload=e,s.appendChild(i)}function merge(t,e){if(!(arguments[0]&&arguments[0]instanceof Object))return t;for(var s in e)t[s]=e[s];return t}function MercadoPago(t){this.type="credit_card",this.name="mercadoPago",this.publicKey=t,this.gatewayUrl="https://secure.mlstatic.com/sdk/javascript/v1/mercadopago.js?nocache="+10*Math.random()}function MundiPagg(t,e){this.type="credit_card",this.name="mundipagg",this.publicKey=t}function Paypal(t,e){this.type="paypal",this.name="paypal",this.publicKey=t}function PayZen(){this.type="debit_card",this.name="payzen"}ClickPromise.prototype.success=function(t){return this.callbackSuccess=t,this},ClickPromise.prototype.fail=function(t){return this.callbackFail=t,this},ClickPromise.prototype.call=function(){this.callable()},ClickPromise.prototype.finish=function(t,e){try{201==t||200==t?(this.successPromises++,this.clickbusPayments.successResponse.token[e.name]=e.content):(this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=e.cause)}catch(t){this.errorPromises++,this.clickbusPayments.errorResponse[e.name]=[t]}finally{if(this.errorPromises==this.totalPromises)return void this.callbackFail(this.clickbusPayments.errorResponse);this.successPromises+this.errorPromises==this.totalPromises&&this.callbackSuccess(this.clickbusPayments.successResponse)}},ClickBusPayments.prototype.init=function(){this.start(),this.successResponse.token={}},ClickBusPayments.prototype.setPaymentFormId=function(t){return this.options.paymentFormId=t,this.personalizedOptions[0].paymentFormId=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setCreditcardFieldClass=function(t){return this.options.creditcardFieldClass=t,this.personalizedOptions[0].creditcardFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setSecurityCodeFieldClass=function(t){return this.options.securityCodeFieldClass=t,this.personalizedOptions[0].securityCodeFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationMonthFieldClass=function(t){return this.options.expirationMonthFieldClass=t,this.personalizedOptions[0].expirationMonthFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setExpirationYearFieldClass=function(t){return this.options.expirationYearFieldClass=t,this.personalizedOptions[0].expirationYearFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setHolderNameFieldClass=function(t){return this.options.holderNameFieldClass=t,this.personalizedOptions[0].holderNameFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocTypeFieldClass=function(t){return this.options.docTypeFieldClass=t,this.personalizedOptions[0].docTypeFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setDocNumberFieldClass=function(t){return this.options.docNumberFieldClass=t,this.personalizedOptions[0].docNumberFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setAmountFieldClass=function(t){return this.options.amountFieldClass=t,this.personalizedOptions[0].amountFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.setInstallmentFieldClass=function(t){return this.options.installmentFieldClass=t,this.personalizedOptions[0].installmentFieldClass=t,this.updateForm(),this.start(),this},ClickBusPayments.prototype.subscribe=function(t){this.gateways.push(t)},ClickBusPayments.prototype.start=function(){this.gateways.forEach(function(t){t.start()})},ClickBusPayments.prototype.updateForm=function(){this.options=merge(this.options,this.personalizedOptions[0]);for(var t in this.options)if(!this.optionalValues.hasOwnProperty(t)){var e=document.getElementsByClassName(this.options[t]);if(!e){var s=this.options[t]+" is required";throw new Error(s)}}},ClickBusPayments.prototype.generateToken=function(t){var e=document.getElementById(this.options.paymentFormId);return"credit_card"!=t&&"debit_card"!=t||(this.successResponse.brand=this.getCardBrand()),this.gatewayType=t,this.clickPromise=new ClickPromise(function(){var s=this.clickbusPayments.gateways;s.forEach(function(s){s.type==t&&(this.totalPromises++,s.createToken(e,this))},this)},this),this.clickPromise},ClickBusPayments.prototype.getElement=function(t){var e=document.getElementsByClassName(t);if(0==e.length)return!1;for(var s in e){var i=e[s];if(i.offsetWidth>0&&i.offsetHeight)return i}},ClickBusPayments.prototype.getCardBrand=function(){var t=[{name:"visa",pattern:/^4/},{name:"mastercard",pattern:/^5[0-5][0-9][0-9]/},{name:"amex",pattern:/^3[47]/},{name:"diners",pattern:/^3(?:0[0-5]|[68][0-9])/},{name:"elo",pattern:/^401178|^401179|^431274|^438935|^451416|^457393|^457631|^457632|^504175|^627780|^636297|^636368|^(506699|5067[0-6]\d|50677[0-8])|^(50900\d|5090[1-9]\d|509[1-9]\d{2})|^65003[1-3]|^(65003[5-9]|65004\d|65005[0-1])|^(65040[5-9]|6504[1-3]\d)|^(65048[5-9]|65049\d|6505[0-2]\d|65053 [0-8])|^(65054[1-9]|6505[5-8]\d|65059[0-8])|^(65070\d|65071[0-8])|^65072[0-7]|^(65090[1-9]|65091\d|650920)|^(65165[2-9]|6516[6-7]\d)|^(65500\d|65501\d)|^(65502[1-9]|6550[3-4]\d|65505[0-8])/},{name:"hipercard",pattern:/^3841[046]0|^60/}],e=this.getCreditCard();for(var s in t){var i=t[s];if(e.match(i.pattern))return i.name}return null},ClickBusPayments.prototype.getAmount=function(){var t=this.getElement(this.options.amountFieldClass);if(!t)throw new Error("amountFieldClass is required");return t.value.replace(/[ .]/g,"")},ClickBusPayments.prototype.getInstallment=function(){var t=this.getElement(this.options.installmentFieldClass);if(!t)throw new Error("installmentFieldClass is required");return t.value},ClickBusPayments.prototype.getHolderName=function(){var t=this.getElement(this.options.holderNameFieldClass);if(!t)throw new Error("holderNameFieldClass is required");return t.value},ClickBusPayments.prototype.getCreditCard=function(){var t=this.getElement(this.options.creditcardFieldClass);if(!t)throw new Error("creditcardFieldClass is required");return t.value.replace(/[ .-]/g,"")},ClickBusPayments.prototype.getExpirationMonth=function(){var t=this.getElement(this.options.expirationMonthFieldClass);if(!t)throw new Error("expirationMonthFieldClass is required");return t.value},ClickBusPayments.prototype.getExpirationYear=function(){var t=this.getElement(this.options.expirationYearFieldClass);if(!t)throw new Error("expirationYearFieldClass is required");return t.value},ClickBusPayments.prototype.getSecurityCode=function(){var t=this.getElement(this.options.securityCodeFieldClass);if(!t)throw new Error("securityCodeFieldClass is required");return t.value},MercadoPago.prototype.start=function(){loadScript(this.gatewayUrl,function(){Mercadopago.setPublishableKey(this.publicKey)}.bind(this))},MercadoPago.prototype.createToken=function(t,e){var s=e.clickbusPayments.successResponse.token;s.hasOwnProperty(this.name)&&this.clearSession(),Mercadopago.createToken(t,function(t,s){s.name=this.name,s.content=s.id,e.finish(t,s)}.bind(this))},MercadoPago.prototype.clearSession=function(){Mercadopago.clearSession()},MundiPagg.prototype.start=function(){},MundiPagg.prototype.clearSession=function(){},MundiPagg.prototype.createToken=function(t,e){var s=new XMLHttpRequest;s.open("POST","/payment/token/mundipagg"),s.onload=function(){var t=JSON.parse(s.response);return 201==s.status?void e.finish(s.status,{content:t.token,name:this.name}):void e.finish(s.status,{name:this.name,cause:t.ErrorReport})}.bind(this),s.onerror=function(){},s.send(JSON.stringify(this.formatRequest(e.clickbusPayments)))},MundiPagg.prototype.formatRequest=function(t){return{CreditCardBrand:t.getCardBrand(),CreditCardNumber:t.getCreditCard(),ExpMonth:t.getExpirationMonth(),ExpYear:t.getExpirationYear(),HolderName:t.getHolderName(),SecurityCode:t.getSecurityCode(),IsOneDollarAuthEnabled:!1}},Paypal.prototype.start=function(){},Paypal.prototype.clearSession=function(){},Paypal.prototype.createToken=function(t,e){var s=new XMLHttpRequest;s.open("GET","/payment/token/paypal"),s.onload=function(){var t=JSON.parse(s.response);return 200==s.status?void e.finish(s.status,{content:t,name:this.name}):void e.finish(s.status,{name:this.name,cause:t.ErrorReport})}.bind(this),s.onerror=function(){console.log(s)},s.send()},PayZen.prototype.start=function(){},PayZen.prototype.clearSession=function(){},PayZen.prototype.createToken=function(t,e){var s=new XMLHttpRequest;s.open("POST","/payment/token/debit_card"),s.onload=function(){var t=JSON.parse(s.response);return 200==s.status?void e.finish(s.status,{content:t,name:this.name}):void e.finish(s.status,{name:this.name,cause:t.ErrorReport})}.bind(this),s.onerror=function(){},s.send(JSON.stringify(this.formatRequest(e.clickbusPayments)))},PayZen.prototype.formatRequest=function(t){return{paymentName:this.name,cardBrand:t.getCardBrand(),cardNumber:t.getCreditCard(),cardExpirationMonth:t.getExpirationMonth(),cardExpirationYear:parseInt(t.getExpirationYear(),10)+2e3,cardSecurityCode:t.getSecurityCode()}}; \ No newline at end of file