<?php
require_once(FCPATH . 'vendor/autoload.php');

defined('BASEPATH') OR exit('No direct script access allowed');

class Stripe extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
	{
		parent::__construct();
		\Stripe\Stripe::setApiKey($this->config->item('stripe')['secret_key']);
	}

	public function index()
	{
		echo "<h1>Access Denied</h1>";
		exit;
	}

	public function config()
	{
		echo json_encode(['publicKey' => $this->config->item('stripe')['publishable_key']]);
	}

	public function create_checkout_session()
	{
		$request = $this->input->post();
		$checkout_session = \Stripe\Checkout\Session::create([
			'success_url' => base_url() . 'selling/success?session_id={CHECKOUT_SESSION_ID}',
			'cancel_url' => base_url() . 'selling/fail',
			'payment_method_types' => ['card'],
			'mode' => 'payment',
			'line_items' => [[
				'price_data' => [
					'currency' => 'hkd',
					'product_data' => [
						'name' => '「網上企業教練證書課程」',
					],
					'unit_amount' => 320000,
				],
				'quantity' => 1,
			]],
		]);

		echo json_encode(['sessionId' => $checkout_session['id']]);
	}

}
