<?php
$customer_code=$_POST['customer_code'];
if($customer_code==''){
    $error=fl();set_message('Please enter customer code');
}else{
    $customer=$db->get_row('customers','customer_code',$customer_code);
    if(empty($customer)){$error=fl();set_message('Invalid customer code');}
}

if(!isset($error)){
    $data=[
        'customer_id'   => $customer['customer_id'],
        'amount'        => SUBSCRIBE_AMOUNT,
        'charge'        => SUBSCRIBE_CHARGE,
        'total'         => SUBSCRIBE_AMOUNT+SUBSCRIBE_CHARGE,
        'created_time'  => TIME
    ];
    $request_id=$db->insert('subscribe_request',$data,true);
    if($request_id){
        include_once ROOT_DIR.'/core/class/ssl.php';
        $ssl=new SSL();
        $invoice=$ssl->createInvoice(SUBSCRIBE_AMOUNT+SUBSCRIBE_CHARGE,"SC-$request_id",$customer,$jArray);
        if($invoice['status']==1){
            $subscribe_data=[
                'ssl'=>$invoice['ssl'],
            ];
            $data=[
                'gateway_id'=>$invoice['sessionkey'],
                'data'=>json_encode($subscribe_data)
            ];
            $update=$db->update('subscribe_request',$data,['id'=>$request_id]);
            $jArray['status']=1;
            $jArray['link']=$invoice['url']; 
        }
        else{
            $error=fl(); 
            set_message($invoice['message']);
        }
    }
    else{
        set_message_with_code(66);
    }
}