| 1 |
<?php |
| 2 |
|
| 3 |
use Imoje\Payment\Api; |
| 4 |
use Imoje\Payment\CartData; |
| 5 |
use Imoje\Payment\Notification; |
| 6 |
use Imoje\Payment\Util; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Helper |
| 10 |
*/ |
| 11 |
class Helper |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* @param WC_Order $order |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
public static function get_cart($order) |
| 20 |
{ |
| 21 |
|
| 22 |
$wc = WC(); |
| 23 |
|
| 24 |
$cart_data = new CartData(); |
| 25 |
|
| 26 |
$cart_data->setCreatedAt(strtotime($order->get_date_created())); |
| 27 |
$cart_data->setAmount(Util::convertAmountToFractional($order->get_total())); |
| 28 |
|
| 29 |
foreach($wc->cart->get_cart() as $cart_item) { |
| 30 |
|
| 31 |
// region get tax for each product |
| 32 |
$product = wc_get_product($cart_item['product_id']); |
| 33 |
$tax_rate_product = current(WC_Tax::get_rates($product->get_tax_class())); |
| 34 |
|
| 35 |
$rate = 0; |
| 36 |
if($tax_rate_product && isset($tax_rate_product['rate'])) { |
| 37 |
$rate = (float) $tax_rate_product['rate']; |
| 38 |
} |
| 39 |
// endregion |
| 40 |
|
| 41 |
$price = wc_get_price_including_tax($product); |
| 42 |
|
| 43 |
if($product->is_type('variable')) { |
| 44 |
|
| 45 |
$product_variation = new WC_Product_Variation($cart_item['variation_id']); |
| 46 |
$price = wc_get_price_including_tax($product_variation); |
| 47 |
} |
| 48 |
|
| 49 |
$cart_data->addItem( |
| 50 |
$product->get_id(), |
| 51 |
$rate, |
| 52 |
$cart_item['data']->get_name(), |
| 53 |
Util::convertAmountToFractional($price), |
| 54 |
$cart_item['quantity'] |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
$phone = $order->get_billing_phone(); |
| 59 |
|
| 60 |
$cart_data->setAddressBilling( |
| 61 |
$order->get_billing_city(), |
| 62 |
$order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), |
| 63 |
$phone |
| 64 |
?: '', |
| 65 |
$order->get_billing_address_1() . ' ' . $order->get_billing_address_2(), |
| 66 |
$order->get_billing_country(), |
| 67 |
$order->get_billing_postcode() |
| 68 |
); |
| 69 |
|
| 70 |
if($order->get_total_discount(false) > 0) { |
| 71 |
$cart_data->setDiscount( |
| 72 |
0, |
| 73 |
'discount', |
| 74 |
Util::convertAmountToFractional($order->get_total_discount(false)) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
$shipping_data = current($order->get_items('shipping')); |
| 79 |
|
| 80 |
if(!$shipping_data) { |
| 81 |
$cart_data->setAddressDelivery( |
| 82 |
$order->get_billing_city(), |
| 83 |
$order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), |
| 84 |
$phone |
| 85 |
?: '', |
| 86 |
$order->get_billing_address_1() . ' ' . $order->get_billing_address_2(), |
| 87 |
$order->get_billing_country(), |
| 88 |
$order->get_billing_postcode() |
| 89 |
); |
| 90 |
$cart_data->setShipping( |
| 91 |
0, |
| 92 |
'noshipping', |
| 93 |
0 |
| 94 |
); |
| 95 |
|
| 96 |
return $cart_data->prepareCartData(); |
| 97 |
} |
| 98 |
|
| 99 |
$shipping_data = $shipping_data->get_data(); |
| 100 |
$id_shipping_tax = null; |
| 101 |
foreach($order->get_items('shipping') as $item_id => $item_tax) { |
| 102 |
$shipping_data = json_decode($item_tax, true); |
| 103 |
$id_shipping_tax = current(array_keys($shipping_data['taxes']['total'])); |
| 104 |
} |
| 105 |
|
| 106 |
$cart_data->setShipping($id_shipping_tax |
| 107 |
? (float) WC_Tax::_get_tax_rate($id_shipping_tax)['tax_rate'] |
| 108 |
: 0, |
| 109 |
$shipping_data['name'], |
| 110 |
Util::convertAmountToFractional($shipping_data['total'] + $shipping_data['total_tax'])); |
| 111 |
|
| 112 |
$cart_data->setAddressDelivery( |
| 113 |
$order->get_shipping_city(), |
| 114 |
$order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), |
| 115 |
$phone |
| 116 |
?: '', |
| 117 |
$order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), |
| 118 |
$order->get_shipping_country(), |
| 119 |
$order->get_shipping_postcode() |
| 120 |
); |
| 121 |
|
| 122 |
return $cart_data->prepareCartData(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @param string $service_id |
| 127 |
* @param string $service_key |
| 128 |
* @param string $debug_mode |
| 129 |
* |
| 130 |
* @throws Exception |
| 131 |
*/ |
| 132 |
public static function check_notification($service_id, $service_key, $debug_mode) |
| 133 |
{ |
| 134 |
$notification = new Notification( |
| 135 |
$service_id, |
| 136 |
$service_key |
| 137 |
); |
| 138 |
|
| 139 |
// it can be order data or notification code - depends on verification notification |
| 140 |
$result_check_request_notification = $notification->checkRequest(); |
| 141 |
$is_debug_mode = $debug_mode === 'yes'; |
| 142 |
|
| 143 |
if(is_int($result_check_request_notification)) { |
| 144 |
echo Notification::formatResponse(Notification::NS_ERROR, $result_check_request_notification, $is_debug_mode); |
| 145 |
exit(); |
| 146 |
} |
| 147 |
|
| 148 |
if(!($order = wc_get_order($result_check_request_notification['transaction']['orderId']))) { |
| 149 |
|
| 150 |
echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_ORDER_NOT_FOUND, $is_debug_mode); |
| 151 |
exit(); |
| 152 |
} |
| 153 |
|
| 154 |
if($order->get_status() === 'completed' || $order->get_status() === 'processing') { |
| 155 |
|
| 156 |
echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_INVALID_ORDER_STATUS, $is_debug_mode); |
| 157 |
exit(); |
| 158 |
} |
| 159 |
|
| 160 |
if(!Notification::checkRequestAmount($result_check_request_notification, Util::convertAmountToFractional($order->data['total']), $order->data['currency'])) { |
| 161 |
|
| 162 |
echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_AMOUNT_NOT_MATCH, $is_debug_mode); |
| 163 |
exit(); |
| 164 |
} |
| 165 |
|
| 166 |
$transactionStatuses = Util::getTransactionStatuses(); |
| 167 |
|
| 168 |
if(!isset($transactionStatuses[$result_check_request_notification['transaction']['status']])) { |
| 169 |
echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_UNHANDLED_STATUS, $is_debug_mode); |
| 170 |
exit; |
| 171 |
} |
| 172 |
|
| 173 |
switch($result_check_request_notification['transaction']['status']) { |
| 174 |
case 'settled': |
| 175 |
|
| 176 |
$order->update_status($order->needs_processing() |
| 177 |
? 'processing' |
| 178 |
: 'completed'); |
| 179 |
|
| 180 |
$order->update_meta_data('imoje_transaction_uuid', $result_check_request_notification['transaction']['id']); |
| 181 |
$order->save_meta_data(); |
| 182 |
|
| 183 |
wc_reduce_stock_levels($order->get_id()); |
| 184 |
|
| 185 |
$order->add_order_note(__('Transaction reference', 'imoje') . ': ' . $result_check_request_notification['transaction']['id']); |
| 186 |
echo Notification::formatResponse(Notification::NS_OK); |
| 187 |
exit; |
| 188 |
case 'rejected': |
| 189 |
$order->update_status('failed'); |
| 190 |
$order->add_order_note(__('Transaction reference', 'imoje') . ': ' . $result_check_request_notification['transaction']['id']); |
| 191 |
echo Notification::formatResponse(Notification::NS_OK); |
| 192 |
exit; |
| 193 |
default: |
| 194 |
echo Notification::formatResponse(Notification::NS_OK, Notification::NC_UNHANDLED_STATUS, $is_debug_mode); |
| 195 |
exit; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* @param int $order_id |
| 201 |
* @param string $authorization_token |
| 202 |
* @param string $merchant_id |
| 203 |
* @param string $service_id |
| 204 |
* @param float $amount |
| 205 |
* |
| 206 |
* @return bool |
| 207 |
* @throws Exception |
| 208 |
*/ |
| 209 |
public static function process_refund($order_id, $authorization_token, $merchant_id, $service_id, $amount) |
| 210 |
{ |
| 211 |
|
| 212 |
$order = wc_get_order($order_id); |
| 213 |
|
| 214 |
$api = new Api($authorization_token, $merchant_id, $service_id); |
| 215 |
|
| 216 |
$refund = $api->createRefund( |
| 217 |
$api->prepareRefundData( |
| 218 |
Util::convertAmountToFractional($amount) |
| 219 |
), |
| 220 |
$order->get_meta('imoje_transaction_uuid') |
| 221 |
); |
| 222 |
|
| 223 |
if($refund['success']) { |
| 224 |
|
| 225 |
if($refund['body']['transaction']['status'] !== 'settled') { |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
$order->add_order_note( |
| 230 |
__('Refund for amount:', 'imoje') |
| 231 |
. ' ' |
| 232 |
. $amount |
| 233 |
. ' ' |
| 234 |
. __('with UUID:', 'imoje') |
| 235 |
. ' ' |
| 236 |
. $refund['body']['transaction']['id'] |
| 237 |
. ' ' |
| 238 |
. __('has been created.', 'imoje')); |
| 239 |
|
| 240 |
return true; |
| 241 |
} |
| 242 |
|
| 243 |
if(isset($refund['data']['body']) && $refund['data']['body']) { |
| 244 |
$refund_error = __('Refund error: ', 'imoje') . ' ' . $refund['data']['body']; |
| 245 |
} else { |
| 246 |
$refund_error = __('Refund error.', 'imoje'); |
| 247 |
} |
| 248 |
|
| 249 |
$order->add_order_note( |
| 250 |
$refund_error |
| 251 |
); |
| 252 |
|
| 253 |
throw new Exception($refund_error); |
| 254 |
} |
| 255 |
} |
| 256 |
|