undefined method getSubscriptionId() as errror in editor when creating a Recurring Billing

39 Views Asked by At

mycode:

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

Hi expert, I'm using laravel and I'm trying y to create a Recurring Billing, After a few hour of fighting i literally copy and paste the sample code and just add my sandbox credentials to it text I'm getting the error undefined method at line 68: echo "SUCCESS: Subscription ID : " . $response->getSubscriptionId() . "\n";

can anyone tell me why ? and how to solved it please.

I'm using php 8.2.14 authorizenet 2.0 laravel framework 8.83 the Recurring Billing is created and it response form the API is "Ok", but I'm not able to get the SubscriptionId due to this error.

1

There are 1 best solutions below

0
Vladyslav Beziazychenko On

It seems like there might be an issue with the response object not having the getSubscriptionId() method. Based on the Authorize.Net SDK documentation, the correct method to retrieve the subscription ID is getSubscriptionId().

However, there are a few things you can check:

SDK Version Compatibility: Ensure that the version of the Authorize.Net SDK you're using supports the getSubscriptionId() method. There might be changes or updates in different versions.

Response Object Check: Verify the structure of the $response object by printing its details. You can use var_dump($response) or print_r($response) to inspect the response structure.

API Response: Double-check the response you're receiving from the API. It's possible that the subscription ID is not being returned for some reason.

Here's a quick modification to help you debug:

Replace the line:

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

With:

var_dump($response);

This will output the complete response object. Check if there's any information related to the subscription ID.

If the issue persists, you may need to consult the Authorize.Net SDK documentation or community forums for the specific version you are using. It's possible there might be changes in the SDK or API responses that you need to account for.