Laravel Spark扩展Laravel \ Cashier供应商类以覆盖功能

时间:2019-11-09 16:28:18

标签: php laravel laravel-cashier laravel-spark

我希望覆盖Cashier的SubscriptionBuilder::buildPayload()。看起来像这样:

    protected function buildPayload()
    {
        return array_filter([
            'billing_cycle_anchor' => $this->billingCycleAnchor,
            'coupon' => $this->coupon,
            'expand' => ['latest_invoice.payment_intent'],
            'metadata' => $this->metadata,
            'plan' => $this->plan,
            'quantity' => $this->quantity,
            'tax_percent' => $this->getTaxPercentageForPayload(),
            'trial_end' => $this->getTrialEndForPayload(),
            'off_session' => true,
        ]);
    }

我正在为此添加1个参数,即'collection_method': 'invoice'

所以我试图覆盖此功能,以便可以对其进行修改。

我尝试了一些方法,即遵循以下一些答案:

Strategy to override a class in a library installed with Composer

Laravel 5.7 Override vendor class and extend old one

我已将CustomSubscriptionBuilder添加到App\SparkOverrides\

<?php

namespace Laravel\Cashier;

class CustomSubscriptionBuilder extends SubscriptionBuilder
{
    protected function buildPayload()
    {
        dd('here');
    }
}

然后在composer.json中添加:

"autoload": {
        ...
        "files": [
            "app/SparkOverrides/CustomSubscriptionBuilder.php"
        ]
    },

然后我运行了composer dump-autoload。但是,当我尝试创建订阅时,dd()永远不会受到打击。为了使事情变得更加混乱,我在vendor buildPayload()中添加了一个转储并死掉了,但这也没有受到打击。

我觉得我已经接近了,但是缺少一些东西。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

想通了!感谢@lagbox为我指出正确的方向。

我创建了一个CustomBillable类和一个CustomSubscriptionBuilder类。

这两个类都在app/SparkOverrides/

<?php

namespace App\SparkOverrides;

use Laravel\Spark\Billable;

trait CustomBillable
{
    use Billable;

    /**
     * Overriding Cashier's newSubscription to use
     * my CustomSubscriptionBuilder
     */
    public function newSubscription($subscription, $plan)
    {
        return new CustomSubscriptionBuilder($this, $subscription, $plan);
    }
}
<?php

namespace App\SparkOverrides;

use Laravel\Cashier\SubscriptionBuilder;

class CustomSubscriptionBuilder extends SubscriptionBuilder
{
    protected function buildPayload()
    {
        return array_filter([
            'billing_cycle_anchor' => $this->billingCycleAnchor,
            'coupon' => $this->coupon,
            'expand' => ['latest_invoice.payment_intent'],
            'metadata' => $this->metadata,
            'plan' => $this->plan,
            'quantity' => $this->quantity,
            'tax_percent' => $this->getTaxPercentageForPayload(),
            'trial_end' => $this->getTrialEndForPayload(),
            'off_session' => true,
            'collection_method' => 'send_invoice',
            'days_until_due' => 30,
        ]);
    }
}

然后我用Billable上的CustomBillable特征替换了Spark\User特征。

<?php

namespace Laravel\Spark;

use App\SparkOverrides\CustomBillable;
use Illuminate\Support\Str;
use Illuminate\Notifications\RoutesNotifications;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use CustomBillable, HasApiTokens, RoutesNotifications;
    ...
}

应用程序的User扩展了Spark\User。因此,现在调用newSubscription()时,它使用的是CustomBillable的{​​{1}},而后者又使用了newSubscription()

我希望这可以帮助某人。在这个上修补了一段时间。

答案 1 :(得分:0)

我来晚了,但是您可以覆盖newSubscription类中的App\User方法,该方法继承自Laravel\Spark\User类。只是一个想法...