woocommerce-square
/
includes
/
Framework
/
PaymentGateway
/
ApplePay
/
Payment_Gateway_Apple_Pay_Admin.php
Api
1 year ago
Payment_Gateway_Apple_Pay.php
2 years ago
Payment_Gateway_Apple_Pay_Admin.php
2 years ago
Payment_Gateway_Apple_Pay_Ajax.php
1 year ago
Payment_Gateway_Apple_Pay_Frontend.php
4 months ago
Payment_Gateway_Apple_Pay_Orders.php
2 years ago
Payment_Gateway_Apple_Pay_Admin.php
401 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Payment Gateway Framework |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@skyverge.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * @since 3.0.0 |
| 14 | * @author WooCommerce / SkyVerge |
| 15 | * @copyright Copyright (c) 2013-2016, SkyVerge, Inc. |
| 16 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 17 | * |
| 18 | * Modified by WooCommerce on 19 December 2021. |
| 19 | */ |
| 20 | |
| 21 | namespace WooCommerce\Square\Framework\PaymentGateway\ApplePay; |
| 22 | |
| 23 | use WooCommerce\Square\Framework\Square_Helper; |
| 24 | |
| 25 | defined( 'ABSPATH' ) or exit; |
| 26 | |
| 27 | /** |
| 28 | * Sets up the Apple Pay settings screen. |
| 29 | * |
| 30 | * @since 3.0.0 |
| 31 | */ |
| 32 | class Payment_Gateway_Apple_Pay_Admin { |
| 33 | |
| 34 | |
| 35 | /** @var Payment_Gateway_Apple_Pay the Apple Pay handler instance */ |
| 36 | protected $handler; |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Construct the class. |
| 41 | * |
| 42 | * @since 3.0.0 |
| 43 | * |
| 44 | * @param Payment_Gateway_Apple_Pay $handler main Apple Pay handler instance |
| 45 | */ |
| 46 | public function __construct( $handler ) { |
| 47 | |
| 48 | $this->handler = $handler; |
| 49 | |
| 50 | // add Apple Pay to the checkout settings sections |
| 51 | add_filter( 'woocommerce_get_sections_checkout', array( $this, 'add_settings_section' ), 99 ); |
| 52 | |
| 53 | // output the settings |
| 54 | add_action( 'woocommerce_settings_checkout', array( $this, 'add_settings' ) ); |
| 55 | |
| 56 | // render the special "static" gateway select |
| 57 | add_action( 'woocommerce_admin_field_static', array( $this, 'render_static_setting' ) ); |
| 58 | |
| 59 | // save the settings |
| 60 | add_action( 'woocommerce_settings_save_checkout', array( $this, 'save_settings' ) ); |
| 61 | |
| 62 | // add admin notices for configuration options that need attention |
| 63 | add_action( 'admin_footer', array( $this, 'add_admin_notices' ), 10 ); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Adds Apple Pay to the checkout settings sections. |
| 69 | * |
| 70 | * @internal |
| 71 | * |
| 72 | * @since 3.0.0 |
| 73 | * |
| 74 | * @param array $sections the existing sections |
| 75 | * @return array |
| 76 | */ |
| 77 | public function add_settings_section( $sections ) { |
| 78 | |
| 79 | $sections['apple-pay'] = __( 'Apple Pay', 'woocommerce-square' ); |
| 80 | |
| 81 | return $sections; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Gets all of the combined settings. |
| 87 | * |
| 88 | * @since 3.0.0 |
| 89 | * |
| 90 | * @return array $settings The combined settings. |
| 91 | */ |
| 92 | public function get_settings() { |
| 93 | |
| 94 | $settings = array( |
| 95 | |
| 96 | array( |
| 97 | 'title' => __( 'Apple Pay', 'woocommerce-square' ), |
| 98 | 'type' => 'title', |
| 99 | ), |
| 100 | |
| 101 | array( |
| 102 | 'id' => 'sv_wc_apple_pay_enabled', |
| 103 | 'title' => __( 'Enable / Disable', 'woocommerce-square' ), |
| 104 | 'desc' => __( 'Accept Apple Pay', 'woocommerce-square' ), |
| 105 | 'type' => 'checkbox', |
| 106 | 'default' => 'no', |
| 107 | ), |
| 108 | |
| 109 | array( |
| 110 | 'id' => 'sv_wc_apple_pay_display_locations', |
| 111 | 'title' => __( 'Allow Apple Pay on', 'woocommerce-square' ), |
| 112 | 'type' => 'multiselect', |
| 113 | 'class' => 'wc-enhanced-select', |
| 114 | 'css' => 'width: 350px;', |
| 115 | 'options' => $this->get_display_location_options(), |
| 116 | 'default' => array_keys( $this->get_display_location_options() ), |
| 117 | ), |
| 118 | |
| 119 | array( |
| 120 | 'id' => 'sv_wc_apple_pay_button_style', |
| 121 | 'title' => __( 'Button Style', 'woocommerce-square' ), |
| 122 | 'type' => 'select', |
| 123 | 'options' => array( |
| 124 | 'black' => __( 'Black', 'woocommerce-square' ), |
| 125 | 'white' => __( 'White', 'woocommerce-square' ), |
| 126 | 'white-with-line' => __( 'White with outline', 'woocommerce-square' ), |
| 127 | ), |
| 128 | 'default' => 'black', |
| 129 | ), |
| 130 | |
| 131 | array( |
| 132 | 'type' => 'sectionend', |
| 133 | ), |
| 134 | ); |
| 135 | |
| 136 | $connection_settings = array( |
| 137 | array( |
| 138 | 'title' => __( 'Connection Settings', 'woocommerce-square' ), |
| 139 | 'type' => 'title', |
| 140 | ), |
| 141 | |
| 142 | array( |
| 143 | 'id' => 'sv_wc_apple_pay_merchant_id', |
| 144 | 'title' => __( 'Apple Merchant ID', 'woocommerce-square' ), |
| 145 | 'type' => 'text', |
| 146 | 'desc' => sprintf( |
| 147 | /* translators: Placeholders: %1$s - <a> tag, %2$s - </a> tag */ |
| 148 | __( 'This is found in your %1$sApple developer account%2$s', 'woocommerce-square' ), |
| 149 | '<a href="https://developer.apple.com" target="_blank">', '</a>' |
| 150 | ), |
| 151 | ), |
| 152 | |
| 153 | array( |
| 154 | 'id' => 'sv_wc_apple_pay_cert_path', |
| 155 | 'title' => __( 'Certificate Path', 'woocommerce-square' ), |
| 156 | 'type' => 'text', |
| 157 | 'desc_tip' => __( 'The full system path to your certificate file from Apple. For security reasons you should store this outside of your web root.', 'woocommerce-square' ), |
| 158 | 'desc' => sprintf( |
| 159 | /* translators: Placeholders: %s - the server's web root path */ |
| 160 | __( 'For reference, your current web root path is: %s', 'woocommerce-square' ), |
| 161 | '<code>' . ABSPATH . '</code>' |
| 162 | ), |
| 163 | ), |
| 164 | ); |
| 165 | |
| 166 | $gateway_setting_id = 'sv_wc_apple_pay_payment_gateway'; |
| 167 | $gateway_options = $this->get_gateway_options(); |
| 168 | |
| 169 | if ( 1 === count( $gateway_options ) ) { |
| 170 | |
| 171 | $connection_settings[] = array( |
| 172 | 'id' => $gateway_setting_id, |
| 173 | 'title' => __( 'Processing Gateway', 'woocommerce-square' ), |
| 174 | 'type' => 'static', |
| 175 | 'value' => key( $gateway_options ), |
| 176 | 'label' => current( $gateway_options ), |
| 177 | ); |
| 178 | |
| 179 | } else { |
| 180 | |
| 181 | $connection_settings[] = array( |
| 182 | 'id' => $gateway_setting_id, |
| 183 | 'title' => __( 'Processing Gateway', 'woocommerce-square' ), |
| 184 | 'type' => 'select', |
| 185 | 'options' => $this->get_gateway_options(), |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | $connection_settings[] = array( |
| 190 | 'id' => 'sv_wc_apple_pay_test_mode', |
| 191 | 'title' => __( 'Test Mode', 'woocommerce-square' ), |
| 192 | 'desc' => __( 'Enable to test Apple Pay functionality throughout your sites without processing real payments.', 'woocommerce-square' ), |
| 193 | 'type' => 'checkbox', |
| 194 | 'default' => 'no', |
| 195 | ); |
| 196 | |
| 197 | $connection_settings[] = array( |
| 198 | 'type' => 'sectionend', |
| 199 | ); |
| 200 | |
| 201 | $settings = array_merge( $settings, $connection_settings ); |
| 202 | |
| 203 | /** |
| 204 | * Filter the combined settings. |
| 205 | * |
| 206 | * @since 3.0.0 |
| 207 | * @param array $settings The combined settings. |
| 208 | */ |
| 209 | return apply_filters( 'woocommerce_get_settings_apple_pay', $settings ); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /** |
| 214 | * Outputs the settings fields. |
| 215 | * |
| 216 | * @internal |
| 217 | * |
| 218 | * @since 3.0.0 |
| 219 | */ |
| 220 | public function add_settings() { |
| 221 | global $current_section; |
| 222 | |
| 223 | if ( 'apple-pay' === $current_section ) { |
| 224 | \WC_Admin_Settings::output_fields( $this->get_settings() ); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /** |
| 230 | * Saves the settings. |
| 231 | * |
| 232 | * @internal |
| 233 | * |
| 234 | * @since 3.0.0 |
| 235 | * |
| 236 | * @global string $current_section The current settings section. |
| 237 | */ |
| 238 | public function save_settings() { |
| 239 | global $current_section; |
| 240 | |
| 241 | // Output the general settings |
| 242 | if ( 'apple-pay' == $current_section ) { |
| 243 | |
| 244 | \WC_Admin_Settings::save_fields( $this->get_settings() ); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /** |
| 250 | * Renders a static setting. |
| 251 | * |
| 252 | * This "setting" just displays simple text instead of a <select> with only |
| 253 | * one option. |
| 254 | * |
| 255 | * @since 3.0.0 |
| 256 | * |
| 257 | * @param array $setting |
| 258 | */ |
| 259 | public function render_static_setting( $setting ) { |
| 260 | |
| 261 | ?> |
| 262 | |
| 263 | <tr valign="top"> |
| 264 | <th scope="row" class="titledesc"> |
| 265 | <label for="<?php echo esc_attr( $setting['id'] ); ?>"><?php echo esc_html( $setting['title'] ); ?></label> |
| 266 | </th> |
| 267 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $setting['type'] ) ); ?>"> |
| 268 | <?php echo esc_html( $setting['label'] ); ?> |
| 269 | <input |
| 270 | name="<?php echo esc_attr( $setting['id'] ); ?>" |
| 271 | id="<?php echo esc_attr( $setting['id'] ); ?>" |
| 272 | value="<?php echo esc_html( $setting['value'] ); ?>" |
| 273 | type="hidden" |
| 274 | > |
| 275 | </td> |
| 276 | </tr><?php |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /** |
| 281 | * Adds admin notices for configuration options that need attention. |
| 282 | * |
| 283 | * @since 3.0.0 |
| 284 | */ |
| 285 | public function add_admin_notices() { |
| 286 | |
| 287 | // if the feature is not enabled, bail |
| 288 | if ( ! $this->handler->is_enabled() ) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | // if not on the settings screen, bail |
| 293 | if ( ! $this->is_settings_screen() ) { |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | $errors = array(); |
| 298 | |
| 299 | // HTTPS notice |
| 300 | if ( ! wc_site_is_https() ) { |
| 301 | $errors[] = __( 'Your site must be served over HTTPS with a valid SSL certificate.', 'woocommerce-square' ); |
| 302 | } |
| 303 | |
| 304 | // Currency notice |
| 305 | if ( ! in_array( get_woocommerce_currency(), $this->handler->get_accepted_currencies(), true ) ) { |
| 306 | |
| 307 | $accepted_currencies = $this->handler->get_accepted_currencies(); |
| 308 | |
| 309 | $errors[] = sprintf( |
| 310 | /* translators: Placeholders: %1$s - plugin name, %2$s - a currency/comma-separated list of currencies, %3$s - <a> tag, %4$s - </a> tag */ |
| 311 | _n( |
| 312 | 'Accepts payment in %1$s only. %2$sConfigure%3$s WooCommerce to accept %1$s to enable Apple Pay.', |
| 313 | 'Accepts payment in one of %1$s only. %2$sConfigure%3$s WooCommerce to accept one of %1$s to enable Apple Pay.', |
| 314 | count( $accepted_currencies ), |
| 315 | 'woocommerce-square' |
| 316 | ), |
| 317 | '<strong>' . implode( ', ', $accepted_currencies ) . '</strong>', |
| 318 | '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=general' ) ) . '">', |
| 319 | '</a>' |
| 320 | ); |
| 321 | } |
| 322 | |
| 323 | // bad cert config notice |
| 324 | // this first checks if the option has been set so the notice is not |
| 325 | // displayed without the user having the chance to set it. |
| 326 | if ( false !== $this->handler->get_cert_path() && ! $this->handler->is_cert_configured() ) { |
| 327 | |
| 328 | $errors[] = sprintf( |
| 329 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag */ |
| 330 | __( 'Your %1$sMerchant Identity Certificate%2$s cannot be found. Please check your path configuration.', 'woocommerce-square' ), |
| 331 | '<strong>', '</strong>' |
| 332 | ); |
| 333 | } |
| 334 | |
| 335 | if ( ! empty( $errors ) ) { |
| 336 | |
| 337 | $message = '<strong>' . __( 'Apple Pay is disabled.', 'woocommerce-square' ) . '</strong>'; |
| 338 | |
| 339 | if ( 1 === count( $errors ) ) { |
| 340 | $message .= ' ' . current( $errors ); |
| 341 | } else { |
| 342 | $message .= '<ul><li>' . implode( '</li><li>', $errors ) . '</li></ul>'; |
| 343 | } |
| 344 | |
| 345 | $this->handler->get_plugin()->get_admin_notice_handler()->add_admin_notice( $message, 'apple-pay-https-required', array( |
| 346 | 'notice_class' => 'error', |
| 347 | 'dismissible' => false, |
| 348 | ) ); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /** |
| 354 | * Determines if the user is currently on the settings screen. |
| 355 | * |
| 356 | * @since 3.0.0 |
| 357 | * |
| 358 | * @return bool |
| 359 | */ |
| 360 | protected function is_settings_screen() { |
| 361 | |
| 362 | return 'wc-settings' === Square_Helper::get_request( 'page' ) && 'apple-pay' === Square_Helper::get_request( 'section' ); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /** |
| 367 | * Gets the available display location options. |
| 368 | * |
| 369 | * @since 3.0.0 |
| 370 | * |
| 371 | * @return array |
| 372 | */ |
| 373 | protected function get_display_location_options() { |
| 374 | |
| 375 | return array( |
| 376 | 'product' => __( 'Single products', 'woocommerce-square' ), |
| 377 | 'cart' => __( 'Cart', 'woocommerce-square' ), |
| 378 | 'checkout' => __( 'Checkout', 'woocommerce-square' ), |
| 379 | ); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | /** |
| 384 | * Gets the available gateway options. |
| 385 | * |
| 386 | * @since 3.0.0 |
| 387 | * |
| 388 | * @return array |
| 389 | */ |
| 390 | protected function get_gateway_options() { |
| 391 | |
| 392 | $gateways = $this->handler->get_supporting_gateways(); |
| 393 | |
| 394 | foreach ( $gateways as $id => $gateway ) { |
| 395 | $gateways[ $id ] = $gateway->get_method_title(); |
| 396 | } |
| 397 | |
| 398 | return $gateways; |
| 399 | } |
| 400 | } |
| 401 |