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

class Dima_Gateway_Zarinpal {
    
    private $merchant_id;
    private $sandbox = false;
    
    public function __construct() {
        $this->merchant_id = get_option('dima_zarinpal_merchant');
        $this->sandbox = get_option('dima_zarinpal_sandbox', false);
    }
    
    public function request_payment($order_id, $amount, $description, $callback_url, $customer_data = array()) {
        
        if (empty($this->merchant_id)) {
            return new WP_Error('no_merchant', 'Merchant ID زرین‌پال تنظیم نشده است');
        }
        
        $api_url = $this->sandbox ? 
            'https://sandbox.zarinpal.com/pg/v4/payment/request.json' : 
            'https://api.zarinpal.com/pg/v4/payment/request.json';
        
        $data = array(
            'MerchantID' => $this->merchant_id,
            'Amount' => intval($amount),
            'CallbackURL' => $callback_url,
            'Description' => $description,
            'Email' => isset($customer_data['email']) ? $customer_data['email'] : '',
            'Mobile' => isset($customer_data['phone']) ? $customer_data['phone'] : ''
        );
        
        $response = wp_remote_post($api_url, array(
            'body' => json_encode($data),
            'headers' => array(
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'
            ),
            'timeout' => 30
        ));
        
        if (is_wp_error($response)) {
            return $response;
        }
        
        $body = json_decode(wp_remote_retrieve_body($response), true);
        
        if (isset($body['data']['authority'])) {
            return array(
                'success' => true,
                'authority' => $body['data']['authority'],
                'url' => $this->get_payment_url($body['data']['authority'])
            );
        }
        
        $error_message = isset($body['errors']['message']) ? $body['errors']['message'] : 'خطا در اتصال به زرین‌پال';
        return new WP_Error('payment_request_failed', $error_message);
    }
    
    public function verify_payment($authority, $amount) {
        
        $api_url = $this->sandbox ? 
            'https://sandbox.zarinpal.com/pg/v4/payment/verify.json' : 
            'https://api.zarinpal.com/pg/v4/payment/verify.json';
        
        $data = array(
            'MerchantID' => $this->merchant_id,
            'Authority' => $authority,
            'Amount' => intval($amount)
        );
        
        $response = wp_remote_post($api_url, array(
            'body' => json_encode($data),
            'headers' => array(
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'
            ),
            'timeout' => 30
        ));
        
        if (is_wp_error($response)) {
            return false;
        }
        
        $body = json_decode(wp_remote_retrieve_body($response), true);
        
        if (isset($body['data']['code']) && $body['data']['code'] == 100) {
            return array(
                'success' => true,
                'ref_id' => $body['data']['ref_id']
            );
        }
        
        return false;
    }
    
    private function get_payment_url($authority) {
        if ($this->sandbox) {
            return 'https://sandbox.zarinpal.com/pg/StartPay/' . $authority;
        }
        return 'https://www.zarinpal.com/pg/StartPay/' . $authority;
    }
}