<?php
class PixSicredi{

  private $access_token = false;
  public $location = false;
  public $infCob = false;

  public function __construct(){}

  // Gerar token de acesso
  public function tokenGenerator(){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, GLOBAL_PIX_URL."/oauth/token?grant_type=client_credentials&scope=cob.write+cob.read+cobv.write+cobv.read+lotecobv.write+lotecobv.read+pix.write+pix.read+webhook.read+webhook.write+payloadlocation.read+payloadlocation.write");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_SSLVERSION_TLSv1_2);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_CAINFO, GLOBAL_PIX_CERT_PATH.'/cadeia.cer');
    curl_setopt($ch, CURLOPT_SSLCERT, GLOBAL_PIX_CERT_PATH.'/cert.cer');
    curl_setopt($ch, CURLOPT_SSLKEY, GLOBAL_PIX_CERT_PATH.'/pixsicredi.key');
    curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
      "Authorization: Basic " . base64_encode(GLOBAL_PIX_CLIENT_ID.':'.GLOBAL_PIX_CLIENT_SECRET),
      "Content-type: application/x-www-form-urlencoded"
    ]);
    $response = curl_exec($ch);
    $err = curl_error($ch);
    if(!$err){
      $dados = json_decode($response, true);
      $this->access_token = $dados['access_token'];
    }
    curl_close($ch);
  }

  // Cria registro da cobrança na api do banco
  public function createCob($txid, $data){
    if($this->access_token){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, GLOBAL_PIX_URL."/api/v2/cob/".$txid);
      curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_SSLVERSION_TLSv1_2);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_CAINFO, GLOBAL_PIX_CERT_PATH.'/cadeia.cer');
      curl_setopt($ch, CURLOPT_SSLCERT, GLOBAL_PIX_CERT_PATH.'/cert.cer');
      curl_setopt($ch, CURLOPT_SSLKEY, GLOBAL_PIX_CERT_PATH.'/pixsicredi.key');
      curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer " . $this->access_token,
        "Content-type: application/json"
      ]);
      $response = curl_exec($ch);
      $err = curl_error($ch);
      if(!$err){
        $dados = json_decode($response, true);
        $this->location = $dados['location'];
      }
      return $this->location;
    }
  }

  // Pega as informações de uma cobrança
  public function infCob($txid){
    if($this->access_token){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, GLOBAL_PIX_URL."/api/v2/cob/".$txid);
      curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_SSLVERSION_TLSv1_2);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_CAINFO, GLOBAL_PIX_CERT_PATH.'/cadeia.cer');
      curl_setopt($ch, CURLOPT_SSLCERT, GLOBAL_PIX_CERT_PATH.'/cert.cer');
      curl_setopt($ch, CURLOPT_SSLKEY, GLOBAL_PIX_CERT_PATH.'/pixsicredi.key');
      curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer " . $this->access_token,
        "Content-type: application/json"
      ]);
      $response = curl_exec($ch);
      $err = curl_error($ch);
      if(!$err){
        $dados = json_decode($response, true);
        $this->infCob = $dados;
      }
      return $this->infCob;
    }
  }


}