<?php
class sepal
{
    public static function create_request($api, $price, $transaction)
    {
        /*
        * return array
        * error_code => error
        * url => payment url
        * success => status (true , false)
        * authority => authority
        */
        $response = array('success' => false);
        $sand_box = get_option('subscription_sandbox');
        $sand_box        = (!empty($sand_box)) ? true : false;
        $links_info = array(
            'request' => 'https://sepal.ir/api/request.json',
            'payment' => 'https://sepal.ir/payment/'
        );
        if ($sand_box == true) {
            $links_info = array(
                'request' => 'https://sepal.ir/api/sandbox/request.json',
                'payment' => 'https://sepal.ir/sandbox/payment/'
            );
        }
        $callback    = SITE_URL . "/payment/" . $transaction . '/';
        $description    = "خرید اشتراک در " . get_bloginfo('name');
        $params = array(
            'apiKey' => (!empty($sand_box) && $sand_box == true) ? 'test' : $api,
            'amount' => $price * 10,
            'callbackUrl' => $callback,
            'invoiceNumber' => $transaction,
            'payerName' => '',
            'payerMobile' => '',
            'payerEmail' => '',
            'description' => $description
        );
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $links_info['request']);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type' => 'application/json'));
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result = json_decode(curl_exec($curl));
        curl_close($curl);
        if (isset($result->status) && $result->status == 1) {
            $response['success'] = true;
            $response['url'] = $links_info['payment'] . $result->paymentNumber;
            $response['authority'] = $result->paymentNumber;
        } else if (!empty($result->message)) $response['error_code'] = $result->message;
        return $response;
    }

    public static function verify_request($api, $price, $authority, $transaction)
    {
        /*
        * return array
        * error_code => error
        * success => status (true , false)
        */
        $response = array('success' => false);
        $sand_box   = get_option('subscription_sandbox');
        $sand_box   = (!empty($sand_box)) ? true : false;
        $links_info = array(
            'verify' => 'https://sepal.ir/api/verify.json'
        );
        if ($sand_box == true) {
            $links_info = array(
                'verify' => 'https://sepal.ir/api/sandbox/verify.json'
            );
        }
        $params = array(
            'apiKey' => (!empty($sand_box) && $sand_box == true) ? 'test' : $api,
            'paymentNumber' => $authority,
        );
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $links_info['verify']);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type' => 'application/json'));
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result = json_decode(curl_exec($curl));
        curl_close($curl);
        if (isset($result->status) && $result->status == 1) $response['success'] = true;
        else if (!empty($result->message)) $response['error_code'] = $result->message;
        return $response;
    }
}
