PluginProbe
PayPlug for WooCommerce (Official) / 2.9.0
PayPlug for WooCommerce (Official) v2.9.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Front / ApplePay.php

ApplePay.php in PayPlug for WooCommerce (Official) 2.9.0, at src/Front/ApplePay.php

439 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Payplug\PayplugWoocommerce\Front;
3
4 use Payplug\Payplug;
5 use Payplug\PayplugWoocommerce\Controller\ApplePay as ApplePayGateway;
6 use Payplug\PayplugWoocommerce\Gateway\PayplugAddressData;
7 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
8 use function is_cart;
9
10 class ApplePay {
11
12 public function __construct() {
13
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 }
21
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');
27
28 $packages = WC()->cart->get_shipping_packages();
29 $shippings = [];
30
31 foreach ( $packages as $package_key => $package ) {
32 $shipping_methods = $this->get_shipping_methods_from_package($package);
33
34 foreach ( $shipping_methods as $shipping_method ) {
35
36 if (!$shipping_method->supports('shipping-zones') || !$shipping_method->is_enabled()) {
37 continue;
38 }
39
40 $rates = $shipping_method->get_rates_for_package($package);
41 if($this->checkApplePayShipping($shipping_method)){
42 $shipping_rate = $rates[$shipping_method->get_rate_id()];
43
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 }
55
56 /**
57 * @param $shipping
58 * @return bool
59 */
60 private function checkApplePayShipping($shipping = []){
61 if(empty($shipping)){
62 return false;
63 }
64
65 $apple_pay_options = PayplugWoocommerceHelper::get_applepay_options();
66 $apple_pay_carriers = $apple_pay_options['carriers'];
67
68 $exists = false;
69 foreach($apple_pay_carriers as $carrier => $carrier_id){
70 if($carrier_id === $shipping->id){
71 return true;
72 }
73 }
74
75 return $exists;
76 }
77
78
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 }
83
84 public function place_order_with_dummy_data() {
85 $apple_pay = new ApplePayGateway();
86 if ( is_admin() ) {
87 return;
88 }
89
90 $cart = WC()->cart;
91
92
93 if ( ! $cart->is_empty() ) {
94 try{
95 $checkout = WC()->checkout();
96
97 $order_id = $checkout->create_order(array('payment_method' => $apple_pay->id));
98 $order = wc_get_order($order_id);
99
100
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' => 'payplug_applepay_email@payplug.com'
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' => 'payplug_applepay_email@payplug.com'
120 ], 'shipping' );
121
122 $order->set_payment_method($apple_pay);
123
124 $packages = WC()->cart->get_shipping_packages();
125
126 WC()->shipping()->reset_shipping();
127
128 foreach ( $packages as $package_key => $package ) {
129
130 $shipping_methods = $this->get_shipping_methods_from_package($package);
131
132 foreach ( $shipping_methods as $shipping_method ) {
133
134 if ( ! $shipping_method->supports( 'shipping-zones' ) || ! $shipping_method->is_enabled() ) {
135 continue;
136 }
137
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());
147
148 $shipping_taxes = \WC_Tax::calc_shipping_tax(
149 $rate->cost,
150 \WC_Tax::get_shipping_tax_rates()
151 );
152
153 $shipping->set_taxes([
154 'total' => $shipping_taxes
155 ]);
156
157 $shipping->save();
158 $order->add_item($shipping);
159 break;
160 }
161 }
162 }
163 }
164
165 $order->calculate_taxes();
166
167 $order->calculate_totals();
168
169 $order->save();
170
171 $cart->empty_cart();
172
173 $this->process_cart_payment($order, $apple_pay);
174
175 }catch (\Exception $exception){
176 wp_send_json_error([
177 'message' => __('Your order was cancelled.', 'woocommerce')
178 ]);
179 }
180
181 } else {
182 wp_send_json_error();
183 }
184 }
185
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 ]);
196
197 }
198
199 public function update_applepay_order() {
200 $order_id = $_POST['order_id'];
201 $order = wc_get_order( $order_id );
202
203 $selected_shipping_method = $_POST['shipping_method'];
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 }
264
265 $address_data = PayplugAddressData::from_order($order);
266
267 $order->set_shipping_country($address_data->get_shipping()['country']);
268 $order->set_billing_country($address_data->get_billing()['country']);
269
270 $shipping_address= $address_data->get_shipping();
271 $shipping_address['state'] = '';
272
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 );
282
283 foreach ($order->get_items('shipping') as $item_id => $shipping_item) {
284 $order->remove_item($item_id);
285 }
286
287
288 $shipping_zone = \WC_Shipping_Zones::get_zone_matching_package( $package );
289 if ( $shipping_zone ) {
290
291 $shipping_methods = $shipping_zone->get_shipping_methods( true );
292
293 foreach ( $shipping_methods as $shipping_method ) {
294 if ( ! $shipping_method->supports( 'shipping-zones' ) || ! $shipping_method->is_enabled() ) {
295 continue;
296 }
297
298 if ($shipping_method->id === $selected_shipping_method) {
299
300 $shipping_method->calculate_shipping( $package );
301 $rates = $shipping_method->get_rates_for_package($package);
302
303 if ( ! empty( $rates ) ) {
304
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() );
310
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 ] );
318
319 $item->calculate_taxes();
320
321 $item->save();
322
323 $order->add_item( $item );
324 break;
325 }
326 }
327 }
328 }
329
330 $order->calculate_taxes();
331 $order->calculate_totals();
332 $order->save();
333
334 wp_send_json($order);
335
336 }
337
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() {
344
345 $applepay = new ApplePayGateway();
346
347 $payment_id = $_POST['payment_id'];
348 $order_id = $_POST['order_id'];
349 $payment_token = $_POST['payment_token'];
350 $amount = $_POST['amount']/100;
351
352
353 $payment = \Payplug\Payment::retrieve($payment_id);
354
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
438 }
439