-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpagseguro.js
143 lines (125 loc) · 4.08 KB
/
pagseguro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
$("document").ready(function(){
getAllPaymentMethods();
});
function getAllPaymentMethods() {
PagSeguroDirectPayment.getPaymentMethods({
amount: totalPurchase,
success: function(response) {
console.log(response);
drawPaymentsMethodsView(response.paymentMethods);
},
error: function(response) {
console.log(response);
},
complete: function(response) {
console.log(response);
}
});
}
$(document).on('keyup', '#cardNumber', function () {
getBrand();
if ($('#cardNumber').val().length == 0) {
$('#brand').empty();
}
});
function getBrand() {
let value = $('#cardNumber').val();
if (value.length >= 6) {
PagSeguroDirectPayment.getBrand({
cardBin: value.substr(0, 6),
success: function(response) {
let brand = '<img src="https://stc.pagseguro.uol.com.br/public/img/payment-methods-flags/68x30/'+ response.brand.name + '.png">';
$('#brand').html('Bandeira: ' + brand);
$("#cardBrand").val(response.brand.name);
getInstallments(totalPurchase);
},
error: function(err) {
//console.log('error', err);
},
complete: function(response) {
//console.log('complete', response);
}
});
}
}
function getInstallments(amount) {
let brand = $("#cardBrand").val();
$("#installmentsArea").html("<h4>Carregando opções de parcelamento...</h4>");
PagSeguroDirectPayment.getInstallments({
amount: amount,
maxInstallmentNoInterest: 12,
brand: brand,
success: function(s) {
//console.log(s);
},
error: function(e) {
//console.log(e);
},
complete: function(s) {
//console.log(s);
let installmentsDraw = drawInstallments(s.installments[brand]);
$("#installmentsArea").html(installmentsDraw);
}
});
}
$(document).on('click', '#pay',function () {
// $(this).addClass('disabled');
let method = $(this).attr('href');
method = method.replace('#', '');
if(method == 'CREDIT_CARD') {
PagSeguroDirectPayment.createCardToken({
cardNumber: $('#cardNumber').val(),
brand: $('#cardBrand').val(),
cvv: $('#cardCVV').val(),
expirationMonth: $('#cardMonth').val(),
expirationYear: $('#cardYear').val(),
success: function (response) {
document.querySelector('input[name=card_token]').value = response.card.token;
document.querySelector('input[name=hash]').value = PagSeguroDirectPayment.getSenderHash()
//makePurchase(response.card.token, method);
},
error: function(d) {
console.log(d);
}
});
} else {
makePurchase('', method);
}
return false;
});
function makePurchase(token, method) {
$(".loading").show();
let data = {
method: method,
hash: PagSeguroDirectPayment.getSenderHash()
};
if(method == 'CREDIT_CARD') {
data.token = token;
data.installments = $("#installmentsSelect").val();
}
data.items = [
{'name': 'Teste Check', 'price': 240, 'id': 1}
];
$.ajax({
type: "POST",
url: mainUrl + 'checkout/',
data: $.param(data),
dataType: 'json',
headers: {
'Authorization': 'Bearer ' + tokenMin
},
success: function (response) {
$(".loading").hide();
if(response.msg) {
$(".msg").html('<div class="alert alert-success">Compra efetuada com sucesso!</div>');
// window.location.href = mainUrl + 'payments/success';
} else {
$(".msg").html('<div class="alert alert-danger">Erro ao realizar transação!</div>');
$('#pay').removeClass('disabled');
}
},
error: function(d) {
console.log(d);
}
});
}