我正在使用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 助手?使用路线?哪一个?