<?php
/*
 * FastPayApi
 * Version: 1.0.3
 * Date: 10.11.2024
 */
class FastPayApi {

    private $baseUrl = 'https://fast-pay.top/api/';
    private $shopId;
    private $shopKey;
	private $apiId;
	private $apiKey;

    public function __construct($shopId = null, $shopKey = null, $apiId = null, $apiKey = null) {
        $this->shopId = $shopId;
        $this->shopKey = $shopKey;
        $this->apiId = $apiId;
        $this->apiKey = $apiKey;
    }

    public function setShopId($shopId) {
        $this->shopId = $shopId;
    }

    public function setShopKey($shopKey) {
        $this->shopKey = $shopKey;
    }

    public function setApiId($apiId) {
        $this->apiId = $apiId;
    }

    public function setApiKey($apiKey) {
        $this->apiKey = $apiKey;
    }

    public function getCurrencyRate($coin) {
        $endpoint = 'currency-rate';
        $params = [
            'api_id' => $this->apiId,
            'api_key' => $this->apiKey,
            'coin' => $coin
        ];

        return $this->sendPostRequest($endpoint, $params);
    }
	
    public function getAddress($coin, $order_id) {
        $endpoint = 'get-address';
        $params = [
            'shop_id' => $this->shopId,
            'shop_key' => $this->shopKey,
			'order_id' => $order_id,
            'coin' => $coin
        ];

        return $this->sendPostRequest($endpoint, $params);
    }

    public function getUserBalance() {
        $endpoint = 'balance';
        $params = [
			'api_id' => $this->apiId,
            'api_key' => $this->apiKey
        ];

        return $this->sendPostRequest($endpoint, $params);
    }
	
	public function payout($amount, $coin, $order_id, $address, $is_subtract = true) {
        $endpoint = 'payout';
        $params = [
			'api_id' => $this->apiId,
            'api_key' => $this->apiKey,
			'amount' => $amount,
			'coin' => $coin,
			'order_id' => $order_id,
			'address' => $address,
			'is_subtract' => $is_subtract
        ];

        return $this->sendPostRequest($endpoint, $params);
    }
	
	public function payoutInfo($payout_id, $order_id) {
        $endpoint = 'payout-info';
        $params = [
			'api_id' => $this->apiId,
            'api_key' => $this->apiKey,
			'order_id' => $order_id,
			'payout_id' => $payout_id
        ];

        return $this->sendPostRequest($endpoint, $params);
    }
	
    private function sendPostRequest($endpoint, $params) {
		$url = $this->baseUrl . $endpoint;
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            return ['errors' => ['error' => 'Curl error: ' . curl_error($ch)]];
        }

        curl_close($ch);

		return $response;
    }
}
?>