<?php

class SiTransfer {
    private $baseUrl  = "https://rest.sitranfer.com/payment/api";
    private $storeKey; // JANGAN DIISI MANUAL LAGI DISINI

    // TAMBAHIN INI BIAR BISA NANGKEP KEY DARI DATABASE
    public function __construct($key) {
        $this->storeKey = $key;
    }

    public function generateQRIS($playerUsername, $amount) {
        return $this->sendRequest('generate', [
            'channel'         => 'QRIS',
            'amount'          => $amount,
            'player_username' => $playerUsername
        ]);
    }
    public function generateDana($playerUsername, $amount) {
        return $this->sendRequest('generate', [
            'channel'         => 'DANA',
            'amount'          => $amount,
            'player_username' => $playerUsername
        ]);
    }
    public function checkStatus($transactionId) {
        return $this->sendRequest('status', [
            'transaction_id' => $transactionId
        ]);
    }
    public function payout($playerUsername, $accName, $accNumber, $bankTarget, $amount, $description = "") {
        return $this->sendRequest('payout', [
            'player_username' => $playerUsername,
            'account_name'    => $accName,
            'account_number'  => $accNumber,
            'bank_target'     => $bankTarget,
            'amount'          => $amount,
            'description'     => $description
        ]);
    }
    private function sendRequest($action, $params) {
        $fullUrl = $this->baseUrl . "/" . $action;
        
        $params['key'] = $this->storeKey;

        $ch = curl_init($fullUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $response = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);

        if ($error) {
            return ['success' => false, 'message' => 'Connection Error: ' . $error];
        }

        return json_decode($response, true);
    }
}