<?php
if (!defined("ROOT_PATH"))
{
	header("HTTP/1.1 403 Forbidden");
	exit;
}
class pjStripeSDK
{
	protected $merchantId;
	protected $privateKey;
	protected $publicKey;
	protected $sandbox;

	protected static $version = "2022-08-01";

	protected static $endPoint = "https://api.stripe.com/v1/";

	public function __construct($merchant_id=null, $public_key=null, $private_key=null, $sandbox=false)
	{
		$this->setMerchantId($merchant_id);
		$this->setPublicKey($public_key);
		$this->setPrivateKey($private_key);
		$this->setSandbox($sandbox);
	}

	public function getMerchantId()
	{
		return $this->merchantId;
	}

	public function getPrivateKey()
	{
		return $this->privateKey;
	}

	public function getPublicKey()
	{
		return $this->publicKey;
	}

	public function getSandbox()
	{
		return $this->sandbox;
	}

	public function setMerchantId($value)
	{
		$this->merchantId = $value;
		return $this;
	}

	public function setPrivateKey($value)
	{
		$this->privateKey = $value;
		return $this;
	}

	public function setPublicKey($value)
	{
		$this->publicKey = $value;
		return $this;
	}

	public function setSandbox($value)
	{
		$this->sandbox = (bool) $value;
		return $this;
	}

	protected function getEndPoint($path=null)
	{
		return self::$endPoint . $path;
	}

	protected function request($path=null, $params=null)
	{
		$http = new pjHttp();

		if ($params)
		{
			$http->setMethod("POST");
			if (!is_bool($params))
			{
				$http->setData($params);
			}
		} else {
			$http->setMethod("GET");
		}

		$http
			->addHeader("Authorization: Basic " . base64_encode($this->getPrivateKey() . ":"))
			->addHeader("Stripe-Version: " . self::$version)
			->curlRequest($this->getEndPoint($path));

		$error = $http->getError();
		if ($error)
		{
			return array('status' => 'ERR', 'code' => 100, 'text' => $error['text']);
		}

		$response = $http->getResponse();
		if (!$response)
		{
			return array('status' => 'ERR', 'code' => 102, 'text' => 'Empty response');
		}

		$result = json_decode($response, true);

		return array('status' => 'OK', 'code' => 200, 'text' => 'Success', 'result' => $result);
	}

	/**
	 * Runs a request and throws on any Stripe-level or transport-level error.
	 */
	protected function call($path, $params=null)
	{
		$data = $this->request($path, $params);
		if ($data['status'] != 'OK')
		{
			throw new Exception($data['text']);
		}

		$response = new pjStripeSDKResponse($data['result']);
		if (!$response->isOK())
		{
			$error = $response->getError();
			throw new Exception(@$error['message']);
		}

		return $data['result'];
	}

	/**
	 * Create a hosted Checkout Session.
	 */
	public function createSession($params)
	{
		return $this->call('checkout/sessions', $params);
	}

	/**
	 * Retrieve a single Checkout Session by its id.
	 */
	public function retrieveSession($session_id)
	{
		return $this->call('checkout/sessions/' . $session_id);
	}

	/**
	 * List today's events (used as a fallback to confirm a completed checkout).
	 */
	public function getEvents($limit=100)
	{
		list($year, $month, $day) = explode("-", date("Y-n-j"));

		$queryString = http_build_query(array(
			'type' => 'checkout.session.completed',
			'limit' => (int) $limit,
			'created' => array(
				'gte' => mktime(0, 0, 0, $month, $day, $year),
			),
		), '', '&');

		return $this->call('events?' . $queryString);
	}
}
?>
