【问题标题】:L4.2 Extend/override Cashier to add Tax featureL4.2 扩展/覆盖收银员以添加税收功能
【发布时间】:2015-11-24 18:00:02
【问题描述】:

我将 Laravel 4.2 与 Cashier 一起使用,我需要修改其受保护的函数 buildPayload() 但我不想直接在供应商文件中执行此操作,因为我在编写 Update 时可能会破坏代码...应该如何我继续用我自己的逻辑覆盖这个函数?

我目前在我的一个控制器中使用 Cashier,方法是:

$user->subscription('testplan')
                    ->create(Input::get('stripeToken'), [
                        'email' => 'email@email.com,
                    ]);

但我想添加一个 withTax() 参数... 像这样:

$user->subscription('testplan')
                        ->withTax(10)
                        ->create(Input::get('stripeToken'), [
                            'email' => 'email@email.com,
                        ]);

我已经知道如何直接在 StripeGateway.php 文件中执行此操作,但这是不好的做法...

我知道我需要补充:

protected $taxPercent = 0;

    public function withTax($tax)
    {
        $this->taxPercent = $tax;

        return $this;
    }

    protected function buildPayload()
    {
        $payload = [
            'plan' => $this->plan, 'prorate' => $this->prorate,
            'quantity' => $this->quantity, 'trial_end' => $this->getTrialEndForUpdate(),
            'tax_percent' => $this->taxPercent,
        ];

        return $payload;
    }

我不知道如何不直接在 Cashier Original 文件中添加此代码。

【问题讨论】:

    标签: php laravel laravel-4 laravel-cashier


    【解决方案1】:

    您可以在user 模型中添加getTaxPercent 方法,它也会计算税款。Laravel docs

    遗憾的是,没有选项可以扩展 StripeGateway 类,因为它硬编码在 Billable 特征中,您可以做的是更改这些功能 -

    public function charge($amount, array $options = []) { return (new StripeGateway($this))->charge($amount, $options); } public function subscription($plan = null) { return new StripeGateway($this, $plan); } 在你的user 类中,这里的问题是你永远不知道Billable 会发生什么变化,他们可能会更改函数的名称并添加更多使用StripeGateway 的函数,直到错误将会到来。

    第一个选项可能会好得多,因为您不需要每次都提及 withTax 方法。

    【讨论】:

    • 你知道它是否适用于与 Laravel 4.2 兼容的 Cashier v2.0.7 吗?有关税收的文档适用于 Laravel 5/Cashier v5
    • 似乎没有人会更新Laravel/cashier 2.0 并且从那里的来源看来它似乎不支持这种方法。我看不出有什么不更新到 5.1 的理由,是的,这不是一项简单的任务,但这是您保持最新状态的唯一方法,几乎​​没有什么不错的新功能,而且它比 4.X 更有条理。如果那不是一个选项,那么我会选择覆盖和扩展 StripeGateway 类的方法并添加您的 withTax 方法
    • 我将执行覆盖/扩展“选项”,因为目前传递到 5.1 对我来说并不是一个真正的选择......我将在我的 composer.json 中阻止对 v2.0.7 的更新确保将来不会有任何更改......但我最初的问题是“如何”做到这一点......我知道我必须扩展/覆盖但我不知道如何做到这一点,我不舒服足够用 PHP 知道该怎么做,你能给我简单的细节来告诉我如何做,我会把你的准确答案标记为正确;)谢谢你 Gal
    【解决方案2】:

    我自己找到了怎么做,第一次做这种事情……如果我的方法不对,请纠正我!

    第一:

    我创建了一个名为 Lib\Cashier 的文件夹,如下所示:laravel/app/Lib/Cashier

    然后我创建了 2 个文件:BillableTrait.phpNewStripeGateway.php

    BillableTrait.php 代码:

    <?php namespace Lib\Cashier;
    
    use Laravel\Cashier;
    use Lib\Cashier\NewStripeGateway as StripeGateway;
    
    trait BillableTrait {
        use Cashier\BillableTrait;
    
        /**
         * Get a new billing gateway instance for the given plan.
         *
         * @param  \Laravel\Cashier\PlanInterface|string|null  $plan
         * @return \Laravel\Cashier\StripeGateway
         */
        public function subscription($plan = null)
        {
            if ($plan instanceof PlanInterface) $plan = $plan->getStripeId();
    
            return new StripeGateway($this, $plan);
        }
    
    
    }
    

    对于NewStripeGateway.php

    <?php namespace Lib\Cashier;
    
    use Laravel\Cashier\StripeGateway;
    
    class NewStripeGateway extends StripeGateway {
    
        protected $taxPercent = 0;
    
        public function withTax($tax)
        {
            $this->taxPercent = $tax;
    
            return $this;
        }
    
        protected function buildPayload()
        {
            $payload = [
                'plan' => $this->plan, 'prorate' => $this->prorate,
                'quantity' => $this->quantity, 'trial_end' => $this->getTrialEndForUpdate(),
                'tax_percent' => $this->taxPercent,
            ];
    
            return $payload;
        }
    }
    

    然后我像这样编辑了使用 Cashier 的模型(仅更改了 USE 块):

    use Lib\Cashier\BillableTrait;
    use Laravel\Cashier\BillableInterface;
    

    我现在可以直接执行此操作来为订阅设置税费:

    $user->subscription('testplan')
                            ->withTax(10)
                            ->create(Input::get('stripeToken'), [
                                'email' => 'email@email.com',
                            ]);
    

    效果很好!!如果我做错了什么,请注意我的变化,这是我第一次自己挖掘 PHP 类(特征、扩展等)..

    谢谢!

    拉斐尔

    【讨论】:

    • 是的,我唯一要改变的就是不要让它看起来像原来的 (use Lib\Cashier\NewStripeGateway as StripeGateway;) 我会把它保留为“NewStripeGateway”,然后你会做 new StripeGateway 而不是 @ 987654327@。你也可以在你的用户模型中覆盖subscription 方法[如果你只使用一次Billable trait],但你所做的可能更有条理。 [只是想提一下,它不是覆盖 trait 的唯一方法]
    猜你喜欢
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 2016-02-19
    • 1970-01-01
    相关资源
    最近更新 更多