创建事务控制器会导致意外错误

时间:2019-01-07 20:48:23

标签: php joomla authorize.net

我目前正在为我的一个Joomla网站开发扩展程序,以与Authorize.net集成。我从他们的网站上遵循了“ Hello World”示例,以获取以下沙箱源代码:

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";
}

我遇到一个错误,指出syntax error, unexpected '?', expecting variable (T_VARIABLE)。我找不到发生错误的那一行的任何错误,因此在尝试调试该错误时,我将其追溯到net\authorize\api\controller\base\ApiOperationBase,其构造函数在多个点生成了此错误。在我看来,这似乎不合适,尤其是当考虑到其中一项令人反感的内容时:

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

所以现在我迷路了,因为我不确定在Hello World沙箱事务中还能调试什么。我使用composer安装了authorize.net,并且正在Joomla上使用PHP v7.0.32运行它。 v3.9.1稳定。任何帮助将不胜感激!

编辑:AnetController\CreateTransactionController的构造函数如下所示:

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

和父构造函数(ApiOperationBase)看起来像这样,分别指出了分别引起相同错误的三行:

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();
}

如Alex Howansky所评论的那样,源代码中除了常规标记外没有任何问号。对我来说很困惑。

1 个答案:

答案 0 :(得分:1)

请阅读有关问题的评论,以获取有关此解决方案的详细信息。

事实证明,PHP版本(7.0.32)导致我使用的Authorize.net版本(1.9.9)出现问题。将PHP更新到7.3.0修复了该错误。