AddressRequirements.php
8 months ago
Buttons.php
5 months ago
Constants.php
3 months ago
Helper.php
3 months ago
Notices.php
5 months ago
Request.php
2 months ago
TransactAccountManager.php
5 months ago
WebhookHandler.php
3 months ago
Notices.php
323 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Gateways\PayPal; |
| 6 | |
| 7 | use WC_Order; |
| 8 | use Automattic\WooCommerce\Gateways\PayPal\Helper as PayPalHelper; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * PayPal Notices Class |
| 14 | * |
| 15 | * Handles admin notices for PayPal gateway including migration notices, |
| 16 | * account restriction warnings, and currency support notifications. |
| 17 | * |
| 18 | * @since 10.5.0 |
| 19 | */ |
| 20 | class Notices { |
| 21 | /** |
| 22 | * The name of the notice for PayPal migration. |
| 23 | * |
| 24 | * @since 10.5.0 |
| 25 | * @var string |
| 26 | */ |
| 27 | private const PAYPAL_MIGRATION_NOTICE = 'paypal_migration_completed'; |
| 28 | |
| 29 | /** |
| 30 | * The name of the notice for PayPal account restriction. |
| 31 | * |
| 32 | * @since 10.5.0 |
| 33 | * @var string |
| 34 | */ |
| 35 | private const PAYPAL_ACCOUNT_RESTRICTED_NOTICE = 'paypal_account_restricted'; |
| 36 | |
| 37 | /** |
| 38 | * The name of the notice for PayPal unsupported currency. |
| 39 | * |
| 40 | * @since 10.5.0 |
| 41 | * @var string |
| 42 | */ |
| 43 | private const PAYPAL_UNSUPPORTED_CURRENCY_NOTICE = 'paypal_unsupported_currency'; |
| 44 | |
| 45 | /** |
| 46 | * PayPal account restriction issue codes from PayPal API. |
| 47 | * |
| 48 | * @since 10.5.0 |
| 49 | * @var array |
| 50 | */ |
| 51 | protected const PAYPAL_ACCOUNT_RESTRICTION_ISSUES = array( |
| 52 | Constants::PAYPAL_ISSUE_PAYEE_ACCOUNT_LOCKED_OR_CLOSED, |
| 53 | Constants::PAYPAL_ISSUE_PAYEE_ACCOUNT_RESTRICTED, |
| 54 | ); |
| 55 | |
| 56 | /** |
| 57 | * The PayPal gateway instance. |
| 58 | * |
| 59 | * @var \WC_Gateway_Paypal |
| 60 | */ |
| 61 | private $gateway; |
| 62 | |
| 63 | /** |
| 64 | * Constructor. |
| 65 | */ |
| 66 | public function __construct() { |
| 67 | $this->gateway = \WC_Gateway_Paypal::get_instance(); |
| 68 | if ( ! $this->gateway ) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | // Only register admin notice hooks in the admin area. |
| 73 | if ( is_admin() ) { |
| 74 | add_action( 'admin_notices', array( $this, 'add_paypal_notices' ) ); |
| 75 | |
| 76 | // Use admin_head to inject notice on payments settings page. |
| 77 | // This bypasses the suppress_admin_notices() function which removes all admin_notices hooks on the payments page. |
| 78 | // This is a workaround to avoid the notice being suppressed by the suppress_admin_notices() function. |
| 79 | add_action( 'admin_head', array( $this, 'add_paypal_notices_on_payments_settings_page' ) ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Add PayPal Standard notices. |
| 85 | * |
| 86 | * @since 10.5.0 |
| 87 | * @return void |
| 88 | */ |
| 89 | public function add_paypal_notices(): void { |
| 90 | // Show only to users who can manage the site. |
| 91 | if ( ! current_user_can( 'manage_woocommerce' ) && ! current_user_can( 'manage_options' ) ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // Skip if the gateway is not available or the merchant has not been onboarded. |
| 96 | if ( ! PayPalHelper::is_paypal_gateway_available() || ! $this->gateway->should_use_orders_v2() ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $this->add_paypal_migration_notice(); |
| 101 | $this->add_paypal_account_restricted_notice(); |
| 102 | $this->add_paypal_unsupported_currency_notice(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Add PayPal notices on the payments settings page. |
| 107 | * |
| 108 | * @since 10.5.0 |
| 109 | * @return void |
| 110 | */ |
| 111 | public function add_paypal_notices_on_payments_settings_page(): void { |
| 112 | global $current_tab, $current_section; |
| 113 | |
| 114 | $screen = get_current_screen(); |
| 115 | $screen_id = $screen ? $screen->id : ''; |
| 116 | |
| 117 | $is_payments_settings_page = 'woocommerce_page_wc-settings' === $screen_id && 'checkout' === $current_tab && empty( $current_section ); |
| 118 | |
| 119 | // Only add the notice from this callback on the payments settings page. |
| 120 | if ( ! $is_payments_settings_page ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | $this->add_paypal_notices(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Add notice warning about the migration to PayPal Payments. |
| 129 | * |
| 130 | * @since 10.5.0 |
| 131 | * @return void |
| 132 | */ |
| 133 | public function add_paypal_migration_notice(): void { |
| 134 | // Skip if the notice has been dismissed. |
| 135 | if ( $this->is_notice_dismissed( self::PAYPAL_MIGRATION_NOTICE ) ) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | $doc_url = 'https://woocommerce.com/document/woocommerce-paypal-payments/paypal-payments-upgrade-guide/'; |
| 140 | $dismiss_url = $this->get_dismiss_url( self::PAYPAL_MIGRATION_NOTICE ); |
| 141 | $message = sprintf( |
| 142 | /* translators: 1: opening <a> tag, 2: closing </a> tag */ |
| 143 | esc_html__( 'WooCommerce has upgraded your PayPal integration from PayPal Standard to PayPal Payments (PPCP), for a more reliable and modern checkout experience. If you do not prefer the upgraded integration in WooCommerce, we recommend switching to %1$sPayPal Payments%2$s extension.', 'woocommerce' ), |
| 144 | '<a href="' . esc_url( $doc_url ) . '" target="_blank" rel="noopener noreferrer">', |
| 145 | '</a>', |
| 146 | ); |
| 147 | |
| 148 | $notice_html = '<div class="notice notice-warning is-dismissible">' |
| 149 | . '<a class="woocommerce-message-close notice-dismiss" style="text-decoration: none;" href="' . esc_url( $dismiss_url ) . '" aria-label="' . esc_attr__( 'Dismiss this notice', 'woocommerce' ) . '"></a>' |
| 150 | . '<p>' . $message . '</p>' |
| 151 | . '</div>'; |
| 152 | |
| 153 | echo wp_kses_post( $notice_html ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Add notice warning about PayPal account restriction. |
| 158 | * |
| 159 | * @since 10.5.0 |
| 160 | * @return void |
| 161 | */ |
| 162 | private function add_paypal_account_restricted_notice(): void { |
| 163 | // Skip if there's no account restriction flag. |
| 164 | if ( ! $this->has_account_restriction_flag() ) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | // Skip if the notice has been dismissed. |
| 169 | if ( $this->is_notice_dismissed( self::PAYPAL_ACCOUNT_RESTRICTED_NOTICE ) ) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | $support_url = 'https://www.paypal.com/smarthelp/contact-us'; |
| 174 | $dismiss_url = $this->get_dismiss_url( self::PAYPAL_ACCOUNT_RESTRICTED_NOTICE ); |
| 175 | $message = sprintf( |
| 176 | /* translators: 1: opening <a> tag, 2: closing </a> tag */ |
| 177 | esc_html__( 'Your PayPal account has been restricted by PayPal. This may prevent customers from completing payments. Please %1$scontact PayPal support%2$s to resolve this issue and restore full functionality to your account.', 'woocommerce' ), |
| 178 | '<a href="' . esc_url( $support_url ) . '" target="_blank" rel="noopener noreferrer">', |
| 179 | '</a>', |
| 180 | ); |
| 181 | |
| 182 | $notice_html = '<div class="notice notice-error is-dismissible">' |
| 183 | . '<a class="woocommerce-message-close notice-dismiss" style="text-decoration: none;" href="' . esc_url( $dismiss_url ) . '" aria-label="' . esc_attr__( 'Dismiss this notice', 'woocommerce' ) . '"></a>' |
| 184 | . '<p><strong>' . esc_html__( 'PayPal Account Restricted', 'woocommerce' ) . '</strong></p>' |
| 185 | . '<p>' . $message . '</p>' |
| 186 | . '</div>'; |
| 187 | |
| 188 | echo wp_kses_post( $notice_html ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Add notice warning when PayPal does not support the store's currency. |
| 193 | * |
| 194 | * @since 10.5.0 |
| 195 | * @return void |
| 196 | */ |
| 197 | private function add_paypal_unsupported_currency_notice(): void { |
| 198 | $currency = get_woocommerce_currency(); |
| 199 | |
| 200 | // Skip if the currency is supported by PayPal. |
| 201 | if ( $this->gateway->is_valid_for_use() ) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | // Skip if the notice has been dismissed. |
| 206 | if ( $this->is_notice_dismissed( self::PAYPAL_UNSUPPORTED_CURRENCY_NOTICE ) ) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | $dismiss_url = $this->get_dismiss_url( self::PAYPAL_UNSUPPORTED_CURRENCY_NOTICE ); |
| 211 | $message = sprintf( |
| 212 | /* translators: %s: Currency code */ |
| 213 | esc_html__( 'PayPal Standard does not support your store currency (%s).', 'woocommerce' ), |
| 214 | $currency |
| 215 | ); |
| 216 | |
| 217 | $notice_html = '<div class="notice notice-error is-dismissible">' |
| 218 | . '<a class="woocommerce-message-close notice-dismiss" style="text-decoration: none;" href="' . esc_url( $dismiss_url ) . '" aria-label="' . esc_attr__( 'Dismiss this notice', 'woocommerce' ) . '"></a>' |
| 219 | . '<p>' . $message . '</p>' |
| 220 | . '</div>'; |
| 221 | |
| 222 | echo wp_kses_post( $notice_html ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get the dismiss URL for a notice. |
| 227 | * |
| 228 | * @since 10.5.0 |
| 229 | * @param string $notice_name The name of the notice. |
| 230 | * @return string |
| 231 | */ |
| 232 | private function get_dismiss_url( string $notice_name ): string { |
| 233 | return wp_nonce_url( |
| 234 | add_query_arg( 'wc-hide-notice', $notice_name ), |
| 235 | 'woocommerce_hide_notices_nonce', |
| 236 | '_wc_notice_nonce' |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Check if the notice has been dismissed. |
| 242 | * |
| 243 | * User meta keys for notice dismissals: |
| 244 | * - dismissed_paypal_migration_completed_notice |
| 245 | * - dismissed_paypal_account_restricted_notice |
| 246 | * - dismissed_paypal_unsupported_currency_notice |
| 247 | * |
| 248 | * The meta keys are set by WC_Admin_Notices when the notice is dismissed by the user. |
| 249 | * |
| 250 | * @since 10.5.0 |
| 251 | * @param string $notice_name The name of the notice. |
| 252 | * @return bool |
| 253 | */ |
| 254 | private function is_notice_dismissed( string $notice_name ): bool { |
| 255 | return (bool) get_user_meta( get_current_user_id(), 'dismissed_' . $notice_name . '_notice', true ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Check if there's a flag indicating PayPal account restriction. |
| 260 | * |
| 261 | * @since 10.5.0 |
| 262 | * @return bool |
| 263 | */ |
| 264 | private function has_account_restriction_flag(): bool { |
| 265 | return 'yes' === get_option( 'woocommerce_paypal_account_restricted_status', 'no' ); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Set the flag indicating PayPal account restriction. |
| 270 | * |
| 271 | * @since 10.5.0 |
| 272 | * @return void |
| 273 | */ |
| 274 | public static function set_account_restriction_flag(): void { |
| 275 | if ( 'no' === get_option( 'woocommerce_paypal_account_restricted_status', 'no' ) ) { |
| 276 | update_option( 'woocommerce_paypal_account_restricted_status', 'yes', false ); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Clear the flag indicating PayPal account restriction. |
| 282 | * |
| 283 | * @since 10.5.0 |
| 284 | * @return void |
| 285 | */ |
| 286 | public static function clear_account_restriction_flag(): void { |
| 287 | if ( 'yes' === get_option( 'woocommerce_paypal_account_restricted_status', 'no' ) ) { |
| 288 | update_option( 'woocommerce_paypal_account_restricted_status', 'no' ); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Handle PayPal order response to manage account restriction notices. |
| 294 | * |
| 295 | * @since 10.5.0 |
| 296 | * @param int|string $http_code The HTTP status code from the PayPal API response. |
| 297 | * @param array $response_data The decoded response data from the PayPal API. |
| 298 | * @param WC_Order $order The WooCommerce order object. |
| 299 | * @return void |
| 300 | */ |
| 301 | public static function manage_account_restriction_flag_for_notice( $http_code, array $response_data, WC_Order $order ): void { |
| 302 | // Clear the restriction flag on successful responses. |
| 303 | if ( in_array( (int) $http_code, array( 200, 201 ), true ) ) { |
| 304 | self::clear_account_restriction_flag(); |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | if ( empty( $response_data ) ) { |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | // Set the restriction flag for account-related errors. |
| 313 | if ( 422 === (int) $http_code ) { |
| 314 | $issue = isset( $response_data['details'][0]['issue'] ) ? $response_data['details'][0]['issue'] : ''; |
| 315 | |
| 316 | if ( in_array( $issue, self::PAYPAL_ACCOUNT_RESTRICTION_ISSUES, true ) ) { |
| 317 | \WC_Gateway_Paypal::log( 'PayPal account restriction flag set due to issues when handling the order: ' . $order->get_id() ); |
| 318 | self::set_account_restriction_flag(); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 |