首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >L4.2扩展/重写出纳以添加税收功能

L4.2扩展/重写出纳以添加税收功能
EN

Stack Overflow用户
提问于 2015-08-30 16:27:53
回答 2查看 371关注 0票数 2

我和收银员一起使用Laravel4.2,我需要修改它的受保护函数buildPayload(),但是我不想直接在供应商文件中这样做,因为当我编写器更新时我可能会破坏代码.我应该如何用我自己的逻辑来重写这个函数?

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

代码语言:javascript
运行
复制
$user->subscription('testplan')
                    ->create(Input::get('stripeToken'), [
                        'email' => 'email@email.com,
                    ]);

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

代码语言:javascript
运行
复制
$user->subscription('testplan')
                        ->withTax(10)
                        ->create(Input::get('stripeToken'), [
                            'email' => 'email@email.com,
                        ]);

我已经知道如何在StripeGateway.php文件中直接做这件事了,但这是错误的做法.

我知道我需要补充:

代码语言:javascript
运行
复制
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;
    }

我不知道的是如何添加这些代码,而不是直接在收银机原始文件中添加。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-30 23:07:14

我自己想出了办法,这是我第一次做这种事.如果我的方法错了,请纠正我!

第一:

我创建了一个名为laravel/app/Lib/Cashier的文件夹,名为Lib\

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

BillableTrait.php代码:

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
<?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;
    }
}

然后,我编辑了使用类似收银机的模型(只更改了use块):

代码语言:javascript
运行
复制
use Lib\Cashier\BillableTrait;
use Laravel\Cashier\BillableInterface;

我现在可以直接这样做来设置订阅的税收:

代码语言:javascript
运行
复制
$user->subscription('testplan')
                        ->withTax(10)
                        ->create(Input::get('stripeToken'), [
                            'email' => 'email@email.com',
                        ]);

它工作得很好!!如果我做错了什么,请注意更改,这是我第一次自己挖掘PHP类(特性、扩展等)。

谢谢!

拉斐尔

票数 1
EN

Stack Overflow用户

发布于 2015-08-30 17:38:55

您可以在您的getTaxPercent模型中添加user方法,它也将计算税收。

遗憾的是,没有办法扩展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); }类中,这里的问题是您永远不可能知道Billable中会发生什么变化,它们可能会更改函数的名称,并添加少量使用StripeGateway的函数,直到bug出现时,您才会知道。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32298744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档