我希望覆盖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()
中添加了一个转储并死掉了,但这也没有受到打击。
我觉得我已经接近了,但是缺少一些东西。感谢您的帮助。
答案 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
类。只是一个想法...