Skip to content

Commit

Permalink
Merge pull request #39 from RocketBus/fix/SWEB-370-token-error
Browse files Browse the repository at this point in the history
#SWEB-370 - Loading "Enviando seu pagamento" fica eternamente com erro na geração de token
  • Loading branch information
guilhermeestrela authored Oct 10, 2019
2 parents 781bd4b + 5734c85 commit b1a0a4a
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 41 deletions.
2 changes: 1 addition & 1 deletion clickbus-payments-min.js

Large diffs are not rendered by default.

52 changes: 32 additions & 20 deletions clickbus-payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
/**
* Created by tiagobutzke on 7/2/15.
*/


var ERROR_TEXT = 'error';
var TYPE_CREDIT_CARD = 'credit_card';
var TYPE_DEBIT_CARD = 'debit_card';

function ClickPromise(callable, clickbusPayments) {
this.callable = callable;
this.clickbusPayments = clickbusPayments;
Expand Down Expand Up @@ -39,7 +45,7 @@ ClickPromise.prototype.finish = function(status, response) {
this.clickbusPayments.successResponse[response.type]['token'] = {};
}

if ((response.type == 'credit_card' && !response.oneClickPayment) || response.type == 'debit_card') {
if ((response.type == TYPE_CREDIT_CARD && !response.oneClickPayment) || response.type == TYPE_DEBIT_CARD) {
this.clickbusPayments.successResponse[response.type]['brand'] = this.clickbusPayments.getCardBrand();
}

Expand Down Expand Up @@ -93,10 +99,11 @@ ClickPromise.prototype.finish = function(status, response) {
* @param {Object} options
* @api public
*/

function ClickBusPayments() {
this.options = {
paymentFormId: "payment_form",
creditcardFieldClass: "credit_card",
creditcardFieldClass: TYPE_CREDIT_CARD,
securityCodeFieldClass: "security_code",
expirationMonthFieldClass: "expiration_month",
expirationYearFieldClass: "expiration_year",
Expand Down Expand Up @@ -540,7 +547,7 @@ function merge(primary, secundary) {
"use strict";

function MercadoPago(publicKey, customName) {
this.type = 'credit_card';
this.type = TYPE_CREDIT_CARD;
this.name = 'mercadoPago';
this.customName = customName;

Expand Down Expand Up @@ -635,9 +642,11 @@ MercadoPago.prototype.reset = function() {

"use strict";

var MUNDIPAGG_NAME = 'mundipagg';

function MundiPagg(publicKey, isTest) {
this.type = 'credit_card';
this.name = 'mundipagg';
this.type = TYPE_CREDIT_CARD;
this.name = MUNDIPAGG_NAME;

this.gatewayUrl = "https://api.mundipagg.com/core/v1/tokens?appId="+publicKey;
}
Expand All @@ -662,18 +671,18 @@ MundiPagg.prototype.createToken = function(form, clickPromise, options) {
request.open('POST', this.gatewayUrl);
request.setRequestHeader("Content-Type", "application/json");
request.onload = function() {
var response = JSON.parse(request.response);

if (request.status == 200) {
var response = JSON.parse(request.response);

clickPromise.finish(request.status, {content: response.id, type: this.type, name: this.name});
return;
}

clickPromise.finish(request.status, {name: this.name, type: this.type, cause: response.ErrorReport});
clickPromise.finish(request.status, {name: this.name, type: this.type, cause: ERROR_TEXT});
}.bind(this);

request.onerror = function() {
clickPromise.finish(request.status, {name: this.name, cause: 'error'});
clickPromise.finish(request.status, {name: MUNDIPAGG_NAME, cause: ERROR_TEXT, type: TYPE_CREDIT_CARD});
};

request.send(JSON.stringify(this.formatRequest(clickPromise.clickbusPayments)));
Expand Down Expand Up @@ -719,9 +728,11 @@ EletronicFundsTransfer.prototype.createToken = function() { };

"use strict";

var PAYPAL_NAME = 'paypal';

function Paypal(publicKey, isTest) {
this.type = 'paypal';
this.name = 'paypal';
this.type = PAYPAL_NAME;
this.name = PAYPAL_NAME;
this.publicKey = publicKey;
}

Expand All @@ -733,28 +744,29 @@ Paypal.prototype.createToken = function(form, clickPromise) {
var request = new XMLHttpRequest();
request.open('GET', '/payment/token/paypal');
request.onload = function() {
var response = JSON.parse(request.response);
if (request.status == 200) {
var response = JSON.parse(request.response);
clickPromise.finish(request.status, {content: response, type: this.type, name: this.name});
return;
}

clickPromise.finish(request.status, {name: this.name, cause: response.ErrorReport});
clickPromise.finish(request.status, {name: this.name, cause: ERROR_TEXT, type: this.type});
}.bind(this);

request.onerror = function() {
console.log(request);
clickPromise.finish(request.status, {name: this.name, cause: 'error'});
clickPromise.finish(request.status, {name: PAYPAL_NAME, cause: ERROR_TEXT, type: PAYPAL_NAME});
};

request.send();
};

"use strict";

var PAYZEN_NAME = 'payzen';

function PayZen() {
this.type = 'debit_card';
this.name = 'payzen';
this.type = TYPE_DEBIT_CARD;
this.name = PAYZEN_NAME;
}

PayZen.prototype.start = function() { };
Expand All @@ -765,17 +777,17 @@ 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) {
var response = JSON.parse(request.response);
clickPromise.finish(request.status, {content: response, type: this.type, name: this.name});
return;
}

clickPromise.finish(request.status, {name: this.name, cause: response.ErrorReport});
clickPromise.finish(request.status, {name: this.name, cause: ERROR_TEXT, type: this.type});
}.bind(this);

request.onerror = function() {
clickPromise.finish(request.status, {name: this.name, cause: 'error'});
clickPromise.finish(request.status, {name: PAYZEN_NAME, cause: ERROR_TEXT, type: TYPE_DEBIT_CARD});
};

request.send(JSON.stringify(this.formatRequest(clickPromise.clickbusPayments)));
Expand Down
8 changes: 7 additions & 1 deletion src/click-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
/**
* Created by tiagobutzke on 7/2/15.
*/


var ERROR_TEXT = 'error';
var TYPE_CREDIT_CARD = 'credit_card';
var TYPE_DEBIT_CARD = 'debit_card';

function ClickPromise(callable, clickbusPayments) {
this.callable = callable;
this.clickbusPayments = clickbusPayments;
Expand Down Expand Up @@ -39,7 +45,7 @@ ClickPromise.prototype.finish = function(status, response) {
this.clickbusPayments.successResponse[response.type]['token'] = {};
}

if ((response.type == 'credit_card' && !response.oneClickPayment) || response.type == 'debit_card') {
if ((response.type == TYPE_CREDIT_CARD && !response.oneClickPayment) || response.type == TYPE_DEBIT_CARD) {
this.clickbusPayments.successResponse[response.type]['brand'] = this.clickbusPayments.getCardBrand();
}

Expand Down
2 changes: 1 addition & 1 deletion src/gateways/mercadopago.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

function MercadoPago(publicKey, customName) {
this.type = 'credit_card';
this.type = TYPE_CREDIT_CARD;
this.name = 'mercadoPago';
this.customName = customName;

Expand Down
14 changes: 8 additions & 6 deletions src/gateways/mundipagg.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use strict";

var MUNDIPAGG_NAME = 'mundipagg';

function MundiPagg(publicKey, isTest) {
this.type = 'credit_card';
this.name = 'mundipagg';
this.type = TYPE_CREDIT_CARD;
this.name = MUNDIPAGG_NAME;

this.gatewayUrl = "https://api.mundipagg.com/core/v1/tokens?appId="+publicKey;
}
Expand All @@ -27,18 +29,18 @@ MundiPagg.prototype.createToken = function(form, clickPromise, options) {
request.open('POST', this.gatewayUrl);
request.setRequestHeader("Content-Type", "application/json");
request.onload = function() {
var response = JSON.parse(request.response);

if (request.status == 200) {
var response = JSON.parse(request.response);

clickPromise.finish(request.status, {content: response.id, type: this.type, name: this.name});
return;
}

clickPromise.finish(request.status, {name: this.name, type: this.type, cause: response.ErrorReport});
clickPromise.finish(request.status, {name: this.name, type: this.type, cause: ERROR_TEXT});
}.bind(this);

request.onerror = function() {
clickPromise.finish(request.status, {name: this.name, cause: 'error'});
clickPromise.finish(request.status, {name: MUNDIPAGG_NAME, cause: ERROR_TEXT, type: TYPE_CREDIT_CARD});
};

request.send(JSON.stringify(this.formatRequest(clickPromise.clickbusPayments)));
Expand Down
13 changes: 7 additions & 6 deletions src/gateways/paypal.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use strict";

var PAYPAL_NAME = 'paypal';

function Paypal(publicKey, isTest) {
this.type = 'paypal';
this.name = 'paypal';
this.type = PAYPAL_NAME;
this.name = PAYPAL_NAME;
this.publicKey = publicKey;
}

Expand All @@ -14,18 +16,17 @@ Paypal.prototype.createToken = function(form, clickPromise) {
var request = new XMLHttpRequest();
request.open('GET', '/payment/token/paypal');
request.onload = function() {
var response = JSON.parse(request.response);
if (request.status == 200) {
var response = JSON.parse(request.response);
clickPromise.finish(request.status, {content: response, type: this.type, name: this.name});
return;
}

clickPromise.finish(request.status, {name: this.name, cause: response.ErrorReport});
clickPromise.finish(request.status, {name: this.name, cause: ERROR_TEXT, type: this.type});
}.bind(this);

request.onerror = function() {
console.log(request);
clickPromise.finish(request.status, {name: this.name, cause: 'error'});
clickPromise.finish(request.status, {name: PAYPAL_NAME, cause: ERROR_TEXT, type: PAYPAL_NAME});
};

request.send();
Expand Down
12 changes: 7 additions & 5 deletions src/gateways/payzen.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use strict";

var PAYZEN_NAME = 'payzen';

function PayZen() {
this.type = 'debit_card';
this.name = 'payzen';
this.type = TYPE_DEBIT_CARD;
this.name = PAYZEN_NAME;
}

PayZen.prototype.start = function() { };
Expand All @@ -13,17 +15,17 @@ 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) {
var response = JSON.parse(request.response);
clickPromise.finish(request.status, {content: response, type: this.type, name: this.name});
return;
}

clickPromise.finish(request.status, {name: this.name, cause: response.ErrorReport});
clickPromise.finish(request.status, {name: this.name, cause: ERROR_TEXT, type: this.type});
}.bind(this);

request.onerror = function() {
clickPromise.finish(request.status, {name: this.name, cause: 'error'});
clickPromise.finish(request.status, {name: PAYZEN_NAME, cause: ERROR_TEXT, type: TYPE_DEBIT_CARD});
};

request.send(JSON.stringify(this.formatRequest(clickPromise.clickbusPayments)));
Expand Down
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
* @param {Object} options
* @api public
*/

function ClickBusPayments() {
this.options = {
paymentFormId: "payment_form",
creditcardFieldClass: "credit_card",
creditcardFieldClass: TYPE_CREDIT_CARD,
securityCodeFieldClass: "security_code",
expirationMonthFieldClass: "expiration_month",
expirationYearFieldClass: "expiration_year",
Expand Down

0 comments on commit b1a0a4a

Please sign in to comment.