PluginProbe
Tamara Checkout / trunk
Tamara Checkout vtrunk
1.9.9.23 1.9.9.22 1.9.9.20 trunk 1.0.13 1.7.4 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 1.9.9.1 1.9.9.11 1.9.9.12 1.9.9.13 1.9.9.14 1.9.9.15 1.9.9.16 1.9.9.17 1.9.9.2 1.9.9.3 1.9.9.4 1.9.9.5 All 29 releases
tamara-checkout / src / Services / WCTamaraGatewayCheckout.php

WCTamaraGatewayCheckout.php in Tamara Checkout trunk, at src/Services/WCTamaraGatewayCheckout.php

143 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Tamara\Wp\Plugin\Services;
4
5 use Tamara\Wp\Plugin\Dependencies\Tamara\Model\Money;
6 use Tamara\Wp\Plugin\Dependencies\Tamara\Model\Checkout\PaymentOptionsAvailability;
7 use Tamara\Wp\Plugin\Dependencies\Tamara\Request\Checkout\CheckPaymentOptionsAvailabilityRequest;
8 use Tamara\Wp\Plugin\Dependencies\Tamara\Response\Checkout\CheckPaymentOptionsAvailabilityResponse;
9 use Tamara\Wp\Plugin\Helpers\PhoneHelper;
10 use Tamara\Wp\Plugin\TamaraCheckout;
11 use Tamara\Wp\Plugin\Traits\ConfigTrait;
12 use Tamara\Wp\Plugin\Traits\ServiceTrait;
13 use Tamara\Wp\Plugin\Traits\WPAttributeTrait;
14
15 class WCTamaraGatewayCheckout extends WCTamaraGateway
16 {
17 use ConfigTrait;
18 use ServiceTrait;
19 use WPAttributeTrait;
20
21 /**
22 * Initialize attributes that are fixed
23 */
24 protected function initBaseAttributes()
25 {
26 parent::initBaseAttributes();
27 $this->id = TamaraCheckout::TAMARA_GATEWAY_CHECKOUT_ID;
28 }
29
30 protected function initGatewayTranslatedAttributes()
31 {
32 // translators: %d: Number of instalments
33 $this->title = sprintf(__('Tamara: Split in %d, interest-free', 'tamara-checkout'), 4);
34 }
35
36 /** @noinspection PhpFullyQualifiedNameUsageInspection */
37 /**
38 * Render description for Tamara Single Checkout
39 *
40 * @param $description
41 * @param $gatewayId
42 *
43 * @return string
44 *
45 * @throws \Illuminate\Contracts\Container\BindingResolutionException
46 */
47 public function renderPaymentTypeDescription($description, $gatewayId)
48 {
49 if ($this->id === $gatewayId) {
50 $cartTotal = TamaraCheckout::getInstance()->getCartTotal();
51 $description .= TamaraCheckout::getInstance()->getServiceView()->render('views/woocommerce/checkout/tamara-gateway-checkout-description',
52 [
53 'cartTotal' => $cartTotal,
54 'defaultDescription' => $this->populateTamaraDefaultDescription(),
55 'inlineType' => TamaraCheckout::TAMARA_INLINE_TYPE_SINGLE_CHECKOUT_WIDGET,
56 ]);
57 }
58
59 return $description;
60 }
61
62 /**
63 * Hide Tamara Payment Gateway on checkout if total value of order is under/over limit
64 * and the shipping country is different than countries set in Tamara payment settings
65 *
66 * @param WC_Payment_Gateway $availableGateways
67 *
68 * @return WC_Payment_Gateway $availableGateways
69 *
70 * @throws Exception
71 */
72 public function adjustTamaraGatewayOnCheckout($availableGateways)
73 {
74 if (is_checkout()) {
75 $cartTotal = TamaraCheckout::getInstance()->getCartTotal();
76 $currentCountryCode = $this->getCurrencyToCountryMapping()[get_woocommerce_currency()];
77 $tamaraExcludedProductItems = TamaraCheckout::getInstance()->getExcludedProductIds() ?? null;
78 $tamaraExcludedProductCategories = TamaraCheckout::getInstance()->getExcludedProductCategoryIds() ?? null;
79 $cartItemIds = TamaraCheckout::getInstance()->getAllProductIdsInCart();
80 $cartItemCategoryIds = TamaraCheckout::getInstance()->getAllProductCategoryIdsInCart();
81 $tamaraExcludedProductItemsInCart = (count(array_intersect(
82 $cartItemIds, $tamaraExcludedProductItems))) ? true : false;
83 $tamaraExcludedProductCategoriesInCart = (count(array_intersect(
84 $cartItemCategoryIds, $tamaraExcludedProductCategories))) ? true : false;
85 $customerPhone = TamaraCheckout::getInstance()->getCustomerPhoneNumber() ?? (WC()->customer ? WC()->customer->get_billing_phone() : '');
86
87 // Hide Tamara when billing country is not KSA (SA) or UAE (AE), including initial checkout form data.
88 $billingCountry = TamaraCheckout::getInstance()->getCustomerBillingCountry();
89 if (empty($billingCountry)) {
90 $billingCountry = $this->getDefaultBillingCountryCode();
91 }
92 if (!PhoneHelper::isQualifiedCountry($billingCountry)) {
93 unset($availableGateways[$this->id]);
94
95 return $availableGateways;
96 }
97
98 if (!TamaraCheckout::getInstance()->hasAvailablePaymentOptions($cartTotal, $customerPhone, $currentCountryCode)
99 || $tamaraExcludedProductItemsInCart || $tamaraExcludedProductCategoriesInCart) {
100 unset($availableGateways[$this->id]);
101 } else {
102 $getAvailableMethod = $this->isMethodAvailableFromRemote($cartTotal, $customerPhone, $currentCountryCode);
103 $countPaymentOption = $this->isMethodAvailableFromRemote($cartTotal, $customerPhone, $currentCountryCode);
104 $siteLocale = substr(get_locale(), 0, 2) ?? 'en';
105 if ('ar' === $siteLocale) {
106 $this->title = $getAvailableMethod['descriptionAr'];
107 } else {
108 $this->title = $getAvailableMethod['descriptionEn'];
109 }
110 }
111 }
112
113 return $availableGateways;
114 }
115
116 /**
117 * Check if a payment method is available from remote and get full description
118 *
119 * @param $cartTotal
120 * @param $customerPhone
121 * @param $countryCode
122 *
123 * @return array
124 */
125 protected function isMethodAvailableFromRemote($cartTotal, $customerPhone, $countryCode)
126 {
127 $paymentOptions = TamaraCheckout::getInstance()->getPaymentOptions($cartTotal, $customerPhone, $countryCode) ?? [];
128 $descriptionEn = '';
129 $descriptionAr = '';
130 if (!empty($paymentOptions)) {
131 foreach ($paymentOptions as $payment_option) {
132 $descriptionEn = $payment_option['description_en'];
133 $descriptionAr = $payment_option['description_ar'];
134 break;
135 }
136 }
137 return [
138 'descriptionEn' => $descriptionEn,
139 'descriptionAr' => $descriptionAr
140 ];
141 }
142 }
143