-
Notifications
You must be signed in to change notification settings - Fork 47
/
class-smartbutton.php
883 lines (808 loc) · 25.3 KB
/
class-smartbutton.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
<?php
/**
* Registers and configures the necessary Javascript for the button, credit messaging and DCC fields.
*
* @package Inpsyde\PayPalCommerce\Button\Assets
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Assets;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies;
use Inpsyde\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
use Inpsyde\PayPalCommerce\Button\Helper\MessagesApply;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class SmartButton
*/
class SmartButton implements SmartButtonInterface {
/**
* The URL to the module.
*
* @var string
*/
private $module_url;
/**
* The Session Handler.
*
* @var SessionHandler
*/
private $session_handler;
/**
* The settings.
*
* @var Settings
*/
private $settings;
/**
* The Payee Repository.
*
* @var PayeeRepository
*/
private $payee_repository;
/**
* The Identity Token.
*
* @var IdentityToken
*/
private $identity_token;
/**
* The Payer Factory.
*
* @var PayerFactory
*/
private $payer_factory;
/**
* The client ID.
*
* @var string
*/
private $client_id;
/**
* The Request Data.
*
* @var RequestData
*/
private $request_data;
/**
* The DCC Applies helper.
*
* @var DccApplies
*/
private $dcc_applies;
/**
* The Subscription Helper.
*
* @var SubscriptionHelper
*/
private $subscription_helper;
/**
* The Messages apply helper.
*
* @var MessagesApply
*/
private $messages_apply;
/**
* SmartButton constructor.
*
* @param string $module_url The URL to the module.
* @param SessionHandler $session_handler The Session Handler.
* @param Settings $settings The Settings.
* @param PayeeRepository $payee_repository The Payee Repository.
* @param IdentityToken $identity_token The Identity Token.
* @param PayerFactory $payer_factory The Payer factory.
* @param string $client_id The client ID.
* @param RequestData $request_data The Request Data helper.
* @param DccApplies $dcc_applies The DCC applies helper.
* @param SubscriptionHelper $subscription_helper The subscription helper.
* @param MessagesApply $messages_apply The Messages apply helper.
*/
public function __construct(
string $module_url,
SessionHandler $session_handler,
Settings $settings,
PayeeRepository $payee_repository,
IdentityToken $identity_token,
PayerFactory $payer_factory,
string $client_id,
RequestData $request_data,
DccApplies $dcc_applies,
SubscriptionHelper $subscription_helper,
MessagesApply $messages_apply
) {
$this->module_url = $module_url;
$this->session_handler = $session_handler;
$this->settings = $settings;
$this->payee_repository = $payee_repository;
$this->identity_token = $identity_token;
$this->payer_factory = $payer_factory;
$this->client_id = $client_id;
$this->request_data = $request_data;
$this->dcc_applies = $dcc_applies;
$this->subscription_helper = $subscription_helper;
$this->messages_apply = $messages_apply;
}
/**
* Registers the necessary action hooks to render the HTML depending on the settings.
*
* @return bool
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
*/
public function render_wrapper(): bool {
if ( ! $this->can_save_vault_token() && $this->has_subscriptions() ) {
return false;
}
if ( $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' ) ) {
$this->render_button_wrapper_registrar();
$this->render_message_wrapper_registrar();
}
if (
$this->settings->has( 'dcc_enabled' )
&& $this->settings->get( 'dcc_enabled' )
&& ! $this->session_handler->order()
) {
add_action(
'woocommerce_review_order_after_submit',
array(
$this,
'dcc_renderer',
),
11
);
}
return true;
}
/**
* Registers the hooks to render the credit messaging HTML depending on the settings.
*
* @return bool
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
*/
private function render_message_wrapper_registrar(): bool {
$not_enabled_on_cart = $this->settings->has( 'message_cart_enabled' ) &&
! $this->settings->get( 'message_cart_enabled' );
if (
is_cart()
&& ! $not_enabled_on_cart
) {
add_action(
'woocommerce_proceed_to_checkout',
array(
$this,
'message_renderer',
),
19
);
}
$not_enabled_on_product_page = $this->settings->has( 'message_product_enabled' ) &&
! $this->settings->get( 'message_product_enabled' );
if (
( is_product() || wc_post_content_has_shortcode( 'product_page' ) )
&& ! $not_enabled_on_product_page
) {
add_action(
'woocommerce_single_product_summary',
array(
$this,
'message_renderer',
),
30
);
}
$not_enabled_on_checkout = $this->settings->has( 'message_enabled' ) &&
! $this->settings->get( 'message_enabled' );
if ( ! $not_enabled_on_checkout ) {
add_action(
'woocommerce_review_order_after_submit',
array(
$this,
'message_renderer',
),
11
);
}
return true;
}
/**
* Registers the hooks where to render the button HTML according to the settings.
*
* @return bool
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
*/
private function render_button_wrapper_registrar(): bool {
$not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) &&
! $this->settings->get( 'button_cart_enabled' );
if (
is_cart()
&& ! $not_enabled_on_cart
) {
add_action(
'woocommerce_proceed_to_checkout',
array(
$this,
'button_renderer',
),
20
);
}
$not_enabled_on_product_page = $this->settings->has( 'button_single_product_enabled' ) &&
! $this->settings->get( 'button_single_product_enabled' );
if (
( is_product() || wc_post_content_has_shortcode( 'product_page' ) )
&& ! $not_enabled_on_product_page
) {
add_action(
'woocommerce_single_product_summary',
array(
$this,
'button_renderer',
),
31
);
}
$not_enabled_on_minicart = $this->settings->has( 'button_mini_cart_enabled' ) &&
! $this->settings->get( 'button_mini_cart_enabled' );
if (
! $not_enabled_on_minicart
) {
add_action(
'woocommerce_widget_shopping_cart_after_buttons',
static function () {
echo '<p
id="ppc-button-minicart"
class="woocommerce-mini-cart__buttons buttons"
></p>';
},
30
);
}
add_action( 'woocommerce_review_order_after_submit', array( $this, 'button_renderer' ), 10 );
return true;
}
/**
* Enqueues the script.
*
* @return bool
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
*/
public function enqueue(): bool {
$buttons_enabled = $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' );
if ( ! is_checkout() && ! $buttons_enabled ) {
return false;
}
if ( ! $this->can_save_vault_token() && $this->has_subscriptions() ) {
return false;
}
if ( $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ) ) {
wp_enqueue_style(
'ppcp-hosted-fields',
$this->module_url . '/assets/css/hosted-fields.css',
array(),
1
);
}
$load_script = false;
if ( is_checkout() && $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ) ) {
$load_script = true;
}
if ( $this->load_button_component() ) {
$load_script = true;
}
if ( $load_script ) {
wp_enqueue_script(
'ppcp-smart-button',
$this->module_url . '/assets/js/button.js',
array( 'jquery' ),
1,
true
);
wp_localize_script(
'ppcp-smart-button',
'PayPalCommerceGateway',
$this->localize_script()
);
}
return true;
}
/**
* Renders the HTML for the buttons.
*/
public function button_renderer() {
$product = wc_get_product();
if (
! is_checkout() && is_a( $product, \WC_Product::class )
&& (
$product->is_type( array( 'external', 'grouped' ) )
|| ! $product->is_in_stock()
)
) {
return;
}
echo '<div id="ppc-button"></div>';
}
/**
* Renders the HTML for the credit messaging.
*/
public function message_renderer() {
echo '<div id="ppcp-messages"></div>';
}
/**
* The values for the credit messaging.
*
* @return array
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
*/
private function message_values(): array {
if (
$this->settings->has( 'disable_funding' )
&& in_array( 'credit', (array) $this->settings->get( 'disable_funding' ), true )
) {
return array();
}
$placement = 'product';
$product = wc_get_product();
$amount = ( is_a( $product, \WC_Product::class ) ) ? wc_get_price_including_tax( $product ) : 0;
$layout = $this->settings->has( 'message_product_layout' ) ?
$this->settings->get( 'message_product_layout' ) : 'text';
$logo_type = $this->settings->has( 'message_product_logo' ) ?
$this->settings->get( 'message_product_logo' ) : 'primary';
$logo_position = $this->settings->has( 'message_product_position' ) ?
$this->settings->get( 'message_product_position' ) : 'left';
$text_color = $this->settings->has( 'message_product_color' ) ?
$this->settings->get( 'message_product_color' ) : 'black';
$style_color = $this->settings->has( 'message_product_flex_color' ) ?
$this->settings->get( 'message_product_flex_color' ) : 'blue';
$ratio = $this->settings->has( 'message_product_flex_ratio' ) ?
$this->settings->get( 'message_product_flex_ratio' ) : '1x1';
$should_show = $this->settings->has( 'message_product_enabled' )
&& $this->settings->get( 'message_product_enabled' );
if ( is_checkout() ) {
$placement = 'payment';
$amount = WC()->cart->get_total( 'raw' );
$layout = $this->settings->has( 'message_layout' ) ?
$this->settings->get( 'message_layout' ) : 'text';
$logo_type = $this->settings->has( 'message_logo' ) ?
$this->settings->get( 'message_logo' ) : 'primary';
$logo_position = $this->settings->has( 'message_position' ) ?
$this->settings->get( 'message_position' ) : 'left';
$text_color = $this->settings->has( 'message_color' ) ?
$this->settings->get( 'message_color' ) : 'black';
$style_color = $this->settings->has( 'message_flex_color' ) ?
$this->settings->get( 'message_flex_color' ) : 'blue';
$ratio = $this->settings->has( 'message_flex_ratio' ) ?
$this->settings->get( 'message_flex_ratio' ) : '1x1';
$should_show = $this->settings->has( 'message_enabled' )
&& $this->settings->get( 'message_enabled' );
}
if ( is_cart() ) {
$placement = 'cart';
$amount = WC()->cart->get_total( 'raw' );
$layout = $this->settings->has( 'message_cart_layout' ) ?
$this->settings->get( 'message_cart_layout' ) : 'text';
$logo_type = $this->settings->has( 'message_cart_logo' ) ?
$this->settings->get( 'message_cart_logo' ) : 'primary';
$logo_position = $this->settings->has( 'message_cart_position' ) ?
$this->settings->get( 'message_cart_position' ) : 'left';
$text_color = $this->settings->has( 'message_cart_color' ) ?
$this->settings->get( 'message_cart_color' ) : 'black';
$style_color = $this->settings->has( 'message_cart_flex_color' ) ?
$this->settings->get( 'message_cart_flex_color' ) : 'blue';
$ratio = $this->settings->has( 'message_cart_flex_ratio' ) ?
$this->settings->get( 'message_cart_flex_ratio' ) : '1x1';
$should_show = $this->settings->has( 'message_cart_enabled' )
&& $this->settings->get( 'message_cart_enabled' );
}
if ( ! $should_show ) {
return array();
}
$values = array(
'wrapper' => '#ppcp-messages',
'amount' => $amount,
'placement' => $placement,
'style' => array(
'layout' => $layout,
'logo' => array(
'type' => $logo_type,
'position' => $logo_position,
),
'text' => array(
'color' => $text_color,
),
'color' => $style_color,
'ratio' => $ratio,
),
);
return $values;
}
/**
* Renders the HTML for the DCC fields.
*
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting hasnt been found.
*/
public function dcc_renderer() {
$id = 'ppcp-hosted-fields';
$can_render_dcc = $this->dcc_applies->for_country_currency()
&& $this->settings->has( 'client_id' )
&& $this->settings->get( 'client_id' );
if ( ! $can_render_dcc ) {
return;
}
$save_card = $this->can_save_vault_token() ? sprintf(
'<div>
<label for="ppcp-vault-%1$s">%2$s</label>
<input
type="checkbox"
id="ppcp-vault-%1$s"
class="ppcp-credit-card-vault"
name="vault"
>
</div>',
esc_attr( $id ),
esc_html__( 'Save your card', 'paypal-for-woocommerce' )
) : '';
printf(
'<form id="%1$s">
<div class="ppcp-dcc-credit-card-wrapper" style="display: none">
<label for="ppcp-credit-card-%1$s">%2$s</label>
<span id="ppcp-credit-card-%1$s" class="ppcp-credit-card"></span>
<label for="ppcp-expiration-date-%1$s">%3$s</label>
<span
id="ppcp-expiration-date-%1$s"
class="ppcp-expiration-date"
></span>
<label for="ppcp-cvv-%1$s">%4$s</label>
<span id="ppcp-cvv-%1$s" class="ppcp-cvv"></span>
%5$s
<button class="button alt">%6$s</button>
</div>
</form><div id="payments-sdk__contingency-lightbox"></div>',
esc_attr( $id ),
esc_html__( 'Credit Card number', 'paypal-for-woocommerce' ),
esc_html__( 'Expiration', 'paypal-for-woocommerce' ),
esc_html__( 'CVV', 'paypal-for-woocommerce' ),
//phpcs:ignore
$save_card,
esc_html__( 'Place order', 'paypal-for-woocommerce' )
);
}
/**
* Whether we can store vault tokens or not.
*
* @return bool
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting hasnt been found.
*/
public function can_save_vault_token(): bool {
if ( ! $this->settings->has( 'client_id' ) || ! $this->settings->get( 'client_id' ) ) {
return false;
}
if ( ! $this->settings->has( 'vault_enabled' ) || ! $this->settings->get( 'vault_enabled' ) ) {
return false;
}
return is_user_logged_in();
}
/**
* Whether we need to initialize the script to enable tokenization for subscriptions or not.
*
* @return bool
*/
private function has_subscriptions(): bool {
if ( ! $this->subscription_helper->accept_only_automatic_payment_gateways() ) {
return false;
}
if ( is_product() ) {
return $this->subscription_helper->current_product_is_subscription();
}
return $this->subscription_helper->cart_contains_subscription();
}
/**
* The localized data for the smart button.
*
* @return array
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting hasn't been found.
*/
private function localize_script(): array {
$this->request_data->enqueue_nonce_fix();
$localize = array(
'script_attributes' => $this->attributes(),
'data_client_id' => array(
'set_attribute' => ( is_checkout() && $this->dcc_is_enabled() )
|| $this->can_save_vault_token(),
'endpoint' => home_url( \WC_AJAX::get_endpoint( DataClientIdEndpoint::ENDPOINT ) ),
'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ),
'user' => get_current_user_id(),
),
'redirect' => wc_get_checkout_url(),
'context' => $this->context(),
'ajax' => array(
'change_cart' => array(
'endpoint' => home_url( \WC_AJAX::get_endpoint( ChangeCartEndpoint::ENDPOINT ) ),
'nonce' => wp_create_nonce( ChangeCartEndpoint::nonce() ),
),
'create_order' => array(
'endpoint' => home_url( \WC_AJAX::get_endpoint( CreateOrderEndpoint::ENDPOINT ) ),
'nonce' => wp_create_nonce( CreateOrderEndpoint::nonce() ),
),
'approve_order' => array(
'endpoint' => home_url( \WC_AJAX::get_endpoint( ApproveOrderEndpoint::ENDPOINT ) ),
'nonce' => wp_create_nonce( ApproveOrderEndpoint::nonce() ),
),
),
'enforce_vault' => $this->has_subscriptions(),
'bn_codes' => $this->bn_codes(),
'payer' => $this->payerData(),
'button' => array(
'wrapper' => '#ppc-button',
'mini_cart_wrapper' => '#ppc-button-minicart',
'cancel_wrapper' => '#ppcp-cancel',
'url' => $this->url(),
'mini_cart_style' => array(
'layout' => $this->style_for_context( 'layout', 'mini-cart' ),
'color' => $this->style_for_context( 'color', 'mini-cart' ),
'shape' => $this->style_for_context( 'shape', 'mini-cart' ),
'label' => $this->style_for_context( 'label', 'mini-cart' ),
'tagline' => $this->style_for_context( 'tagline', 'mini-cart' ),
),
'style' => array(
'layout' => $this->style_for_context( 'layout', $this->context() ),
'color' => $this->style_for_context( 'color', $this->context() ),
'shape' => $this->style_for_context( 'shape', $this->context() ),
'label' => $this->style_for_context( 'label', $this->context() ),
'tagline' => $this->style_for_context( 'tagline', $this->context() ),
),
),
'hosted_fields' => array(
'wrapper' => '#ppcp-hosted-fields',
'mini_cart_wrapper' => '#ppcp-hosted-fields-mini-cart',
'labels' => array(
'credit_card_number' => '',
'cvv' => '',
'mm_yyyy' => __( 'MM/YYYY', 'paypal-for-woocommerce' ),
'fields_not_valid' => __(
'Unfortunatly, your credit card details are not valid.',
'paypal-for-woocommerce'
),
),
),
'messages' => $this->message_values(),
'labels' => array(
'error' => array(
'generic' => __(
'Something went wrong. Please try again or choose another payment source.',
'paypal-for-woocommerce'
),
),
),
);
if ( $this->style_for_context( 'layout', 'mini-cart' ) !== 'horizontal' ) {
unset( $localize['button']['mini_cart_style']['tagline'] );
}
if ( $this->style_for_context( 'layout', $this->context() ) !== 'horizontal' ) {
unset( $localize['button']['style']['tagline'] );
}
$this->request_data->dequeue_nonce_fix();
return $localize;
}
/**
* If we can find the payer data for a current customer, we will return it.
*
* @return array|null
*/
private function payerData(): ?array {
$customer = WC()->customer;
if ( ! is_user_logged_in() || ! is_a( $customer, \WC_Customer::class ) ) {
return null;
}
return $this->payer_factory->from_customer( $customer )->to_array();
}
/**
* The JavaScript SDK url to load.
*
* @return string
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found.
*/
private function url(): string {
$params = array(
'client-id' => $this->client_id,
'currency' => get_woocommerce_currency(),
'locale' => get_user_locale(),
// ToDo: Update date on releases.
'integration-date' => gmdate( 'Y-m-d' ),
'components' => implode( ',', $this->components() ),
'vault' => ( is_checkout() && $this->dcc_is_enabled() ) || $this->can_save_vault_token() ?
'true' : 'false',
'commit' => is_checkout() ? 'true' : 'false',
'intent' => ( $this->settings->has( 'intent' ) ) ?
$this->settings->get( 'intent' ) : 'capture',
);
if (
defined( 'WP_DEBUG' ) && \WP_DEBUG
&& WC()->customer && WC()->customer->get_billing_country()
) {
$params['buyer-country'] = WC()->customer->get_billing_country();
}
$payee = $this->payee_repository->payee();
if ( $payee->merchant_id() ) {
$params['merchant-id'] = $payee->merchant_id();
}
$disable_funding = $this->settings->has( 'disable_funding' ) ?
$this->settings->get( 'disable_funding' ) : array();
$disable_funding[] = 'venmo';
if ( ! is_checkout() ) {
$disable_funding[] = 'card';
}
/**
* Disable card for UK.
*/
$region = wc_get_base_location();
$country = $region['country'];
if ( 'GB' === $country ) {
$disable_funding[] = 'card';
}
$params['disable-funding'] = implode( ',', array_unique( $disable_funding ) );
$smart_button_url = add_query_arg( $params, 'https://www.paypal.com/sdk/js' );
return $smart_button_url;
}
/**
* The attributes we need to load for the JS SDK.
*
* @return array
*/
private function attributes(): array {
return array(
'data-partner-attribution-id' => $this->bn_code_for_context( $this->context() ),
);
}
/**
* What BN Code to use in a given context.
*
* @param string $context The context.
* @return string
*/
private function bn_code_for_context( string $context ): string {
$codes = $this->bn_codes();
return ( isset( $codes[ $context ] ) ) ? $codes[ $context ] : '';
}
/**
* BN Codes to use.
*
* @return array
*/
private function bn_codes(): array {
return array(
'checkout' => 'Woo_PPCP',
'cart' => 'Woo_PPCP',
'mini-cart' => 'Woo_PPCP',
'product' => 'Woo_PPCP',
);
}
/**
* The JS SKD components we need to load.
*
* @return array
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found.
*/
private function components(): array {
$components = array();
if ( $this->load_button_component() ) {
$components[] = 'buttons';
}
if ( $this->messages_apply->for_country() ) {
$components[] = 'messages';
}
if ( $this->dcc_is_enabled() ) {
$components[] = 'hosted-fields';
}
return $components;
}
/**
* Determines whether the button component should be loaded.
*
* @return bool
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting has not been found.
*/
private function load_button_component() : bool {
$load_buttons = false;
if (
$this->context() === 'checkout'
&& $this->settings->has( 'button_enabled' )
&& $this->settings->get( 'button_enabled' )
) {
$load_buttons = true;
}
if (
$this->context() === 'product'
&& $this->settings->has( 'button_product_enabled' )
&& $this->settings->get( 'button_product_enabled' )
) {
$load_buttons = true;
}
if (
$this->settings->has( 'button_mini-cart_enabled' )
&& $this->settings->get( 'button_mini-cart_enabled' )
) {
$load_buttons = true;
}
if (
$this->context() === 'cart'
&& $this->settings->has( 'button_cart_enabled' )
&& $this->settings->get( 'button_cart_enabled' )
) {
$load_buttons = true;
}
return $load_buttons;
}
/**
* The current context.
*
* @return string
*/
private function context(): string {
$context = 'mini-cart';
if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) {
$context = 'product';
}
if ( is_cart() ) {
$context = 'cart';
}
if ( is_checkout() && ! $this->session_handler->order() ) {
$context = 'checkout';
}
return $context;
}
/**
* Whether DCC is enabled or not.
*
* @return bool
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting has not been found.
*/
private function dcc_is_enabled(): bool {
if ( ! $this->dcc_applies->for_country_currency() ) {
return false;
}
$keys = array(
'dcc_enabled' => 'is_checkout',
);
foreach ( $keys as $key => $callback ) {
if ( $this->settings->has( $key ) && $this->settings->get( $key ) && $callback() ) {
return true;
}
}
return false;
}
/**
* Determines the style for a given indicator in a given context.
*
* @param string $style The style.
* @param string $context The context.
*
* @return string
* @throws \Inpsyde\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting hasn't been found.
*/
private function style_for_context( string $style, string $context ): string {
$defaults = array(
'layout' => 'vertical',
'size' => 'responsive',
'color' => 'gold',
'shape' => 'pill',
'label' => 'paypal',
'tagline' => true,
);
$value = isset( $defaults[ $style ] ) ?
$defaults[ $style ] : '';
$value = $this->settings->has( 'button_' . $style ) ?
$this->settings->get( 'button_' . $style ) : $value;
$value = $this->settings->has( 'button_' . $context . '_' . $style ) ?
$this->settings->get( 'button_' . $context . '_' . $style ) : $value;
if ( is_bool( $value ) ) {
$value = $value ? 'true' : 'false';
}
return (string) $value;
}
}