| Server IP : 54.94.228.101 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/app/Services/ |
Upload File : |
<?php
namespace App\Services;
use App\Modules\CadastroEndereco\Models\CadastroEndereco;
use Illuminate\Support\Facades\Log;
class PagarMeService
{
public static function clearCaracteres($string){
$comAcentos = array('à', 'á', 'â', 'ã', 'ä', 'å', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ù', 'ü', 'ú', 'ÿ', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'O', 'Ù', 'Ü', 'Ú');
$semAcentos = array('a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'y', 'A', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U');
$nova_string = str_replace($comAcentos, $semAcentos, $string);
return trim($nova_string);
}
public static function getCustomer($customer_id, $apiKey){
$customer = self::callAPI('customers/'.$customer_id, $apiKey);
return $customer;
}
public static function getOrder($order_id, $apiKey){
$order = self::callAPI('orders/'.$order_id, $apiKey);
return $order;
}
public static function createCustomer($Cadastro, $apiKey, $metadata = []){
try {
$cpf = preg_replace('/[^0-9]/', '', (string) $Cadastro->cpf);
$cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
$telefone = preg_replace('/[^0-9]/', '', (string) $Cadastro->telefone);
$ddd = substr($telefone, 0, 2);
$celular = substr($telefone, 2);
if(empty($ddd) || $ddd == '00'){
throw new \Exception('Cadastro PagarMe: DDD Inválido');
}
$CadastroEndereco = CadastroEndereco::selectRaw("cadastro_endereco.*, estado.sigla AS 'estado_nome', cidade.nome AS 'cidade_nome'")
->leftJoin('estado', 'estado.id', '=', 'cadastro_endereco.estado_id')
->leftJoin('cidade', 'cidade.id', '=', 'cadastro_endereco.cidade_id')
->where('cadastro_id', $Cadastro->id)
->orderBy('id', 'desc')
->first();
$address = [];
if(!empty($CadastroEndereco->numero)){ $address[] = $CadastroEndereco->numero;}
if(!empty($CadastroEndereco->logradouro)){ $address[] = $CadastroEndereco->logradouro;}
if(!empty($CadastroEndereco->bairro)){ $address[] = $CadastroEndereco->bairro;}
$estado_nome = ((strlen($CadastroEndereco->estado_nome) > 2 || empty($CadastroEndereco->estado_nome)) ? 'RS' : $CadastroEndereco->estado_nome);
$dados = [
'name' => trim($Cadastro->nome),
'email' => trim($Cadastro->user->email),
'code' => $Cadastro->id,
'document' => $cpf,
'type' => 'individual',
'document_type' => 'cpf',
'gender' => 'male',
'address' => [
'line_1' => implode(",", $address),
'line_2' => trim($CadastroEndereco->complemento),
'zip_code' => preg_replace('/[^0-9]/', '', (string) trim($CadastroEndereco->cep)),
'city' => $CadastroEndereco->cidade_nome,
'state' => $estado_nome,
'country' => 'BR'
],
'birthdate' => $Cadastro->data_nascimento,
'phones' => [
'mobile_phone' => [
'country_code' => '55',
'area_code' => $ddd,
'number' => $celular
]
],
'metadata' => ''
];
//,'metadata' => $metadata
Log::info('PagarMeService::createCustomer => Dados: ' . json_encode($dados));
$customer = self::callAPI('customers', $apiKey, 'POST', $dados);
Log::info('PagarMeService::createCustomer => Response: ' . json_encode($customer));
return $customer;
}catch (\Exception $exception){
throw new \Exception($exception->getMessage());
}
}
public static function getCustomerCard($customer_id, $card_id, $apiKey){
$card = self::callAPI('customers/'.$customer_id.'/cards/'.$card_id, $apiKey);
return $card;
}
public static function createCustomerCard($Cadastro, $post, $apiKey){
$CadastroEndereco = CadastroEndereco::where('cadastro_id', $Cadastro->id)->first();
$address = [];
if (!empty($CadastroEndereco->numero)) {
$address[] = $CadastroEndereco->numero;
}
if (!empty($CadastroEndereco->logradouro)) {
$address[] = $CadastroEndereco->logradouro;
}
if (!empty($CadastroEndereco->bairro)) {
$address[] = $CadastroEndereco->bairro;
}
$data_validade = explode('/', $post['data_validade']);
$mes_validade = (int)$data_validade[0];
$ano_validade = (int)$data_validade[1];
if(strlen($ano_validade) > 2){
$ano_validade = substr($ano_validade, 2 , 2);
}
$estado_nome = ((strlen($CadastroEndereco->estado_nome) > 2 || empty($CadastroEndereco->estado_nome)) ? 'RS' : $CadastroEndereco->estado_nome);
//Número Cartão
$numero_cartao = str_replace(' ', '', $post['numero_cartao']);
$numero_cartao = preg_replace('/[^0-9]/', '', (string) trim($numero_cartao));
$numero_cartao = str_pad($numero_cartao, 16, '0', STR_PAD_LEFT);
$dados = [
'billing_address' => [
'line_1' => implode(",", $address),
'line_2' => trim($CadastroEndereco->complemento),
'zip_code' => preg_replace('/[^0-9]/', '', (string) trim($CadastroEndereco->cep)),
'city' => $CadastroEndereco->cidade_nome,
'state' => $estado_nome,
'country' => 'BR'
],
'options' => [
'verify_card' => false
],
'number' => $numero_cartao,
'holder_name' => PagarMeService::clearCaracteres($post['nome_titular']),
'exp_month' => $mes_validade,
'exp_year' => $ano_validade,
'cvv' => str_pad($post['codigo_seguranca'], 3, '0', STR_PAD_LEFT)
// 'brand' => ''
];
Log::info('PagarMeService::createCustomerCard => Dados: ' . json_encode($dados));
$card = self::callAPI('customers/'.$Cadastro->customer_id.'/cards', $apiKey, 'POST', $dados);
Log::info('PagarMeService::createCustomerCard => Response: ' . json_encode($card));
return $card;
}
public static function createOrderPix($Cadastro, $Produto, $apiKey){
$total = $Produto->preco_final;
$totalTransaction = $total * 100;
$totalTransaction = number_format($totalTransaction, 0, '','');
$dados = [
'customer_id' => $Cadastro->customer_id,
'items' => [
[
'amount' => (int) $totalTransaction,
'description' => $Produto->nome,
'quantity' => 1,
'code' => $Produto->id
]
],
'payments' => [
[
'pix' => [
// 'expires_in' => (int) 52134613,
'expires_at' => date('Y-m-d H:i:s', strtotime("+1 day",strtotime(date('Y-m-d H:i:s')))),
'additional_information' => [
[
'name' => $Produto->nome,
'value' => 1
]
]
],
'payment_method' => 'pix'
]
]
];
Log::info('PagarMeService::createOrderPix => Dados: ' . json_encode($dados));
$order = self::callAPI('orders', $apiKey, 'POST', $dados);
Log::info('PagarMeService::createOrderPix => ' . json_encode($order));
return $order;
}
public static function createOrderBoleto($Cadastro, $Produto, $apiKey){
$total = $Produto->preco_final;
$totalTransaction = $total * 100;
$totalTransaction = number_format($totalTransaction, 0, '','');
$dados = [
'customer_id' => $Cadastro->customer_id,
'items' => [
[
'amount' => $totalTransaction,
'description' => $Produto->nome,
'quantity' => 1,
'code' => $Produto->id
]
],
'payments' => [
[
'payment_method' => 'boleto',
'boleto' => [
'instructions' => "Pagar até a data do Vencimento",
'due_at' => date('Y-m-d H:i:s', strtotime("+1 day",strtotime(date('Y-m-d H:i:s')))),
'document_number' => '',
'type' => 'DM'
]
]
]
];
Log::info('PagarMeService::createOrderBoleto => Dados: ' . json_encode($dados));
$order = self::callAPI('orders', $apiKey, 'POST', $dados);
Log::info('PagarMeService::createOrderBoleto => ' . json_encode($order));
return $order;
}
public static function createOrderCreditCard($Cadastro, $total, $card_id, $parcelas, $apiKey, $hash){
$totalTransaction = $total * 100;
$totalTransaction = number_format($totalTransaction, 0, '','');
$parcelas = (!empty($parcelas) ? $parcelas : 12);
$dados = [
'customer_id' => $Cadastro->customer_id,
'items' => [
[
'amount' => $totalTransaction,
'description' => 'Produtos Vinumday',
'quantity' => 1,
'code' => $hash
]
],
'antifraud_enabled' => false,
'payments' => [
[
'payment_method' => 'credit_card',
'credit_card' => [
'card' => [
'billing_address' => [
"line_1" => "61, R OLAVO BILAC, RIO BRANCO",
"line_2" => "",
"zip_code" => "95010080",
"city" => "Caxias do Sul",
"state" => "RS",
"country" => "BR"
],
'billing' => [
'name' => PagarMeService::clearCaracteres($Cadastro->nome),
'address' => [
"street" => "R OLAVO BILAC",
"neighborhood" => 'RIO BRANCO',
"street_number" => "61",
"zipcode" => "95010080",
"city" => "Caxias do Sul",
"state" => "RS",
"country" => "br"
]
]
],
'card_id' => $card_id,
'recurrence' => false,
'installments' => $parcelas,
'statement_descriptor' => 'Vinumday'
]
]
]
];
Log::info('PagarMeService::createOrderCreditCard => Dados: ' . json_encode($dados));
$order = self::callAPI('orders', $apiKey, 'POST', $dados);
Log::info('PagarMeService::createOrderCreditCard => Response: ' . json_encode($order));
return $order;
}
protected static function callAPI($endpoint, $apiKey, $method = 'GET', $body = []){
$client = new \GuzzleHttp\Client();
$secretKey = $apiKey;
$apiSecretKey = base64_encode($secretKey.':');
$content = [
'headers' => [
'accept' => 'application/json',
'authorization' => 'Basic ' . $apiSecretKey,
'content-type' => 'application/json',
]
];
if($method == 'POST' && count($body) > 0){
$content = array_merge($content, ['body' => json_encode($body)]);
}
$response = $client->request($method, 'https://api.pagar.me/core/v5/' . $endpoint, $content);
$response = $response->getBody();
return json_decode($response);
}
}