| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hyperpay\Gateways\Brands; |
| 4 |
|
| 5 |
use Hyperpay\Gateways\App\DefaultGateway; |
| 6 |
use Hyperpay\Gateways\Main; |
| 7 |
use WC_Order; |
| 8 |
|
| 9 |
|
| 10 |
class Tamara extends DefaultGateway |
| 11 |
{ |
| 12 |
|
| 13 |
|
| 14 |
public $trans_mode = 'EXTERNAL'; |
| 15 |
|
| 16 |
public $server_to_server = true; |
| 17 |
|
| 18 |
public $isDynamicCheck = true; |
| 19 |
public $description, $configurations; |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* should be lower case and unique |
| 24 |
* @var string $id |
| 25 |
*/ |
| 26 |
public $id = "hyperpay_tamara"; |
| 27 |
|
| 28 |
/** |
| 29 |
* The title which appear next to gateway on setting page |
| 30 |
* @var string $method_title |
| 31 |
*/ |
| 32 |
public $method_title = "Tamara"; |
| 33 |
|
| 34 |
/** |
| 35 |
* Description of gateways which will appear next to title |
| 36 |
* @var string $method_description |
| 37 |
*/ |
| 38 |
public $method_description = "Tamara Plugin for Woocommerce"; |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* |
| 43 |
* the Brands supported by the gateway |
| 44 |
* @var array $supported_brands |
| 45 |
*/ |
| 46 |
protected $supported_brands = [ |
| 47 |
"TAMARA" => "Tamara", |
| 48 |
]; |
| 49 |
|
| 50 |
|
| 51 |
public function boot() |
| 52 |
{ |
| 53 |
|
| 54 |
add_filter('woocommerce_available_payment_gateways', [$this, 'conditional_payment_gateways'], 10, 1); |
| 55 |
} |
| 56 |
|
| 57 |
public function configuration($country, $amount) |
| 58 |
{ |
| 59 |
|
| 60 |
$url = $this->testMode ? 'https://tamara-dev.hyperpay.com' : 'https://tamara-prod.hyperpay.com'; |
| 61 |
|
| 62 |
$data = [ |
| 63 |
"country" => $country, |
| 64 |
"order_value" => [ |
| 65 |
"amount" => $amount, |
| 66 |
"currency" => $this->currency |
| 67 |
] |
| 68 |
]; |
| 69 |
|
| 70 |
$res = wp_remote_post("$url/api/tamara-description/{$this->entityId}", ['body' => $data]); |
| 71 |
$configurations = json_decode(wp_remote_retrieve_body($res), true); |
| 72 |
|
| 73 |
$locale = substr(get_locale(), 0, 2); |
| 74 |
if (isset($configurations['available_payment_labels'][0]["description_$locale"])) { |
| 75 |
$this->description = $configurations['available_payment_labels'][0]["description_$locale"]; |
| 76 |
} |
| 77 |
|
| 78 |
return $configurations; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function canMakePayment() |
| 84 |
{ |
| 85 |
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'hyperpay_tamara_nonce')) { |
| 86 |
return wp_send_json([ |
| 87 |
"canMakePayment" => false, |
| 88 |
]); |
| 89 |
} |
| 90 |
|
| 91 |
if ( |
| 92 |
!isset($_POST['billingData']['country']) || |
| 93 |
!isset($_POST['cartTotals']['total_price']) || |
| 94 |
!isset($_POST['cartTotals']['currency_minor_unit']) |
| 95 |
) { |
| 96 |
return wp_send_json([ |
| 97 |
"canMakePayment" => false, |
| 98 |
]); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
$country = sanitize_text_field(wp_unslash($_POST['billingData']['country'])); |
| 103 |
$amount = sanitize_text_field((wp_unslash($_POST['cartTotals']['total_price'])) / 100 ** (wp_unslash($_POST['cartTotals']['currency_minor_unit']))); |
| 104 |
$this->configurations = $this->configuration($country, $amount); |
| 105 |
|
| 106 |
if (!isset($this->configurations['available_payment_labels']) || !count($this->configurations['available_payment_labels'])) { |
| 107 |
return wp_send_json([ |
| 108 |
"canMakePayment" => false, |
| 109 |
"details" => $this->configurations["message"] ?? $this->configurations |
| 110 |
]); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
return wp_send_json([ |
| 115 |
"canMakePayment" => true, |
| 116 |
"description" => $this->description, |
| 117 |
]);; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* check if the order have the minimum requirements |
| 123 |
* to display Tamara |
| 124 |
* |
| 125 |
* @param array $available_gateways |
| 126 |
* |
| 127 |
* @return array $available_gateways |
| 128 |
*/ |
| 129 |
|
| 130 |
public function conditional_payment_gateways($available_gateways) |
| 131 |
{ |
| 132 |
if (!is_admin() && is_checkout()) { |
| 133 |
$country = WC()->customer->get_billing_country(); |
| 134 |
$amount = WC()->cart->total; |
| 135 |
$this->configurations = $this->configuration($country, $amount); |
| 136 |
|
| 137 |
if (!(isset($this->configurations['available_payment_labels']) && count($this->configurations['available_payment_labels']))) { |
| 138 |
unset($available_gateways['hyperpay_tamara']); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $available_gateways; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* to set extra parameter on requested data to connector |
| 147 |
* just uncomment the function below |
| 148 |
* |
| 149 |
* @param object $order |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
|
| 153 |
public function setExtraData(WC_Order $order): array |
| 154 |
{ |
| 155 |
global $woocommerce; |
| 156 |
$data = [ |
| 157 |
"customer.mobile" => $order->get_billing_phone() |
| 158 |
]; |
| 159 |
|
| 160 |
$cart_index = 0; |
| 161 |
foreach ($woocommerce->cart->get_cart() as $cart_item) { |
| 162 |
|
| 163 |
$data["cart.items[$cart_index].merchantItemId"] = "By_Hyperpay" . $cart_item['data']->get_id(); |
| 164 |
$data["cart.items[$cart_index].type"] = $cart_item['data']->get_type() ?? 'N/A'; |
| 165 |
$data["cart.items[$cart_index].name"] = $cart_item['data']->get_title(); |
| 166 |
$data["cart.items[$cart_index].totalAmount"] = number_format((float)$cart_item['data']->get_price(), 2, '.', ''); |
| 167 |
$data["cart.items[$cart_index].quantity"] = $cart_item['quantity']; |
| 168 |
$data["cart.items[$cart_index].sku"] = wp_rand(111111, 999999); |
| 169 |
$cart_index++; |
| 170 |
} |
| 171 |
$amount = number_format($order->get_total(), 2, '.', ''); |
| 172 |
$country = $order->get_billing_country(); |
| 173 |
|
| 174 |
$configurations = $this->configuration($country, $amount); |
| 175 |
$data["customParameters[discount]"] = 00.00; |
| 176 |
$data["customParameters[tamara_payment_type]"] = $configurations['available_payment_labels'][0]['payment_type']; |
| 177 |
$data["customParameters[instalments]"] = $configurations['available_payment_labels'][0]['instalment']; |
| 178 |
$data["taxAmount"] = 00.00; |
| 179 |
|
| 180 |
return ['body' => $data]; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
public function iconSrc() |
| 185 |
{ |
| 186 |
$locale = substr(get_locale(), 0, 2); |
| 187 |
|
| 188 |
$img = HYPERPAY_PLUGIN_DIR . '/src/assets/images/default.png'; |
| 189 |
|
| 190 |
if (file_exists(Main::ROOT_PATH . "/assets/images/TAMARA-logo_$locale.svg")) { |
| 191 |
$img = HYPERPAY_PLUGIN_DIR . "/src/assets/images/TAMARA-logo_$locale.svg"; |
| 192 |
} |
| 193 |
|
| 194 |
return [$img]; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Set Tamara terms and conditions |
| 200 |
* based on Tamara API configuration |
| 201 |
* @param string $icon |
| 202 |
* @param string $id |
| 203 |
* |
| 204 |
* @return string $icon |
| 205 |
*/ |
| 206 |
|
| 207 |
public function set_icons($icon, $id) |
| 208 |
{ |
| 209 |
|
| 210 |
if ($id == $this->id) { |
| 211 |
|
| 212 |
$img = $this->iconSrc(); |
| 213 |
$icons = "<img width='48.33' style='border:unset !important' class='hyperpay_gateways_logo' src='$img[0]'>"; |
| 214 |
|
| 215 |
return $icons . |
| 216 |
" |
| 217 |
</label> |
| 218 |
<div class='payment_box terms_zoodpay payment_method_hyperpay_zoodpay'> |
| 219 |
<p>{$this->description}</p> |
| 220 |
</div>"; |
| 221 |
} |
| 222 |
return $icon; |
| 223 |
} |
| 224 |
} |
| 225 |
|