WooPayments
5 months ago
Affirm.php
1 year ago
AfterpayClearpay.php
1 year ago
Airwallex.php
1 year ago
AmazonPay.php
1 year ago
Antom.php
1 year ago
Eway.php
9 months ago
GoCardless.php
1 year ago
HelioPay.php
1 year ago
Klarna.php
1 year ago
KlarnaCheckout.php
1 year ago
MercadoPago.php
1 year ago
Mollie.php
1 year ago
Monei.php
1 year ago
NexiCheckout.php
9 months ago
PayPal.php
1 year ago
PayUIndia.php
1 year ago
Payfast.php
1 year ago
PaymentGateway.php
3 months ago
Paymob.php
1 year ago
Payoneer.php
1 year ago
Paystack.php
1 year ago
Paytrail.php
1 year ago
PseudoWCPaymentGateway.php
1 year ago
Razorpay.php
1 year ago
Stripe.php
1 year ago
Tilopay.php
1 year ago
Visa.php
9 months ago
Vivacom.php
1 year ago
WCCore.php
7 months ago
WooPayments.php
7 months ago
Paymob.php
109 lines
| 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 |