WooPayments
1 month 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
11 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
11 months ago
PayPal.php
1 year ago
PayUIndia.php
1 year ago
Payfast.php
1 year ago
PaymentGateway.php
5 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
11 months ago
Vivacom.php
1 year ago
WCCore.php
9 months ago
WooPayments.php
9 months ago
Mollie.php
188 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 | * Mollie payment gateway provider class. |
| 14 | * |
| 15 | * This class handles all the custom logic for the Mollie payment gateway provider. |
| 16 | */ |
| 17 | class Mollie extends PaymentGateway { |
| 18 | |
| 19 | /** |
| 20 | * Get the settings URL for a payment gateway. |
| 21 | * |
| 22 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 23 | * |
| 24 | * @return string The settings URL for the payment gateway. |
| 25 | */ |
| 26 | public function get_settings_url( WC_Payment_Gateway $payment_gateway ): string { |
| 27 | // Don't target any section because there are none to target when Mollie is not connected. |
| 28 | if ( 'mollie_stand_in' === $payment_gateway->id ) { |
| 29 | return $this->get_custom_settings_url(); |
| 30 | } |
| 31 | |
| 32 | // Target the payment methods section when the gateway is connected. |
| 33 | return $this->get_custom_settings_url( 'mollie_payment_methods' ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Check if the payment gateway has a payments processor account connected. |
| 38 | * |
| 39 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 40 | * |
| 41 | * @return bool True if the payment gateway account is connected, false otherwise. |
| 42 | * If the payment gateway does not provide the information, it will return true. |
| 43 | */ |
| 44 | public function is_account_connected( WC_Payment_Gateway $payment_gateway ): bool { |
| 45 | try { |
| 46 | $sandbox_mode = $this->is_mollie_in_sandbox_mode( $payment_gateway ); |
| 47 | // Let null results bubble up to the parent class. |
| 48 | if ( true === $sandbox_mode ) { |
| 49 | // If Mollie is in sandbox mode, we consider the account connected if the test API key is set. |
| 50 | return ! empty( get_option( 'mollie-payments-for-woocommerce_test_api_key', '' ) ); |
| 51 | } elseif ( false === $sandbox_mode ) { |
| 52 | // In production mode, we check the live API key. |
| 53 | return ! empty( get_option( 'mollie-payments-for-woocommerce_live_api_key', '' ) ); |
| 54 | } |
| 55 | } catch ( Throwable $e ) { |
| 56 | // Do nothing but log so we can investigate. |
| 57 | SafeGlobalFunctionProxy::wc_get_logger()->debug( |
| 58 | 'Failed to determine if gateway has an account connected: ' . $e->getMessage(), |
| 59 | array( |
| 60 | 'gateway' => $payment_gateway->id, |
| 61 | 'source' => 'settings-payments', |
| 62 | 'exception' => $e, |
| 63 | ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return parent::is_account_connected( $payment_gateway ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Determine if the payment gateway is in test mode. |
| 72 | * |
| 73 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 74 | * |
| 75 | * @return bool True if the payment gateway is in test mode, false otherwise. |
| 76 | */ |
| 77 | public function is_in_test_mode( WC_Payment_Gateway $payment_gateway ): bool { |
| 78 | return $this->is_mollie_in_sandbox_mode( $payment_gateway ) ?? parent::is_in_test_mode( $payment_gateway ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Try to determine if the payment gateway is in test mode onboarding (aka sandbox or test-drive). |
| 83 | * |
| 84 | * This is a best-effort attempt, as there is no standard way to determine this. |
| 85 | * Trust the true value, but don't consider a false value as definitive. |
| 86 | * |
| 87 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 88 | * |
| 89 | * @return bool True if the payment gateway is in test mode onboarding, false otherwise. |
| 90 | */ |
| 91 | public function is_in_test_mode_onboarding( WC_Payment_Gateway $payment_gateway ): bool { |
| 92 | return $this->is_mollie_in_sandbox_mode( $payment_gateway ) ?? parent::is_in_test_mode_onboarding( $payment_gateway ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Determine if at least a Mollie gateway is registered. |
| 97 | * |
| 98 | * @param array $payment_gateways The payment gateways objects. |
| 99 | * |
| 100 | * @return bool True if at least a Mollie gateway is registered, false otherwise. |
| 101 | */ |
| 102 | public function is_gateway_registered( array $payment_gateways ): bool { |
| 103 | $mollie_gateways = array_filter( |
| 104 | $payment_gateways, |
| 105 | function ( $gateway ) { |
| 106 | return str_starts_with( $gateway->id, 'mollie_wc_gateway_' ); |
| 107 | } |
| 108 | ); |
| 109 | |
| 110 | return ! empty( $mollie_gateways ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the pseudo Mollie gateway object. |
| 115 | * |
| 116 | * @param array $suggestion The suggestion data. |
| 117 | * |
| 118 | * @return PseudoWCPaymentGateway The pseudo gateway object. |
| 119 | */ |
| 120 | public function get_pseudo_gateway( array $suggestion ): PseudoWCPaymentGateway { |
| 121 | // We will generate a generic gateway to represent Mollie in the settings page. |
| 122 | // The generic gateway's state will be not enabled, not connected, and not onboarded. |
| 123 | // The presentational details will be minimal, letting the suggestion provide most of the information. |
| 124 | return new PseudoWCPaymentGateway( |
| 125 | 'mollie_stand_in', |
| 126 | array( |
| 127 | 'method_title' => $suggestion['title'], |
| 128 | 'method_description' => $suggestion['description'], |
| 129 | 'enabled' => false, |
| 130 | 'needs_setup' => true, |
| 131 | 'test_mode' => false, |
| 132 | 'dev_mode' => false, |
| 133 | 'account_connected' => false, |
| 134 | 'onboarding_started' => false, |
| 135 | 'onboarding_completed' => false, |
| 136 | 'settings_url' => $this->get_custom_settings_url(), |
| 137 | 'plugin_slug' => $suggestion['plugin']['slug'], |
| 138 | 'plugin_file' => $suggestion['plugin']['file'], |
| 139 | ), |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the URL to the custom settings page for Mollie. |
| 145 | * |
| 146 | * @param string $section Optional. The section to navigate to. |
| 147 | * |
| 148 | * @return string The URL to the custom settings page for Mollie. |
| 149 | */ |
| 150 | private function get_custom_settings_url( string $section = '' ): string { |
| 151 | $settings_url = admin_url( 'admin.php?page=wc-settings&tab=mollie_settings' ); |
| 152 | |
| 153 | if ( ! empty( $section ) ) { |
| 154 | $settings_url = add_query_arg( 'section', $section, $settings_url ); |
| 155 | } |
| 156 | |
| 157 | return $settings_url; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Check if the Mollie payment gateway is in sandbox mode. |
| 162 | * |
| 163 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 164 | * |
| 165 | * @return ?bool True if the payment gateway is in sandbox mode, false otherwise. |
| 166 | * Null if the environment could not be determined. |
| 167 | */ |
| 168 | private function is_mollie_in_sandbox_mode( WC_Payment_Gateway $payment_gateway ): ?bool { |
| 169 | try { |
| 170 | // Unfortunately, Mollie does not provide a standard way to determine if the gateway is in sandbox mode. |
| 171 | return filter_var( get_option( 'mollie-payments-for-woocommerce_test_mode_enabled', 'yes' ), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ); |
| 172 | } catch ( \Throwable $e ) { |
| 173 | // Do nothing but log so we can investigate. |
| 174 | SafeGlobalFunctionProxy::wc_get_logger()->debug( |
| 175 | 'Failed to determine if gateway is in sandbox mode: ' . $e->getMessage(), |
| 176 | array( |
| 177 | 'gateway' => $payment_gateway->id, |
| 178 | 'source' => 'settings-payments', |
| 179 | 'exception' => $e, |
| 180 | ) |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | // Let the caller know that we couldn't determine the environment. |
| 185 | return null; |
| 186 | } |
| 187 | } |
| 188 |