| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Front; |
| 4 |
|
| 5 |
use Payplug\Payplug; |
| 6 |
use Payplug\PayplugWoocommerce\Controller\ApplePay as ApplePayGateway; |
| 7 |
use Payplug\PayplugWoocommerce\Gateway\PayplugAddressData; |
| 8 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 9 |
use Payplug\PayplugWoocommerce\Traits\ServiceGetter; |
| 10 |
|
| 11 |
class ApplePay |
| 12 |
{ |
| 13 |
use ServiceGetter; |
| 14 |
|
| 15 |
private $options; |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
$this->options = $this->get_service('configuration')->get_options(); |
| 20 |
|
| 21 |
//routes for apple_pay on cart and product page |
| 22 |
add_action('wc_ajax_applepay_get_shippings', [$this, 'applepay_get_shippings']); |
| 23 |
add_action('wc_ajax_place_order_with_dummy_data', [$this, 'place_order_with_dummy_data']); |
| 24 |
add_action('wc_ajax_update_applepay_order', [$this, 'update_applepay_order']); |
| 25 |
add_action('wc_ajax_update_applepay_payment', [$this, 'update_applepay_payment']); |
| 26 |
add_action('wc_ajax_applepay_cancel_order', [$this, 'applepay_cancel_order']); |
| 27 |
add_action('wc_ajax_applepay_empty_cart', [$this, 'applepay_empty_cart']); |
| 28 |
add_action('wc_ajax_applepay_add_to_cart', [$this, 'applepay_add_to_cart']); |
| 29 |
} |
| 30 |
|
| 31 |
public function applepay_get_shippings(): void |
| 32 |
{ |
| 33 |
WC()->customer->set_shipping_country('FR'); |
| 34 |
WC()->customer->set_shipping_city('Paris'); |
| 35 |
WC()->customer->set_shipping_postcode('12345'); |
| 36 |
WC()->customer->set_shipping_address('Dummy Address'); |
| 37 |
|
| 38 |
$packages = WC()->cart->get_shipping_packages(); |
| 39 |
$shippings = []; |
| 40 |
|
| 41 |
foreach ($packages as $package_key => $package) { |
| 42 |
$shipping_methods = $this->get_shipping_methods_from_package($package); |
| 43 |
|
| 44 |
foreach ($shipping_methods as $shipping_method) { |
| 45 |
if (!$shipping_method->supports('shipping-zones') || !$shipping_method->is_enabled()) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
|
| 49 |
$rates = $shipping_method->get_rates_for_package($package); |
| 50 |
$rate_id = $shipping_method->get_rate_id(); |
| 51 |
if (isset($rates[$rate_id]) && $this->checkApplePayShipping($shipping_method)) { |
| 52 |
$shipping_rate = $rates[$rate_id]; |
| 53 |
|
| 54 |
array_push($shippings, [ |
| 55 |
'identifier' => $shipping_method->id . '_' . $shipping_method->instance_id, |
| 56 |
'label' => $shipping_method->method_title, |
| 57 |
'detail' => strip_tags($shipping_method->method_description), |
| 58 |
'amount' => $shipping_rate->get_cost() + $shipping_rate->get_shipping_tax(), |
| 59 |
]); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
wp_send_json_success($shippings); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param $shipping |
| 68 |
* |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
private function checkApplePayShipping($shipping = []) |
| 72 |
{ |
| 73 |
if (empty($shipping)) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
$apple_pay_options = PayplugWoocommerceHelper::get_applepay_options(); |
| 78 |
$apple_pay_carriers = $apple_pay_options['carriers']; |
| 79 |
|
| 80 |
$exists = false; |
| 81 |
foreach ($apple_pay_carriers as $carrier => $carrier_id) { |
| 82 |
if ($carrier_id === $shipping->id) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return $exists; |
| 88 |
} |
| 89 |
|
| 90 |
private function get_shipping_methods_from_package($package) |
| 91 |
{ |
| 92 |
$shipping_zone = \WC_Shipping_Zones::get_zone_matching_package($package); |
| 93 |
|
| 94 |
return $shipping_zone->get_shipping_methods(true); |
| 95 |
} |
| 96 |
|
| 97 |
public function place_order_with_dummy_data(): void |
| 98 |
{ |
| 99 |
$apple_pay = new ApplePayGateway(); |
| 100 |
if (is_admin()) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$cart = WC()->cart; |
| 105 |
|
| 106 |
if (!$cart->is_empty()) { |
| 107 |
try { |
| 108 |
$checkout = WC()->checkout(); |
| 109 |
|
| 110 |
$order_id = $checkout->create_order(['payment_method' => $apple_pay->id]); |
| 111 |
$order = wc_get_order($order_id); |
| 112 |
|
| 113 |
$order->set_address([ |
| 114 |
'first_name' => 'payplug_applepay_first_name', |
| 115 |
'last_name' => 'payplug_applepay_last_name', |
| 116 |
'address_1' => 'payplug_applepay_address', |
| 117 |
'address_2' => '', |
| 118 |
'city' => 'payplug_applepay_city', |
| 119 |
'postcode' => 'payplug_applepay_psotcode', |
| 120 |
'country' => WC()->countries->get_base_country(), |
| 121 |
'email' => 'payplug_applepay_email@payplug.com', |
| 122 |
], 'billing'); |
| 123 |
$order->set_address([ |
| 124 |
'first_name' => 'payplug_applepay_first_name', |
| 125 |
'last_name' => 'payplug_applepay_last_name', |
| 126 |
'address_1' => 'payplug_applepay_address', |
| 127 |
'address_2' => '', |
| 128 |
'city' => 'payplug_applepay_city', |
| 129 |
'postcode' => 'payplug_applepay_psotcode', |
| 130 |
'country' => WC()->countries->get_base_country(), |
| 131 |
'email' => 'payplug_applepay_email@payplug.com', |
| 132 |
], 'shipping'); |
| 133 |
|
| 134 |
$order->set_payment_method($apple_pay); |
| 135 |
|
| 136 |
$packages = WC()->cart->get_shipping_packages(); |
| 137 |
|
| 138 |
WC()->shipping()->reset_shipping(); |
| 139 |
|
| 140 |
$order->calculate_taxes(); |
| 141 |
|
| 142 |
$order->calculate_totals(); |
| 143 |
|
| 144 |
$order->save(); |
| 145 |
|
| 146 |
$cart->empty_cart(); |
| 147 |
|
| 148 |
$this->process_cart_payment($order, $apple_pay); |
| 149 |
} catch (\Exception $exception) { |
| 150 |
wp_send_json_error([ |
| 151 |
'message' => __('Your order was cancelled.', 'woocommerce'), |
| 152 |
]); |
| 153 |
} |
| 154 |
} else { |
| 155 |
wp_send_json_error(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public function process_cart_payment($order, $gateway): void |
| 160 |
{ |
| 161 |
$order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id(); |
| 162 |
$customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id(); |
| 163 |
$amount = (int) PayplugWoocommerceHelper::get_payplug_amount($order->get_total() - ($order->get_shipping_tax() + $order->get_shipping_total())); |
| 164 |
$amount = $gateway->validate_order_amount($amount); |
| 165 |
wp_send_json([ |
| 166 |
'total' => $amount, |
| 167 |
'order_id' => $order_id, |
| 168 |
'payment_data' => $gateway->process_standard_payment($order, $amount, $customer_id, 'product'), |
| 169 |
]); |
| 170 |
} |
| 171 |
|
| 172 |
public function update_applepay_order(): void |
| 173 |
{ |
| 174 |
$order_id = $_POST['order_id']; |
| 175 |
$order = wc_get_order($order_id); |
| 176 |
|
| 177 |
$selected_shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : null; |
| 178 |
if (!empty($_POST['shipping'])) { |
| 179 |
foreach ($_POST['shipping'] as $key => $data) { |
| 180 |
switch ($key) { |
| 181 |
case 'familyName': |
| 182 |
$order->set_shipping_last_name($data); |
| 183 |
break; |
| 184 |
case 'givenName': |
| 185 |
$order->set_shipping_first_name($data); |
| 186 |
break; |
| 187 |
case 'country': |
| 188 |
$order->set_shipping_country($data); |
| 189 |
break; |
| 190 |
case 'locality': |
| 191 |
$order->set_shipping_city($data); |
| 192 |
break; |
| 193 |
case 'phoneNumber': |
| 194 |
$order->set_shipping_phone($data); |
| 195 |
break; |
| 196 |
case 'postalCode': |
| 197 |
$order->set_shipping_postcode($data); |
| 198 |
break; |
| 199 |
case 'addressLines': |
| 200 |
$order->set_shipping_address_1($data[0]); |
| 201 |
if (!empty($data[1])) { |
| 202 |
$order->set_shipping_address_2($data[1]); |
| 203 |
} |
| 204 |
break; |
| 205 |
case 'emailAddress': |
| 206 |
$order->set_billing_email($data); |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
if (!empty($_POST['billing'])) { |
| 212 |
foreach ($_POST['billing'] as $key => $data) { |
| 213 |
switch ($key) { |
| 214 |
case 'familyName': |
| 215 |
$order->set_billing_last_name($data); |
| 216 |
break; |
| 217 |
case 'givenName': |
| 218 |
$order->set_billing_first_name($data); |
| 219 |
break; |
| 220 |
case 'addressLines': |
| 221 |
$order->set_billing_address_1($data[0]); |
| 222 |
if (!empty($data[1])) { |
| 223 |
$order->set_billing_address_2($data[1]); |
| 224 |
} |
| 225 |
break; |
| 226 |
case 'locality': |
| 227 |
$order->set_billing_city($data); |
| 228 |
break; |
| 229 |
case 'country': |
| 230 |
$order->set_billing_country($data); |
| 231 |
break; |
| 232 |
case 'postalCode': |
| 233 |
$order->set_billing_postcode($data); |
| 234 |
break; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
$address_data = PayplugAddressData::from_order($order); |
| 240 |
|
| 241 |
$order->set_shipping_country($address_data->get_shipping()['country']); |
| 242 |
$order->set_billing_country($address_data->get_billing()['country']); |
| 243 |
|
| 244 |
$shipping_address = $address_data->get_shipping(); |
| 245 |
$shipping_address['state'] = ''; |
| 246 |
|
| 247 |
$package = [ |
| 248 |
'contents' => [], |
| 249 |
'contents_cost' => 0, |
| 250 |
'destination' => $shipping_address, |
| 251 |
'applied_coupons' => $order->get_used_coupons(), |
| 252 |
'user' => [ |
| 253 |
'ID' => $order->get_customer_id(), |
| 254 |
], |
| 255 |
]; |
| 256 |
|
| 257 |
foreach ($order->get_items('shipping') as $item_id => $shipping_item) { |
| 258 |
$order->remove_item($item_id); |
| 259 |
} |
| 260 |
|
| 261 |
$use_taxes = false; |
| 262 |
$shipping_zone = \WC_Shipping_Zones::get_zone_matching_package($package); |
| 263 |
if ($shipping_zone && !empty($selected_shipping_method)) { |
| 264 |
$shipping_methods = $shipping_zone->get_shipping_methods(true); |
| 265 |
|
| 266 |
foreach ($shipping_methods as $shipping_method) { |
| 267 |
if (!$shipping_method->supports('shipping-zones') || !$shipping_method->is_enabled()) { |
| 268 |
continue; |
| 269 |
} |
| 270 |
|
| 271 |
$id_shipping_method = $shipping_method->id . '_' . $shipping_method->instance_id; |
| 272 |
if ($id_shipping_method === $selected_shipping_method) { |
| 273 |
$shipping_method->calculate_shipping($package); |
| 274 |
$rates = $shipping_method->get_rates_for_package($package); |
| 275 |
|
| 276 |
if (!empty($rates)) { |
| 277 |
$rate = reset($rates); |
| 278 |
|
| 279 |
$item = new \WC_Order_Item_Shipping(); |
| 280 |
$item->set_method_title($rate->get_label()); |
| 281 |
$item->set_method_id($rate->get_id()); |
| 282 |
$item->set_total($rate->get_cost()); |
| 283 |
|
| 284 |
if (!empty($rate->taxes)) { |
| 285 |
$use_taxes = true; |
| 286 |
} |
| 287 |
$shipping_taxes = \WC_Tax::calc_shipping_tax( |
| 288 |
$rate->get_cost(), |
| 289 |
$rate->get_taxes() |
| 290 |
); |
| 291 |
|
| 292 |
$item->set_taxes([ |
| 293 |
'total' => $shipping_taxes, |
| 294 |
]); |
| 295 |
|
| 296 |
$item->calculate_taxes(); |
| 297 |
|
| 298 |
$item->save(); |
| 299 |
|
| 300 |
$order->add_item($item); |
| 301 |
break; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
$order->calculate_totals(wc_tax_enabled()); |
| 308 |
$order->save(); |
| 309 |
|
| 310 |
wp_send_json($order); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* update the payment since customers can change their options during the payment |
| 315 |
* |
| 316 |
* @throws \Payplug\Exception\ConfigurationNotSetException |
| 317 |
* |
| 318 |
* @return void |
| 319 |
*/ |
| 320 |
public function update_applepay_payment(): void |
| 321 |
{ |
| 322 |
$applepay = new ApplePayGateway(); |
| 323 |
|
| 324 |
$payment_id = $_POST['payment_id']; |
| 325 |
$order_id = $_POST['order_id']; |
| 326 |
$payment_token = $_POST['payment_token']; |
| 327 |
$amount = $_POST['amount']; |
| 328 |
|
| 329 |
$payment = \Payplug\Payment::retrieve($payment_id); |
| 330 |
|
| 331 |
$order = wc_get_order($order_id); |
| 332 |
$amount = (int) PayplugWoocommerceHelper::get_payplug_amount($amount); |
| 333 |
$amount = $applepay->validate_order_amount($amount); |
| 334 |
|
| 335 |
$address_data = PayplugAddressData::from_order($order); |
| 336 |
|
| 337 |
// delivery_type must be removed in Apple Pay |
| 338 |
$billing = $address_data->get_billing(); |
| 339 |
unset($billing['delivery_type']); |
| 340 |
$shipping = $address_data->get_shipping(); |
| 341 |
unset($shipping['delivery_type']); |
| 342 |
|
| 343 |
$data = ['apple_pay' => [ |
| 344 |
'amount' => $amount, |
| 345 |
'payment_token' => $payment_token, |
| 346 |
'billing' => $billing, |
| 347 |
'shipping' => $shipping, |
| 348 |
]]; |
| 349 |
|
| 350 |
$update = $payment->update($data); |
| 351 |
|
| 352 |
wp_send_json_success([ |
| 353 |
'amount' => $amount, |
| 354 |
'update' => $update, |
| 355 |
]); |
| 356 |
} |
| 357 |
|
| 358 |
public function applepay_cancel_order(): void |
| 359 |
{ |
| 360 |
$payment_id = !empty($_POST['payment_id']) ? $_POST['payment_id'] : null; |
| 361 |
$order_id = $_POST['order_id']; |
| 362 |
|
| 363 |
if (empty($_POST['order_id'])) { |
| 364 |
wp_send_json_error([ |
| 365 |
'message' => __('Your order was cancelled.', 'woocommerce'), |
| 366 |
]); |
| 367 |
} |
| 368 |
|
| 369 |
$order = wc_get_order($order_id); |
| 370 |
$items = $order->get_items(); |
| 371 |
foreach ($items as $item) { |
| 372 |
$product_id = $item->get_product_id(); |
| 373 |
$quantity = $item->get_quantity(); |
| 374 |
$variation_id = $item->get_variation_id(); |
| 375 |
$variations = []; |
| 376 |
|
| 377 |
if ($variation_id) { |
| 378 |
$product = wc_get_product($variation_id); |
| 379 |
$variations = $product->get_variation_attributes(); |
| 380 |
} |
| 381 |
|
| 382 |
WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations); |
| 383 |
} |
| 384 |
|
| 385 |
//if there's no payment created |
| 386 |
if (!empty($payment_id)) { |
| 387 |
$api_key = json_decode($this->options['api_key'], true); |
| 388 |
$test_key = !empty($api_key['test']) ? $api_key['test'] : ''; |
| 389 |
$live_key = !empty($api_key['live']) ? $api_key['live'] : ''; |
| 390 |
|
| 391 |
if (empty($test_key) && empty($live_key)) { |
| 392 |
wp_send_json_error([ |
| 393 |
'message' => __('Your order was cancelled.', 'woocommerce'), |
| 394 |
]); |
| 395 |
} |
| 396 |
|
| 397 |
$payment = \Payplug\Payment::retrieve($payment_id, new Payplug((bool) $this->options ? $live_key : $test_key)); |
| 398 |
$payment->abort(new Payplug((bool) $this->options ? $live_key : $test_key)); |
| 399 |
} |
| 400 |
|
| 401 |
if ($order->delete(true)) { |
| 402 |
wp_send_json_success([ |
| 403 |
'message' => __('Your order was cancelled.', 'woocommerce'), |
| 404 |
]); |
| 405 |
} else { |
| 406 |
wp_send_json_error([ |
| 407 |
'message' => __('Your order was cancelled.', 'woocommerce'), |
| 408 |
]); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
public function applepay_empty_cart(): void |
| 413 |
{ |
| 414 |
try { |
| 415 |
WC()->cart->empty_cart(); |
| 416 |
wp_send_json_success(); |
| 417 |
} catch (\Exception $e) { |
| 418 |
wp_send_json_error([ |
| 419 |
'message' => __('Your order was cancelled.', 'woocommerce'), |
| 420 |
]); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
public function applepay_add_to_cart(): void |
| 425 |
{ |
| 426 |
try { |
| 427 |
if (!empty($_POST['product_id'])) { |
| 428 |
$product_id = $_POST['product_id']; |
| 429 |
} else { |
| 430 |
$product_id = $_POST['product_variation_id']; |
| 431 |
} |
| 432 |
|
| 433 |
$product_quantity = !empty($_POST['product_quantity']) ? $_POST['product_quantity'] : 1; |
| 434 |
|
| 435 |
WC()->cart->add_to_cart($product_id, $product_quantity); |
| 436 |
wp_send_json_success([ |
| 437 |
'total' => WC()->cart->total - WC()->cart->shipping_total, |
| 438 |
]); |
| 439 |
} catch (\Exception $e) { |
| 440 |
wp_send_json_error([ |
| 441 |
'message' => __('Your order was cancelled.', 'woocommerce'), |
| 442 |
]); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|