| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hyperpay\Gateways\Brands; |
| 4 |
|
| 5 |
use Hyperpay\Gateways\App\DefaultGateway; |
| 6 |
use WC_Order; |
| 7 |
|
| 8 |
|
| 9 |
class ZoodPay extends DefaultGateway |
| 10 |
{ |
| 11 |
protected $service_code; |
| 12 |
|
| 13 |
public $trans_mode = 'EXTERNAL'; |
| 14 |
|
| 15 |
/** |
| 16 |
* should be lower case and unique |
| 17 |
* @var string $id |
| 18 |
*/ |
| 19 |
public $id = "hyperpay_zoodpay"; |
| 20 |
|
| 21 |
/** |
| 22 |
* The title which appear next to gateway on setting page |
| 23 |
* @var string $method_title |
| 24 |
*/ |
| 25 |
public $method_title = "Zoodpay"; |
| 26 |
|
| 27 |
/** |
| 28 |
* Description of gateways which will appear next to title |
| 29 |
* @var string $method_description |
| 30 |
*/ |
| 31 |
public $method_description = "Zoodpay Plugin for Woocommerce"; |
| 32 |
|
| 33 |
/** |
| 34 |
* |
| 35 |
* the Brands supported by the gateway |
| 36 |
* @var array $supported_brands |
| 37 |
*/ |
| 38 |
protected $supported_brands = [ |
| 39 |
"ZOODPAY" => "Zoodpay", |
| 40 |
]; |
| 41 |
|
| 42 |
public $min_limit, $max_limit, $instalment_count, $terms; |
| 43 |
|
| 44 |
public function boot() |
| 45 |
{ |
| 46 |
$service_code['service_code'] = [ |
| 47 |
'title' => __('service code', 'hyperpay-gateways'), |
| 48 |
'type' => 'text', |
| 49 |
'default' => 'ZPI' |
| 50 |
]; |
| 51 |
|
| 52 |
/** |
| 53 |
* unset unwanted fields |
| 54 |
*/ |
| 55 |
unset($this->form_fields['custom_style']); |
| 56 |
unset($this->form_fields['payment_style']); |
| 57 |
unset($this->form_fields['trans_mode']); |
| 58 |
|
| 59 |
/** |
| 60 |
* add service_code field to position number 6 |
| 61 |
*/ |
| 62 |
$this->form_fields = array_merge(array_slice($this->form_fields, 0, 6), $service_code, array_slice($this->form_fields, 6)); |
| 63 |
|
| 64 |
$this->trans_mode = 'EXTERNAL'; // in test mode should be EXTERNAL to redirect to connector |
| 65 |
$this->service_code = $this->get_option('service_code'); |
| 66 |
|
| 67 |
// call zoodpay API to set payment configurations |
| 68 |
$this->configuration(); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
add_filter('woocommerce_available_payment_gateways', [$this, 'conditional_payment_gateways'], 10, 1); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Call Zoodpay API to set terms, instalment count and total limits |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function configuration() |
| 81 |
{ |
| 82 |
|
| 83 |
$url = $this->testMode ? 'https://zoodpay-sandbox.hyperpay.com/api/getTerms' : 'https://zoodpay.hyperpay.com/api/getTerms'; |
| 84 |
|
| 85 |
$res = wp_remote_post($url, ['body' => ['entity_id' => $this->entityId]]); |
| 86 |
$res = json_decode(wp_remote_retrieve_body($res), true); |
| 87 |
|
| 88 |
|
| 89 |
$this->terms = $res['description'] ?? ''; |
| 90 |
|
| 91 |
$this->instalment_count = $res['instalments'] ?? 1; |
| 92 |
|
| 93 |
$this->min_limit = $res['min_limit'] ?? 1; |
| 94 |
$this->max_limit = $res['max_limit'] ?? 200; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Check if total order amount is grater than min limit |
| 99 |
* and less than max limit |
| 100 |
* @param array $available_gateways |
| 101 |
* |
| 102 |
* @return array $available_gateways |
| 103 |
*/ |
| 104 |
|
| 105 |
public function conditional_payment_gateways($available_gateways) |
| 106 |
{ |
| 107 |
|
| 108 |
if(is_checkout()){ |
| 109 |
|
| 110 |
$total = WC()->cart->total; |
| 111 |
if ($total < $this->min_limit || $total > $this->max_limit) |
| 112 |
unset($available_gateways['hyperpay_zoodpay']); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
return $available_gateways; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* to set extra parameter on requested data to connector |
| 121 |
* just uncomment the function below |
| 122 |
* |
| 123 |
* @param object $order |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
|
| 127 |
public function setExtraData(WC_Order $order): array |
| 128 |
{ |
| 129 |
global $woocommerce; |
| 130 |
|
| 131 |
$data = [ |
| 132 |
"customer.mobile" => $this->getPhone($order), |
| 133 |
"customParameters[service_code]" => $this->service_code |
| 134 |
]; |
| 135 |
|
| 136 |
$cart_index = 0; |
| 137 |
foreach ($woocommerce->cart->get_cart() as $cart_item) { |
| 138 |
$data["cart.items[$cart_index].name"] = $cart_item['data']->get_title(); |
| 139 |
$data["cart.items[$cart_index].price"] = number_format((float)$cart_item['data']->get_price(), 2, '.', ''); |
| 140 |
$data["cart.items[$cart_index].quantity"] = $cart_item['quantity']; |
| 141 |
$categories[][][] = wp_strip_all_tags($cart_item['data']->get_categories()); |
| 142 |
$cart_index++; |
| 143 |
} |
| 144 |
|
| 145 |
$data["customParameters[categories]"] = json_encode($categories); |
| 146 |
|
| 147 |
return ['body' => $data]; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* reformat phone number |
| 152 |
* @param WC_Order |
| 153 |
* @return string $phone |
| 154 |
*/ |
| 155 |
private function getPhone(WC_Order $order): string |
| 156 |
{ |
| 157 |
$phone = $order->get_billing_phone(); |
| 158 |
|
| 159 |
if (substr($phone, 0, 2) == '07') |
| 160 |
$phone = substr_replace($phone, '962', 0, 1); |
| 161 |
|
| 162 |
return $phone; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Set Zoodpay tems and conditions |
| 167 |
* based on Zoodpay API configuration |
| 168 |
* @param string $icon |
| 169 |
* @param string $id |
| 170 |
* |
| 171 |
* @return string $icon |
| 172 |
*/ |
| 173 |
|
| 174 |
public function set_icons($icon, $id): string |
| 175 |
{ |
| 176 |
if ($id == $this->id) { |
| 177 |
|
| 178 |
global $woocommerce; |
| 179 |
|
| 180 |
echo "<div id='hyperpayModal' class='modal'> |
| 181 |
<div class='modal-content'> |
| 182 |
<span class='close'>×</span> |
| 183 |
<p></p> |
| 184 |
</div> |
| 185 |
</div>"; |
| 186 |
|
| 187 |
wp_enqueue_script('hyperpay_script_modal', __DIR__ . '../src/assets/js/modal.js', ['jquery'], '1.0.0', true); |
| 188 |
|
| 189 |
|
| 190 |
$img = __DIR__ . "../src/assets/images/ZOODPAY-logo.png"; |
| 191 |
$icons = "<img class='hyperpay_gateways_logo' src='$img'>"; |
| 192 |
|
| 193 |
$amount = $woocommerce->cart->total; |
| 194 |
$instalment = number_format(round($amount / $this->instalment_count, 2), 2, '.', ''); |
| 195 |
|
| 196 |
return $icons . |
| 197 |
" |
| 198 |
</label> |
| 199 |
<div class='payment_box terms_zoodpay payment_method_hyperpay_zoodpay'> |
| 200 |
<p>{$this->instalment_count} " . __('Instalment of', 'hyperpay-gateways') . " <b>$instalment</b>/" . __('mo.', 'hyperpay-gateways') . "</p> <a class='woocommerce-privacy-policy-link ' onClick='openModal(`{$this->terms}`)'>" . __('Terms and Conditions', 'hyperpay-gateways') . "</a> |
| 201 |
</div>"; |
| 202 |
} |
| 203 |
return $icon; |
| 204 |
} |
| 205 |
} |
| 206 |
|