woocommerce-square
/
includes
/
Framework
/
PaymentGateway
/
ApplePay
/
Payment_Gateway_Apple_Pay_Frontend.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_Frontend.php
369 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 | use WooCommerce\Square\Framework as SquareFramework; |
| 23 | |
| 24 | defined( 'ABSPATH' ) or exit; |
| 25 | |
| 26 | /** |
| 27 | * Sets up the Apple Pay front-end functionality. |
| 28 | * |
| 29 | * @since 3.0.0 |
| 30 | */ |
| 31 | class Payment_Gateway_Apple_Pay_Frontend { |
| 32 | |
| 33 | /** @var SquareFramework\PaymentGateway\Payment_Gateway_Plugin $plugin the gateway plugin instance */ |
| 34 | protected $plugin; |
| 35 | |
| 36 | /** @var Payment_Gateway_Apple_Pay $handler the Apple Pay handler instance */ |
| 37 | protected $handler; |
| 38 | |
| 39 | /** @var SquareFramework\PaymentGateway\Payment_Gateway $gateway the gateway instance */ |
| 40 | protected $gateway; |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Constructs the class. |
| 45 | * |
| 46 | * @since 3.0.0 |
| 47 | * |
| 48 | * @param SquareFramework\PaymentGateway\Payment_Gateway_Plugin $plugin the gateway plugin instance |
| 49 | * @param Payment_Gateway_Apple_Pay $handler the Apple Pay handler instance |
| 50 | */ |
| 51 | public function __construct( SquareFramework\PaymentGateway\Payment_Gateway_Plugin $plugin, Payment_Gateway_Apple_Pay $handler ) { |
| 52 | |
| 53 | $this->plugin = $plugin; |
| 54 | |
| 55 | $this->handler = $handler; |
| 56 | |
| 57 | $this->gateway = $this->get_handler()->get_processing_gateway(); |
| 58 | |
| 59 | if ( $this->get_handler()->is_available() ) { |
| 60 | |
| 61 | add_action( 'wp', array( $this, 'init' ) ); |
| 62 | |
| 63 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Initializes the scripts and hooks. |
| 70 | * |
| 71 | * @since 3.0.0 |
| 72 | */ |
| 73 | public function init() { |
| 74 | |
| 75 | $locations = $this->get_display_locations(); |
| 76 | |
| 77 | if ( is_product() && in_array( 'product', $locations, true ) ) { |
| 78 | $this->init_product(); |
| 79 | } else if ( is_cart() && in_array( 'cart', $locations, true ) ) { |
| 80 | $this->init_cart(); |
| 81 | } else if ( is_checkout() && in_array( 'checkout', $locations, true ) ) { |
| 82 | $this->init_checkout(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Gets the configured display locations. |
| 89 | * |
| 90 | * @since 3.0.0 |
| 91 | * |
| 92 | * @return array |
| 93 | */ |
| 94 | protected function get_display_locations() { |
| 95 | |
| 96 | return get_option( 'sv_wc_apple_pay_display_locations', array() ); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Enqueues the scripts. |
| 102 | * |
| 103 | * @since 3.0.0 |
| 104 | */ |
| 105 | public function enqueue_scripts() { |
| 106 | |
| 107 | wp_enqueue_style( 'wc-square-apple-pay', $this->get_plugin()->get_plugin_url() . '/build/assets/frontend/wc-square-payment-gateway-apple-pay.css', array(), $this->get_plugin()->get_version() ); // TODO: min |
| 108 | |
| 109 | wp_enqueue_script( 'wc-square-apple-pay', $this->get_plugin()->get_plugin_url() . '/build/assets/frontend/wc-square-payment-gateway-apple-pay.js', array( 'jquery' ), $this->get_plugin()->get_version(), true ); |
| 110 | |
| 111 | /** |
| 112 | * Filters the Apple Pay JS handler params. |
| 113 | * |
| 114 | * @since 3.0.0 |
| 115 | * @param array $params the JS params |
| 116 | */ |
| 117 | $params = apply_filters( 'sv_wc_apple_pay_js_handler_params', array( |
| 118 | 'gateway_id' => $this->get_gateway()->get_id(), |
| 119 | 'gateway_id_dasherized' => $this->get_gateway()->get_id_dasherized(), |
| 120 | 'merchant_id' => $this->get_handler()->get_merchant_id(), |
| 121 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 122 | 'validate_nonce' => wp_create_nonce( 'sv_wc_apple_pay_validate_merchant' ), |
| 123 | 'recalculate_totals_nonce' => wp_create_nonce( 'sv_wc_apple_pay_recalculate_totals' ), |
| 124 | 'process_nonce' => wp_create_nonce( 'sv_wc_apple_pay_process_payment' ), |
| 125 | 'generic_error' => __( 'An error occurred, please try again or try an alternate form of payment', 'woocommerce-square' ), |
| 126 | ) ); |
| 127 | |
| 128 | wp_localize_script( 'wc-square-apple-pay', 'sv_wc_apple_pay_params', $params ); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Renders an Apple Pay button. |
| 134 | * |
| 135 | * @since 3.0.0 |
| 136 | */ |
| 137 | public function render_button() { |
| 138 | |
| 139 | $button_text = ''; |
| 140 | $classes = array( |
| 141 | 'sv-wc-apple-pay-button', |
| 142 | ); |
| 143 | |
| 144 | switch ( $this->get_handler()->get_button_style() ) { |
| 145 | |
| 146 | case 'black': |
| 147 | $classes[] = 'apple-pay-button-black'; |
| 148 | break; |
| 149 | |
| 150 | case 'white': |
| 151 | $classes[] = 'apple-pay-button-white'; |
| 152 | break; |
| 153 | |
| 154 | case 'white-with-line': |
| 155 | $classes[] = 'apple-pay-button-white-with-line'; |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | // if on the single product page, add some text |
| 160 | if ( is_product() ) { |
| 161 | $classes[] = 'apple-pay-button-buy-now'; |
| 162 | $button_text = __( 'Buy with', 'woocommerce-square' ); |
| 163 | } |
| 164 | |
| 165 | if ( $button_text ) { |
| 166 | $classes[] = 'apple-pay-button-with-text'; |
| 167 | } |
| 168 | |
| 169 | echo '<button class="' . implode( ' ', array_map( 'sanitize_html_class', $classes ) ) . '" lang="' . esc_attr( substr( get_locale(), 0, 2 ) ) . '">'; |
| 170 | |
| 171 | if ( $button_text ) { |
| 172 | echo '<span class="text">' . esc_html( $button_text ) . '</span><span class="logo"></span>'; |
| 173 | } |
| 174 | |
| 175 | echo '</button>'; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /** |
| 180 | * Initializes Apple Pay on the single product page. |
| 181 | * |
| 182 | * @since 3.0.0 |
| 183 | */ |
| 184 | public function init_product() { |
| 185 | |
| 186 | $args = array(); |
| 187 | |
| 188 | try { |
| 189 | |
| 190 | $product = wc_get_product( get_the_ID() ); |
| 191 | |
| 192 | if ( ! $product ) { |
| 193 | throw new \Exception( __( 'Product does not exist.', 'woocommerce-square' ) ); |
| 194 | } |
| 195 | |
| 196 | $payment_request = $this->get_handler()->get_product_payment_request( $product ); |
| 197 | |
| 198 | $args['payment_request'] = $payment_request; |
| 199 | |
| 200 | } catch ( \Exception $e ) { |
| 201 | |
| 202 | $this->get_handler()->log( sprintf( 'Could not initialize Apple Pay. %s', $e->getMessage() ) ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Filters the Apple Pay product handler args. |
| 207 | * |
| 208 | * @since 3.0.0 |
| 209 | * @param array $args |
| 210 | */ |
| 211 | $args = apply_filters( 'sv_wc_apple_pay_product_handler_args', $args ); |
| 212 | |
| 213 | ob_start(); |
| 214 | ?> |
| 215 | jQuery(function($) { |
| 216 | window.sv_wc_apple_pay_handler = new Square_Apple_Pay_Product_Handler(<?php echo wp_json_encode( $args ); ?>); |
| 217 | }); |
| 218 | <?php |
| 219 | $javascript = ob_get_clean(); |
| 220 | \WooCommerce\Square\Utilities\Helper::enqueue_inline_script( 'wc-square-apple-pay-product-inline', $javascript, array( 'jquery', 'wc-square-apple-pay' ) ); |
| 221 | |
| 222 | add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'render_button' ) ); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /** Cart functionality ****************************************************/ |
| 227 | |
| 228 | |
| 229 | /** |
| 230 | * Initializes Apple Pay on the cart page. |
| 231 | * |
| 232 | * @since 3.0.0 |
| 233 | */ |
| 234 | public function init_cart() { |
| 235 | |
| 236 | $args = array(); |
| 237 | |
| 238 | try { |
| 239 | |
| 240 | $payment_request = $this->get_handler()->get_cart_payment_request( WC()->cart ); |
| 241 | |
| 242 | $args['payment_request'] = $payment_request; |
| 243 | |
| 244 | } catch ( \Exception $e ) { |
| 245 | |
| 246 | $args['payment_request'] = false; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Filters the Apple Pay cart handler args. |
| 251 | * |
| 252 | * @since 3.0.0 |
| 253 | * @param array $args |
| 254 | */ |
| 255 | $args = apply_filters( 'sv_wc_apple_pay_cart_handler_args', $args ); |
| 256 | |
| 257 | ob_start(); |
| 258 | ?> |
| 259 | jQuery(function($) { |
| 260 | window.sv_wc_apple_pay_handler = new Square_Apple_Pay_Cart_Handler(<?php echo wp_json_encode( $args ); ?>); |
| 261 | }); |
| 262 | <?php |
| 263 | $javascript = ob_get_clean(); |
| 264 | \WooCommerce\Square\Utilities\Helper::enqueue_inline_script( 'wc-square-apple-pay-cart-inline', $javascript, array( 'jquery', 'wc-square-apple-pay' ) ); |
| 265 | |
| 266 | add_action( 'woocommerce_proceed_to_checkout', array( $this, 'render_button' ) ); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | /** Checkout functionality ************************************************/ |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * Initializes Apple Pay on the checkout page. |
| 275 | * |
| 276 | * @since 3.0.0 |
| 277 | */ |
| 278 | public function init_checkout() { |
| 279 | |
| 280 | /** |
| 281 | * Filters the Apple Pay checkout handler args. |
| 282 | * |
| 283 | * @since 3.0.0 |
| 284 | * @param array $args |
| 285 | */ |
| 286 | $args = apply_filters( 'sv_wc_apple_pay_checkout_handler_args', array() ); |
| 287 | |
| 288 | ob_start(); |
| 289 | ?> |
| 290 | jQuery(function($) { |
| 291 | window.sv_wc_apple_pay_handler = new Square_Apple_Pay_Checkout_Handler(<?php echo wp_json_encode( $args ); ?>); |
| 292 | }); |
| 293 | <?php |
| 294 | $javascript = ob_get_clean(); |
| 295 | \WooCommerce\Square\Utilities\Helper::enqueue_inline_script( 'wc-square-apple-pay-checkout-inline', $javascript, array( 'jquery', 'wc-square-apple-pay' ) ); |
| 296 | |
| 297 | if ( $this->get_plugin()->is_plugin_active( 'woocommerce-checkout-add-ons.php' ) ) { |
| 298 | add_action( 'woocommerce_review_order_before_payment', array( $this, 'render_button' ) ); |
| 299 | } else { |
| 300 | add_action( 'woocommerce_before_checkout_form', array( $this, 'render_checkout_button' ), 15 ); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | |
| 305 | /** |
| 306 | * Renders the Apple Pay button for checkout. |
| 307 | * |
| 308 | * @since 3.0.0 |
| 309 | */ |
| 310 | public function render_checkout_button() { |
| 311 | |
| 312 | ?> |
| 313 | |
| 314 | <div class="sv-wc-apply-pay-checkout"> |
| 315 | |
| 316 | <?php /** translators: Phrase that preceeds the Apple Pay logo, i.e. "Pay with [logo]" */ |
| 317 | $button_text = __( 'Pay with', 'woocommerce-square' ); |
| 318 | |
| 319 | $this->render_button(); ?> |
| 320 | |
| 321 | <span class="divider"> |
| 322 | <?php /** translators: "or" as in "Pay with Apple Pay [or] regular checkout" */ |
| 323 | esc_html_e( 'or', 'woocommerce-square' ); ?> |
| 324 | </span> |
| 325 | |
| 326 | </div> |
| 327 | |
| 328 | <?php |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /** |
| 333 | * Gets the gateway instance. |
| 334 | * |
| 335 | * @since 3.0.0 |
| 336 | * |
| 337 | * @return SquareFramework\PaymentGateway\Payment_Gateway |
| 338 | */ |
| 339 | protected function get_gateway() { |
| 340 | |
| 341 | return $this->gateway; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | /** |
| 346 | * Gets the gateway plugin instance. |
| 347 | * |
| 348 | * @since 3.0.0 |
| 349 | * |
| 350 | * @return SquareFramework\PaymentGateway\Payment_Gateway_Plugin |
| 351 | */ |
| 352 | protected function get_plugin() { |
| 353 | |
| 354 | return $this->plugin; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Gets the Apple Pay handler instance. |
| 359 | * |
| 360 | * @since 3.0.0 |
| 361 | * |
| 362 | * @return Payment_Gateway_Apple_Pay |
| 363 | */ |
| 364 | protected function get_handler() { |
| 365 | |
| 366 | return $this->handler; |
| 367 | } |
| 368 | } |
| 369 |