Search code examples
phpjoomlaauthorize.net

Creating a Transaction Controller Leads to Unexpected Error


I'm currently developing an extension for one of my Joomla websites to integrate with Authorize.net. I followed the 'Hello World' example from their site to derive the following sandbox source code:

require 'vendor/autoload.php'; 
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;

define("AUTHORIZENET_LOG_FILE", "phplog");

...

// Common setup for API credentials  
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();   
$merchantAuthentication->setName("<redacted: login id>");   
$merchantAuthentication->setTransactionKey("<redacted: transaction key>");   
$refId = 'ref' . time();

// Create the payment data for a credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber("4111111111111111");  
$creditCard->setExpirationDate("2038-12");
$paymentOne = new AnetAPI\PaymentType();
$paymentOne->setCreditCard($creditCard);

// Create a transaction
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authCaptureTransaction");   
$transactionRequestType->setAmount(151.51);
$transactionRequestType->setPayment($paymentOne);
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId($refId);
$request->setTransactionRequest($transactionRequestType);

// Error on the following line:
$controller = new AnetController\CreateTransactionController($request);

$response = $controller->executeWithApiResponse(net\authorize\api\constants\ANetEnvironment::SANDBOX);   

if ($response != null) {
    $tresponse = $response->getTransactionResponse();
    if (($tresponse != null) && ($tresponse->getResponseCode() == "1")) {
        echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
        echo "Charge Credit Card TRANS ID  : " . $tresponse->getTransId() . "\n";
    } else {
        echo "Charge Credit Card ERROR :  Invalid response\n";
    }
} else {
  echo  "Charge Credit Card Null response returned";
}

I'm getting an error stating syntax error, unexpected '?', expecting variable (T_VARIABLE). I couldn't find anything wrong with the line the error happened on so in trying to debug the error I traced it back to net\authorize\api\controller\base\ApiOperationBase whose constructor generates this error at several points. That doesn't seem right to me, especially when considering one of the offending lines is simply:

$this->logger = LogFactory::getLog(get_class($this));

And so now I'm lost, because I'm not sure what else I can debug in a Hello World sandbox transaction. I installed authorize.net using composer, and I'm running this with PHP v7.0.32 on Joomla! v3.9.1 Stable. Any help would be greatly appreciated!

Edit: For reference, the constructor of AnetController\CreateTransactionController looks like this:

public function __construct(AnetApiRequestType $request)
{
    $responseType = 'net\authorize\api\contract\v1\CreateTransactionResponse';
    parent::__construct($request, $responseType);
}

and the parent constructor (ApiOperationBase) looks like this, with the three lines that each independently cause the same error noted:

public function __construct(\net\authorize\api\contract\v1\AnetApiRequestType $request, $responseType)
{
    // Error on the line below (syntax error: '?')
    $this->logger = LogFactory::getLog(get_class($this));

    if ( null == $request)
    {
        throw new InvalidArgumentException( "request cannot be null");
    }

    if ( null == $responseType || '' == $responseType)
    {
        throw new InvalidArgumentException( "responseType cannot be null or empty");
    }

    if ( null != $this->apiResponse)
    {
        throw new InvalidArgumentException( "response has to be null");
    }

    $this->apiRequest = $request;
    $this->validate();

    $this->apiResponseType = $responseType;

    // Error on the line below (syntax error: '?')
    $this->httpClient = new HttpClient;

    // Error on the line below (syntax error: '?')
    $serializerBuilder = SerializerBuilder::create();

    $serializerBuilder->addMetadataDir( __DIR__ . '/../../yml/v1', 'net\authorize\api\contract\v1');//..\..\yml\v1\ //'/../lib/net/authorize/api/yml/v1'
    $serializerBuilder->configureHandlers(
        function (HandlerRegistryInterface $h)

        use($serializerBuilder)
        {
            $serializerBuilder->addDefaultHandlers();
            $h->registerSubscribingHandler(new BaseTypesHandler()); // XMLSchema List handling
            $h->registerSubscribingHandler(new XmlSchemaDateHandler()); // XMLSchema date handling
        }
    );
    $this->serializer = $serializerBuilder->build();
}

As commented by Alex Howansky, there aren't any question marks in the source code apart from the regular tags. It's very confusing for me.


Solution

  • Please read the comments on the question for detail about getting to this solution.

    It turns out that the PHP version (7.0.32) was causing problems with the version of Authorize.net I was using (1.9.9). Updating PHP to 7.3.0 fixed the error.