<?php
include_once __dir__ . '/../vendor/autoload.php';
include_once __dir__ . '/../core/Config.php';

use Gerencianet\Exception\GerencianetException;
use Gerencianet\Gerencianet;

const PLANS = array(
    'MONTHLY' => [
        'key' => '000',
        'price' => 0.00,
        'name' => 'Plano Gratuito',
        'interval' => 1,
        'free' => true
    ],
    // 'FREE_BUSINESS' => [
    //     'key' => '889F3540181899BCC4FA2F81A71046B3',
    //     'price' => 137.90,
    //     'name' => 'Plano mensal', 
    //     'interval' => 1,
    //     'free' => true
    // ],
    'MONTHLY_BUSINESS' => [
        'key' => '889F3540181899BCC4FA2F81A71046B3',
        //'price' => 399.07,
        'price' => 10.00,
        'name' => 'Plano Mensal Empresas',
        'interval' => 1,
        'free' => false
    ],
    'MONTHLY_BUSINESS2' => [
        'key' => '889F3540181899BCC4FA2F81A71046B4',
        // 'price' => 4788.84,
        'price' => 10.00,
        'name' => 'Plano Mensal Empresas2',
        'interval' => 12,
        'free' => false
    ],
    'SIXMONTHLY_BUSINESS' => [
        'key' => '889F3540181899BCC4FA2F81A72046B5',
        // 'price' => 257.16,
        'price' => 10.00,
        'name' => 'Plano Semestral Empresas',
        'interval' => 1,
        'free' => false
    ],
    'SIXMONTHLY_BUSINESS2' => [
        'key' => '889F3540181899BCC4FA2F81A72046B6',
        // 'price' => 1492.96,
        'price' => 10.00,
        'name' => 'Plano Semestral Empresas2',
        'interval' => 6,
        'free' => false
    ],
    'YEARLY_BUSINESS' => [
        'key' => 'D6BF97AC2525309CC4499F898747EC2C',
        // 'price' => 192.25,
        'price' => 10.00,
        'name' => 'Plano Anual Empresas2',
        'interval' => 1,
        'free' => false
    ],
    'YEARLY_BUSINESS2' => [
        'key' => 'D6BF97AC2525309CC4499F898747EC2B',
        // 'price' => 2207.00,
        'price' => 10.00,
        'name' => 'Plano Anual Empresas',
        'interval' => 12,
        'free' => false
    ],
);
const STATES = array(
	'AC' => 'Acre',
	'AL' => 'Alagoas',
	'AP' => 'Amapá',
	'AM' => 'Amazonas',
	'BA' => 'Bahia',
	'CE' => 'Ceará',
	'DF' => 'Distrito Federal',
	'ES' => 'Espirito Santo',
	'GO' => 'Goiás',
	'MA' => 'Maranhão',
	'MS' => 'Mato Grosso do Sul',
	'MT' => 'Mato Grosso',
	'MG' => 'Minas Gerais',
	'PA' => 'Pará',
	'PB' => 'Paraíba',
	'PR' => 'Paraná',
	'PE' => 'Pernambuco',
	'PI' => 'Piauí',
	'RJ' => 'Rio de Janeiro',
	'RN' => 'Rio Grande do Norte',
	'RS' => 'Rio Grande do Sul',
	'RO' => 'Rondônia',
	'RR' => 'Roraima',
	'SC' => 'Santa Catarina',
	'SP' => 'São Paulo',
	'SE' => 'Sergipe',
	'TO' => 'Tocantins',
);


class GerencianetPayment extends Core {

    private $api;

    function __construct($conn) {

        parent::__construct($conn);

        $options = [
            'client_id' => GERENCIANET_CLIENT_ID,
            'client_secret' => GERENCIANET_CLIENT_SECRET,
// 			'pix_cert' => GERENCIANET_CERTIFICADO,
            'sandbox' => GERENCIANET_IS_SANDBOX,
			'debug' => false,
			'timeout' => 30
        ];
		
        $this->api = new Gerencianet($options);
        
    }
    
    function listPlans() {

        $params = ['limit' => 20, 'offset' => 0];
        $plans = $this->api->getPlans($params, []);
        return [ 'type' => 'OK', 'data' => $plans ];

    }

    function paymentCreditCard() {

        try {

            $user_id = $_POST['userId'];
            $card_token = $_POST['cardToken'];
            $plan = $_POST['plan'];
            $endcheck = $_POST['acceptEnd'];
            $signature_plan_id = $_POST['signature_plan_id'];
            
            if($plan['free']){
                $this->updateMakeFreeAccount($user['email']);
            }else{
            
            $holder_name = $_POST['holderName'];
			file_put_contents('POST.txt', 'CARTÃO - '.print_r($_POST, true), FILE_APPEND);
            exit;
            $sql = "SELECT * FROM leads WHERE id = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $user_id ]);
            $user = $stmt->fetch(PDO::FETCH_ASSOC);
            
            $state_uf = null;
            
            if(empty($signature_plan_id)){
                $signature_plan_id = $user['signature_plan_id'];
            }
            
            $plan = PLANS[$signature_plan_id];
            
            $birthdate = $_POST['birthdate'];
            
            if(!empty($birthdate)){
                if (strpos($birthdate, '/') !== false) {
                    $birthdate = implode('-', array_reverse(explode('/', $birthdate)));
                }
                
                // Verifica se o formato é "15022001" e converte para "2001-02-15"
                if (strlen($birthdate) === 8 && is_numeric($birthdate)) {
                    $birthdate = substr($birthdate, 4, 4) . '-' . substr($birthdate, 2, 2) . '-' . substr($birthdate, 0, 2);
                }
            }else{
                $birthdate = $user['birthdate'];
            }
            
            if(!empty($user['signature_date'])){
                
                $plan_id = $user['signature_id'];
                
                $params = [
                    'id' => $plan_id
                ];
                
                $details = $this->api->detailSubscription($params, []);
                $statusPlan = $details['data']['status'];
                
                if($statusPlan == 'active' || $statusPlan == 'new'){    
    
                    $this->api->cancelSubscription($params, []);
        
                    $status = 'CANCELED';
                    $status_text = 'Assinatura cancelada troca de plano';
                    $sql = "UPDATE leads SET signature_status = ?, signature_status_text = ? WHERE id = ?";
                    $stmt = parent::$conn->prepare($sql);
                    $stmt->execute([ $status, $status_text, $user_id ]);
                    
                }

            }
            
            $address1 = $_POST['street'];
            $address2 = $_POST['number'];
            $city = $_POST['city'];
            $state_uf = $_POST['state'];
            $zipcode = $_POST['zipCode'];
            $neighborhood = $_POST['district'];
            $complement = $_POST['complement'] ? $_POST['complement'] : '';
            $email = $_POST['email'];
            $phone = $_POST['phone'];
            
            if(isset($endcheck) && $endcheck === "true"){
                
                $companyname = $_POST['companyName'];
                $cnpj = $_POST['cnpj'];
           
            }else{
                
                $cpf = $_POST['cpf'];
                
                if(empty($user['city']) OR $user['city'] == NULL){
                    
                    $cidade = $city.', '.$state_uf;
                    $estado = STATES[$state_uf];
                    
                    $sql = "UPDATE leads SET address1 = ?, address2 = ?, city = ?, state = ?, district = ?, zipcode = ?, birthdate = ? WHERE id = ?";
                    $stmt = parent::$conn->prepare($sql);
                    $stmt->execute([ $address1, $address2, $cidade, $estado, $neighborhood, $zipcode, $birthdate, $user_id ]);
                }
                
            }


            //verifica se existe plano
            $params = array(
                'name' => $plan['name']
            );
            $query_result = $this->api->listPlans($params, []);
            
            if(sizeof($query_result['data']) > 0)
                $plan_id = $query_result['data'][0]['plan_id'];
            else {

                $body = [
                    'name' => $plan['name'] . ' da Memprega.com.br',
                    'interval' => $plan['interval'],
                    'repeats' => null
                ];
                $result = $this->api->createPlan([], $body);
                $plan_id = $result['data']['plan_id'];

            }
            
            $params = ['id' => $plan_id];
            $items = [
                [
                    'name' => $plan['name'],
                    'amount' => 1,
                    'value' => intval(floatval($plan['price']) * 100)
                ]
            ];
            $body = [
                'metadata' => [
                    'notification_url' => GERENCIANET_NOTIFICATION_URL
                ],
                'items' => $items,
            ];            
            $subscription = $this->api->createSubscription($params, $body);            
            $subscription_id = $subscription['data']['subscription_id'];
            $params = [ 'id' => $subscription_id ];
            
            if(!empty($companyname)){
                $juridical_person = [
                    'corporate_name'=> $companyname,
                    'cnpj'=> $cnpj
                ];
                
                $customer = [
                    'phone_number' => $phone,
                    'email' =>  $email,
                    'birth' =>  $birthdate,
                    'juridical_person' => $juridical_person
                ];
            }else{
                $customer = [
                    'name' => $holder_name,
                    'cpf' => $cpf,
                    'phone_number' => $phone,
                    'email' =>  $email,
                    'birth' => $birthdate
                ];  
            }
            
            $billing_address = [
                'street' => $address1,
                'number' => $address2,
                'neighborhood' => $neighborhood,
                'zipcode' => $zipcode,
                'city' => $city,
                'complement' => $complement,
                'state' => $state_uf,
            ];
            
            if(!empty($user['signature_date'])){
                $body = [ 
                    'payment' => [
                        'credit_card' => [ 
                            'billing_address' => $billing_address,
                            'payment_token' => $card_token,
                            'customer' => $customer,                        
                        ]
                    ]
                ];
            }else{
                $trial = 15;
                $body = [ 
                    'payment' => [
                        'credit_card' => [ 
                            'trial_days' => $trial, 
                            'billing_address' => $billing_address,
                            'payment_token' => $card_token,
                            'customer' => $customer,                        
                        ]
                    ]
                ];
            }
            
            
            file_put_contents('CREDIT_CARD.txt', print_r($body, true));
            $payment = $this->api->defineSubscriptionPayMethod($params, $body);
            $signature_id = $subscription_id;
            
            if(!empty($user['signature_date'])){
                $signature_status_text = 'Assinatura Criada, Esperando autorização da operadora do cartão';
                $sql = "UPDATE leads SET signature_id = ?, voucher = NULL, signature_status = 'CANCELED', signature_date = NOW(), signature_status_text = ? WHERE id = ?";
                $stmt = parent::$conn->prepare($sql);
                $stmt->execute([ $signature_id, $signature_status_text, $user_id ]);
            }else{
                $signature_status_text = 'Liberado momentaneamente, esperando autorização da operadora do cartão';
                $sql = "UPDATE leads SET signature_id = ?, voucher = NULL, signature_status = 'ACTIVE', signature_date = NOW(), signature_status_text = ? WHERE id = ?";
                $stmt = parent::$conn->prepare($sql);
                $stmt->execute([ $signature_id, $signature_status_text, $user_id ]);
            }   
            
            $sql = "INSERT INTO lead_lists_leads(leadlist_id, lead_id, date_added, manually_removed, manually_added)
                VALUES(42, ?,NOW(), 0, 0)";        
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $user_id ]);

            //$this->updateMakePaidAccount($user['email']);
            
            return [ 
                'type' => 'OK', 
                'data' => [
                    'id' => $signature_id
                ]
            ];
            }
        } catch(\Exception $err) {
            file_put_contents('GERENCIANET.PAYMENT.err', print_r($err, true));
            return [ 'type' => 'ERROR', 'data' => $err->getMessage() ];
        }

    }
    
    function paymentBillet() {

        try {

            $user_id = $_POST['userId'];
            $endcheck = $_POST['acceptEnd'];
            file_put_contents('POST.txt', 'BOLETO - '.print_r($_POST, true), FILE_APPEND);
            
            $sql = "SELECT * FROM leads WHERE id = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $user_id ]);
            $user = $stmt->fetch(PDO::FETCH_ASSOC);
            
            $signature_plan_id = $_POST['planSelect'];

            if(!$signature_plan_id)
                $signature_plan_id = $user['signature_plan_id'];
            
            $state_uf = null;            
            $plan = PLANS[$signature_plan_id];
            if($plan['free']){
                $this->updateMakeFreeAccount($user['email']);    
            }else{
            
            if(!empty($user['signature_date'])){
                
                $plan_id = $user['signature_id'];
                
                $params = [
                    'id' => $plan_id
                ];
                
                $details = $this->api->detailSubscription($params, []);
                $statusPlan = $details['data']['status'];
                
                if($statusPlan == 'active' || $statusPlan == 'new'){

                    $this->api->cancelSubscription($params, []);
        
                    $status = 'CANCELED';
                    $status_text = 'Assinatura cancelada troca de plano';
                    $sql = "UPDATE leads SET signature_status = ?, signature_status_text = ? WHERE id = ?";
                    $stmt = parent::$conn->prepare($sql);
                    $stmt->execute([ $status, $status_text, $user_id ]);
                }

            }
            
            $address1 = $_POST['street'];
            $address2 = $_POST['number'];
            $city = $_POST['city'];
            $state_uf = $_POST['state'];
            $zipcode = $_POST['zipCode'];
            $neighborhood = $_POST['district'];
            $complement = $_POST['complement'] ? $_POST['complement'] : '';
            $email = $_POST['email'];
            $phone = $_POST['phone'];
            $birthdate = $_POST['birthdate'];
            
            if(!empty($birthdate)){
                if (strpos($birthdate, '/') !== false) {
                    $birthdate = implode('-', array_reverse(explode('/', $birthdate)));
                }
                
                // Verifica se o formato é "15022001" e converte para "2001-02-15"
                if (strlen($birthdate) === 8 && is_numeric($birthdate)) {
                    $birthdate = substr($birthdate, 4, 4) . '-' . substr($birthdate, 2, 2) . '-' . substr($birthdate, 0, 2);
                }
            }else{
                $birthdate = $user['birthdate'];
            }

            
            
            // if(isset($endcheck) && $endcheck === "true"){
                
                $companyname = $_POST['companyName'];
                $cnpj = $_POST['cnpj'];
           
            // }else{
        
            //     $cpf = $_POST['cpf'];
            //     $holder_name = $_POST['fullname'];
                
                if(empty($user['city']) OR $user['city'] == NULL){
                    
                    $cidade = $city.', '.$state_uf;
                    $estado = STATES[$state_uf];
                    
                    $sql = "UPDATE leads SET address1 = ?, address2 = ?, city = ?, state = ?, district = ?, zipcode = ?, birthdate = ? WHERE id = ?";
                    $stmt = parent::$conn->prepare($sql);
                    $stmt->execute([ $address1, $address2, $cidade, $estado, $neighborhood, $zipcode, $birthdate, $user_id ]);
                }
                
            // }
            
            //verifica se existe plano
            $params = array(
                'name' => $plan['name']
            );
			
            $query_result = $this->api->listPlans($params, []);
            
            if(sizeof($query_result['data']) > 0)
                $plan_id = $query_result['data'][0]['plan_id'];
            else {

                $body = [
                    'name' => $plan['name'] . ' da Memprega.com.br',
                    'interval' => $plan['interval'],
                    'repeats' => null
                ];
                $result = $this->api->createPlan([], $body);
                $plan_id = $result['data']['plan_id'];

            }
            
            $params = ['id' => $plan_id];
            $items = [
                [
                    'name' => $plan['name'],
                    'amount' => 1,
                    'value' => intval(floatval($plan['price']) * 100)
                ]
            ];
            
            $metadata = ['notification_url'=>GERENCIANET_NOTIFICATION_URL];
            
            $body  =  [
                'items' => $items,
                'metadata' => $metadata
            ];

            $subscription = $this->api->createSubscription($params, $body);            
            $subscription_id = $subscription['data']['subscription_id'];            
            $params = [ 'id' => $subscription_id ];			
            //$billet_name = $user['account_type'] === 'company' ? $user['company'] : $user['firstname'] . ' ' . $user['lastname'];
            
            if(!empty($companyname)){
                $customer = [
                    'email' => $email,
                    'phone_number' => $phone,
                    'juridical_person' => [
                        'corporate_name' => $companyname,
                        'cnpj' => $cnpj
                    ],
                    'address' => [
                        'street' => $address1,
                        'number' => $address2,
                        'neighborhood' => $neighborhood,
                        'zipcode' => $zipcode,
                        'city' => $city,
                        'complement' => $complement,
                        'state' => $state_uf
                    ]
                ];
            }
            // else{
            //     $customer = [
            //         'name' => $holder_name,
            //         'cpf' => $cpf,
            //         'email' => $email,
            //         'phone_number' => $phone,
            //         'address' => [
            //             'street' => $address1,
            //             'number' => $address2,
            //             'neighborhood' => $neighborhood,
            //             'zipcode' => $zipcode,
            //             'city' => $city,
            //             'complement' => $complement,
            //             'state' => $state_uf
            //         ]
            //     ];
            // }


            $date = new DateTime();
            //$date = $plan['free'] ? $date->add(new DateInterval('P90D')) : $date->add(new DateInterval('P7D'));
            
            // if(!empty($user['signature_date'])){
                $date = $date->add(new DateInterval('P2D'));
            // }else{
            //     $date = $date->add(new DateInterval('P15D'));
            // }
            
            $banking_billet = [
                'expire_at' => $date->format('Y-m-d'),
                'message' => "Memprega.com.br, {$plan['name']}",
                'customer' => $customer
            ];
            $payment = [
                'banking_billet' => $banking_billet, //forma de pagamento (banking_billet = boleto)                
            ];
            $body = [
                'payment' => $payment
            ];
            
			$payment = $this->api->defineSubscriptionPayMethod($params, $body);
            // file_put_contents('RETORNO.PAYLOAD.txt', print_r($payment, true));
            
            $signature_id = $subscription_id;
            $charge_link = $payment['data']['billet_link'];
            
            if(!empty($user['signature_date'])){
                $signature_status_text = 'Assinatura Criada, Aguardando Confirmação de Pgto do Boleto';            
                $sql = "UPDATE leads SET signature_id = ?, voucher = NULL, signature_status = 'CANCELED', signature_date = NOW(), signature_status_text = ?, payment_link = ?, signature_plan_id = ? WHERE id = ?";
                $stmt = parent::$conn->prepare($sql);
                $stmt->execute([ $signature_id, $signature_status_text, $charge_link,  $signature_plan_id, $user_id ]);
            }else{
                $signature_status_text = 'Liberado momentaneamente, esperando autorização da operadora do cartão';            
                $sql = "UPDATE leads SET signature_id = ?, voucher = NULL, signature_status = 'ACTIVE', signature_date = NOW(), signature_status_text = ?, payment_link = ?, signature_plan_id = ? WHERE id = ?";
                $stmt = parent::$conn->prepare($sql);
                $stmt->execute([ $signature_id, $signature_status_text, $charge_link,  $signature_plan_id, $user_id ]);
            }    
            
            $sql = "INSERT IGNORE INTO lead_lists_leads(leadlist_id, lead_id, date_added, manually_removed, manually_added)
                VALUES(42, ?,NOW(), 0, 0)";        
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $user_id ]);

            //$this->updateMakePaidAccount($user['email']);

            return [ 
                'type' => 'OK', 
                'data' => [
                    'id' => $signature_id,
                    'link' => $charge_link
                ]
            ];
            }
        } catch(\Exception $err) {
            file_put_contents('GERENCIANET.PAYMENT.err', print_r($err, true));
            return [ 'type' => 'ERROR', 'data' => $err->getMessage() ];
        }
        
    }

    function notification() {

        define('GEN_STATUS', array(
            'new' => [
                'status' => 'ACTIVE',
                'text' => 'Liberada momentaneamente, esperando autorização da operadora do cartão'
            ],
            'new_charge' => [
                'status' => 'ACTIVE',
                'text' => 'Liberada momentaneamente, esperando autorização da operadora do cartão'
            ],
            'active' => [
                'status' => 'ACTIVE',
                'text' => 'Assinatura ativa'
            ],
            'paid' => [
                'status' => 'ACTIVE',
                'text' => 'Assinatura/cobrança paga'
            ],
            'waiting' => [
                'status' => 'ACTIVE',
                'text' => 'Liberada momentaneamente, aguardando prazo e esperando autorização da operadora do cartão'
            ],
            'canceled' => [
                'status' => 'CANCELED',
                'text' => 'Assinatura cancelada'
            ],
            'expired' => [
                'status' => 'CANCELED',
                'text' => 'Assinatura cancelada por falta de pagamento ou expirada, você pode emitir um novo boleto e fazer o pagamento antes do vencimento'
            ],
        ));

        try {

            $token = $_POST['notification'];
            $params = [
                'token' => $token
            ];
            $notification = $this->api->getNotification($params, []);
            
            $i = count($notification["data"]); 
            $last_status = $notification["data"][$i-1];    
            $status = $last_status["status"];            
            $charge_id = $last_status["identifiers"]["subscription_id"];    
            $current_status = $status["current"];            
            $status = GEN_STATUS[$current_status] ?? [ 'status' => 'CANCELED', 'text' => 'Assinatura cancelada por falta de pagamento ou expirada' ];
            
            $sql = "UPDATE leads SET signature_status = :status, signature_status_text = :text WHERE signature_id = :id";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ 
                ':status' => $status['status'],
                ':text' => $status['text'],
                ':id' => $charge_id,
            ]);

            $sql = "SELECT id, email FROM leads WHERE signature_id = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $charge_id ]);
            $user = $stmt->fetch(PDO::FETCH_ASSOC);
            
            //deleta se estiver nos segmentos de pendentes ou cancelados
            $sql = "DELETE FROM lead_lists_leads WHERE leadlist_id IN(42, 41, 39) AND lead_id = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $user['id'] ]);

            if($status === 'ACTIVE') {

                $sql = "INSERT INTO lead_lists_leads(leadlist_id, lead_id, date_added, manually_removed, manually_added)
                    VALUES(39, ?,NOW(), 0, 0)";   
                $stmt = parent::$conn->prepare($sql);
                $stmt->execute([ $user['id'] ]);

                //$this->updateMakePaidAccount($user['email']);

            } else {

                //Segmentos dos cancelados, ID  41
                $sql = "INSERT INTO lead_lists_leads(leadlist_id, lead_id, date_added, manually_removed, manually_added)
                    VALUES(41, ?,NOW(), 0, 0)";        
                $stmt = parent::$conn->prepare($sql);
                $stmt->execute([ $user['id'] ]);
                
            }


        } catch(\Exception $err) {
            http_response_code(500);
        }

        http_response_code(200);

    }

    private function updateMakePaidAccount($email) {

        $sql = "UPDATE companies SET free_account = 0 WHERE companyemail = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([ $user['email'] ]);

    }

    private function updateMakeFreeAccount($email) {
        
        $sql = "UPDATE leads SET signature_status = 'ACTIVE', voucher = NULL, signature_date = NOW(), signature_status_text = 'Liberado momentaneamente, esperando autorização da operadora do cartão' WHERE email = ?";
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute([ $user['email'] ]);

        // $sql = "UPDATE companies SET free_account = 1 WHERE companyemail = ?";
        // $stmt = parent::$conn->prepare($sql);
        // $stmt->execute([ $user['email'] ]);

    }

    function cancelSignature() {

        try {
            
            $id = $_GET['id'];
            $sql = "SELECT signature_id FROM leads WHERE id = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $id ]);
            $signature = $stmt->fetch(PDO::FETCH_ASSOC);
            $plan_id = $signature['signature_id'];
            $params = [
                'id' => $plan_id
            ];

            $this->api->cancelSubscription($params, []);

            $status = 'CANCELED';
            $status_text = 'Assinatura cancelada pelo assinante';
            $sql = "UPDATE leads SET signature_status = ?, signature_status_text = ? WHERE id = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $status, $status_text, $id ]);

            if($stmt->errorCode() != '00000')
                throw new \Exception(json_encode($stmt->errorInfo()));

            return [ 'type' => 'OK' ];

        } catch(\Exception $err) {

            file_put_contents('GERENCIANET.CANCEL.err', print_r($err, true));
            return [ 'type' => 'ERROR', 'data' => $err->getMessage() ];
            
        }

    }

    function alterPaymentMethod() {

        try {

            /*$user_id = $_POST['userId'];            
            $sql = "SELECT signature_id FROM leads WHERE id = ?";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([ $user_id ]);
            $signature = $stmt->fetch(PDO::FETCH_ASSOC);            
            $plan_id = $signature['signature_id'];
            $params = [
                'id' => $plan_id
            ];

            $this->api->cancelSubscription($params, []);*/

            return $this->paymentCreditCard();

        } catch(\Exception $err) {

            file_put_contents('GERENCIANET.ALTER.err', print_r($err, true));
            return [ 'type' => 'ERROR', 'data' => $err->getMessage() ];

        }

    }

}