PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] Overriding a vendor file in Laravel Framework 7.29.3

 February 05, 2022     composer-php, laravel, php     No comments   

Issue

I'm trying to override a vendor file located at "vendor\cimpleo\omnipay-authorizenetrecurring\src\Objects\Schedule.php" to correct some issues.

composer.json

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Cimpleo\\": "app/Overrides/"
        },
        "classmap": [
            "database/seeds",
            "database/factories",
            "vendor/google/apiclient/src",
            "vendor/google/apiclient-services/src/Google"
        ],
        "exclude-from-classmap": ["vendor\\cimpleo\\omnipay-authorizenetrecurring\\src\\Objects\\Schedule.php"]
    }

Then I copied and edited the Schedule.php to folder "app\Overrides".

namespace Cimpleo;

use Academe\AuthorizeNet\PaymentInterface;
use Academe\AuthorizeNet\AbstractModel;
use Omnipay\Common\Exception\InvalidRequestException;
use DateTime;

class Schedule extends AbstractModel
{
...

The vendor Schedule.php file looks like this.

namespace Omnipay\AuthorizeNetRecurring\Objects;

use Academe\AuthorizeNet\PaymentInterface;
use Academe\AuthorizeNet\AbstractModel;
use Omnipay\Common\Exception\InvalidRequestException;
use DateTime;

class Schedule extends AbstractModel
{

    const SCHEDULE_UNIT_DAYS = 'days';
    const SCHEDULE_UNIT_MONTHS = 'months';

    protected $intervalLength;
    protected $intervalUnit;
    protected $startDate;
    protected $totalOccurrences;
    protected $trialOccurrences;

    public function __construct($parameters = null) {
        parent::__construct();

        $this->setIntervalLength($parameters['intervalLength']);
        $this->setIntervalUnit($parameters['intervalUnit']);
        $this->setStartDate($parameters['startDate']);
        $this->setTotalOccurrences($parameters['totalOccurrences']);
        if (isset($parameters['trialOccurrences'])) {
            $this->setTrialOccurrences($parameters['trialOccurrences']);
        }
    }

    public function jsonSerialize() {
        $data = [];
        if ($this->hasIntervalLength()) {
            $data['interval']['length'] = $this->getIntervalLength();
        }
        if ($this->hasIntervalUnit()) {
            $data['interval']['unit'] = $this->getIntervalUnit();
        }
        if ($this->hasStartDate()) {
            $data['startDate'] = $this->getStartDate();
        }
        if ($this->hasTotalOccurrences()) {
            $data['totalOccurrences'] = $this->getTotalOccurrences();
        }
        if ($this->hasTrialOccurrences()) {
            $data['trialOccurrences'] = $this->getTrialOccurrences();
        }
        return $data;
    }
    
    protected function setIntervalLength(int $value) {
        if ($value < 7 || $value > 365) {
            throw new InvalidRequestException('Interval Length must be a string, between "7" and "365".');
        }
        $this->intervalLength = (string)$value;
    }
...

The class is instantiated here

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Omnipay\Omnipay;
use Omnipay\AuthorizeNetRecurring;
use Omnipay\AuthorizeNetRecurring\Objects\Schedule;
use Omnipay\Common\CreditCard;

class AuthorizeNetRecurringController extends Controller
{
    private $gateway;

    public function __construct() {
        $this->gateway = Omnipay::create('AuthorizeNetRecurring_Recurring');

        $this->gateway->setAuthName('3KJZb44jR');
        $this->gateway->setTransactionKey('2fFqRA7w22a2G7He');
        $this->gateway->setTestMode(true);
    }

    //
    public function createSubscription(Request $request) {

        $schedule = new Schedule([
            //For a unit of days, use an integer between 7 and 365, inclusive. For a unit of months, use an integer between 1 and 12, inclusive.
            'intervalLength' => '1',
            // use values 'days' or 'months'
            'intervalUnit' => 'months',
            //date in format 'YYYY-mm-dd'
            'startDate' => date("Y-m-d"), //'2020-03-10',
            //To create an ongoing subscription without an end date, set totalOccurrences to "9999".
            'totalOccurrences' => '12',
            //If a trial period is specified, include the number of payments during the trial period in totalOccurrences.
            'trialOccurrences' => '1',
        ]);
...

Then run composer dump-autoload. After running the script the app is still calling the vendor file that causes the error below. Composer changes doesn't seem to work.

Omnipay\Common\Exception\InvalidRequestException
Interval Length must be a string, between "7" and "365".
Omnipay\AuthorizeNetRecurring\Objects\Schedule::setIntervalLength
D:\xampp\htdocs\SBF_app_version1.5\vendor\cimpleo\omnipay-authorizenetrecurring\src\Objects\Schedule.php:56

Thanks


Solution

I think you have to import the overridden class, instead of the original one.

use Cimpleo\Schedule;
// use Omnipay\AuthorizeNetRecurring\Objects\Schedule;

But a better solution to this problem would be to use inheritance:

namespace App\Overrides\Cimpleo;

use Omnipay\AuthorizeNetRecurring\Objects\Schedule as BaseSchedule;

class Schedule extends BaseSchedule
{
    ...
}

And then in the controller you would import the new Schedule class:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Omnipay\Omnipay;
use Omnipay\AuthorizeNetRecurring;
use App\Overrides\Cimpleo\Schedule;
use Omnipay\Common\CreditCard;

class AuthorizeNetRecurringController extends Controller
{
    ...
}

Also you would have to remove the new autoloading instruction and also exclude-from-classmap in composer. Just autoload app directory and it would be enough:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },


Answered By - Pedram Behroozi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing