[英]Laravel Spark extend Laravel\Cashier vendor class to override function
I'm looking to override Cashier's SubscriptionBuilder::buildPayload()
.我正在寻找覆盖 Cashier 的SubscriptionBuilder::buildPayload()
。 Which looks like:看起来像:
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,
]);
}
I'm looking to add 1 param to this which is 'collection_method': 'invoice'
我希望为此添加 1 个参数,即'collection_method': 'invoice'
So I'm trying to override this function so I can modify it.所以我试图覆盖这个 function 所以我可以修改它。
I tried a few things, namely following some of the below answers:我尝试了一些事情,即遵循以下一些答案:
Strategy to override a class in a library installed with Composer 在使用 Composer 安装的库中覆盖 class 的策略
Laravel 5.7 Override vendor class and extend old one Laravel 5.7 覆盖供应商 class 并扩展旧的
I have added my CustomSubscriptionBuilder
in App\SparkOverrides\
我在App\SparkOverrides\
添加了我的CustomSubscriptionBuilder
<?php
namespace Laravel\Cashier;
class CustomSubscriptionBuilder extends SubscriptionBuilder
{
protected function buildPayload()
{
dd('here');
}
}
Then in composer.json
I have added:然后在composer.json
我添加了:
"autoload": {
...
"files": [
"app/SparkOverrides/CustomSubscriptionBuilder.php"
]
},
I have then run composer dump-autoload
.然后我运行了composer dump-autoload
。 But then when I try and create a subscription the dd()
never gets hit.但是当我尝试创建订阅时, dd()
永远不会被击中。 To make matters more confusing, I have added a dump and die to the vendor
buildPayload()
and that isn't getting hit either.为了让事情变得更加混乱,我在vendor
buildPayload()
中添加了一个转储和死机,而且它也没有受到影响。
I feel like I'm close but am missing something.我觉得我很接近,但我错过了一些东西。 Thanks for any help.谢谢你的帮助。
Figured it out.弄清楚了。 Thank you to @lagbox for pointing me in the right direction.感谢@lagbox 为我指明了正确的方向。
I created a CustomBillable
class and a CustomSubscriptionBuilder
class.我创建了一个CustomBillable
class 和一个CustomSubscriptionBuilder
class。
Both of these classes are in app/SparkOverrides/
这两个类都在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,
]);
}
}
Then I replaced the Billable
trait with my CustomBillable
trait on the Spark\User
.然后我用Spark\User
上的CustomBillable
特征替换了Billable
特征。
<?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;
...
}
The App's User
extends Spark\User
.应用程序的User
扩展Spark\User
。 So now when newSubscription()
is called it uses CustomBillable
's newSubscription()
which in turn uses the CustomSubscriptionBuilder
.所以现在当newSubscription()
被调用时,它使用CustomBillable
的newSubscription()
,而后者又使用CustomSubscriptionBuilder
。
I hope this helps someone out.我希望这可以帮助某人。 Been tinkering for a while on this one.一直在修修补补这个。
I'm late, but you could just override the newSubscription
method in the App\User
class which inherits it from the Laravel\Spark\User
class.我迟到了,但你可以重写App\User
class 中的newSubscription
方法,该方法继承自Laravel\Spark\User
class。 Just a thought...只是一个想法...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.