<?php
class SSL{
    public function createInvoice($amount,$ref,$customer,&$jArray=[]){
        $return=['status'=>0,'message'=>''];
        $post_data = array();
        $post_data['store_id'] = SSL_STORE_ID;
        $post_data['store_passwd'] = SSL_STORE_PASS;
        $post_data['total_amount'] = $amount;
        $post_data['tran_id'] = $ref;
        $post_data['currency'] = "BDT";
        $post_data['success_url']   = URL."?payment_notice=".$ref."&status=success";
        $post_data['fail_url']      = URL."?payment_notice=".$ref."&status=fail";
        $post_data['cancel_url']    = URL."?payment_notice=".$ref."&status=cancel";
        $post_data['product_amount'] =  $amount;
        
        $post_data['cus_name'] = $customer['customer_code'].' '.$customer['customer_name'];
        $post_data['cus_phone'] = $customer['customer_mobile'];
        $post_data['cus_email'] = $customer['customer_email'];
        $post_data['value_a'] = "WEB";
        /*$post_data['value_b'] = "";
        $post_data['value_c'] = "";
        $post_data['value_d'] = "";*/

        $jArray[fl()]=$post_data;

        $handle = curl_init();
        curl_setopt($handle, CURLOPT_URL, SSL_API_LINK );
        curl_setopt($handle, CURLOPT_TIMEOUT, 30);
        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($handle, CURLOPT_POST, 1 );
        curl_setopt($handle, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE); # KEEP IT FALSE IF YOU RUN FROM LOCAL PC


        $content = curl_exec($handle );
        $jArray[fl()]=$content;
        $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

        if($code == 200 && !( curl_errno($handle))) {
            curl_close( $handle);
            $sslcommerzResponse = $content;
        } else {
            curl_close( $handle);
        }
        // PARSE THE JSON RESPONSE
        $sslcz = json_decode($sslcommerzResponse, true );
        $jArray['ssl']= $sslcz;

        if(isset($sslcz['GatewayPageURL']) && $sslcz['GatewayPageURL']!="" ) {
            $return['status']   = 1;
            $return['url']      = $sslcz['GatewayPageURL'];
            $return['sessionkey']= $sslcz['sessionkey'];
            $return['ssl']= $sslcz;
        }
        else{
            $return['message']='SSL commerz error';
        }


        return $return;
    }

    public function payment_check($sessionkey,&$jArray=[]){
        $jArray[fl()]=$sessionkey;
        $requested_url = SSL_VALIDATOR_LINK."?sessionkey=".$sessionkey."&store_id=".SSL_STORE_ID."&store_passwd=".SSL_STORE_PASS."&v=1&format=json";
        $jArray[fl()]=$requested_url;
        $handle = curl_init(); 
        curl_setopt($handle, CURLOPT_URL, $requested_url); 
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); 
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); 
        $result = curl_exec($handle);
        $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
        $jArray[fl()]=$result;
        if($code == 200 && !( curl_errno($handle))) {
            $result = json_decode($result,true); 
            $jArray[fl()]=$result;
            if($result['status'] == 'VALIDATED'||$result['status'] == 'VALID'){

                $return['status']   = 1;
                $return['amount']   = $result['amount'];
                $return['tran_id']   = $result['tran_id'];

            }else{
                $return['message']='SSL commerz error';
            }
        }else{$return['message']='SSL commerz error';}

        return $return;
    }
}