<?php

class FlipPaymentGateway
{
    private $apiKey;
    private $apiUrl;

    // Konstruktor untuk inisialisasi API Key dan URL
    public function __construct($apiKey, $apiUrl)
    {
        $this->apiKey = $apiKey;
        $this->apiUrl = rtrim($apiUrl, '/');
    }

    // Fungsi untuk membuat transaksi
    public function createBill($title, $amount, $sender_email, $sender_name, $sender_phone_number, $sender_address, $urlback = '')
    {
        $endpoint = '/bill';
        if(!empty($urlback)){
            $data = [
                "title" => $title,
                "amount" => $amount,
                "type" => "MULTIPLE",
                "is_address_required" => 0,
                "is_phone_number_required" => 0,
                "step" => 2,
                "redirect_url" => $urlback,
                "sender_email" => $sender_email,
                "sender_name" => $sender_name,
                "sender_phone_number" => $sender_phone_number,
                "sender_address" => $sender_address
            ];
        } else {
            $data = [
                "title" => $title,
                "amount" => $amount,
                "type" => "MULTIPLE",
                "is_address_required" => 0,
                "is_phone_number_required" => 0,
                "step" => 2,
                "sender_email" => $sender_email,
                "sender_name" => $sender_name,
                "sender_phone_number" => $sender_phone_number,
                "sender_address" => $sender_address
            ];
        }
        // print_r($data);
        return $this->sendRequest('POST', $endpoint, $data);
    }
    //Fungsi edit transaksi
    public function editBill($linkid,$status,$title1, $amount1)
    {
        $endpoint = "/{$linkid}/bill";
        $data = [
            "title" => $title1,
            "amount" => $amount1,
            "type" => "MULTIPLE",
            "is_address_required" => 0,
            "is_phone_number_required" => 0,
            "status" => $status
        ];

        return $this->sendRequest('PUT', $endpoint, $data);
    }
    // Fungsi untuk memeriksa status transaksi
    public function getBillStatus($transactionId)
    {
        $endpoint = "/{$transactionId}/bill";

        return $this->sendRequest('GET', $endpoint);
    }

    // Fungsi untuk mengirimkan request ke API
    private function sendRequest($method, $endpoint, $data = null)
    {
        $url = $this->apiUrl . $endpoint;
        // echo $data;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);

        if ($method === 'POST') {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        } 
        elseif ($method === 'PUT') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

        } 
        elseif ($method == 'GET') { 
            
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          "Content-Type: application/x-www-form-urlencoded"
        ));
        curl_setopt($ch, CURLOPT_USERPWD, $this->apiKey.":");
        $response = curl_exec($ch);
        curl_close($ch);

        return json_decode($response, TRUE);
    }
}
?>