From 5f8cccd8147e6aeeca628606d3562839ccf1bbcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Tue, 4 Oct 2016 17:28:56 -0300 Subject: [PATCH] Change payload for mundipagg token generate --- clickbus-payments-min.js | 2 +- clickbus-payments.js | 22 +++++++--------------- src/gateways/mundipagg.js | 22 +++++++--------------- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/clickbus-payments-min.js b/clickbus-payments-min.js index 7491aef..f80ce0e 100644 --- a/clickbus-payments-min.js +++ b/clickbus-payments-min.js @@ -1 +1 @@ -"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{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 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 +"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 diff --git a/clickbus-payments.js b/clickbus-payments.js index 16994aa..3c241f9 100644 --- a/clickbus-payments.js +++ b/clickbus-payments.js @@ -477,21 +477,13 @@ MundiPagg.prototype.createToken = function(form, clickPromise) { MundiPagg.prototype.formatRequest = function(clickbusPayments) { return { - CreditCardTransactionCollection: [ - { - AmountInCents: clickbusPayments.getAmount(), - CreditCard: { - CreditCardBrand: clickbusPayments.getCardBrand(), - CreditCardNumber: clickbusPayments.getCreditCard(), - ExpMonth: clickbusPayments.getExpirationMonth(), - ExpYear: clickbusPayments.getExpirationYear(), - HolderName: clickbusPayments.getHolderName(), - SecurityCode: clickbusPayments.getSecurityCode() - }, - CreditCardOperation: "AuthOnly", - InstallmentCount: clickbusPayments.getInstallment() - } - ] + CreditCardBrand: clickbusPayments.getCardBrand(), + CreditCardNumber: clickbusPayments.getCreditCard(), + ExpMonth: clickbusPayments.getExpirationMonth(), + ExpYear: clickbusPayments.getExpirationYear(), + HolderName: clickbusPayments.getHolderName(), + SecurityCode: clickbusPayments.getSecurityCode(), + IsOneDollarAuthEnabled: false } }; diff --git a/src/gateways/mundipagg.js b/src/gateways/mundipagg.js index ab5708b..81eb971 100644 --- a/src/gateways/mundipagg.js +++ b/src/gateways/mundipagg.js @@ -31,20 +31,12 @@ MundiPagg.prototype.createToken = function(form, clickPromise) { MundiPagg.prototype.formatRequest = function(clickbusPayments) { return { - CreditCardTransactionCollection: [ - { - AmountInCents: clickbusPayments.getAmount(), - CreditCard: { - CreditCardBrand: clickbusPayments.getCardBrand(), - CreditCardNumber: clickbusPayments.getCreditCard(), - ExpMonth: clickbusPayments.getExpirationMonth(), - ExpYear: clickbusPayments.getExpirationYear(), - HolderName: clickbusPayments.getHolderName(), - SecurityCode: clickbusPayments.getSecurityCode() - }, - CreditCardOperation: "AuthOnly", - InstallmentCount: clickbusPayments.getInstallment() - } - ] + CreditCardBrand: clickbusPayments.getCardBrand(), + CreditCardNumber: clickbusPayments.getCreditCard(), + ExpMonth: clickbusPayments.getExpirationMonth(), + ExpYear: clickbusPayments.getExpirationYear(), + HolderName: clickbusPayments.getHolderName(), + SecurityCode: clickbusPayments.getSecurityCode(), + IsOneDollarAuthEnabled: false } };