2

Cashier で Laravel 4.2 を使用しており、その保護された関数 buildPayload() を変更する必要がありますが、Update を作成するときにコードを壊す可能性があるため、ベンダー ファイルで直接実行したくありません。この関数を自分のロジックでオーバーライドしますか?

現在、次のようにして、コントローラーの1つでキャッシャーを使用しています。

$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 ファイルに直接ではなく、このコードを追加する方法です。

4

2 に答える 2

1

私は自分でそれを行う方法を見つけることができました。この種のことを行うのは初めてです...私の方法が間違っている場合は修正してください!

初め:

次のようにLib\Cashierという名前のフォルダーを作成しました: laravel/app/Lib/Cashier

次に、 BillableTrait.phpNewStripeGateway.phpの2 つのファイルを作成しました。

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;
    }
}

次に、キャッシャーを使用するモデルを次のように編集しました (USE ブロックのみを変更しました)。

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

これを直接実行して、サブスクリプションに税金を設定できるようになりました。

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

それは完全に機能しています!! 私が何か間違ったことをした場合は、変更点に気づいてください.PHPクラス(特性、拡張など)を自分で掘り下げるのは初めてです..

ありがとうございました!

ラファエル

于 2015-08-30T23:07:14.537 に答える
0

getTaxPercentモデルにメソッドを追加userすると、税金も計算されます。Laravel ドキュメント

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最初のオプションは、毎回方法について言及する必要がないため、おそらくはるかに優れています。

于 2015-08-30T17:38:55.980 に答える