<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use DB;

class DebugController extends Controller
{  
	protected $uri = 'https://api.klikbca.com:443';
    protected $client_id = 'ab45dd35-edb0-4c36-a22a-d55011b1b163';
    protected $client_secret = '7e23cc9b-6b2c-4c3c-b5d7-80e4651d2fe6';
    protected $api_secret = '0e175403-7d28-422b-8c5c-82e69e4ae931';
    protected $api_key = '78325aeb-df1b-4c37-99e4-a3116a5dc449';
    protected $corporate_id = 'ibpsegadai';
    protected $account_number = '2683051799';

    protected $auth;
    protected $access_token;
    protected $signature_bca;

    public function index() 
    {             
    	$this->auth = base64_encode("{$this->client_id}:{$this->client_secret}");
        $this->auth = "Basic {$this->auth}";
        
        $this->get_access_token_bca();
        $this->get_currency();        
    }

    private function get_access_token_bca()
    {
        $uri = "{$this->uri}/api/oauth/token";        
        $client = new Client();
        $result = $client->post($uri, [
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded',
                'Authorization' => $this->auth
            ],
            'form_params' => [
                'grant_type' => 'client_credentials'
            ]
        ]);

        $array = json_decode($result->getBody(), true);

        $this->access_token = $array['access_token'];
    }

    private function get_mutasi()
    {
        $start_date = date('Y-m-d', strtotime('-4 days'));
        $end_date = date('Y-m-d');

        $body = '';
        $Timestamp = date('Y-m-d')."T".date('H:i:s').".000".date('P');

        $relative_url = "/banking/v3/corporates/{$this->corporate_id}/accounts/{$this->account_number}/statements?EndDate={$end_date}&StartDate={$start_date}";
        $uri = "{$this->uri}{$relative_url}";

        $encode_body = strtolower(hash('sha256', $body));
        $StringToSign = "GET:{$relative_url}:{$this->access_token}:{$encode_body}:{$Timestamp}";
        $signature_bca = hash_hmac('sha256', $StringToSign, $this->api_secret);

        try {
            $client = new Client();
            $result = $client->get($uri, [
                'headers' => [
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    'Authorization' => "Bearer {$this->access_token}",
                    'Origin' => 'cron.lakuemas.com',
                    'X-BCA-Key' => $this->api_key,
                    'X-BCA-Timestamp' => $Timestamp,
                    'X-BCA-Signature' => $signature_bca
                ]
            ]);
        } catch (RequestException $e) {
            $responseBody = $e->getMessage();

            echo "<pre>";
            print_r(json_decode($responseBody));
            echo "</pre>";
        }
        $array = json_decode($result->getBody(), true);  
        print_r($array);
        //return $array['Data'];
    }

    private function get_saldo()
    {
        $start_date = date('Y-m-d');
        $end_date = date('Y-m-d');

        $body = '';
        $Timestamp = date('Y-m-d')."T".date('H:i:s').".000".date('P');

        $relative_url = "/banking/v3/corporates/{$this->corporate_id}/accounts/{$this->account_number}";
        $uri = "{$this->uri}{$relative_url}";

        $encode_body = strtolower(hash('sha256', $body));
        $StringToSign = "GET:{$relative_url}:{$this->access_token}:{$encode_body}:{$Timestamp}";
        $signature_bca = hash_hmac('sha256', $StringToSign, $this->api_secret);

        try {
            $client = new Client();
            $result = $client->get($uri, [
                'headers' => [
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    'Authorization' => "Bearer {$this->access_token}",
                    'Origin' => 'cron.lakuemas.com',
                    'X-BCA-Key' => $this->api_key,
                    'X-BCA-Timestamp' => $Timestamp,
                    'X-BCA-Signature' => $signature_bca
                ]
            ]);
        } catch (RequestException $e) {
            $responseBody = $e->getMessage();
            dd($responseBody);
        }
        $array = json_decode($result->getBody(), true);  
        print_r($array);
        //return $array['Data'];
    }

    private function get_currency()
    {
        $body = '';
        $Timestamp = date('Y-m-d')."T".date('H:i:s').".000".date('P');
        $relative_url = "/general/rate/forex?CurrencyCode=USD&RateType=erate";
        $uri = "{$this->uri}{$relative_url}";

        $encode_body = strtolower(hash('sha256', $body));
        $StringToSign = "GET:{$relative_url}:{$this->access_token}:{$encode_body}:{$Timestamp}";
        $signature_bca = hash_hmac('sha256', $StringToSign, $this->api_secret);

        try {
            $client = new Client();
            $result = $client->get($uri, [
                'headers' => [
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    'Authorization' => "Bearer {$this->access_token}",
                    'Origin' => 'cron.lakuemas.com',
                    'X-BCA-Key' => $this->api_key,
                    'X-BCA-Timestamp' => $Timestamp,
                    'X-BCA-Signature' => $signature_bca
                ]
            ]);
        } catch (RequestException $e) {
            $responseBody = $e->getMessage();
            dd($responseBody);
        }
        $array = json_decode($result->getBody(), true);
        print_r($array);
        //return $array['Data'];
    }
}