-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexemplo-ilustrativo
216 lines (188 loc) · 6.57 KB
/
exemplo-ilustrativo
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
$url = "https://ws.sandbox.pagseguro.uol.com.br/v2/sessions";
$credenciais = array(
"email" => "xxx",
"token" => "xxx"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($credenciais));
$resultado = curl_exec($curl);
curl_close($curl);
$session = simplexml_load_string($resultado)->id;
?>
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>Checkout Transparente PagSeguro</title>
<script type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="text-center">
<img src="https://assets.pagseguro.com.br/ps-website-assets/v13.13.22/img/for-your-business/online/checkout/banner-image.png" class="img-fluid" alt="Responsive image">
<h1 class = "text-center">CHECKOUT TRANSPARENTE DEMO - PAGSEGURO [SANDBOX]</h1><hr>
</div>
<fieldset>
<div class="row mx-md-n5">
<div class="col px-md-5"><div class="p-3 border bg-light">
<legend class="text-center">GERAR SENDERHASH</legend>
<div>
<input class="form-control" type="text" id="senderHash" class="creditcard" name="senderHash">
<button class="btn btn-info" id="generateSenderHash">Gerar</button>
</div> </div> </div>
</fieldset>
<br>
<fieldset>
<legend class="text-center">CHAMADAS PARA CARTÃO DE CRÉDITO</legend>
<div class="row mx-md-n5">
<div class="col px-md-5"><div class="p-3 border bg-light">
<div class="row">
<div class="col-sm-4">
<div>
<label for="creditCardNumber"<b>Número do cartão:</b></label>
<input type="text" class="form-control" id="creditCardNumber" class="creditcard" name="creditCardNumber">
</div>
<div>
<label for="creditCardBrand"<b>Bandeira:</b></label>
<input type="text" class="form-control" id="creditCardBrand" class="creditcard" name="creditCardBrand" disabled>
</div></div>
<div class="col-sm-4">
<label for="creditCardExpMonth"<b>Validade Mês (mm):</b></label>
<input type="text" class="form-control" id="creditCardExpMonth" class="creditcard" name="creditCardExpMonth" size="2">
<label for="creditCardExpYear"<b>Ano (yyyy):</b></label>
<input type="text" class="form-control" id="creditCardExpYear" class="creditcard" name="creditCardExpYear" size="4">
</div>
<div class="col-sm-4">
<label for="creditCardCvv"<b>CVV:</b></label>
<input type="text" class="form-control" id="creditCardCvv" class="creditcard" name="creditCardCvv">
<label <b>Token:</b></label>
<input type="text" id="creditCardToken" class="form-control" name="creditCardToken" disabled>
<button class="btn btn-info" id="generateCreditCardToken">Gerar Token</button>
</div>
</div>
</div></div>
</fieldset>
<br>
<fieldset>
<legend class="text-center">PARCELAMENTO</legend>
<div class="row mx-md-n5">
<div class="col px-md-5"><div class="p-3 border bg-light">
Valor do Checkout: <input class="form-control" type="text" id="checkoutValue" name="checkoutValue">
<button class="btn btn-info" id="installmentCheck">Ver Parcelamento</button>
</p>
<p>
<select id="InstallmentCombo">
</select>
</p>
</div>
</div>
</fieldset>
</body>
<!-- Incluíndo o arquivo JS do PagSeguro e o JQuery-->
<script type="text/javascript" src="https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js"></script>
"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Funcionalidade do JS -->
<script type="text/javascript">
//Session ID
PagSeguroDirectPayment.setSessionId('<?php echo $session; ?>');
console.log('<?php echo $session; ?>');
//Get SenderHash
$("#generateSenderHash").click(function(){
PagSeguroDirectPayment.onSenderHashReady(function(response){
if(response.status == 'error') {
console.log(response.message);
return false;
}
//Hash estará disponível nesta variável.
$("#senderHash").val(response.senderHash);
});
})
//Get CreditCard Brand and check if is Internationl
$("#creditCardNumber").keyup(function(){
if ($("#creditCardNumber").val().length >= 6) {
PagSeguroDirectPayment.getBrand({
cardBin: $("#creditCardNumber").val().substring(0,6),
success: function(response) {
console.log(response);
$("#creditCardBrand").val(response['brand']['name']);
$("#creditCardCvv").attr('size', response['brand']['cvvSize']);
},
error: function(response) {
console.log(response);
}
})
};
})
function printError(error){
$.each(error['errors'], (function(key, value){
console.log("Foi retornado o código " + key + " com a mensagem: " + value);
}));
}
function getPaymentMethods(valor){
PagSeguroDirectPayment.getPaymentMethods({
amount: valor,
success: function(response) {
//console.log(JSON.stringify(response));
console.log(response);
},
error: function(response) {
console.log(JSON.stringify(response));
}
})
}
//Generates the creditCardToken
$("#generateCreditCardToken").click(function(){
var param = {
cardNumber: $("#creditCardNumber").val(),
cvv: $("#creditCardCvv").val(),
expirationMonth: $("#creditCardExpMonth").val(),
expirationYear: $("#creditCardExpYear").val(),
success: function(response) {
console.log(response);
$("#creditCardToken").val(response['card']['token']);
},
error: function(response) {
console.log(response);
printError(response);
}
}
//parâmetro opcional para qualquer chamada
if($("#creditCardBrand").val() != '') {
param.brand = $("#creditCardBrand").val();
}
PagSeguroDirectPayment.createCardToken(param);
})
//Check installment
$("#installmentCheck").click(function(){
if($("#creditCardBrand").val() != '') {
getInstallments();
} else {
alert("Uma bandeira deve estar selecionada");
}
})
function getInstallments(){
var brand = $("#creditCardBrand").val();
PagSeguroDirectPayment.getInstallments({
amount: $("#checkoutValue").val().replace(",", "."),
brand: brand,
maxInstallmentNoInterest: 2, //calculo de parcelas sem juros
success: function(response) {
var installments = response['installments'][brand];
buildInstallmentSelect(installments);
},
error: function(response) {
console.log(response);
}
})
}
function buildInstallmentSelect(installments){
$.each(installments, (function(key, value){
$("#InstallmentCombo").append("<option value = "+ value['quantity'] +">" + value['quantity'] + "x de " + value['installmentAmount'].toFixed(2) + " - Total de " + value['totalAmount'].toFixed(2)+"</option>");
}))
}
</script>
</html>