-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
144 lines (134 loc) · 4.68 KB
/
example.php
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
<?php
//Iniciando a sessão. Você pode criar um serviço que retorne este valor.
$url = "https://ws.pagseguro.uol.com.br/v2/sessions";
$credenciais = array(
"email" => "[email protected]",
"token" => "C475C83B3C094AAAA83E047D56190F77"
);
$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>Exemplo</title>
<script type="text/javascript"></script>
</head>
<body>
<h1>Exemplo</h1><hr>
<h2>Obtendo os dados do cartão de crédito</h3>
<!--
<p>Seu cartão é emitido fora do Brasil <input type="checkbox" name="isInternational" id="isInternational"> Sim</p>
-->
<p>
Número do cartão: <input type="text" id="creditCardNumber" class="creditcard" name="creditCardNumber"></input>
Bandeira: <input type="text" id="creditCardBrand" class="creditcard" name="creditCardBrand" disabled></input>
</p>
<p>
Validade: Mês (mm) <input type="text" id="creditCardExpMonth" class="creditcard" name="creditCardExpMonth" size="2"></input>
Ano (yyyy) <input type="text" id="creditCardExpYear" class="creditcard" name="creditCardExpYear" size="4"></input>
</p>
<p>
CVV: <input type="text" id="creditCardCvv" class="creditcard" name="creditCardCvv"></input>
</p>
<p>
<button id="generateCreditCardToken">Gerar Token</button>
<input type="text" id="creditCardToken" name="creditCardToken" disabled></input>
</p>
<h2>Parcelamento</h2>
<p>
Valor do Checkout: <input type="text" id="checkoutValue" name="checkoutValue"></input>
<button id="installmentCheck">Ver Parcelamento</button>
</p>
<p>
<select id="InstallmentCombo">
</select>
</p>
<p>
<button id="doPayment">Efetuar pagamento</button> SenderHash: <input type="text" id="senderHash" name="senderHash" size="65"></input>
</p>
</body>
<!-- Incluíndo o arquivo JS do PagSeguro e o JQuery-->
<script type="text/javascript" src="https://stc.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- Funcionalidade do JS -->
<script type="text/javascript">
//Setando o session ID
PagSeguroDirectPayment.setSessionId('<?php echo $session; ?>');
//Get CreditCard Brand and check if is Internationl
$("#creditCardNumber").keyup(function(){
if ($("#creditCardNumber").val().length >= 6) {
PagSeguroDirectPayment.getBrand({
cardBin: $("#creditCardNumber").val().substring(0,6),
internationalMode:true,
success: function(response) {
console.log(JSON.stringify(response));
$("#creditCardBrand").val(response['brand']['name']);
$("#creditCardCvv").attr('size', response['brand']['cvvSize']);
},
error: function(response) {
console.log(response);
}
})
};
})
//Generates the creditCardToken
$("#generateCreditCardToken").click(function(){
var param = {
cardNumber: $("#creditCardNumber").val(),
cvv: $("#creditCardCvv").val(),
internationalMode: $("#isInternational").is(':checked'),
expirationMonth: $("#creditCardExpMonth").val(),
expirationYear: $("#creditCardExpYear").val(),
success: function(response) {
$("#creditCardToken").val(response['card']['token']);
},
error: function(response) {
console.log(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,
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>");
}))
}
//Get SenderHash
$("#doPayment").click(function(){
$("#senderHash").val(PagSeguroDirectPayment.getSenderHash());
})
</script>
</html>