Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
<?php

use Imoje\Payment\Notification;
use Imoje\Payment\Util;

class Helper
{

	/**
	 * @param string $serviceId
	 * @param string $serviceKey
	 * @param string $debugMode
	 *
	 * @throws Exception
	 */
	public static function check_notification($serviceId, $serviceKey, $debugMode)
	{
		$notification = new Notification(
			$serviceId,
			$serviceKey
		);

		// it can be order data or notification code - depends on verification notification
		$result_check_request_notification = $notification->checkRequest();
		$is_debug_mode = $debugMode === 'yes';

		if(is_int($result_check_request_notification)) {
			echo Notification::formatResponse(Notification::NS_ERROR, $result_check_request_notification, $is_debug_mode);
			exit();
		}

		if(!($order = wc_get_order($result_check_request_notification['transaction']['orderId']))) {

			echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_ORDER_NOT_FOUND, $is_debug_mode);
			exit();
		}

		if($order->get_status() === 'completed' || $order->get_status() === 'processing') {

			echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_INVALID_ORDER_STATUS, $is_debug_mode);
			exit();
		}

		if(!Notification::checkRequestAmount($result_check_request_notification, Util::convertAmountToFractional($order->data['total']), $order->data['currency'])) {

			echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_AMOUNT_NOT_MATCH, $is_debug_mode);
			exit();
		}

		$transactionStatuses = Util::getTransactionStatuses();

		if(!isset($transactionStatuses[$result_check_request_notification['transaction']['status']])) {
			echo Notification::formatResponse(Notification::NS_ERROR, Notification::NC_UNHANDLED_STATUS, $is_debug_mode);
			exit;
		}

		switch($result_check_request_notification['transaction']['status']) {
			case 'settled':

				$order->update_status($order->needs_processing()
					? 'processing'
					: 'completed');

				wc_reduce_stock_levels($order->get_id());

				$order->add_order_note(__('Transaction reference', 'imoje') . ': ' . $result_check_request_notification['transaction']['id']);
				echo Notification::formatResponse(Notification::NS_OK);
				exit;
			case 'rejected':
				$order->update_status('failed');
				$order->add_order_note(__('Transaction reference', 'imoje') . ': ' . $result_check_request_notification['transaction']['id']);
				echo Notification::formatResponse(Notification::NS_OK);
				exit;
			default:
				echo Notification::formatResponse(Notification::NS_OK, Notification::NC_UNHANDLED_STATUS, $is_debug_mode);
				exit;
		}
	}
}