id = 'apple_pay'; /** @var \WC_Payment_Gateway overwrite for apple pay settings */ $this->method_title = __('payplug_apple_pay_title', 'payplug'); $this->method_description = ''; $this->has_fields = false; $this->title = __('payplug_apple_pay_title', 'payplug'); $this->description = '
'; $this->domain_name = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : parse_url(home_url(), PHP_URL_HOST); $this->enabled = 'no'; if ($this->checkApplePay() && is_admin()) { $this->enabled = 'yes'; } elseif ($this->checkApplePay() && $this->isSSL()) { if (!is_admin() && $this->get_button_checkout()) { $this->enabled = 'yes'; } if (!is_admin()) { if (!PayplugWoocommerceHelper::is_checkout_block() && $this->get_button_checkout()) { $this->add_apple_pay_css(); add_action('wp_enqueue_scripts', [$this, 'add_apple_pay_js']); } if ($this->get_button_cart() && !PayplugWoocommerceHelper::is_cart_block() && !PayplugWoocommerceHelper::is_subscription()) { $this->enabled = 'yes'; $this->add_apple_pay_css(); add_action('woocommerce_proceed_to_checkout', [$this, 'add_apple_pay_cart_js'], 15); } if ($this->get_button_product() && !PayplugWoocommerceHelper::is_product_block()) { $this->enabled = 'yes'; $this->add_apple_pay_css(); add_action('woocommerce_after_add_to_cart_button', [$this, 'add_apple_pay_product_js'], 15); } } } } /** * Processes admin options for Apple Pay settings. * * @return bool|void */ public function process_admin_options() { $data = $this->get_post_data(); if (isset($data['woocommerce_payplug_mode'])) { if ($this->get_post_data()['woocommerce_payplug_mode'] === '0') { $options = $this->get_configuration()->get_options(); $options['payment_methods']['configuration']['apple_pay']['active'] = false; $this->get_configuration()->update_options($options); } } if (isset($data['woocommerce_payplug_apple_pay'])) { if (($data['woocommerce_payplug_apple_pay'] == 1) && (!$this->checkApplePay())) { add_action('admin_notices', [$this, 'display_notice']); } } } /** * Checks if Apple Pay is authorized and available. * * @return bool */ public function checkApplePay() { $options = $this->settings; //check if module is enabled if (!isset($options['enabled']) || !$options['enabled']) { return false; } if (!isset($options['payment_methods']) || empty($options['payment_methods'])) { return false; } //it's disabled if (!(bool) $options['payment_methods']['configuration']['apple_pay']['active']) { return false; } //Amount validations if (is_cart() && !empty(WC()->cart)) { $order_amount = (float) WC()->cart->total; if ($order_amount < self::MIN_AMOUNT || $order_amount > self::MAX_AMOUNT) { return false; } } $display = json_decode($options['payment_methods']['configuration']['apple_pay']['display'], true); $this->set_button_checkout($display['checkout']); $this->set_button_cart($display['cart']); $this->set_button_product($display['product']); $carriers = json_decode($options['payment_methods']['configuration']['apple_pay']['carriers'], true); if (!empty($carriers)) { $this->set_carriers($carriers); } $account = PayplugWoocommerceHelper::generic_get_account_data_from_options($this->id); //no auth if (!isset($account['payment_methods']['apple_pay']) || !isset($account['payment_methods']['apple_pay']['allowed_domain_names'])) { return false; } //$account has permissions to use apple_pay $auth = isset($account['payment_methods']['apple_pay']['enabled']) && $account['payment_methods']['apple_pay']['enabled']; $domain = parse_url(get_site_url()); $auth_domains = in_array($domain['host'], $account['payment_methods']['apple_pay']['allowed_domain_names']); //lost auth if (!($auth && $auth_domains)) { return false; } return true; } /** * Outputs the payment fields on the checkout page. * * @return void */ public function payment_fields() { $description = $this->get_description(); if (!empty($description)) { echo wpautop(wptexturize($description)); } } /** * extend the woocommmerce get description to include personalized html * * @return mixed|string|null */ public function get_description() { return apply_filters('woocommerce_gateway_description', $this->description, $this->id); } /** * Enqueues Apple Pay scripts for the cart page. * * @return void */ public function add_apple_pay_cart_js() { wp_enqueue_script('apple-pay-sdk', 'https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js', [], false, true); wp_enqueue_script('payplug-apple-pay-cart', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-apple-pay-cart.js', ['jquery', 'apple-pay-sdk'], PAYPLUG_GATEWAY_VERSION, true); wp_localize_script( 'payplug-apple-pay-cart', 'apple_pay_params', [ 'ajax_url_applepay_get_shippings' => \WC_AJAX::get_endpoint('applepay_get_shippings'), 'ajax_url_place_order_with_dummy_data' => \WC_AJAX::get_endpoint('place_order_with_dummy_data'), 'ajax_url_update_applepay_order' => \WC_AJAX::get_endpoint('update_applepay_order'), 'ajax_url_update_applepay_payment' => \WC_AJAX::get_endpoint('update_applepay_payment'), 'ajax_url_applepay_get_order_totals' => \WC_AJAX::get_endpoint('applepay_get_order_totals'), 'ajax_url_applepay_cancel_order' => \WC_AJAX::get_endpoint('applepay_cancel_order'), 'is_cart' => is_cart(), 'is_virtual' => !$this->is_shipping_required(), 'cart_shipping' => WC()->cart->get_shipping_total(), 'countryCode' => WC()->customer->get_billing_country(), 'currencyCode' => get_woocommerce_currency(), 'apple_pay_domain' => $this->domain_name, ] ); if ($this->checkButtonVisibility()) { echo $this->get_description(); } } /** * Enqueues Apple Pay scripts for the product page. * * @return void */ public function add_apple_pay_product_js() { global $product; // Only dispay ApplePay on product page for simple and variable products if ($product->get_type() != 'simple' && $product->get_type() != 'variable') { return; } $apple_pay_params = [ 'ajax_url_applepay_get_shippings' => \WC_AJAX::get_endpoint('applepay_get_shippings'), 'ajax_url_place_order_with_dummy_data' => \WC_AJAX::get_endpoint('place_order_with_dummy_data'), 'ajax_url_update_applepay_order' => \WC_AJAX::get_endpoint('update_applepay_order'), 'ajax_url_update_applepay_payment' => \WC_AJAX::get_endpoint('update_applepay_payment'), 'ajax_url_applepay_get_order_totals' => \WC_AJAX::get_endpoint('applepay_get_order_totals'), 'ajax_url_applepay_cancel_order' => \WC_AJAX::get_endpoint('applepay_cancel_order'), 'ajax_url_applepay_empty_cart' => \WC_AJAX::get_endpoint('applepay_empty_cart'), 'ajax_url_applepay_add_to_cart' => \WC_AJAX::get_endpoint('applepay_add_to_cart'), 'is_product' => is_product(), 'is_virtual' => $product->is_virtual(), 'cart_shipping' => WC()->cart->get_shipping_total(), 'countryCode' => WC()->customer->get_billing_country(), 'currencyCode' => get_woocommerce_currency(), 'apple_pay_domain' => $_SERVER['HTTP_HOST'], ]; wp_enqueue_script('apple-pay-sdk', 'https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js', [], false, true); wp_enqueue_script('payplug-apple-pay-product', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-apple-pay-product.js', ['jquery', 'apple-pay-sdk'], PAYPLUG_GATEWAY_VERSION, true); wp_localize_script('payplug-apple-pay-product', 'apple_pay_params', $apple_pay_params); if ($this->checkButtonVisibility()) { echo $this->get_description(); } } /** * Check if the shipping address is a carrier * * @return bool */ private function checkButtonVisibility() { $apple_carriers = $this->get_carriers(); $allowed = false; $post = $this->get_post_data(); $chosen_method = isset($post['shipping_method'][0]) ? $post['shipping_method'][0] : null; if (empty($chosen_method)) { $chosen_method = !empty(WC()->session->chosen_shipping_methods[0]) ? WC()->session->chosen_shipping_methods[0] : null; } if (!$this->is_shipping_required() || is_product()) { return true; } foreach (WC()->shipping()->get_packages() as $i => $package) { $available_rates = !empty($package['rates']) ? $package['rates'] : []; if (!empty($available_rates)) { foreach ($available_rates as $method) { if (in_array($method->get_method_id(), $apple_carriers)) { if ($chosen_method === $method->get_method_id() . ':' . $method->get_instance_id()) { $allowed = true; } } } } } return $allowed; } /** * check if the shipping is required or not * * @return bool */ public function is_shipping_required() { $cart = WC()->cart->get_cart(); $required = true; foreach ($cart as $cart_item) { if (!empty($cart_item['product_id'])) { $product = wc_get_product($cart_item['product_id']); //not required if it enters here if (!$product->is_virtual() && !$product->is_downloadable()) { return true; } $required = false; } } return $required; } /** * Display unauthorized error * * @return void */ public static function display_notice() { ?>