JMS Serializer Error: Using $this when not in object
JMS Serializer Error: Using $this when not in object
我正在处理付款处理页面。我们正在使用 Authorize.net 来处理交易。我已经导入了 Authorize 的 php 库及其所有依赖项。
当我尝试处理测试交易时出现以下错误:
Fatal error: Using $this when not in object context in /home/ticketstroyfair/public_html/include/authorize/vendor/jms/serializer/src/JMS/Serializer/Serializer.php on line 99
起初我以为这是我代码中的问题,所以我尝试了 运行 Authorize 的 php 示例交易并得到了同样的错误。
昨天刚从 GitHub 下载了序列化程序。 https://github.com/schmittjoh/serializer
授权示例代码如下:
<?php
require 'include/authorize/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("YOU_API_LOGIN_ID");
$merchantAuthentication->setTransactionKey("YOUR_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);
$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";
}
?>
关于导致错误的原因有什么想法吗?
我敢打赌,您使用的 PHP 版本比 5.4.0 更早,对吧?
该版本以下不支持在匿名函数中调用 $this。
参见 Using $this in anonymous function
这里http://php.net/manual/en/functions.anonymous.php
如果我是正确的,我高度建议至少更新到PHP 5.6.x
希望能帮到你。
我正在处理付款处理页面。我们正在使用 Authorize.net 来处理交易。我已经导入了 Authorize 的 php 库及其所有依赖项。
当我尝试处理测试交易时出现以下错误:
Fatal error: Using $this when not in object context in /home/ticketstroyfair/public_html/include/authorize/vendor/jms/serializer/src/JMS/Serializer/Serializer.php on line 99
起初我以为这是我代码中的问题,所以我尝试了 运行 Authorize 的 php 示例交易并得到了同样的错误。
昨天刚从 GitHub 下载了序列化程序。 https://github.com/schmittjoh/serializer
授权示例代码如下:
<?php
require 'include/authorize/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("YOU_API_LOGIN_ID");
$merchantAuthentication->setTransactionKey("YOUR_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);
$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";
}
?>
关于导致错误的原因有什么想法吗?
我敢打赌,您使用的 PHP 版本比 5.4.0 更早,对吧?
该版本以下不支持在匿名函数中调用 $this。 参见 Using $this in anonymous function
这里http://php.net/manual/en/functions.anonymous.php
如果我是正确的,我高度建议至少更新到PHP 5.6.x
希望能帮到你。