| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway\Blocks; |
| 4 |
|
| 5 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 6 |
|
| 7 |
class PayplugApplePay extends PayplugGenericBlock |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Payment method name/id/slug. |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected $name = 'apple_pay'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Returns an associative array of data to be exposed for the payment method's client side. |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
public function get_payment_method_data() |
| 22 |
{ |
| 23 |
$data = parent::get_payment_method_data(); |
| 24 |
$data['icon'] = [ |
| 25 |
'src' => esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/images/checkout/' . $this->gateway->image), |
| 26 |
'icon_alt' => $data['name'], |
| 27 |
]; |
| 28 |
|
| 29 |
$data['payplug_locale'] = get_locale(); |
| 30 |
$data['payplug_countryCode'] = WC()->customer !== null ? WC()->customer->get_billing_country() : 'FR'; |
| 31 |
$data['payplug_currencyCode'] = get_woocommerce_currency(); |
| 32 |
$data['payplug_apple_pay_domain'] = wp_parse_url(home_url(), PHP_URL_HOST); |
| 33 |
$data['is_order_pay'] = is_wc_endpoint_url('order-pay'); |
| 34 |
$data['order_pay_id'] = $data['is_order_pay'] ? (int) get_query_var('order-pay') : 0; |
| 35 |
$data['order_pay_key'] = $data['is_order_pay'] ? wc_clean(wp_unslash($_GET['key'] ?? '')) : ''; |
| 36 |
$data['ajax_url_applepay_update_payment'] = \WC_AJAX::get_endpoint('applepay_update_payment'); |
| 37 |
$data['payplug_create_intent_payment'] = \WC_AJAX::get_endpoint('payplug_create_intent'); |
| 38 |
$data['is_cart'] = is_cart() && $this->gateway->get_button_cart() && !PayplugWoocommerceHelper::is_subscription(); |
| 39 |
$data['is_product'] = is_product() && $this->gateway->get_button_product() && !PayplugWoocommerceHelper::is_subscription(); |
| 40 |
|
| 41 |
$data['ajax_url_applepay_get_shippings'] = \WC_AJAX::get_endpoint('applepay_get_shippings'); |
| 42 |
$data['ajax_url_place_order_with_dummy_data'] = \WC_AJAX::get_endpoint('place_order_with_dummy_data'); |
| 43 |
$data['ajax_url_update_applepay_order'] = \WC_AJAX::get_endpoint('update_applepay_order'); |
| 44 |
$data['ajax_url_update_applepay_payment'] = \WC_AJAX::get_endpoint('update_applepay_payment'); |
| 45 |
$data['ajax_url_applepay_get_order_totals'] = \WC_AJAX::get_endpoint('applepay_get_order_totals'); |
| 46 |
$data['ajax_url_applepay_cancel_order'] = \WC_AJAX::get_endpoint('applepay_cancel_order'); |
| 47 |
$data['ajax_url_applepay_empty_cart'] = \WC_AJAX::get_endpoint('applepay_empty_cart'); |
| 48 |
$data['ajax_url_applepay_add_to_cart'] = \WC_AJAX::get_endpoint('applepay_add_to_cart'); |
| 49 |
|
| 50 |
$data['countryCode'] = WC()->customer !== null ? WC()->customer->get_billing_country() : 'FR'; |
| 51 |
$data['currencyCode'] = get_woocommerce_currency(); |
| 52 |
$data['apple_pay_domain'] = wp_parse_url(home_url(), PHP_URL_HOST); |
| 53 |
$data['payplug_authorized_carriers'] = $this->gateway->get_carriers(); |
| 54 |
$data['payplug_carriers'] = $this->get_carriers(); |
| 55 |
|
| 56 |
return $data; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns an array of scripts/handles to be registered for this payment method. |
| 61 |
* |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function get_payment_method_script_handles() |
| 65 |
{ |
| 66 |
wp_register_script( |
| 67 |
'apple-pay-sdk', |
| 68 |
'https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js', |
| 69 |
[], |
| 70 |
'1.latest', |
| 71 |
true |
| 72 |
); |
| 73 |
|
| 74 |
wp_enqueue_style('payplug-apple-pay', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-apple-pay.css', [], PAYPLUG_GATEWAY_VERSION); |
| 75 |
wp_enqueue_script('apple-pay-sdk'); |
| 76 |
|
| 77 |
return parent::get_payment_method_script_handles(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Checks if the Apple Pay payment method is active. |
| 82 |
* |
| 83 |
* @return bool |
| 84 |
*/ |
| 85 |
public function is_active() |
| 86 |
{ |
| 87 |
if (class_exists('WC_Blocks_Utils')) { |
| 88 |
|
| 89 |
//replace for the helper function when merged |
| 90 |
if (\WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout') || |
| 91 |
\WC_Blocks_Utils::has_block_in_page(wc_get_page_id('cart'), 'woocommerce/cart') || |
| 92 |
\WC_Blocks_Utils::has_block_in_page(wc_get_page_id('product'), 'woocommerce/product')) { |
| 93 |
if (!empty($this->gateway) && $this->gateway->checkApplePay() && $this->gateway->isSSL()) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
return false; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Retrieves available shipping carriers for Apple Pay. |
| 104 |
* |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
public function get_carriers() |
| 108 |
{ |
| 109 |
$shippings = []; |
| 110 |
|
| 111 |
if (is_null(WC()->cart)) { |
| 112 |
wc_load_cart(); |
| 113 |
WC()->cart->get_cart_from_session(); |
| 114 |
} |
| 115 |
|
| 116 |
if (empty(WC()->cart) || empty(WC()->cart->get_shipping_packages())) { |
| 117 |
return $shippings; |
| 118 |
} |
| 119 |
|
| 120 |
$packages = WC()->cart !== null ? WC()->cart->get_shipping_packages() : null; |
| 121 |
|
| 122 |
if (!empty($packages)) { |
| 123 |
foreach ($packages as $package_key => $package) { |
| 124 |
$shipping_methods = $this->get_shipping_methods_from_package($package); |
| 125 |
foreach ($shipping_methods as $shipping_method) { |
| 126 |
if (!$shipping_method->supports('shipping-zones') || !$shipping_method->is_enabled()) { |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
$rates = $shipping_method->get_rates_for_package($package); |
| 131 |
if ($this->checkApplePayShipping($shipping_method)) { |
| 132 |
if (empty($rates[$shipping_method->get_rate_id()])) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
$shipping_rate = $rates[$shipping_method->get_rate_id()]; |
| 136 |
|
| 137 |
!empty($shipping_rate) && method_exists($shipping_rate, 'get_cost') ? $shipping_costs = $shipping_rate->get_cost() : $shipping_costs = 0; |
| 138 |
!empty($shipping_rate) && method_exists($shipping_rate, 'get_shipping_tax') ? $shipping_tax = $shipping_rate->get_shipping_tax() : $shipping_tax = 'Na'; |
| 139 |
|
| 140 |
array_push($shippings, [ |
| 141 |
'identifier' => $shipping_method->id, |
| 142 |
'label' => $shipping_method->method_title, |
| 143 |
'detail' => strip_tags($shipping_method->method_description), |
| 144 |
'amount' => $shipping_costs + $shipping_tax, |
| 145 |
]); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return $shippings; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Checks if the shipping method is compatible with Apple Pay. |
| 156 |
* |
| 157 |
* @param array $shipping |
| 158 |
* |
| 159 |
* @return bool |
| 160 |
*/ |
| 161 |
public function checkApplePayShipping($shipping = []) |
| 162 |
{ |
| 163 |
if (empty($shipping)) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
$apple_pay_options = PayplugWoocommerceHelper::get_applepay_options(); |
| 168 |
$apple_pay_carriers = $apple_pay_options['carriers']; |
| 169 |
|
| 170 |
$exists = false; |
| 171 |
foreach ($apple_pay_carriers as $carrier => $carrier_id) { |
| 172 |
if ($carrier_id === $shipping->id) { |
| 173 |
return true; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return $exists; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Retrieves shipping methods from a package. |
| 182 |
* |
| 183 |
* @param array $package |
| 184 |
* |
| 185 |
* @return array |
| 186 |
*/ |
| 187 |
public function get_shipping_methods_from_package($package) |
| 188 |
{ |
| 189 |
$shipping_zone = \WC_Shipping_Zones::get_zone_matching_package($package); |
| 190 |
|
| 191 |
return $shipping_zone->get_shipping_methods(true); |
| 192 |
} |
| 193 |
} |
| 194 |
|