设置amountToRelad var的值,以便可以在类的任何方法上访问它

时间:2014-11-03 20:19:18

标签: php laravel laravel-4 octobercms

我正在使用OctoberCMS组件,并且在使工作正常时遇到一些问题。看看这段代码:

class Payment extends ComponentBase
{
    /**
     * This hold the amount with PayPal fee and discount applied and pass back to template
     * @var float
     */
    public $amountToReload;

    public function onAmountChange()
    {
        $amount = post('amount');

        if (empty($amount)) {
            throw new \Exception(sprintf('Por favor introduzca un valor.'));
        }

        $this->amountToReload = round($amount - ($amount * (float) Settings::get('ppal_fee') - (float) Settings::get('ppal_discount')), 2);

        return ['#amountToReload' => $this->amountToReload];
    }

    public function onRun()
    {
        $step = $this->param('step');
        $sandboxMode = Settings::get('sandbox_enabled');

        switch ($step) {
            case "step2":

                echo $this->amountToReload;

                $params = [
                    'username' => $sandboxMode ? Settings::get('ppal_api_username_sandbox') : Settings::get('ppal_api_username'),
                    'password' => $sandboxMode ? Settings::get('ppal_api_password_sandbox') : Settings::get('ppal_api_password'),
                    'signature' => $sandboxMode ? Settings::get('ppal_api_signature_sandbox') : Settings::get('ppal_api_signature'),
                    'testMode' => $sandboxMode,
                    'amount' => $this->amountToReload,
                    'cancelUrl' => 'www.xyz.com/returnUrl', // should point to returnUrl method on this class
                    'returnUrl' => 'www.xyz.com/cancelUrl', // should point to cancelUrl method on this class
                    'currency' => 'USD'
                ];

                $response = Omnipay::purchase($params)->send();

                if ($response->isSuccessful()) {
                    // payment was successful: update database
                    print_r($response);
                } elseif ($response->isRedirect()) {
                    // redirect to offsite payment gateway
                    return $response->getRedirectResponse();
                } else {
                    // payment failed: display message to customer
                    echo $response->getMessage();
                }

                break;

            default:
                break;
        }

        $this->page['step'] = $step;
    }

    public function cancelPayment()
    {
       // handle payment cancel   
    }
}

如果我在类的顶部有$amountToReload作为公共声明var并在onAmountChange()方法中设置它的值?然后在onRun()方法中,这个var不应该保持它的设定值?为什么它到达NULL或没有值?我是来自Symfony的Laravel的新手。保持var值的最佳方法是什么,以便我可以在整个班级中使用它而不会出现问题?

作为这篇文章的第二部分,我需要为cancelPayment()方法生成一个有效的路由,这将在这一行:

'returnUrl' => 'www.xyz.com/cancelUrl', // should point to cancelUrl method on this class

如何在Laravel中创建可能包含参数的有效URL?使用URL帮助?使用路线?哪一个?

1 个答案:

答案 0 :(得分:1)

您的方法很好,因为amountToReload被声明为类属性(尽管您可能希望创建该属性protected,除非您明确要将其公开提供)。唯一的问题是需要在onAmountChange()之前调用方法onRun(),以便设置amountToReload的值。

至于生成网址,最简单的方法是使用url()

url('foo/bar', $parameters = array(), $secure = null);

有关详细信息,请查看Laravel Helpers Docs