0

我正在使用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()方法中这个变量不应该保持它的设定值吗?为什么它到达 NULL 或没有值?我是来自 Symfony 的 Laravel 新手。保持 var 值的最佳方法是什么,以便我可以在整个班级中毫无问题地使用它?

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

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

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

4

1 回答 1

1

您的方法很好,因为amountToReload它被声明为一个类属性(尽管您可能想要制作该属性protected,除非您明确想要公开它)。唯一的问题是该方法onAmountChange()需要在之前调用onRun(),才能设置amountToReload.

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

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

有关更多信息,请查看Laravel Helpers 文档

于 2014-11-03T20:37:20.350 回答