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