| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway; |
| 4 |
|
| 5 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 6 |
|
| 7 |
// Exit if accessed directly |
| 8 |
use const OPENSSL_VERSION_TEXT; |
| 9 |
use const PHP_VERSION; |
| 10 |
use function sprintf; |
| 11 |
use function var_dump; |
| 12 |
use Payplug\PayplugWoocommerce\Gateway\PayplugPermissions; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
class PayplugGatewayRequirements { |
| 19 |
|
| 20 |
const PHP_MIN = '5.6'; |
| 21 |
const OPENSSL_MIN = 268439567; |
| 22 |
const OPENSSL_MIN_TEXT = 'OpenSSL 1.0.1 14 Mar 2012'; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var PayplugGateway |
| 26 |
*/ |
| 27 |
private $gateway; |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* @var PayplugPermissions |
| 32 |
*/ |
| 33 |
private $permissions; |
| 34 |
|
| 35 |
/** |
| 36 |
* Gateway settings. |
| 37 |
* |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
protected $settings; |
| 41 |
|
| 42 |
/** |
| 43 |
* PayplugGatewayRequirements constructor. |
| 44 |
* |
| 45 |
* @param PayplugGateway $gateway |
| 46 |
*/ |
| 47 |
public function __construct( PayplugGateway $gateway ) { |
| 48 |
$this->gateway = $gateway; |
| 49 |
$this->permissions = new PayplugPermissions($gateway); |
| 50 |
$this->settings = get_option( 'woocommerce_payplug_settings', [] ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Check if all gateway requirements are fulfilled. |
| 55 |
* |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public function satisfy_requirements() { |
| 59 |
return $this->valid_php() |
| 60 |
&& $this->valid_curl() |
| 61 |
&& $this->valid_openssl() |
| 62 |
&& $this->valid_currency() |
| 63 |
&& $this->valid_account(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function curl_requirement() { |
| 70 |
return array( |
| 71 |
"status" => $this->valid_curl(), |
| 72 |
"text" => __("payplug_section_status_curl", "payplug") |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @return array |
| 78 |
*/ |
| 79 |
public function php_requirement() { |
| 80 |
return array( |
| 81 |
"status" => $this->valid_php(), |
| 82 |
"text" => __("payplug_section_status_php", "payplug") |
| 83 |
); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
public function openssl_requirement() { |
| 91 |
return array( |
| 92 |
"status" => $this->valid_openssl(), |
| 93 |
"text" => __("payplug_section_status_ssl", "payplug") |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public function currency_requirement() { |
| 101 |
return array( |
| 102 |
"status" => $this->valid_currency(), |
| 103 |
"text" => __("payplug_section_status_currency", "payplug") |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
public function account_requirement() { |
| 111 |
return array( |
| 112 |
"status" => $this->valid_account(), |
| 113 |
"text" => __("payplug_section_status_account", "payplug") |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public function oney_requirement() { |
| 121 |
return ($this->valid_oney_check()) |
| 122 |
? '<p class="success">' . __( 'Oney is active on your store.', 'payplug' ) . '</p>' |
| 123 |
: ''; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Check if CURL is available and support SSL |
| 128 |
* |
| 129 |
* @return bool |
| 130 |
*/ |
| 131 |
public function valid_curl() { |
| 132 |
if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' ) ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
// Also Check for SSL support |
| 137 |
$curl_version = curl_version(); |
| 138 |
if ( ! ( CURL_VERSION_SSL & $curl_version['features'] ) ) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Check if PHP version is equal or above the minimum supported. |
| 147 |
* |
| 148 |
* @return bool |
| 149 |
*/ |
| 150 |
public function valid_php() { |
| 151 |
return version_compare( PHP_VERSION, self::PHP_MIN, '>=' ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Check if OPENSSL version is equal or above the minimum supported. |
| 156 |
* |
| 157 |
* @return bool |
| 158 |
*/ |
| 159 |
public function valid_openssl() { |
| 160 |
return OPENSSL_VERSION_NUMBER >= self::OPENSSL_MIN; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Check if the shop currency is Euro. |
| 165 |
* |
| 166 |
* @return bool |
| 167 |
*/ |
| 168 |
public function valid_currency() { |
| 169 |
return 'EUR' === get_woocommerce_currency(); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Check if the user is logged in. |
| 174 |
* |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
public function valid_account() { |
| 178 |
return $this->gateway->user_logged_in(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* Check if the user is in live mode and has activated oney |
| 184 |
* |
| 185 |
* @return bool |
| 186 |
*/ |
| 187 |
public function valid_oney_check() { |
| 188 |
if (!isset($this->settings['mode']) || !isset($this->settings['oney'])) { |
| 189 |
return false; |
| 190 |
} |
| 191 |
return ("yes" === $this->settings['mode'] && "yes" === $this->settings['oney'] && $this->permissions->has_permissions(PayplugPermissions::USE_ONEY)); |
| 192 |
} |
| 193 |
} |
| 194 |
|