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.WCGatewayImojeCards.php

<?php

use Imoje\Payment\Paywall;
use Imoje\Payment\Util;

/**
 * Class WC_Gateway_ImojeCards
 */
class WC_Gateway_ImojeCards extends WC_Payment_Gateway
{

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

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

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

		$this->setup_properties();

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

		if($this->get_option('imoje_cards_sandbox') === 'yes') {
			$this->description = __('Sandbox mode is enabled.', 'imoje');
		}

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

		$this->icon = $this->get_option('imoje_cards_hide_brand') === 'no'
			? WOOCOMMERCE_IMOJE_PLUGIN_URL . 'resources/images/cards.png'
			: 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_cards_notification',
		]);
		// endregion

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

	/**
	 * Setup general properties for the gateway.
	 */
	protected function setup_properties()
	{
		$this->id = self::PAYMENT_METHOD;
		$this->method_title = __('imoje - Cards', 'imoje');
		$this->method_description = __('imoje Cards payments', 'imoje');
		$this->has_fields = false;
	}

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

		$currencies = Util::getSupportedCurrencies();

		$this->form_fields = [
			'imoje_cards_sandbox_hint' => [
				'title' => ($this->get_option('imoje_cards_sandbox') === "yes")
					? __('Sandbox is enabled', 'imoje')
					: null,
				'type'  => 'title',
			],
			'imoje_cards_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_cards_enabled'      => [
				'title'   => __('Enable / Disable', 'imoje'),
				'type'    => 'checkbox',
				'label'   => __('Enable imoje - Cards', 'imoje'),
				'default' => 'yes',
			],
			'imoje_cards_debug_mode'   => [
				'title'   => __('Debug mode', 'imoje'),
				'type'    => 'checkbox',
				'label'   => __('Enable debug mode', 'imoje'),
				'default' => 'no',
			],
			'imoje_cards_sandbox'      => [
				'title'   => __('Sandbox', 'imoje'),
				'type'    => 'checkbox',
				'default' => 'no',
				'label'   => __('Enable sandbox', 'imoje'),
			],
			'imoje_cards_hide_brand'   => [
				'title'   => __('Display Cards brand', 'imoje'),
				'type'    => 'checkbox',
				'default' => 'no',
				'label'   => __('Hide Cards brand', 'imoje'),
			],
			'imoje_cards_title'        => [
				'title'   => __('Payment title', 'imoje'),
				'type'    => 'text',
				'default' => __('Payment cards', 'imoje'),
			],
			'imoje_cards_merchant_id'  => [
				'title'   => __('Merchant ID', 'imoje'),
				'type'    => 'text',
				'default' => '',
			],
			'imoje_cards_service_id'   => [
				'title'   => __('Service ID', 'imoje'),
				'type'    => 'text',
				'default' => '',
			],
			'imoje_cards_service_key'  => [
				'title'   => __('Service Key', 'imoje'),
				'type'    => 'text',
				'default' => '',
			],
			'imoje_cards_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_imojecards', 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
	 */
	public function receipt_page($order_id)
	{

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

		global $wp_query;

		$sandbox = $this->get_option('imoje_cards_sandbox');

		$environment = $sandbox === "yes"
			? Util::ENVIRONMENT_SANDBOX
			: Util::ENVIRONMENT_PRODUCTION;

		$paywall = new Paywall(
			$environment,
			$this->get_option('imoje_cards_service_key'),
			$this->get_option('imoje_cards_service_id'),
			$this->get_option('imoje_cards_merchant_id')
		);

		$wp_query->query_vars['order_form'] = $paywall->buildOrderForm(
			$this->generate_imoje_form($order_id),
			__('Continue', 'imoje')
		);

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

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

	/**
	 * @param string $order_id
	 *
	 * @return array
	 */
	public function generate_imoje_form($order_id)
	{

		$order = new WC_Order($order_id);

		$wc = WC();

		return Paywall::prepareData(
			Util::convertAmountToFractional($order->get_total()),
			$order->get_currency(),
			$order_id,
			$order->get_billing_first_name(),
			$order->get_billing_last_name(),
			'',
			$order->get_billing_email(),
			$phone = $order->get_billing_phone()
				?: '',
			$this->get_return_url($order),
			$this->get_return_url($order),
			$this->get_return_url($order),
			self::VERSION . ';woocommerce_' . $wc->version,
			null,
			Util::getPaymentMethod('card')
		);
	}

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

		include_once 'Helper.php';

		Helper::check_notification(
			$this->get_option('imoje_cards_service_id'),
			$this->get_option('imoje_cards_service_key'),
			$this->get_option('imoje_cards_debug_mode')
		);
	}

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

		if(!in_array(strtolower(get_woocommerce_currency()), $this->get_option('imoje_cards_currencies', []))
			|| $this->get_option('imoje_cards_enabled') === 'no'
			|| empty($this->get_option('imoje_cards_service_key'))
			|| empty($this->get_option('imoje_cards_service_id'))
			|| empty($this->get_option('imoje_cards_merchant_id'))) {


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

		return $gateways;
	}
}