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
Stripe.php
226 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\Payments; |
| 7 | use Automattic\WooCommerce\Internal\Admin\Settings\Utils; |
| 8 | use Automattic\WooCommerce\Internal\Logging\SafeGlobalFunctionProxy; |
| 9 | use Throwable; |
| 10 | use WC_Payment_Gateway; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Stripe payment gateway provider class. |
| 16 | * |
| 17 | * This class handles all the custom logic for the Stripe payment gateway provider. |
| 18 | */ |
| 19 | class Stripe extends PaymentGateway { |
| 20 | |
| 21 | /** |
| 22 | * Try to determine if the payment gateway is in test mode. |
| 23 | * |
| 24 | * This is a best-effort attempt, as there is no standard way to determine this. |
| 25 | * Trust the true value, but don't consider a false value as definitive. |
| 26 | * |
| 27 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 28 | * |
| 29 | * @return bool True if the payment gateway is in test mode, false otherwise. |
| 30 | */ |
| 31 | public function is_in_test_mode( WC_Payment_Gateway $payment_gateway ): bool { |
| 32 | try { |
| 33 | if ( class_exists( '\WC_Stripe_Mode' ) && |
| 34 | is_callable( '\WC_Stripe_Mode::is_test' ) ) { |
| 35 | |
| 36 | return wc_string_to_bool( \WC_Stripe_Mode::is_test() ); |
| 37 | } |
| 38 | } catch ( Throwable $e ) { |
| 39 | // Do nothing but log so we can investigate. |
| 40 | SafeGlobalFunctionProxy::wc_get_logger()->debug( |
| 41 | 'Failed to determine if gateway is in test mode: ' . $e->getMessage(), |
| 42 | array( |
| 43 | 'gateway' => $payment_gateway->id, |
| 44 | 'source' => 'settings-payments', |
| 45 | 'exception' => $e, |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | return parent::is_in_test_mode( $payment_gateway ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Try to determine if the payment gateway is in dev mode. |
| 55 | * |
| 56 | * This is a best-effort attempt, as there is no standard way to determine this. |
| 57 | * Trust the true value, but don't consider a false value as definitive. |
| 58 | * |
| 59 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 60 | * |
| 61 | * @return bool True if the payment gateway is in dev mode, false otherwise. |
| 62 | */ |
| 63 | public function is_in_dev_mode( WC_Payment_Gateway $payment_gateway ): bool { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check if the payment gateway has a payments processor account connected. |
| 69 | * |
| 70 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 71 | * |
| 72 | * @return bool True if the payment gateway account is connected, false otherwise. |
| 73 | * If the payment gateway does not provide the information, it will return true. |
| 74 | */ |
| 75 | public function is_account_connected( WC_Payment_Gateway $payment_gateway ): bool { |
| 76 | try { |
| 77 | if ( class_exists( '\WC_Stripe' ) && is_callable( '\WC_Stripe::get_instance' ) ) { |
| 78 | $stripe = \WC_Stripe::get_instance(); |
| 79 | if ( is_object( $stripe ) && isset( $stripe->account ) && |
| 80 | class_exists( '\WC_Stripe_Account' ) && |
| 81 | defined( '\WC_Stripe_Account::STATUS_NO_ACCOUNT' ) && |
| 82 | $stripe->account instanceof \WC_Stripe_Account && |
| 83 | is_callable( array( $stripe->account, 'get_account_status' ) ) ) { |
| 84 | |
| 85 | return \WC_Stripe_Account::STATUS_NO_ACCOUNT !== $stripe->account->get_account_status(); |
| 86 | } |
| 87 | } |
| 88 | } catch ( Throwable $e ) { |
| 89 | // Do nothing but log so we can investigate. |
| 90 | SafeGlobalFunctionProxy::wc_get_logger()->debug( |
| 91 | 'Failed to determine if gateway has account connected: ' . $e->getMessage(), |
| 92 | array( |
| 93 | 'gateway' => $payment_gateway->id, |
| 94 | 'source' => 'settings-payments', |
| 95 | 'exception' => $e, |
| 96 | ) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return parent::is_account_connected( $payment_gateway ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Check if the payment gateway has started the onboarding process. |
| 105 | * |
| 106 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 107 | * |
| 108 | * @return bool True if the payment gateway has started the onboarding process, false otherwise. |
| 109 | * If the payment gateway does not provide the information, |
| 110 | * it will infer it from having a connected account. |
| 111 | */ |
| 112 | public function is_onboarding_started( WC_Payment_Gateway $payment_gateway ): bool { |
| 113 | // Fall back to inferring this from having a connected account. |
| 114 | return $this->is_account_connected( $payment_gateway ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Check if the payment gateway has completed the onboarding process. |
| 119 | * |
| 120 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 121 | * |
| 122 | * @return bool True if the payment gateway has completed the onboarding process, false otherwise. |
| 123 | * If the payment gateway does not provide the information, |
| 124 | * it will infer it from having a connected account. |
| 125 | */ |
| 126 | public function is_onboarding_completed( WC_Payment_Gateway $payment_gateway ): bool { |
| 127 | // Sanity check: If the onboarding has not started, it cannot be completed. |
| 128 | if ( ! $this->is_onboarding_started( $payment_gateway ) ) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // Fall back to inferring this from having a connected account. |
| 133 | return $this->is_account_connected( $payment_gateway ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Try to determine if the payment gateway is in test mode onboarding (aka sandbox or test-drive). |
| 138 | * |
| 139 | * This is a best-effort attempt, as there is no standard way to determine this. |
| 140 | * Trust the true value, but don't consider a false value as definitive. |
| 141 | * |
| 142 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 143 | * |
| 144 | * @return bool True if the payment gateway is in test mode onboarding, false otherwise. |
| 145 | */ |
| 146 | public function is_in_test_mode_onboarding( WC_Payment_Gateway $payment_gateway ): bool { |
| 147 | try { |
| 148 | if ( class_exists( '\WC_Stripe' ) && is_callable( '\WC_Stripe::get_instance' ) ) { |
| 149 | $stripe = \WC_Stripe::get_instance(); |
| 150 | if ( is_object( $stripe ) && isset( $stripe->connect ) && |
| 151 | class_exists( '\WC_Stripe_Connect' ) && |
| 152 | $stripe->connect instanceof \WC_Stripe_Connect && |
| 153 | is_callable( array( $stripe->connect, 'is_connected' ) ) ) { |
| 154 | |
| 155 | return $stripe->connect->is_connected( 'test' ) |
| 156 | && ! $stripe->connect->is_connected( 'live' ); |
| 157 | } |
| 158 | } |
| 159 | } catch ( Throwable $e ) { |
| 160 | // Do nothing but log so we can investigate. |
| 161 | SafeGlobalFunctionProxy::wc_get_logger()->debug( |
| 162 | 'Failed to determine if gateway is in test mode onboarding: ' . $e->getMessage(), |
| 163 | array( |
| 164 | 'gateway' => $payment_gateway->id, |
| 165 | 'source' => 'settings-payments', |
| 166 | 'exception' => $e, |
| 167 | ) |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | return parent::is_in_test_mode_onboarding( $payment_gateway ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get the settings URL for a payment gateway. |
| 176 | * |
| 177 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 178 | * |
| 179 | * @return string The settings URL for the payment gateway. |
| 180 | */ |
| 181 | public function get_settings_url( WC_Payment_Gateway $payment_gateway ): string { |
| 182 | return Utils::wc_payments_settings_url( |
| 183 | null, |
| 184 | array( |
| 185 | 'section' => strtolower( $payment_gateway->id ), |
| 186 | 'from' => Payments::FROM_PAYMENTS_SETTINGS, |
| 187 | ) |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get the onboarding URL for the payment gateway. |
| 193 | * |
| 194 | * This URL should start or continue the onboarding process. |
| 195 | * |
| 196 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 197 | * @param string $return_url Optional. The URL to return to after onboarding. |
| 198 | * This will likely get attached to the onboarding URL. |
| 199 | * |
| 200 | * @return string The onboarding URL for the payment gateway. |
| 201 | */ |
| 202 | public function get_onboarding_url( WC_Payment_Gateway $payment_gateway, string $return_url = '' ): string { |
| 203 | // Fall back to pointing users to the payment gateway settings page to handle onboarding. |
| 204 | return $this->get_settings_url( $payment_gateway ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Try and determine a list of recommended payment methods for a payment gateway. |
| 209 | * |
| 210 | * This data is not always available, and it is up to the payment gateway to provide it. |
| 211 | * This is not a definitive list of payment methods that the gateway supports. |
| 212 | * The data is aimed at helping the user understand what payment methods are recommended for the gateway |
| 213 | * and potentially help them make a decision on which payment methods to enable. |
| 214 | * |
| 215 | * @param WC_Payment_Gateway $payment_gateway The payment gateway object. |
| 216 | * @param string $country_code Optional. The country code for which to get recommended payment methods. |
| 217 | * This should be an ISO 3166-1 alpha-2 country code. |
| 218 | * |
| 219 | * @return array The recommended payment methods list for the payment gateway. |
| 220 | * Empty array if there are none. |
| 221 | */ |
| 222 | public function get_recommended_payment_methods( WC_Payment_Gateway $payment_gateway, string $country_code = '' ): array { |
| 223 | return array(); |
| 224 | } |
| 225 | } |
| 226 |