-
-
Save microgenios/40cb95c80fa22409a049a7989905b5ed to your computer and use it in GitHub Desktop.
Exemplo de pagamento e assinatura pelo PagSeguro com PHP e CURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = 'https://ws.pagseguro.uol.com.br/v2/pre-approvals/request'; | |
$data['email'] = '[email protected]'; | |
$data['token'] = 'TOKEN'; | |
$data['currency'] = 'BRL'; | |
$data['reference'] = $id_cliente; | |
$data['senderName'] = $cliente['nome']; | |
$data['senderEmail'] = $cliente['email']; | |
// $data['senderAreaCode'] = '55'; | |
// $data['senderPhone'] = $cliente['telefone']; | |
// $data['shippingAddressStreet'] = ""; | |
// $data['shippingAddressNumber'] = ""; | |
// $data['shippingAddressPostalCode'] = ""; | |
// $data['shippingAddressCity'] = ""; | |
// $data['shippingAddressState'] = ""; | |
// $data['shippingAddressCountry'] = 'BRA'; | |
$data['redirectURL'] = 'http://seusite.com.br/perfil?pagseguro-ok'; | |
$data['preApprovalCharge'] = 'auto'; | |
$data['preApprovalName'] = 'Assinatura mensal'; | |
$data['preApprovalDetails'] = 'Cobrança de valor mensal para assinatura'; | |
$data['preApprovalAmountPerPayment'] = 199.99; | |
$data['preApprovalPeriod'] = 'MONTHLY'; | |
$data['preApprovalFinalDate'] = '2020-10-17T19:20:30.45+01:00'; | |
$data['preApprovalMaxTotalAmount'] = '999.00'; | |
$data['reviewURL'] = 'http://seusite.com.br.com.br/planos'; | |
$data = http_build_query($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
$xml= curl_exec($curl); | |
if($xml == 'Unauthorized'){ | |
echo "Unauthorized"; | |
exit(); | |
} | |
curl_close($curl); | |
$xml= simplexml_load_string($xml); | |
if(count($xml->error) > 0){ | |
echo "XML ERRO"; | |
exit(); | |
} | |
header('Location: https://pagseguro.uol.com.br/v2/pre-approvals/request.html?code='.$xml->code); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Documentação disponível em: | |
// https://dev.pagseguro.uol.com.br/documentacao/pagamentos/pagamento-padrao | |
// URL DE SANDBOX | |
$url = 'https://ws.sandbox.pagseguro.uol.com.br/v2/checkout'; | |
$data['email'] = '[email protected]'; | |
$data['token'] = 'TOKEN'; | |
$data['currency'] = 'BRL'; | |
$data['itemId1'] = "1"; | |
$data['itemDescription1'] = "Descrição do item/produto"; | |
$data['itemAmount1'] = 199.90; | |
$data['itemQuantity1'] = 1; | |
$data['itemWeight1'] = 0; | |
$data['reference'] = $id_produto; //aqui vai o código que será usado para receber os retornos das notificações | |
$data['senderName'] = "Nome do comprador"; | |
// $data['senderAreaCode'] = ""; | |
// $data['senderPhone'] = ""; | |
$data['senderEmail'] = "[email protected]"; | |
// $data['shippingType'] = ""; | |
// $data['shippingAddressStreet'] = ""; | |
// $data['shippingAddressNumber'] = ""; | |
// $data['shippingAddressComplement'] = ""; | |
// $data['shippingAddressDistrict'] = ""; | |
// $data['shippingAddressPostalCode'] = ""; | |
// $data['shippingAddressCity'] = ""; | |
// $data['shippingAddressState'] = ""; | |
// $data['shippingAddressCountry'] = ""; | |
$data['redirectURL'] = 'http://seusite.com.br/pedido-finalizado'; | |
$data = http_build_query($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
$xml= curl_exec($curl); | |
if($xml == 'Unauthorized'){ | |
echo "Unauthorized"; | |
exit(); | |
} | |
curl_close($curl); | |
$xml= simplexml_load_string($xml); | |
if(count($xml->error) > 0){ | |
echo "XML ERRO"; | |
exit(); | |
} | |
// Utilize sua lógica para atualizar o pedido com o código da transação, para ser atualizado depois | |
$db->query("UPDATE pedido SET token = '{$xml->code}' WHERE id = $pedido_id"); | |
// Redireciona o comprador para a página de pagamento | |
header('Location: https://sandbox.pagseguro.uol.com.br/v2/checkout/payment.html?code='.$xml->code); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$code = $_POST['notificationCode']; | |
$type = $_POST['notificationType']; | |
if($type == 'transaction'){ | |
$url = "https://ws.sandbox.pagseguro.uol.com.br/v2/transactions/notifications/".$code."[email protected]&token=TOKEN"; | |
$content = file_get_contents($url); | |
$xml = simplexml_load_string($content); | |
if($xml->status > 3){ | |
$db->query("UPDATE pedido SET status = 2 WHERE token = '{$xml->reference}'"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment