WooPayments
5 days 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
Helcim.php
3 weeks ago
HelioPay.php
1 year ago
Klarna.php
1 year ago
Komoju.php
3 weeks ago
KustomCheckout.php
1 week ago
Mastercard.php
3 weeks ago
MercadoPago.php
1 year ago
Mollie.php
3 weeks 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
MercadoPago.php
186 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\Admin\Settings\Utils; |
| 7 | use Automattic\WooCommerce\Internal\Logging\SafeGlobalFunctionProxy; |
| 8 | use WC_Payment_Gateway; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * MercadoPago payment gateway provider class. |
| 14 | * |
| 15 | * This class handles all the custom logic for the MercadoPago payment gateway provider. |
| 16 | */ |
| 17 | class MercadoPago extends PaymentGateway { |
| 18 | |
| 19 | /** |
| 20 | * Check if the payment gateway needs setup. |
| 21 | * |
| 22 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 23 | * |
| 24 | * @return bool True if the payment gateway needs setup, false otherwise. |
| 25 | */ |
| 26 | public function needs_setup( WC_Payment_Gateway $payment_gateway ): bool { |
| 27 | $is_onboarded = $this->is_mercado_pago_onboarded( $payment_gateway ); |
| 28 | if ( ! is_null( $is_onboarded ) ) { |
| 29 | return ! $is_onboarded; |
| 30 | } |
| 31 | |
| 32 | return parent::needs_setup( $payment_gateway ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Try to determine if the payment gateway is in test mode. |
| 37 | * |
| 38 | * This is a best-effort attempt, as there is no standard way to determine this. |
| 39 | * Trust the true value, but don't consider a false value as definitive. |
| 40 | * |
| 41 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 42 | * |
| 43 | * @return bool True if the payment gateway is in test mode, false otherwise. |
| 44 | */ |
| 45 | public function is_in_test_mode( WC_Payment_Gateway $payment_gateway ): bool { |
| 46 | return $this->is_mercado_pago_in_sandbox_mode( $payment_gateway ) ?? parent::is_in_test_mode( $payment_gateway ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Try to determine if the payment gateway is in dev mode. |
| 51 | * |
| 52 | * This is a best-effort attempt, as there is no standard way to determine this. |
| 53 | * Trust the true value, but don't consider a false value as definitive. |
| 54 | * |
| 55 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 56 | * |
| 57 | * @return bool True if the payment gateway is in dev mode, false otherwise. |
| 58 | */ |
| 59 | public function is_in_dev_mode( WC_Payment_Gateway $payment_gateway ): bool { |
| 60 | return $this->is_mercado_pago_in_sandbox_mode( $payment_gateway ) ?? parent::is_in_dev_mode( $payment_gateway ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check if the payment gateway has a payments processor account connected. |
| 65 | * |
| 66 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 67 | * |
| 68 | * @return bool True if the payment gateway account is connected, false otherwise. |
| 69 | * If the payment gateway does not provide the information, it will return true. |
| 70 | */ |
| 71 | public function is_account_connected( WC_Payment_Gateway $payment_gateway ): bool { |
| 72 | return $this->is_mercado_pago_onboarded( $payment_gateway ) ?? parent::is_account_connected( $payment_gateway ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Check if the payment gateway has completed the onboarding process. |
| 77 | * |
| 78 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 79 | * |
| 80 | * @return bool True if the payment gateway has completed the onboarding process, false otherwise. |
| 81 | * If the payment gateway does not provide the information, |
| 82 | * it will infer it from having a connected account. |
| 83 | */ |
| 84 | public function is_onboarding_completed( WC_Payment_Gateway $payment_gateway ): bool { |
| 85 | return $this->is_mercado_pago_onboarded( $payment_gateway ) ?? parent::is_onboarding_completed( $payment_gateway ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Try to determine if the payment gateway is in test mode onboarding (aka sandbox or test-drive). |
| 90 | * |
| 91 | * This is a best-effort attempt, as there is no standard way to determine this. |
| 92 | * Trust the true value, but don't consider a false value as definitive. |
| 93 | * |
| 94 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 95 | * |
| 96 | * @return bool True if the payment gateway is in test mode onboarding, false otherwise. |
| 97 | */ |
| 98 | public function is_in_test_mode_onboarding( WC_Payment_Gateway $payment_gateway ): bool { |
| 99 | return $this->is_mercado_pago_in_sandbox_mode( $payment_gateway ) ?? parent::is_in_test_mode_onboarding( $payment_gateway ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Check if the MercadoPago payment gateway is in sandbox mode. |
| 104 | * |
| 105 | * For MercadoPago, there are two different environments: sandbox and production. |
| 106 | * |
| 107 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 108 | * |
| 109 | * @return ?bool True if the payment gateway is in sandbox mode, false otherwise. |
| 110 | * Null if the environment could not be determined. |
| 111 | */ |
| 112 | private function is_mercado_pago_in_sandbox_mode( WC_Payment_Gateway $payment_gateway ): ?bool { |
| 113 | global $mercadopago; |
| 114 | |
| 115 | try { |
| 116 | // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 117 | if ( class_exists( '\MercadoPago\Woocommerce\WoocommerceMercadoPago' ) && |
| 118 | class_exists( '\MercadoPago\Woocommerce\Configs\Store' ) && |
| 119 | $mercadopago instanceof \MercadoPago\Woocommerce\WoocommerceMercadoPago && |
| 120 | ! is_null( $mercadopago->storeConfig ) && |
| 121 | $mercadopago->storeConfig instanceof \MercadoPago\Woocommerce\Configs\Store && |
| 122 | is_callable( array( $mercadopago->storeConfig, 'isTestMode' ) ) |
| 123 | ) { |
| 124 | return wc_string_to_bool( $mercadopago->storeConfig->isTestMode() ); |
| 125 | |
| 126 | } |
| 127 | } catch ( \Throwable $e ) { |
| 128 | // Do nothing but log so we can investigate. |
| 129 | SafeGlobalFunctionProxy::wc_get_logger()->debug( |
| 130 | 'Failed to determine if gateway is in sandbox mode: ' . $e->getMessage(), |
| 131 | array( |
| 132 | 'gateway' => $payment_gateway->id, |
| 133 | 'source' => 'settings-payments', |
| 134 | 'exception' => $e, |
| 135 | ) |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | // Let the caller know that we couldn't determine the environment. |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Check if the MercadoPago payment gateway is onboarded. |
| 145 | * |
| 146 | * For MercadoPago, there are two different environments: sandbox/test and production/sale. |
| 147 | * |
| 148 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 149 | * |
| 150 | * @return ?bool True if the payment gateway is onboarded, false otherwise. |
| 151 | * Null if we failed to determine the onboarding status. |
| 152 | */ |
| 153 | private function is_mercado_pago_onboarded( WC_Payment_Gateway $payment_gateway ): ?bool { |
| 154 | global $mercadopago; |
| 155 | |
| 156 | try { |
| 157 | // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 158 | if ( class_exists( '\MercadoPago\Woocommerce\WoocommerceMercadoPago' ) && |
| 159 | class_exists( '\MercadoPago\Woocommerce\Configs\Seller' ) && |
| 160 | $mercadopago instanceof \MercadoPago\Woocommerce\WoocommerceMercadoPago && |
| 161 | ! is_null( $mercadopago->sellerConfig ) && |
| 162 | $mercadopago->sellerConfig instanceof \MercadoPago\Woocommerce\Configs\Seller && |
| 163 | is_callable( array( $mercadopago->sellerConfig, 'getCredentialsPublicKey' ) ) && |
| 164 | is_callable( array( $mercadopago->sellerConfig, 'getCredentialsAccessToken' ) ) |
| 165 | ) { |
| 166 | return ! empty( $mercadopago->sellerConfig->getCredentialsPublicKey() ) && |
| 167 | ! empty( $mercadopago->sellerConfig->getCredentialsAccessToken() ); |
| 168 | |
| 169 | } |
| 170 | } catch ( \Throwable $e ) { |
| 171 | // Do nothing but log so we can investigate. |
| 172 | SafeGlobalFunctionProxy::wc_get_logger()->debug( |
| 173 | 'Failed to determine if gateway is onboarded: ' . $e->getMessage(), |
| 174 | array( |
| 175 | 'gateway' => $payment_gateway->id, |
| 176 | 'source' => 'settings-payments', |
| 177 | 'exception' => $e, |
| 178 | ) |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | // Let the caller know that we couldn't determine the onboarding status. |
| 183 | return null; |
| 184 | } |
| 185 | } |
| 186 |