| 1 |
<?php |
| 2 |
|
| 3 |
use Imoje\Payment\Api; |
| 4 |
use Imoje\Payment\CartData; |
| 5 |
use Imoje\Payment\Invoice; |
| 6 |
use Imoje\Payment\Notification; |
| 7 |
use Imoje\Payment\Util; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Helper |
| 11 |
*/ |
| 12 |
class Helper |
| 13 |
{ |
| 14 |
|
| 15 |
/** |
| 16 |
* @return string |
| 17 |
*/ |
| 18 |
public static function get_version() |
| 19 |
{ |
| 20 |
$wc = WC(); |
| 21 |
|
| 22 |
return get_file_data(dirname(dirname(__FILE__)) . '/woocommerce-imoje.php', ['Version'], 'plugin')[0] . ';woocommerce_' . $wc->version; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @param WC_Order $order |
| 27 |
* @param bool $isEnable |
| 28 |
* @param bool $isApi |
| 29 |
* |
| 30 |
* @return array|string |
| 31 |
*/ |
| 32 |
public static function get_invoice($order, $isEnable, $isApi) |
| 33 |
{ |
| 34 |
|
| 35 |
if(!$isEnable) { |
| 36 |
return []; |
| 37 |
} |
| 38 |
|
| 39 |
$cart = self::get_cart($order); |
| 40 |
|
| 41 |
$invoice = new Invoice(); |
| 42 |
|
| 43 |
if(empty($cart['items'])) { |
| 44 |
return []; |
| 45 |
} |
| 46 |
|
| 47 |
foreach($cart['items'] as $item) { |
| 48 |
|
| 49 |
$tax = null; |
| 50 |
try { |
| 51 |
$tax = constant('\Imoje\Payment\Invoice::TAX_' . $item['vat']); |
| 52 |
} catch(Exception $e) { |
| 53 |
} |
| 54 |
|
| 55 |
if(!$tax) { |
| 56 |
return []; |
| 57 |
} |
| 58 |
|
| 59 |
$invoice->addItem($item['name'], |
| 60 |
$item['id'], |
| 61 |
$item['quantity'], |
| 62 |
constant('\Imoje\Payment\Invoice::TAX_' . $item['vat']), |
| 63 |
$item['amount'] |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
if(isset($cart['shipping']) && $cart['shipping']) { |
| 68 |
$invoice->addItem($cart['shipping']['name'], |
| 69 |
1, |
| 70 |
1, |
| 71 |
constant('\Imoje\Payment\Invoice::TAX_' . $cart['shipping']['vat']), |
| 72 |
$cart['shipping']['amount'] |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
$invoice->setBuyer(Invoice::BUYER_PERSON, |
| 77 |
$order->get_billing_email(), |
| 78 |
$cart['address']['billing']['name'], |
| 79 |
$cart['address']['billing']['street'], |
| 80 |
$cart['address']['billing']['city'], |
| 81 |
$cart['address']['billing']['postalCode'], |
| 82 |
$cart['address']['billing']['country']); |
| 83 |
|
| 84 |
return $invoice->prepare($isApi); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @param WC_Order $order |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public static function get_cart($order) |
| 93 |
{ |
| 94 |
|
| 95 |
$cart_data = new CartData(); |
| 96 |
|
| 97 |
$cart_data->setCreatedAt(strtotime($order->get_date_created())); |
| 98 |
$cart_data->setAmount(Util::convertAmountToFractional($order->get_total())); |
| 99 |
|
| 100 |
foreach($order->get_items() as $item) { |
| 101 |
|
| 102 |
$product = $item->get_product(); |
| 103 |
|
| 104 |
$tax_rate_product = current(WC_Tax::get_rates($product->get_tax_class())); |
| 105 |
|
| 106 |
$rate = 0; |
| 107 |
if($tax_rate_product && isset($tax_rate_product['rate'])) { |
| 108 |
$rate = (float) $tax_rate_product['rate']; |
| 109 |
} |
| 110 |
// endregion |
| 111 |
|
| 112 |
$cart_data->addItem( |
| 113 |
$product->get_id(), |
| 114 |
$rate, |
| 115 |
$item->get_name(), |
| 116 |
Util::convertAmountToFractional((float) $item->get_total() + (float) $item->get_total_tax()), |
| 117 |
$item->get_quantity() |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
$phone = $order->get_billing_phone(); |
| 122 |
|
| 123 |
$cart_data->setAddressBilling( |
| 124 |
$order->get_billing_city(), |
| 125 |
$order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), |
| 126 |
$phone |
| 127 |
?: '', |
| 128 |
$order->get_billing_address_1() . ' ' . $order->get_billing_address_2(), |
| 129 |
$order->get_billing_country(), |
| 130 |
$order->get_billing_postcode() |
| 131 |
); |
| 132 |
|
| 133 |
if($order->get_total_discount(false) > 0) { |
| 134 |
$cart_data->setDiscount( |
| 135 |
0, |
| 136 |
'discount', |
| 137 |
Util::convertAmountToFractional($order->get_total_discount(false)) |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
$shipping_data = current($order->get_items('shipping')); |
| 142 |
|
| 143 |
if(!$shipping_data) { |
| 144 |
$cart_data->setAddressDelivery( |
| 145 |
$order->get_billing_city(), |
| 146 |
$order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), |
| 147 |
$phone |
| 148 |
?: '', |
| 149 |
$order->get_billing_address_1() . ' ' . $order->get_billing_address_2(), |
| 150 |
$order->get_billing_country(), |
| 151 |
$order->get_billing_postcode() |
| 152 |
); |
| 153 |
$cart_data->setShipping( |
| 154 |
0, |
| 155 |
'noshipping', |
| 156 |
0 |
| 157 |
); |
| 158 |
|
| 159 |
return $cart_data->prepareCartDataArray(); |
| 160 |
} |
| 161 |
|
| 162 |
$shipping_data = $shipping_data->get_data(); |
| 163 |
$id_shipping_tax = null; |
| 164 |
foreach($order->get_items('shipping') as $item_id => $item_tax) { |
| 165 |
$shipping_data = json_decode($item_tax, true); |
| 166 |
$id_shipping_tax = current(array_keys($shipping_data['taxes']['total'])); |
| 167 |
} |
| 168 |
|
| 169 |
$cart_data->setShipping($id_shipping_tax |
| 170 |
? (float) WC_Tax::_get_tax_rate($id_shipping_tax)['tax_rate'] |
| 171 |
: 0, |
| 172 |
$shipping_data['name'], |
| 173 |
Util::convertAmountToFractional($shipping_data['total'] + $shipping_data['total_tax'])); |
| 174 |
|
| 175 |
$cart_data->setAddressDelivery( |
| 176 |
$order->get_shipping_city(), |
| 177 |
$order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), |
| 178 |
$phone |
| 179 |
?: '', |
| 180 |
$order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), |
| 181 |
$order->get_shipping_country(), |
| 182 |
$order->get_shipping_postcode() |
| 183 |
); |
| 184 |
|
| 185 |
return $cart_data->prepareCartDataArray(); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* @param string $service_id |
| 190 |
* @param string $service_key |
| 191 |
* @param string $debug_mode |
| 192 |
* |
| 193 |
* @throws Exception |
| 194 |
*/ |
| 195 |
public static function check_notification($service_id, $service_key, $debug_mode) |
| 196 |
{ |
| 197 |
$notification = new Notification( |
| 198 |
$service_id, |
| 199 |
$service_key |
| 200 |
); |
| 201 |
|
| 202 |
// it can be order data or notification code - depends on verification notification |
| 203 |
$result_check_request_notification = $notification->checkRequest(); |
| 204 |
$is_debug_mode = $debug_mode === 'yes'; |
| 205 |
|
| 206 |
if(is_int($result_check_request_notification)) { |
| 207 |
echo Notification::formatResponse(Notification::NS_ERROR, $result_check_request_notification, $is_debug_mode); |
| 208 |
exit(); |
| 209 |
} |
| 210 |
|
| 211 |
if(!($order = wc_get_order($result_check_request_notification['transaction']['orderId']))) { |
| 212 |
|
| 213 |
echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_ORDER_NOT_FOUND, $is_debug_mode); |
| 214 |
exit(); |
| 215 |
} |
| 216 |
|
| 217 |
if($order->get_status() === 'completed' || $order->get_status() === 'processing') { |
| 218 |
|
| 219 |
echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_INVALID_ORDER_STATUS, $is_debug_mode); |
| 220 |
exit(); |
| 221 |
} |
| 222 |
|
| 223 |
if(!Notification::checkRequestAmount($result_check_request_notification, Util::convertAmountToFractional($order->data['total']), $order->data['currency'])) { |
| 224 |
|
| 225 |
echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_AMOUNT_NOT_MATCH, $is_debug_mode); |
| 226 |
exit(); |
| 227 |
} |
| 228 |
|
| 229 |
$transactionStatuses = Util::getTransactionStatuses(); |
| 230 |
|
| 231 |
if(!isset($transactionStatuses[$result_check_request_notification['transaction']['status']])) { |
| 232 |
echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_UNHANDLED_STATUS, $is_debug_mode); |
| 233 |
exit; |
| 234 |
} |
| 235 |
|
| 236 |
switch($result_check_request_notification['transaction']['status']) { |
| 237 |
case 'settled': |
| 238 |
|
| 239 |
$order->update_status($order->needs_processing() |
| 240 |
? 'processing' |
| 241 |
: 'completed'); |
| 242 |
|
| 243 |
$order->update_meta_data('imoje_transaction_uuid', $result_check_request_notification['transaction']['id']); |
| 244 |
$order->save_meta_data(); |
| 245 |
|
| 246 |
wc_reduce_stock_levels($order->get_id()); |
| 247 |
|
| 248 |
$order->add_order_note(__('Transaction reference', 'imoje') . ': ' . $result_check_request_notification['transaction']['id']); |
| 249 |
echo Notification::formatResponse(Notification::NS_OK); |
| 250 |
exit; |
| 251 |
case 'rejected': |
| 252 |
$order->update_status('failed'); |
| 253 |
$order->add_order_note(__('Transaction reference', 'imoje') . ': ' . $result_check_request_notification['transaction']['id']); |
| 254 |
echo Notification::formatResponse(Notification::NS_OK); |
| 255 |
exit; |
| 256 |
default: |
| 257 |
echo Notification::formatResponse(Notification::NS_OK, Notification::NC_UNHANDLED_STATUS, $is_debug_mode); |
| 258 |
exit; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* @param int $order_id |
| 264 |
* @param string $authorization_token |
| 265 |
* @param string $merchant_id |
| 266 |
* @param string $service_id |
| 267 |
* @param float $amount |
| 268 |
* |
| 269 |
* @return bool |
| 270 |
* @throws Exception |
| 271 |
*/ |
| 272 |
public static function process_refund($order_id, $authorization_token, $merchant_id, $service_id, $amount) |
| 273 |
{ |
| 274 |
|
| 275 |
$order = wc_get_order($order_id); |
| 276 |
|
| 277 |
$api = new Api($authorization_token, $merchant_id, $service_id); |
| 278 |
|
| 279 |
$refund = $api->createRefund( |
| 280 |
$api->prepareRefundData( |
| 281 |
Util::convertAmountToFractional($amount) |
| 282 |
), |
| 283 |
$order->get_meta('imoje_transaction_uuid') |
| 284 |
); |
| 285 |
|
| 286 |
if($refund['success']) { |
| 287 |
|
| 288 |
if($refund['body']['transaction']['status'] !== 'settled') { |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
$order->add_order_note( |
| 293 |
__('Refund for amount:', 'imoje') |
| 294 |
. ' ' |
| 295 |
. $amount |
| 296 |
. ' ' |
| 297 |
. __('with UUID:', 'imoje') |
| 298 |
. ' ' |
| 299 |
. $refund['body']['transaction']['id'] |
| 300 |
. ' ' |
| 301 |
. __('has been created.', 'imoje')); |
| 302 |
|
| 303 |
return true; |
| 304 |
} |
| 305 |
|
| 306 |
if(isset($refund['data']['body']) && $refund['data']['body']) { |
| 307 |
$refund_error = __('Refund error: ', 'imoje') . ' ' . $refund['data']['body']; |
| 308 |
} else { |
| 309 |
$refund_error = __('Refund error.', 'imoje'); |
| 310 |
} |
| 311 |
|
| 312 |
$order->add_order_note( |
| 313 |
$refund_error |
| 314 |
); |
| 315 |
|
| 316 |
throw new Exception($refund_error); |
| 317 |
} |
| 318 |
} |
| 319 |
|