创建定期账单时,未定义的方法 getSubscriptionId() 在编辑器中出现错误

问题描述 投票:0回答:1

我的代码:

<?php

namespace App\Http\Controllers;
use App\Models\User;
use DateTime;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;

use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;


class SubscriptionController extends Controller
{

    public function Misubscripcion($intervalLength)
    {
            /* Create a merchantAuthenticationType object with authentication details
               retrieved from the constants file */
            $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
            $merchantAuthentication->setName(env("MERCHANT_LOGIN_ID"));
            $merchantAuthentication->setTransactionKey(env("MERCHANT_TRANSACTION_KEY"));

            // Set the transaction's refId
            $refId = 'ref' . time();

            // Subscription Type Info
            $subscription = new AnetAPI\ARBSubscriptionType();
            $subscription->setName("Sample Subscription");

            $interval = new AnetAPI\PaymentScheduleType\IntervalAType();
            $interval->setLength($intervalLength);
            $interval->setUnit("days");

            $paymentSchedule = new AnetAPI\PaymentScheduleType();
            $paymentSchedule->setInterval($interval);
            $paymentSchedule->setStartDate(new DateTime('2035-12-30'));
            $paymentSchedule->setTotalOccurrences("12");
            $paymentSchedule->setTrialOccurrences("1");

            $subscription->setPaymentSchedule($paymentSchedule);
            $subscription->setAmount(rand(1,99999)/12.0*12);
            $subscription->setTrialAmount("0.00");

            $creditCard = new AnetAPI\CreditCardType();
            $creditCard->setCardNumber("4111111111111111");
            $creditCard->setExpirationDate("2038-12");

            $payment = new AnetAPI\PaymentType();
            $payment->setCreditCard($creditCard);
            $subscription->setPayment($payment);

            $order = new AnetAPI\OrderType();
            $order->setInvoiceNumber("1234354");
            $order->setDescription("Description of the subscription");
            $subscription->setOrder($order);

            $billTo = new AnetAPI\NameAndAddressType();
            $billTo->setFirstName("John");
            $billTo->setLastName("Smith");

            $subscription->setBillTo($billTo);

            $request = new AnetAPI\ARBCreateSubscriptionRequest();
            $request->setmerchantAuthentication($merchantAuthentication);
            $request->setRefId($refId);
            $request->setSubscription($subscription);

            $controller = new AnetController\ARBCreateSubscriptionController($request);

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


            if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") )
            {
                echo "SUCCESS: Subscription ID : " . $response->getSubscriptionId() . "\n";
             }
            else
            {
                echo "ERROR :  Invalid response\n";
                $errorMessages = $response->getMessages()->getMessage();
                echo "Response : " . $errorMessages[0]->getCode() . "  " .$errorMessages[0]->getText() . "\n";
            }


            return $response;
    }
}

专家您好, 我正在使用 laravel,我正在尝试创建一个定期计费,之后 经过几个小时的战斗,我复制并粘贴了示例代码,然后将我的沙箱凭据添加到其中 文字 我在第 68 行收到错误未定义的方法: echo "成功:订阅 ID:" 。 $response->getSubscriptionId() 。 ” ”;

谁能告诉我为什么?以及如何解决。

我正在使用 PHP 8.2.14 授权网2.0 Laravel 框架 8.83 定期计费已创建,并且 API 的响应为“正常”,但由于此错误,我无法获取 SubscriptionId。

php laravel authorize.net
1个回答
0
投票

响应对象没有 getSubscriptionId() 方法似乎可能存在问题。根据 Authorize.Net SDK 文档,检索订阅 ID 的正确方法是 getSubscriptionId()。

但是,您可以检查一些事项:

SDK版本兼容性: 确保您使用的 Authorize.Net SDK 版本支持 getSubscriptionId() 方法。不同版本可能会有更改或更新。

响应对象检查: 通过打印 $response 对象的详细信息来验证其结构。您可以使用 var_dump($response) 或 print_r($response) 来检查响应结构。

API响应: 仔细检查您从 API 收到的响应。订阅 ID 可能由于某种原因没有被返回。

这里有一个快速修改可以帮助您调试:

更换线路:

echo "SUCCESS: Subscription ID : " . $response->getSubscriptionId() . "\n";

与:

var_dump($response);

这将输出完整的响应对象。检查是否有与订阅ID相关的信息。

如果问题仍然存在,您可能需要查阅 Authorize.Net SDK 文档或社区论坛以了解您所使用的特定版本。您需要考虑的 SDK 或 API 响应可能会发生变化。

© www.soinside.com 2019 - 2024. All rights reserved.