woocommerce-paypal-payments/modules/ppcp-applepay/src/Assets/DataToAppleButtonScripts.php

278 lines
8.6 KiB
PHP
Raw Normal View History

<?php
/**
* Prepares the necessary data for the Apple button script.
*
* @package WooCommerce\PayPalCommerce\Applepay
*/
declare(strict_types=1);
2023-08-24 18:05:05 +02:00
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
2023-09-08 16:58:32 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class DataToAppleButtonScripts
*/
class DataToAppleButtonScripts {
2023-08-24 18:05:05 +02:00
/**
* The URL to the SDK.
*
* @var string
*/
private $sdk_url;
2023-09-05 09:14:20 +02:00
/**
* The settings.
*
2023-09-08 16:58:32 +02:00
* @var Settings
2023-09-05 09:14:20 +02:00
*/
private $settings;
2023-08-24 18:05:05 +02:00
2023-09-08 16:58:32 +02:00
/**
* DataToAppleButtonScripts constructor.
*
2023-09-09 11:42:11 +02:00
* @param string $sdk_url The URL to the SDK.
2023-09-08 16:58:32 +02:00
* @param Settings $settings The settings.
*/
2023-09-09 11:42:11 +02:00
public function __construct( string $sdk_url, Settings $settings ) {
2023-09-08 16:58:32 +02:00
$this->sdk_url = $sdk_url;
2023-09-05 09:14:20 +02:00
$this->settings = $settings;
2023-08-24 18:05:05 +02:00
}
/**
* Sets the appropriate data to send to ApplePay script
* Data differs between product page and cart page
*
2023-09-08 16:58:32 +02:00
* @param bool $is_block Whether the button is in a block or not.
* @return array
2023-09-08 16:58:32 +02:00
* @throws NotFoundException When the setting is not found.
*/
public function apple_pay_script_data( bool $is_block = false ): array {
2023-09-08 16:58:32 +02:00
$base_location = wc_get_base_location();
$shop_country_code = $base_location['country'];
2023-09-08 16:58:32 +02:00
$currency_code = get_woocommerce_currency();
$total_label = get_bloginfo( 'name' );
if ( is_product() ) {
return $this->data_for_product_page(
$shop_country_code,
$currency_code,
$total_label
);
}
2023-09-05 09:14:20 +02:00
return $this->data_for_cart_page(
$shop_country_code,
$currency_code,
$total_label
);
}
/**
* Returns the appropriate admin data to send to ApplePay script
*
* @return array
* @throws NotFoundException When the setting is not found.
*/
public function apple_pay_script_data_for_admin(): array {
$base_location = wc_get_base_location();
$shop_country_code = $base_location['country'];
$currency_code = get_woocommerce_currency();
$total_label = get_bloginfo( 'name' );
return $this->data_for_admin_page(
$shop_country_code,
$currency_code,
$total_label
);
}
/**
* Check if the product needs shipping
*
2023-09-08 16:58:32 +02:00
* @param \WC_Product $product The product.
*
* @return bool
*/
2023-09-08 16:58:32 +02:00
protected function check_if_need_shipping( $product ) {
if (
! wc_shipping_enabled()
|| 0 === wc_get_shipping_method_count(
true
)
) {
return false;
}
$needs_shipping = false;
if ( $product->needs_shipping() ) {
$needs_shipping = true;
}
return $needs_shipping;
}
/**
2023-09-08 16:58:32 +02:00
* Prepares the data for the product page.
*
* @param string $shop_country_code The shop country code.
* @param string $currency_code The currency code.
* @param string $total_label The label for the total amount.
*
* @return array
2023-09-08 16:58:32 +02:00
* @throws NotFoundException When the setting is not found.
*/
protected function data_for_product_page(
$shop_country_code,
$currency_code,
$total_label
) {
$product = wc_get_product( get_the_id() );
if ( ! $product ) {
return array();
}
$is_variation = false;
if ( $product->get_type() === 'variable' || $product->get_type() === 'variable-subscription' ) {
$is_variation = true;
}
$product_need_shipping = $this->check_if_need_shipping( $product );
2023-09-08 16:58:32 +02:00
$product_id = get_the_id();
$product_price = $product->get_price();
$product_stock = $product->get_stock_status();
2023-09-11 13:19:27 +02:00
$type = $this->settings->has( 'applepay_button_type' ) ? $this->settings->get( 'applepay_button_type' ) : '';
$color = $this->settings->has( 'applepay_button_color' ) ? $this->settings->get( 'applepay_button_color' ) : '';
$lang = $this->settings->has( 'applepay_button_language' ) ? $this->settings->get( 'applepay_button_language' ) : '';
$checkout_data_mode = $this->settings->has( 'applepay_checkout_data_mode' ) ? $this->settings->get( 'applepay_checkout_data_mode' ) : PropertiesDictionary::BILLING_DATA_MODE_DEFAULT;
return array(
'sdk_url' => $this->sdk_url,
'is_debug' => defined( 'WP_DEBUG' ) && WP_DEBUG ? true : false,
'preferences' => array(
'checkout_data_mode' => $checkout_data_mode,
),
'button' => array(
2023-09-11 14:23:05 +02:00
'wrapper' => 'applepay-container',
'mini_cart_wrapper' => 'applepay-container-minicart',
2023-09-08 16:58:32 +02:00
'type' => $type,
'color' => $color,
'lang' => $lang,
2023-08-31 12:48:01 +02:00
),
'product' => array(
'needShipping' => $product_need_shipping,
'id' => $product_id,
'price' => $product_price,
'isVariation' => $is_variation,
'stock' => $product_stock,
),
'shop' => array(
'countryCode' => $shop_country_code,
'currencyCode' => $currency_code,
'totalLabel' => $total_label,
),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'woocommerce-process_checkout' ),
);
}
/**
2023-09-08 16:58:32 +02:00
* Prepares the data for the cart page.
*
* @param string $shop_country_code The shop country code.
* @param string $currency_code The currency code.
* @param string $total_label The label for the total amount.
*
* @return array
*/
protected function data_for_cart_page(
$shop_country_code,
$currency_code,
$total_label
) {
2023-09-12 14:07:09 +02:00
$cart = WC()->cart;
2023-09-12 11:43:11 +02:00
if ( ! $cart ) {
return array();
}
2023-10-26 16:33:00 +01:00
$type = $this->settings->has( 'applepay_button_type' ) ? $this->settings->get( 'applepay_button_type' ) : '';
$color = $this->settings->has( 'applepay_button_color' ) ? $this->settings->get( 'applepay_button_color' ) : '';
$lang = $this->settings->has( 'applepay_button_language' ) ? $this->settings->get( 'applepay_button_language' ) : '';
$lang = apply_filters( 'woocommerce_paypal_payments_applepay_button_language', $lang );
$checkout_data_mode = $this->settings->has( 'applepay_checkout_data_mode' ) ? $this->settings->get( 'applepay_checkout_data_mode' ) : PropertiesDictionary::BILLING_DATA_MODE_DEFAULT;
2023-09-18 11:18:40 +02:00
return array(
'sdk_url' => $this->sdk_url,
'is_debug' => defined( 'WP_DEBUG' ) && WP_DEBUG ? true : false,
'preferences' => array(
'checkout_data_mode' => $checkout_data_mode,
),
'button' => array(
2023-09-10 13:21:19 +02:00
'wrapper' => 'applepay-container',
'mini_cart_wrapper' => 'applepay-container-minicart',
2023-09-18 11:18:40 +02:00
'type' => $type,
'color' => $color,
'lang' => $lang,
2023-09-05 09:14:20 +02:00
),
'product' => array(
'needShipping' => $cart->needs_shipping(),
'subtotal' => $cart->get_subtotal(),
),
'shop' => array(
'countryCode' => $shop_country_code,
'currencyCode' => $currency_code,
'totalLabel' => $total_label,
),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'woocommerce-process_checkout' ),
);
}
/**
* Prepares the data for the cart page.
* Consider refactoring this method along with data_for_cart_page() and data_for_product_page() methods.
*
* @param string $shop_country_code The shop country code.
* @param string $currency_code The currency code.
* @param string $total_label The label for the total amount.
*
* @return array
*/
protected function data_for_admin_page(
$shop_country_code,
$currency_code,
$total_label
) {
$type = $this->settings->has( 'applepay_button_type' ) ? $this->settings->get( 'applepay_button_type' ) : '';
$color = $this->settings->has( 'applepay_button_color' ) ? $this->settings->get( 'applepay_button_color' ) : '';
$lang = $this->settings->has( 'applepay_button_language' ) ? $this->settings->get( 'applepay_button_language' ) : '';
$lang = apply_filters( 'woocommerce_paypal_payments_applepay_button_language', $lang );
$checkout_data_mode = $this->settings->has( 'applepay_checkout_data_mode' ) ? $this->settings->get( 'applepay_checkout_data_mode' ) : PropertiesDictionary::BILLING_DATA_MODE_DEFAULT;
return array(
'sdk_url' => $this->sdk_url,
'is_debug' => defined( 'WP_DEBUG' ) && WP_DEBUG ? true : false,
'preferences' => array(
'checkout_data_mode' => $checkout_data_mode,
),
'button' => array(
'wrapper' => 'applepay-container',
'mini_cart_wrapper' => 'applepay-container-minicart',
'type' => $type,
'color' => $color,
'lang' => $lang,
),
'product' => array(
'needShipping' => false,
'subtotal' => 0,
),
'shop' => array(
'countryCode' => $shop_country_code,
'currencyCode' => $currency_code,
'totalLabel' => $total_label,
),
'ajax_url' => admin_url( 'admin-ajax.php' ),
);
}
}