<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
namespace net\authorize\api\controller\test;

use net\authorize\api\contract\v1 AS apiContract;
use net\authorize\api\controller AS apiController;

require_once __DIR__ . '/ApiCoreTestBase.php';

class CreateTransactionControllerTest extends ApiCoreTestBase
{
    public function testCreateTransactionCreditCard()
    {
        $merchantAuthentication = new apiContract\MerchantAuthenticationType();
        $merchantAuthentication->setName(self::$LoginName);
        $merchantAuthentication->setTransactionKey(self::$TransactionKey);

        //create a transaction
        $transactionRequestType = new apiContract\TransactionRequestType();
        $transactionRequestType->setTransactionType( "authCaptureTransaction"); // TODO Change to Enum
        $transactionRequestType->setAmount( $this->setValidAmount( $this->counter));
        $transactionRequestType->setPayment( $this->paymentOne);
        $transactionRequestType->setOrder( $this->orderType);
        $transactionRequestType->setCustomer( $this->customerDataOne);
        $transactionRequestType->setBillTo( $this->customerAddressOne);

        $request = new apiContract\CreateTransactionRequest();
        $request->setMerchantAuthentication($merchantAuthentication);
        $request->setRefId( $this->refId);
        $request->setTransactionRequest( $transactionRequestType);

        $controller = new apiController\CreateTransactionController($request);
        $controller->execute( self::$TestEnvironment);
        $response = $controller->getApiResponse();

        // Handle and validate the response
        self::displayMessages( $response);
        $this->assertNotNull($response, "null response");
        $this->assertNotNull($response->getMessages());
        $logMessage = sprintf("\n%s: Controller Response ResultCode: '%s'.", \net\authorize\util\Helpers::now(), $response->getMessages()->getResultCode());
        if (self::$log_file) {
            file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
        } else {
            echo $logMessage;
        }
        $this->assertEquals("Ok", $response->getMessages()->getResultCode());
        $this->assertEquals( $this->refId, $response->getRefId());
        $this->assertTrue(0 < count($response->getMessages()));
        foreach ($response->getMessages() as $message)
        {
            $this->assertEquals("I00001", $message->getCode());
            $this->assertEquals("Successful.", $response->getText());
        }
    }

    public function testCreateTransactionPayPal()
    {
        $merchantAuthentication = new apiContract\MerchantAuthenticationType();
        $merchantAuthentication->setName(self::$LoginName);
        $merchantAuthentication->setTransactionKey(self::$TransactionKey);

        $paymentType = new apiContract\PaymentType();
        $paymentType->setPayPal($this->payPalOne);

        //create a transaction
        $transactionRequestType = new apiContract\TransactionRequestType();
        $transactionRequestType->setTransactionType( "authOnlyTransaction"); // TODO Change to Enum
        $transactionRequestType->setAmount( $this->setValidAmount( $this->counter));
        $transactionRequestType->setPayment( $paymentType);

        $request = new apiContract\CreateTransactionRequest();
        $request->setMerchantAuthentication($merchantAuthentication);
        $request->setTransactionRequest( $transactionRequestType);

        $controller = new apiController\CreateTransactionController($request);
        $response = $controller->executeWithApiResponse( self::$TestEnvironment);

        // Handle the response.
        $this->assertNotNull($response, "null response");
        $this->assertNotNull($response->getMessages());

        self::displayMessages( $response);
        if ( "Ok" != $response->getMessages()->getResultCode())
        {
            $this->displayTransactionMessages( $response);
            //Ignore assertion for now
            //$this->assertTrue( false, "Should not reach here.");
        }
        else
        {
            $this->assertEquals("Ok", $response->getMessages()->getResultCode());
            $this->assertTrue(0 < count($response->getMessages()));
            foreach ($response->getMessages() as $message)
            {
                $this->assertEquals("I00001", $message->getCode());
                $this->assertEquals("Successful.", $response->getText());
            }
        }
    }

    public function testCreateTransactionApplePay()
    {
        $merchantAuthentication = new apiContract\MerchantAuthenticationType();
        $merchantAuthentication->setName(self::$LoginName);
        $merchantAuthentication->setTransactionKey(self::$TransactionKey);

        $OpaqueData = new apiContract\OpaqueDataType();
        $OpaqueData->setDataDescriptor("COMMON.APPLE.INAPP.PAYMENT");
        $OpaqueData->setDataValue("eyJkYXRhIjoiQkRQTldTdE1tR2V3UVVXR2c0bzdFXC9qKzFjcTFUNzhxeVU4NGI2N2l0amNZSTh3UFlBT2hzaGpoWlBycWRVcjRYd1BNYmo0emNHTWR5KysxSDJWa1BPWStCT01GMjV1YjE5Y1g0bkN2a1hVVU9UakRsbEIxVGdTcjhKSFp4Z3A5ckNnc1NVZ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WN0lTODJPdXA5TU1vMUJQVnJYVFBITmNzTTk5RVBVblBxZGJlR2M4N20wckFnTUJBQUdqWERCYU1GZ0dBMVVkQVFSUk1FK0FFSFpXUHJXdEpkN1laNDMxaENnN1lGU2hLVEFuTVNVd0l3WURWUVFESGh3QVl3Qm9BRzBBWVFCcEFFQUFkZ0JwQUhNQVlRQXVBR01BYndCdGdoQmNsK1BmMytVNHBrMTNuVkQ5bndRUU1Ba0dCU3NPQXdJZEJRQURnWUVBYlVLWUNrdUlLUzlRUTJtRmNNWVJFSW0ybCtYZzhcL0pYditHQlZRSmtPS29zY1k0aU5ERkFcL2JRbG9nZjlMTFU4NFRId05SbnN2VjNQcnY3UlRZODFncTBkdEM4elljQWFBa0NISUkzeXFNbko0QU91NkVPVzlrSmsyMzJnU0U3V2xDdEhiZkxTS2Z1U2dRWDhLWFFZdVpMazJScjYzTjhBcFhzWHdCTDNjSjB4Z2VBd2dkMENBUUV3T3pBbk1TVXdJd1lEVlFRREhod0FZd0JvQUcwQVlRQnBBRUFBZGdCcEFITUFZUUF1QUdNQWJ3QnRBaEJjbCtQZjMrVTRwazEzblZEOW53UVFNQWtHQlNzT0F3SWFCUUF3RFFZSktvWklodmNOQVFFQkJRQUVnWUJhSzNFbE9zdGJIOFdvb3NlREFCZitKZ1wvMTI5SmNJYXdtN2M2VnhuN1phc05iQXEzdEF0OFB0eSt1UUNnc3NYcVprTEE3a3oyR3pNb2xOdHY5d1ltdTlVandhcjFQSFlTK0JcL29Hbm96NTkxd2phZ1hXUnowbk1vNXkzTzFLelgwZDhDUkhBVmE4OFNyVjFhNUpJaVJldjNvU3RJcXd2NXh1WmxkYWc2VHI4dz09In0=");

        $paymentType = new apiContract\PaymentType();
        $paymentType->setOpaqueData( $OpaqueData);

        //create a transaction
        $transactionRequestType = new apiContract\TransactionRequestType();
        $transactionRequestType->setTransactionType( "authCaptureTransaction"); // TODO Change to Enum
        $transactionRequestType->setAmount( $this->setValidAmount( $this->counter));
        $transactionRequestType->setPayment( $paymentType);

        $request = new apiContract\CreateTransactionRequest();
        $request->setMerchantAuthentication($merchantAuthentication);
        $request->setTransactionRequest( $transactionRequestType);

        $controller = new apiController\CreateTransactionController($request);
        $response = $controller->executeWithApiResponse( self::$TestEnvironment);

        // Handle the response.
        $this->assertNotNull($response, "null response");
        $this->assertNotNull($response->getMessages());

        self::displayMessages( $response);
        if ( "Ok" != $response->getMessages()->getResultCode())
        {
            $this->displayTransactionMessages( $response);
            //Ignore assertion for now
            //$this->assertTrue( false, "Should not reach here.");
        }
        else
        {
            $this->assertEquals("Ok", $response->getMessages()->getResultCode());
            $this->assertTrue(0 < count($response->getMessages()));
            foreach ($response->getMessages() as $message)
            {
                $this->assertEquals("I00001", $message->getCode());
                $this->assertEquals("Successful.", $response->getText());
            }
        }
    }


    function displayTransactionMessages( apiContract\CreateTransactionResponse $response)
    {
        if ( null != $response)
        {
            $logMessage = sprintf("\n%s: Displaying Transaction Response.", \net\authorize\util\Helpers::now());
            echo $logMessage;
            if (self::$log_file)
            {
                file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
            }

            if ( null != $response->getTransactionResponse())
            {
                $logMessage = sprintf("\n%s: Transaction Response Code: '%s'.", \net\authorize\util\Helpers::now(), $response->getTransactionResponse()->getResponseCode());
                echo $logMessage;
                if (self::$log_file)
                {
                    file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
                }

                $allMessages = $response->getTransactionResponse()->getMessages();
                $allErrors = $response->getTransactionResponse()->getErrors();
                $errorCount = 0;
                if ( null != $allErrors)
                {
                    foreach ( $allErrors as $error)
                    {
                        $errorCount++;
                        $logMessage = sprintf("\n%s: %d - Error: Code:'%s', Text:'%s'", \net\authorize\util\Helpers::now(), $errorCount, $error->getErrorCode(), $error->getErrorText());
                        echo $logMessage;
                        if (self::$log_file)
                        {
                            file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
                        }
                    }
                }
                $messageCount = 0;
                if ( null != $allMessages)
                {
                    foreach ( $allMessages as $message)
                    {
                        $messageCount++;
                        //$logMessage = sprintf("\n%s: %d - Message: Code:'%s', Description:'%s'", \net\authorize\util\Helpers::now(), $errorCount, $message->getCode(), $message->getDescription());
                        $logMessage = sprintf("\n%s: %d - Message: ", \net\authorize\util\Helpers::now(), $messageCount);
                        echo $logMessage;
                        if (self::$log_file)
                        {
                            file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
                        }
                    }
                }
                $logMessage = sprintf("\n%s: Transaction Response, Errors: '%d', Messages: '%d'.", \net\authorize\util\Helpers::now(), $errorCount, $messageCount);
                echo $logMessage;
                if (self::$log_file)
                {
                    file_put_contents(self::$log_file, $logMessage, FILE_APPEND);
                }
            }
        }
    }
}
