Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/ecommerce/Config.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Tutor pro ecommerce gateway config
4 + *
5 + * Extends Tutor settings for payment configuration
6 + *
7 + * @package TutorPro\Ecommerce
8 + * @author Themeum <support@themeum.com>
9 + * @link https://themeum.com
10 + * @since 1.0.0
11 + */
12 +
13 + namespace TutorPro\Ecommerce;
14 +
15 + use Tutor\Helpers\HttpHelper;
16 + use Tutor\Traits\JsonResponse;
17 + use Tutor\Ecommerce\OptionKeys;
18 +
19 + /**
20 + * Init payment gateway init class
21 + */
22 + class Config {
23 +
24 + use JsonResponse;
25 +
26 + /**
27 + * Register hooks
28 + *
29 + * @since 1.0.0
30 + */
31 + public function __construct() {
32 + add_action( 'tutor_payment_gateways', array( $this, 'extend_tutor_gateways' ) );
33 + }
34 +
35 + /**
36 + * Get available payment gateways
37 + *
38 + * @since 1.0.0
39 + *
40 + * @throws \Throwable Throw throwable if exception occur.
41 + *
42 + * @return array
43 + */
44 + public static function get_payment_gateways(): array {
45 + try {
46 + $gateways = self::fetch_available_gateways();
47 + $gateways = (array) $gateways;
48 +
49 + $pro_gateways = array();
50 +
51 + foreach ( $gateways as $key => $gateway ) {
52 + $gateway_arr = (array) $gateway;
53 + $basename = "tutor-$key/tutor-{$key}.php";
54 +
55 + $gateway_arr['name'] = $key;
56 + $gateway_arr['is_installed'] = self::is_installed( $basename );
57 + $gateway_arr['is_plugin_active'] = is_plugin_active( $basename );
58 + $gateway_arr['is_active'] = self::is_active( $key );
59 + $gateway_arr['icon'] = $gateway->icon;
60 + $gateway_arr['latest_version'] = $gateway->latest_version;
61 +
62 + // Override.
63 + $gateway_arr['support_subscription'] = 'Yes' === $gateway->support_subscription;
64 + $gateway_arr['is_installable'] = 'Yes' === $gateway->is_installable;
65 + $gateway_arr['fields'] = self::get_config_fields( $key );
66 +
67 + $pro_gateways[ $key ] = $gateway_arr + self::get_plugin_info( $basename, $gateway->latest_version );
68 + }
69 +
70 + return apply_filters( 'tutor_pro_payment_gateways', $pro_gateways );
71 + } catch ( \Throwable $th ) {
72 + throw $th;
73 + }
74 + }
75 +
76 + /**
77 + * Fetch available payment gateways
78 + *
79 + * @since 3.0.0
80 + *
81 + * @throws \Exception Throw exception if any error found.
82 + *
83 + * @return array
84 + */
85 + private static function fetch_available_gateways() {
86 + $domain = site_url();
87 + $license_info = get_option( 'tutor_license_info' );
88 + $license_key = $license_info ? $license_info['license_key'] : '';
89 +
90 + $data = array(
91 + 'domain' => $domain,
92 + 'license_key' => $license_key,
93 + );
94 +
95 + if ( tutor_is_dev_mode() ) {
96 + $api_url = 'https://tutor.prismbuilder.com/wp-json/themeum-products/v1/tutor/payment-gateways';
97 + } else {
98 + $api_url = 'https://tutorlms.com/wp-json/themeum-products/v1/tutor/payment-gateways';
99 + }
100 +
101 + $remote_post = HttpHelper::post( $api_url, $data );
102 + if ( is_wp_error( $remote_post ) ) {
103 + throw new \Exception( $remote_post->get_error_message() );
104 + } else {
105 + $status_code = $remote_post->get_status_code();
106 + $res_body = json_decode( stripslashes( $remote_post->get_body() ) );
107 + if ( 200 === $status_code ) {
108 + return $res_body->body_response;
109 + } else {
110 + throw new \Exception( $res_body->response ?? 'Failed to fetch payment gateways' );
111 + }
112 + }
113 + }
114 +
115 + /**
116 + * Check whether a payment gateway is active
117 + *
118 + * @since 1.0.0
119 + *
120 + * @param string $gateway Gateway key name.
121 + *
122 + * @see get_payment_gateways method
123 + *
124 + * @return boolean
125 + */
126 + public static function is_active( string $gateway ): bool {
127 + $payments = tutor_utils()->get_option( OptionKeys::PAYMENT_SETTINGS );
128 + $payments = json_decode( stripslashes( $payments ) );
129 +
130 + if ( $payments ) {
131 + foreach ( $payments->payment_methods as $method ) {
132 + if ( $method->name === $gateway ) {
133 + return (bool) $method->is_active;
134 + }
135 + }
136 + }
137 +
138 + return false;
139 + }
140 +
141 + /**
142 + * Check whether a payment gateway is connected
143 + *
144 + * This method will return true if package available
145 + * in the source codebase: ecommerce/PaymentGateways/Package/Package.php
146 + *
147 + * @since 1.0.0
148 + *
149 + * @param string $plugin_basename Gateway label name. This name should
150 + * be same as package dir & main file name. For ex: AliPay, Eway etc.
151 + *
152 + * @see get_payment_gateways method
153 + *
154 + * @return boolean
155 + */
156 + public static function is_installed( string $plugin_basename ): bool {
157 + return file_exists( trailingslashit( WP_PLUGIN_DIR ) . $plugin_basename );
158 + }
159 +
160 + /**
161 + * Construct config fields based on gateway
162 + *
163 + * This method will return an array of config fields based on the provided gateway key.
164 + *
165 + * @since 1.0.0
166 + *
167 + * @param string $gateway Gateway name.
168 + *
169 + * @return array
170 + */
171 + public static function get_config_fields( string $gateway ): array {
172 + $config_keys_method = 'get_' . $gateway . '_config_keys';
173 +
174 + if ( ! method_exists( __CLASS__, $config_keys_method ) ) {
175 + return array();
176 + }
177 +
178 + $config_keys = self::$config_keys_method();
179 + $config_fields = array();
180 +
181 + foreach ( $config_keys as $key => $type ) {
182 + if ( 'environment' === $type ) {
183 + $config_fields[] = array(
184 + 'name' => $key,
185 + 'type' => 'select',
186 + 'options' => self::get_payment_environments(),
187 + 'label' => ucfirst( str_replace( '_', ' ', $key ) ),
188 + 'value' => 'test',
189 + );
190 + } elseif ( 'alipay_region' === $type ) {
191 + $config_fields[] = array(
192 + 'name' => $key,
193 + 'type' => 'select',
194 + 'options' => array(
195 + 'na' => __( 'North America', 'tutor-pro' ),
196 + 'asia' => __( 'Asia', 'tutor-pro' ),
197 + ),
198 + 'label' => ucfirst( str_replace( '_', ' ', $key ) ),
199 + 'value' => 'na',
200 + );
201 + } elseif ( 'options' === $type ) {
202 + $options = call_user_func( array( __CLASS__, 'get_' . $gateway . '_options' ) );
203 + $config_fields[] = array(
204 + 'name' => $key,
205 + 'type' => 'select',
206 + 'options' => $options,
207 + 'label' => ucfirst( str_replace( '_', ' ', $key ) ),
208 + 'value' => $options ? array_keys( $options )[0] : '',
209 + );
210 + } else {
211 + $config_fields[] = array(
212 + 'name' => $key,
213 + 'type' => $type,
214 + 'label' => ucfirst( str_replace( '_', ' ', $key ) ),
215 + 'value' => '',
216 + );
217 + }
218 + }
219 +
220 + return $config_fields;
221 + }
222 +
223 + /**
224 + * Get payment environments
225 + *
226 + * @since 1.0.0
227 + *
228 + * @return array
229 + */
230 + public static function get_payment_environments() {
231 + return array(
232 + 'test' => __( 'Test', 'tutor-pro' ),
233 + 'live' => __( 'Live', 'tutor-pro' ),
234 + );
235 + }
236 +
237 + /**
238 + * Get Razorpay config keys
239 + *
240 + * @since 1.0.0
241 + *
242 + * @return array
243 + */
244 + public static function get_razorpay_config_keys() {
245 + return array(
246 + 'environment' => 'environment',
247 + 'key_id' => 'secret_key',
248 + 'key_secret' => 'secret_key',
249 + 'webhook_secret' => 'secret_key',
250 + 'webhook_url' => 'webhook_url',
251 + );
252 + }
253 +
254 + /**
255 + * Get AmazonPay config keys
256 + *
257 + * @since 1.0.0
258 + *
259 + * @return array
260 + */
261 + public static function get_amazonpay_config_keys() {
262 + return array(
263 + 'environment' => 'environment',
264 + 'store_id' => 'secret_key',
265 + 'merchant_id' => 'secret_key',
266 + 'public_key_id' => 'secret_key',
267 + );
268 + }
269 +
270 + /**
271 + * Get Paddle config keys
272 + *
273 + * @since 1.0.0
274 + *
275 + * @return array
276 + */
277 + public static function get_paddle_config_keys() {
278 + return array(
279 + 'environment' => 'environment',
280 + 'api_key' => 'secret_key',
281 + 'webhook_secret_key' => 'secret_key',
282 + 'client_side_token' => 'secret_key',
283 + 'webhook_url' => 'webhook_url',
284 + );
285 + }
286 +
287 +
288 + /**
289 + * Get PayStack config keys
290 + *
291 + * @since 1.0.0
292 + *
293 + * @return array
294 + */
295 + public static function get_paystack_config_keys() {
296 + return array(
297 + 'environment' => 'environment',
298 + 'secret_key' => 'secret_key',
299 + 'webhook_url' => 'webhook_url',
300 + );
301 + }
302 +
303 + /**
304 + * Get Redsys config keys
305 + *
306 + * @since 1.0.0
307 + *
308 + * @return array
309 + */
310 + public static function get_redsys_config_keys() {
311 + return array(
312 + 'environment' => 'environment',
313 + 'merchant_code' => 'secret_key',
314 + 'trade_key' => 'secret_key',
315 + 'terminal' => 'secret_key',
316 + );
317 + }
318 +
319 + /**
320 + * Get Authorize.Net config keys
321 + *
322 + * @since 1.0.0
323 + *
324 + * @return array
325 + */
326 + public static function get_authorizenet_config_keys() {
327 + return array(
328 + 'environment' => 'environment',
329 + 'login_id' => 'secret_key',
330 + 'transaction_key' => 'secret_key',
331 + 'signature_key' => 'secret_key',
332 + 'webhook_url' => 'webhook_url',
333 + );
334 + }
335 +
336 + /**
337 + * Get Moneris config keys
338 + *
339 + * @since 1.0.0
340 + *
341 + * @return array
342 + */
343 + public static function get_moneris_config_keys() {
344 + return array(
345 + 'environment' => 'environment',
346 + 'store_id' => 'secret_key',
347 + 'api_token' => 'secret_key',
348 + 'checkout_id' => 'secret_key',
349 + );
350 + }
351 +
352 + /**
353 + * Get PayFast config keys
354 + *
355 + * @since 1.0.0
356 + *
357 + * @return array
358 + */
359 + public static function get_payfast_config_keys() {
360 + return array(
361 + 'environment' => 'environment',
362 + 'merchant_id' => 'secret_key',
363 + 'merchant_key' => 'secret_key',
364 + 'pass_phrase' => 'secret_key',
365 + );
366 + }
367 +
368 + /**
369 + * Get Mollie config keys
370 + *
371 + * @since 1.0.0
372 + *
373 + * @return array
374 + */
375 + public static function get_mollie_config_keys() {
376 + return array(
377 + 'environment' => 'environment',
378 + 'api_key' => 'secret_key',
379 + );
380 + }
381 +
382 + /**
383 + * Get Klarna config keys
384 + *
385 + * @since 1.0.0
386 + *
387 + * @return array
388 + */
389 + public static function get_klarna_config_keys() {
390 + return array(
391 + 'environment' => 'environment',
392 + 'username' => 'secret_key',
393 + 'password' => 'secret_key',
394 + 'region' => 'options',
395 + );
396 + }
397 +
398 + /**
399 + * Get QuickPay config keys
400 + *
401 + * @since 1.0.0
402 + *
403 + * @return array
404 + */
405 + public static function get_quickpay_config_keys() {
406 + return array(
407 + 'environment' => 'environment',
408 + 'api_key' => 'secret_key',
409 + 'private_key' => 'secret_key',
410 + );
411 + }
412 +
413 + /**
414 + * Get AliPay config keys
415 + *
416 + * @since 1.0.0
417 + *
418 + * @return array
419 + */
420 + public static function get_alipay_config_keys() {
421 + return array(
422 + 'environment' => 'environment',
423 + 'client_id' => 'secret_key',
424 + 'private_key' => 'secret_key',
425 + 'public_key' => 'secret_key',
426 + 'region' => 'alipay_region',
427 + );
428 + }
429 +
430 + /**
431 + * Get Eway config keys
432 + *
433 + * @since 1.0.0
434 + *
435 + * @return array
436 + */
437 + public static function get_eway_config_keys() {
438 + return array(
439 + 'environment' => 'environment',
440 + 'api_key' => 'secret_key',
441 + 'api_password' => 'secret_key',
442 + );
443 + }
444 +
445 + /**
446 + * Get stripe config keys
447 + *
448 + * @since 1.0.0
449 + *
450 + * @return array
451 + */
452 + public static function get_stripe_config_keys() {
453 + return array(
454 + 'environment' => 'environment',
455 + 'public_key' => 'secret_key',
456 + 'secret_key' => 'secret_key',
457 + 'webhook_signature_key' => 'secret_key',
458 + 'webhook_url' => 'webhook_url',
459 + );
460 + }
461 +
462 + /**
463 + * Get plugin info
464 + *
465 + * @since 3.0.0
466 + *
467 + * @param string $gateway_basename Gateway slug.
468 + * @param mixed $latest_version Latest version.
469 + *
470 + * @return array
471 + */
472 + public static function get_plugin_info( string $gateway_basename, $latest_version ) {
473 + // Ensure the WordPress functions are loaded.
474 + if ( ! function_exists( 'get_plugins' ) ) {
475 + require_once ABSPATH . 'wp-admin/includes/plugin.php';
476 + }
477 +
478 + $all_plugins = get_plugins();
479 + $plugin_info = array(
480 + 'active_version' => null,
481 + 'update_available' => false,
482 + );
483 +
484 + if ( in_array( $gateway_basename, array_keys( $all_plugins ) ) ) {
485 + $details = $all_plugins[ $gateway_basename ];
486 +
487 + $plugin_info['active_version'] = $details['Version'];
488 +
489 + // Compare the active version with the latest version.
490 + if ( $latest_version && version_compare( $details['Version'], $latest_version, '<' ) ) {
491 + $plugin_info['update_available'] = true;
492 + }
493 + }
494 +
495 + return $plugin_info;
496 + }
497 +
498 + /**
499 + * Add pro payment gateways with default
500 + *
501 + * @since 3.0.0
502 + *
503 + * @param array $payment_gateways Tutor default payment gateways.
504 + *
505 + * @return array
506 + */
507 + public function extend_tutor_gateways( $payment_gateways ) {
508 + try {
509 + $gateways = self::get_payment_gateways();
510 + foreach ( $gateways as $gateway ) {
511 + $payment_gateways[] = $gateway;
512 + }
513 + } catch ( \Throwable $th ) {
514 + tutor_log( $th );
515 + }
516 +
517 + return $payment_gateways;
518 + }
519 +
520 + /**
521 + * Retrieves the configuration keys for the 2Checkout payment gateway.
522 + *
523 + * @return array An associative array where the keys represent the configuration options and the values represent the
524 + * field types.
525 + *
526 + * @since 3.3.0
527 + */
528 + public static function get_twocheckout_config_keys() {
529 + return array(
530 + 'environment' => 'environment',
531 + 'merchant_code' => 'secret_key',
532 + 'secret_key' => 'secret_key',
533 + 'buy_link_secret_word' => 'secret_key',
534 + 'webhook_url' => 'webhook_url',
535 + );
536 + }
537 +
538 + /**
539 + * Get Klarna region options.
540 + *
541 + * @since 3.6.0
542 + *
543 + * @return array Associative array with region codes as keys and translated region names as values .
544 + */
545 + private static function get_klarna_options(): array {
546 + return array(
547 + 'eu' => __( 'Europe', 'tutor-pro' ),
548 + 'na' => __( 'North America', 'tutor-pro' ),
549 + 'oc' => __( 'Oceania', 'tutor-pro' ),
550 + );
551 + }
552 + }
553 +