<?php
if (!defined('ABSPATH')) exit;

class Dima_Gateway_Mellat {
    
    private $terminal_id;
    private $username;
    private $password;
    
    public function __construct() {
        $this->terminal_id = get_option('dima_mellat_terminal_id');
        $this->username = get_option('dima_mellat_username');
        $this->password = get_option('dima_mellat_password');
    }
    
    public function request_payment($order_id, $amount, $callback_url) {
        
        if (empty($this->terminal_id) || empty($this->username) || empty($this->password)) {
            return new WP_Error('no_credentials', 'اطلاعات درگاه ملت تنظیم نشده است');
        }
        
        try {
            $soap = new SoapClient('https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl');
            
            $date = date('Ymd');
            $time = date('His');
            
            $result = $soap->bpStartPayment(array(
                'terminalId' => intval($this->terminal_id),
                'userName' => $this->username,
                'userPassword' => $this->password,
                'callBackUrl' => $callback_url,
                'amount' => intval($amount),
                'localDate' => $date,
                'localTime' => $time,
                'orderId' => intval($order_id),
                'additionalData' => 'سفارش #' . $order_id
            ));
            
            $response = explode(',', $result->return);
            $res_code = $response[0];
            
            if ($res_code == '0') {
                $ref_id = $response[1];
                return array(
                    'success' => true,
                    'ref_id' => $ref_id,
                    'url' => 'https://bpm.shaparak.ir/pgwchannel/startpay.mellat',
                    'post_data' => array(
                        'RefId' => $ref_id
                    )
                );
            }
            
            return new WP_Error('payment_request_failed', 'خطا در اتصال به درگاه ملت - کد خطا: ' . $res_code);
            
        } catch (Exception $e) {
            return new WP_Error('soap_error', 'خطا در اتصال SOAP: ' . $e->getMessage());
        }
    }
    
    public function verify_payment($ref_id, $order_id, $sale_order_id, $sale_reference_id) {
        
        try {
            $soap = new SoapClient('https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl');
            
            // بررسی پرداخت
            $result = $soap->bpCheckRequest(array(
                'terminalId' => intval($this->terminal_id),
                'userName' => $this->username,
                'userPassword' => $this->password,
                'orderId' => intval($order_id),
                'saleOrderId' => intval($sale_order_id),
                'saleReferenceId' => intval($sale_reference_id)
            ));
            
            if ($result->return == '0') {
                // تایید پرداخت
                $result = $soap->bpSettleRequest(array(
                    'terminalId' => intval($this->terminal_id),
                    'userName' => $this->username,
                    'userPassword' => $this->password,
                    'orderId' => intval($order_id),
                    'saleOrderId' => intval($sale_order_id),
                    'saleReferenceId' => intval($sale_reference_id)
                ));
                
                if ($result->return == '0' || $result->return == '45') {
                    return array(
                        'success' => true,
                        'ref_id' => $sale_reference_id
                    );
                }
            }
            
            return false;
            
        } catch (Exception $e) {
            return false;
        }
    }
}