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

class Dima_Gateway_IDPay {
    
    private $api_key;
    private $sandbox = false;
    
    public function __construct() {
        $this->api_key = get_option('dima_idpay_api_key');
        $this->sandbox = get_option('dima_idpay_sandbox', false);
    }
    
    public function request_payment($order_id, $amount, $callback_url, $customer_data = array()) {
        
        if (empty($this->api_key)) {
            return new WP_Error('no_api_key', 'API Key IDPay تنظیم نشده است');
        }
        
        $data = array(
            'order_id' => $order_id,
            'amount' => intval($amount),
            'callback' => $callback_url,
            'name' => isset($customer_data['name']) ? $customer_data['name'] : '',
            'phone' => isset($customer_data['phone']) ? $customer_data['phone'] : '',
            'mail' => isset($customer_data['email']) ? $customer_data['email'] : '',
            'desc' => 'پرداخت سفارش #' . $order_id
        );
        
        $response = wp_remote_post('https://api.idpay.ir/v1.1/payment', array(
            'body' => json_encode($data),
            'headers' => array(
                'Content-Type' => 'application/json',
                'X-API-KEY' => $this->api_key,
                'X-SANDBOX' => $this->sandbox ? '1' : '0'
            ),
            'timeout' => 30
        ));
        
        if (is_wp_error($response)) {
            return $response;
        }
        
        $body = json_decode(wp_remote_retrieve_body($response), true);
        
        if (isset($body['id']) && isset($body['link'])) {
            return array(
                'success' => true,
                'transaction_id' => $body['id'],
                'url' => $body['link']
            );
        }
        
        $error_message = isset($body['error_message']) ? $body['error_message'] : 'خطا در اتصال به IDPay';
        return new WP_Error('payment_request_failed', $error_message);
    }
    
    public function verify_payment($transaction_id, $order_id) {
        
        $data = array(
            'id' => $transaction_id,
            'order_id' => $order_id
        );
        
        $response = wp_remote_post('https://api.idpay.ir/v1.1/payment/verify', array(
            'body' => json_encode($data),
            'headers' => array(
                'Content-Type' => 'application/json',
                'X-API-KEY' => $this->api_key,
                'X-SANDBOX' => $this->sandbox ? '1' : '0'
            ),
            'timeout' => 30
        ));
        
        if (is_wp_error($response)) {
            return false;
        }
        
        $body = json_decode(wp_remote_retrieve_body($response), true);
        
        if (isset($body['status']) && in_array($body['status'], array(100, 101))) {
            return array(
                'success' => true,
                'track_id' => isset($body['track_id']) ? $body['track_id'] : '',
                'payment_id' => isset($body['payment']['track_id']) ? $body['payment']['track_id'] : ''
            );
        }
        
        return false;
    }
    
    public function inquiry_payment($transaction_id, $order_id) {
        
        $data = array(
            'id' => $transaction_id,
            'order_id' => $order_id
        );
        
        $response = wp_remote_post('https://api.idpay.ir/v1.1/payment/inquiry', array(
            'body' => json_encode($data),
            'headers' => array(
                'Content-Type' => 'application/json',
                'X-API-KEY' => $this->api_key,
                'X-SANDBOX' => $this->sandbox ? '1' : '0'
            ),
            'timeout' => 30
        ));
        
        if (is_wp_error($response)) {
            return false;
        }
        
        $body = json_decode(wp_remote_retrieve_body($response), true);
        
        return $body;
    }
}