<?php
defined('ABSPATH') || exit;
if (!class_exists('yascode_gateway')) :
    class yascode_gateway
    {
        public static function create_payment($plan_id, $gateway_id, $discount_code = null)
        {
            $msg = '';
            $sts = 'error';
            $gateways = self::get_subscription_gateways();
            if (!empty($gateways)) {
                foreach ($gateways as $gateway) {
                    if (!empty($gateway['id']) && $gateway['id'] == $gateway_id && !empty($gateway['status']) && $gateway['status'] == 'active') {
                        if (file_exists(THEME_TEMPLATE_URL_PATH . '/app/class/gateways/' . $gateway_id . '-gateway.php')) {
                            $gateway_api = (!empty($gateway['api'])) ? $gateway['api'] : '';
                            if (!empty($gateway_api)) {
                                require_once(THEME_TEMPLATE_URL_PATH . '/app/class/gateways/' . $gateway_id . '-gateway.php');
                                $user_id = get_current_user_id();
                                $hash = hash('md4', time() . $user_id . $plan_id . rand());
                                $plan_price = yascode_vip::get_plan_price($plan_id, $discount_code);
                                $plan_days = yascode_vip::get_plan_days($plan_id);
                                $payment_price = (is_numeric($plan_price['price']) && $plan_price['price'] >= 0) ? $plan_price['price'] : null;
                                $payment_code = (!empty($plan_price['code'])) ? $plan_price['code'] : null;
                                $payment_code_percent = (!empty($plan_price['percent'])) ? $plan_price['percent'] : 0;
                                if (is_numeric($payment_price) && $payment_price > 0) {
                                    $request = $gateway_id::create_request($gateway_api, $payment_price, $hash);
                                    if ($request['success'] == true && !empty($request['url'])) {
                                        $created = yascode_vip::create_payment_row($user_id, $gateway_id, $hash, $request['authority'], $payment_price, $plan_days, $payment_code, $payment_code_percent);
                                        if ($created) {
                                            wp_redirect($request['url']);
                                            exit;
                                        } else $msg = 'خطایی پیش آمد';
                                    } else if (!empty($request['error_code'])) {
                                        $msg = 'خطایی پیش آمد! خطا: ' . $request['error_code'];
                                    } else $msg = 'خطایی پیش آمد';
                                } else if (is_numeric($payment_price) && $payment_price == 0) {
                                    $created = yascode_vip::create_payment_row($user_id, '', $hash, null, $payment_price, $plan_days, $payment_code, $payment_code_percent, 'success');
                                    yascode_vip::add_subscriber($user_id, $plan_days);
                                    $msg = 'پرداخت شما با موفقیت ثبت شد';
                                    $sts = 'ok';
                                }
                            } else $msg = 'کلید api نامعتبر است';
                        } else $msg = 'درگاه نامعتبر';
                    }
                }
            } else $msg = 'درگاهی یافت نشد';
            wp_redirect(PANEL_URL . '/plans?status=' . $sts . '&msg=' . $msg);
        }

        public static function get_subscription_gateways()
        {
            $gateways = yc_get_option('yascode_gateways');
            $result = (!empty($gateways) && is_array($gateways)) ? $gateways : array();
            return $result;
        }
    }
endif;
