Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

jsarnowski / jsarnowski/woocommerce-imoje   php

Repository URL to install this package:

/ class.WCGatewayImojeBlik.php

<?php

use Imoje\Payment\Api;
use Imoje\Payment\Util;

/**
 * Class WC_Gateway_ImojeBlik
 */
class WC_Gateway_ImojeBlik extends WC_Payment_Gateway
{

	/**
	 * @var string
	 */
	const PAYMENT_METHOD = 'imoje_blik';

	/**
	 * @var string
	 */
	const VERSION = '2.0.1';

	/**
	 * initialise gateway with custom settings
	 */
	public function __construct()
	{

		$this->setup_properties();

		$this->title = $this->get_option('imoje_blik_title');

		$this->init_form_fields();
		$this->init_settings();

		$this->icon = $this->get_option('imoje_blik_hide_brand') === 'no'
			? WOOCOMMERCE_IMOJE_PLUGIN_URL . 'resources/images/blik.svg'
			: null;

		// region actions
		add_action('woocommerce_update_options_payment_gateways_' . $this->id, [
			$this,
			'process_admin_options',
		]);
		add_action('woocommerce_receipt_' . $this->id, [
			$this,
			'receipt_page',
		]);
		add_action('woocommerce_api_' . strtolower(get_class($this)), [
			$this,
			'imoje_blik_notification',
		]);
		// endregion

		// region filters
		add_filter('woocommerce_available_payment_gateways', [
			$this,
			'check_imoje_blik_available_payment_gateways',
		]);
		// endregion
	}

	/**
	 * Setup general properties for the gateway.
	 */
	protected function setup_properties()
	{

		$this->id = self::PAYMENT_METHOD;
		$this->method_title = __('imoje - BLIK', 'imoje');
		$this->method_description = __('BLIK payments', 'imoje');
		$this->has_fields = false;
	}

	/**
	 * Initialize form in admin panel.
	 */
	public function init_form_fields()
	{

		$currencies = Util::getSupportedCurrencies();

		$this->form_fields = [
			'imoje_blik_hint'                => [
				'title'       => __('Hint', 'imoje'),
				'class'       => 'hidden',
				'type'        => 'title',
				'description' => __('The module requires a configuration in the imoje administration panel. <br/> Go to imoje.ingbank.pl and log in to the administration panel. <br/> Go to stores tab and enter the notification address in the appropriate field and copy the configuration keys. <br/> Copy the keys into the fields described below.', 'imoje'),
			],
			'imoje_blik_enabled'             => [
				'title'   => __('Enable / Disable', 'imoje'),
				'type'    => 'checkbox',
				'label'   => __('Enable imoje - BLIK', 'imoje'),
				'default' => 'no',
			],
			'imoje_blik_debug_mode'          => [
				'title'   => __('Debug mode', 'imoje'),
				'type'    => 'checkbox',
				'label'   => __('Enable debug mode', 'imoje'),
				'default' => 'no',
			],
			'imoje_blik_hide_brand'          => [
				'title'   => __('Display BLIK brand', 'imoje'),
				'type'    => 'checkbox',
				'default' => 'no',
				'label'   => __('Hide BLIK brand', 'imoje'),
			],
			'imoje_blik_title'               => [
				'title'   => __('Payment title', 'imoje'),
				'type'    => 'text',
				'default' => 'BLIK',
			],
			'imoje_blik_merchant_id'         => [
				'title'   => __('Merchant ID', 'imoje'),
				'type'    => 'text',
				'default' => '',
			],
			'imoje_blik_service_id'          => [
				'title'   => __('Service ID', 'imoje'),
				'type'    => 'text',
				'default' => '',
			],
			'imoje_blik_authorization_token' => [
				'title'   => __('Authorization token', 'imoje'),
				'type'    => 'text',
				'default' => '',
			],
			'imoje_blik_currencies'          => [
				'title'   => __('Currency', 'imoje'),
				'type'    => 'multiselect',
				'class'   => 'wc-enhanced-select',
				'options' => $currencies,
			],
			[
				'title'       => __('Your addres for notifications', 'imoje') . ': ',
				'type'        => 'title',
				'description' => add_query_arg('wc-api', 'wc_gateway_imojeblik', home_url('/')),
			],
		];
	}

	/**
	 * @param int $order_id
	 *
	 * @return array
	 */
	public function process_payment($order_id)
	{

		$order = new WC_Order($order_id);

		// Return thank you redirect
		return [
			'result'   => 'success',
			'redirect' => $order->get_checkout_payment_url(true),
		];
	}

	/**
	 * @param string $order_id
	 *
	 * @throws Exception
	 */
	public function receipt_page($order_id)
	{

		header("Cache-Control: no-cache, no-store, must-revalidate");

		$api = new Api(
			$this->get_option('imoje_blik_authorization_token'),
			$this->get_option('imoje_blik_merchant_id'),
			$this->get_option('imoje_blik_service_id')
		);

		$order = new WC_Order($order_id);

		$order_form = $api->buildOrderForm(
			$api->createTransaction(
				$api->prepareData(
					Util::convertAmountToFractional($order->get_total()),
					$order->get_currency(),
					$order_id,
					Util::getPaymentMethod('blik'),
					Util::getPaymentMethodCode('blik'),
					$this->get_return_url($order),
					$this->get_return_url($order),
					$order->get_billing_first_name(),
					$order->get_billing_last_name(),
					$order->get_billing_email(),
					Api::TRANSACTION_TYPE_SALE
				)
			),
			__('Continue', 'imoje')
		);

		global $wp_query;

		$wp_query->query_vars['order_form'] = $order_form;

		// Remove cart
		$wc = WC();
		$wc->cart->empty_cart();

		load_template(__DIR__ . '/templates/redirect.php', false);
	}

	/**
	 * Method that checking notify and changing status
	 *
	 * @throws Exception
	 */
	public function imoje_blik_notification()
	{

		include_once 'Helper.php';

		Helper::check_notification(
			$this->get_option('imoje_blik_service_id'),
			$this->get_option('imoje_blik_service_key'),
			$this->get_option('imoje_blik_debug_mode')
		);
	}

	/**
	 * Validate data on checkout page. Remove imoje when there is an error.
	 *
	 * @param array $gateways
	 *
	 * @return array
	 */
	public function check_imoje_blik_available_payment_gateways($gateways)
	{

		if(!in_array(strtolower(get_woocommerce_currency()), $this->get_option('imoje_blik_currencies', []))
			|| $this->get_option('imoje_blik_enabled') === 'no'
			|| empty($this->get_option('imoje_blik_authorization_token'))
			|| empty($this->get_option('imoje_blik_merchant_id'))
			|| empty($this->get_option('imoje_blik_service_id'))) {

			unset($gateways['imoje_blik']);
		}

		return $gateways;
	}

}