PluginProbe
WooCommerce / 11.0.1
WooCommerce v11.0.1
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Internal / Admin / Settings / PaymentsProviders / Paymob.php
Paymob.php
109 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare( strict_types=1 );
3
4 namespace Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders;
5
6 use Automattic\WooCommerce\Internal\Logging\SafeGlobalFunctionProxy;
7 use Throwable;
8 use WC_Payment_Gateway;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Paymob payment gateway provider class.
14 *
15 * This class handles all the custom logic for the Paymob payment gateway provider.
16 */
17 class Paymob extends PaymentGateway {
18
19 /**
20 * Check if the payment gateway needs setup.
21 *
22 * Note: We are overriding the parent method to avoid infinite recursion with the is_account_connected method.
23 *
24 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
25 *
26 * @return bool True if the payment gateway needs setup, false otherwise.
27 */
28 public function needs_setup( WC_Payment_Gateway $payment_gateway ): bool {
29 $needs_setup = wc_string_to_bool( $payment_gateway->needs_setup() );
30 // If we get a true value, it means the gateway needs setup.
31 if ( $needs_setup ) {
32 return true;
33 }
34
35 // If we reach here, just assume that the gateway does not need setup.
36 return false;
37 }
38
39 /**
40 * Try to determine if the payment gateway is in test mode.
41 *
42 * This is a best-effort attempt, as there is no standard way to determine this.
43 * Trust the true value, but don't consider a false value as definitive.
44 *
45 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
46 *
47 * @return bool True if the payment gateway is in test mode, false otherwise.
48 */
49 public function is_in_test_mode( WC_Payment_Gateway $payment_gateway ): bool {
50 return $this->is_paymob_in_sandbox_mode( $payment_gateway ) ?? parent::is_in_test_mode( $payment_gateway );
51 }
52
53 /**
54 * Check if the payment gateway has a payments processor account connected.
55 *
56 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
57 *
58 * @return bool True if the payment gateway account is connected, false otherwise.
59 * If the payment gateway does not provide the information, it will return true.
60 */
61 public function is_account_connected( WC_Payment_Gateway $payment_gateway ): bool {
62 // The Paymob gateway ties needs_setup only to the API keys, so if they are set, we consider the account connected.
63 return ! $this->needs_setup( $payment_gateway );
64 }
65
66 /**
67 * Try to determine if the payment gateway is in test mode onboarding (aka sandbox or test-drive).
68 *
69 * This is a best-effort attempt, as there is no standard way to determine this.
70 * Trust the true value, but don't consider a false value as definitive.
71 *
72 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
73 *
74 * @return bool True if the payment gateway is in test mode onboarding, false otherwise.
75 */
76 public function is_in_test_mode_onboarding( WC_Payment_Gateway $payment_gateway ): bool {
77 return $this->is_paymob_in_sandbox_mode( $payment_gateway ) ?? parent::is_in_test_mode_onboarding( $payment_gateway );
78 }
79
80 /**
81 * Check if the Paymob payment gateway is in sandbox mode.
82 *
83 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
84 *
85 * @return ?bool True if the payment gateway is in sandbox mode, false otherwise.
86 * Null if the environment could not be determined.
87 */
88 private function is_paymob_in_sandbox_mode( WC_Payment_Gateway $payment_gateway ): ?bool {
89 try {
90 // Unfortunately, Paymob does not provide a standard way to determine if the gateway is in sandbox mode.
91 $options = get_option( 'woocommerce_paymob-main_settings', array() );
92 return 'test' === ( $options['mode'] ?? 'test' );
93 } catch ( \Throwable $e ) {
94 // Do nothing but log so we can investigate.
95 SafeGlobalFunctionProxy::wc_get_logger()->debug(
96 'Failed to determine if gateway is in sandbox mode: ' . $e->getMessage(),
97 array(
98 'gateway' => $payment_gateway->id,
99 'source' => 'settings-payments',
100 'exception' => $e,
101 )
102 );
103 }
104
105 // Let the caller know that we couldn't determine the environment.
106 return null;
107 }
108 }
109