<?php
/**
 * Test script for Swish API.
 *
 * @package flo-checkout
 * @noinspection PhpUndefinedConstantInspection
 * @noinspection PhpMultipleClassDeclarationsInspection
 *
 * @phpcs:disable WordPress.PHP.DevelopmentFunctions.prevent_path_disclosure_error_reporting
 * @phpcs:disable WordPress.PHP.IniSet.display_errors_Disallowed
 * @phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting
 * @phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed
 */

namespace Flo_Checkout\Gateways\Swish;

ini_set( 'display_errors', 1 );
error_reporting( E_ALL );

$incl_path = defined( 'NM_SWISH_INCLUDES_PATH' ) ? NM_SWISH_INCLUDES_PATH : '../../includes';
$cert_path = defined( 'NM_SWISH_CERTS_PATH' ) ? NM_SWISH_CERTS_PATH : getcwd() . '/MSS/';

$root_certificate    = defined( 'NM_SWISH_CERT_ROOT' ) ? NM_SWISH_CERT_ROOT : $cert_path . 'Swish_TechnicalSupplier_TestCertificate_9870474641.pem';
$public_certificate  = defined( 'NM_SWISH_CERT_PUBLIC' ) ? NM_SWISH_CERT_PUBLIC : $cert_path . 'Swish_TechnicalSupplier_TestCertificate_9870474641.p12';
$private_certificate = defined( 'NM_SWISH_CERT_PRIVATE' ) ? NM_SWISH_CERT_PRIVATE : '';
$private_key         = defined( 'NM_SWISH_CERT_KEY' ) ? NM_SWISH_CERT_KEY : 'swish';
$swish_domain        = defined( 'NM_SWISH_DOMAIN' ) ? NM_SWISH_DOMAIN : 'mss.cpc.getswish.net';
$payee_alias         = defined( 'NM_SWISH_PAYEE_ALIAS' ) ? NM_SWISH_PAYEE_ALIAS : '1234679304';
$payer_alias         = defined( 'NM_SWISH_PAYER_ALIAS' ) ? NM_SWISH_PAYER_ALIAS : '46700000000';

require_once $incl_path . '/Gateways/Swish/Swish_API.php';
require_once $incl_path . '/Gateways/Swish/Swish_Errors.php';

$test_data = array(
	'payeePaymentReference' => '0123456789',
	'callbackUrl'           => 'https://example.com',
	'payerAlias'            => $payer_alias,
	'payeeAlias'            => $payee_alias,
	'amount'                => '1',
	'currency'              => 'SEK',
	'message'               => 'Northmill Test Product',
);

$end_point = 'paymentrequests';

$request_type = 'POST';


$end_point   = 'https://' . $swish_domain . '/swish-cpcapi/api/v1/' . $end_point;
$data_string = json_encode( $test_data, JSON_UNESCAPED_SLASHES ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode


$response = Swish_API::send_curl_request(
	$root_certificate,
	$public_certificate,
	$private_certificate,
	$private_key,
	$end_point,
	$data_string,
	$request_type
);

Swish_Settings::log( 'debug', $response );


/**
 * Class to help log.
 */
class Swish_Settings {
	/**
	 * Log a message.
	 *
	 * @param string $level  The log level.
	 * @param mixed  $message The message to log.
	 *
	 * @return void
	 */
	public static function log( string $level, mixed $message ): void {
		if ( ! is_string( $message ) ) {
			$message = json_encode( $message, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode

		}
		echo $level . ': ' . $message . PHP_EOL; // phpcs:ignore
	}
}

/**
 * Since __() is a WordPress function, we need to define it here to avoid errors.
 *
 * @param string $string_name The string to be translated.
 * @param string $text_domain The text domain to use for translation.
 *
 * @return string The translated string.
 * @noinspection PhpUnusedParameterInspection
 *
 * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
 */
function __( string $string_name, string $text_domain ): string {
	return $string_name;
}
