<?php

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

/**
 * @property Invoice_model $Invoice_model
 */
class Paypal extends CI_Controller
{
    protected $authorizedRoutes = [];
    protected $authorizedAdmin = [];
    private $json;

    public function __construct()
    {
        parent::__construct();
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        // log_message('debug', json_encode($this->input->get()));
        //    $this->load->database();

        // $this->json = json_decode($this->security->xss_clean($this->input->raw_input_stream), true);
    }

    // public function index()
    // {
    //     // log_message('debug', 'paypal');


    //     if (!empty($params['fcm_token'])) $fcm_token = '&fcm_token=' . $params['fcm_token'];
    //     unset($params['fcm_token']);

    //     $this->load->model("Order_model");
    //     $this->load->model('Order_payment_paypal_model');
    //     $this->load->model('Order_payment_model');
    //     $this->load->model('Order_output_model');
    //     // $this->Order_model->truncate();
    //     // $this->Order_payment_paypal_model->truncate();

    //     return $this->Order_model->paypal_checkout($params);
    // }

    // Invoice as product, a special flow
    public function index()
    {
        $encoded = $this->input->get('encoded');
        $tenant_no = $this->input->get('tenant_no');
        $invoice_ids = json_decode($this->input->get('invoice_ids'), true);

        $this->load->model("Invoice_model");
        $params = json_decode($encoded, true);
        $this->Invoice_model->setTenantNo($tenant_no);

        $params['product_json'] = $params['item_list'];
        $params['remarks'] = null;
        unset($params['item_list']);
        return $this->Invoice_model->paypal_checkout($params, $invoice_ids);
    }

    public function test_invoice()
    {
        $tenant_no = "main";
        $invoice_ids = "1";

        $params = [
            "payment_description" => "付款",
            "payment_total" => 540.00,
            "payment_currency" => 'HKD',
            "total_quantity" => 1,
            "product_json" => json_decode('[{"id":1,"name":"HQIN21050003 ,"quantity":1,"currency":"HKD","price":540}]', true),
            "billing_address_1" => 'Billing address 1',
            "billing_address_2" => 'Billing address 2' ?? '',
            "billing_address_3" => 'Billing address 3' ?? '',
            "shipping_address_1" => 'Shipping address 1',
            "shipping_address_2" => 'Shipping address 2' ?? '',
            "shipping_address_3" => 'Shipping address 3' ?? '',
            "member_id" => 1,
            "remarks" => null,
        ];

        $this->load->model("Invoice_model");
        $this->Invoice_model->setTenantNo($tenant_no);
        return $this->Invoice_model->paypal_checkout($params, $invoice_ids);
    }

    // TODO: update record in database
    public function success()
    {
        $invoice_ids_str = $this->input->get('invoice_ids');
        $payment_id = $this->input->get('paymentId');
        $payer_id =  $this->input->get('PayerID');
        $order_type = $this->input->get('order_type');

        $invoice_ids = explode(',', $invoice_ids_str);
        $this->load->model('Invoice_model');
        $this->Invoice_model->paypal_checkout_success($invoice_ids, $payment_id, $payer_id, $order_type);


        echo '你已成功在Paypal付款!';
    }

    public function cancel()
    {
        $invoice_ids = $this->input->get('invoice_ids');
        $payment_id = $this->input->get('paymentId');
        $payer_id =  $this->input->get('PayerID');

        $this->load->model('Invoice_model');
        $this->Invoice_model->paypal_checkout_cancelled($invoice_ids, $payment_id, $payer_id);
        echo '你已取消在Paypal付款的操作!';
    }

    protected function is_get()
    {
        $this->input->method(false) == 'get' or exit(json_encode([]));
    }


    protected function is_post()
    {
        $this->input->method(false) == 'post' or exit(json_encode([]));
    }

    protected function is_json()
    {
        count($this->json) > 0 or exit(json_encode([]));
    }
}
