问题
I'm working in a OctoberCMS component and have some problems getting things working. Take a look at this code:
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
}
}
If I have $amountToReload
as public declared var on top of the class and set the value for it in onAmountChange()
method? Then in onRun()
method this var shouldn't keep it's set value? Why it's arriving NULL or without values? I'm newbie to Laravel coming from Symfony. What is the best way to keep the var value so I can use it through the entire class without problems?
As a second part of this post I need to generate a valid route for cancelPayment()
method and this will go on this line:
'returnUrl' => 'www.xyz.com/cancelUrl', // should point to cancelUrl method on this class
How do I create a valid URL with, possibly, parameters, in Laravel? using URL helper? Using Route? Which one?
回答1:
Your approach is good because amountToReload
is declared as a class property (although you might want to make that property protected
unless you explicitly want to make it available publicly). The only issue is that the method onAmountChange()
needs to be called before onRun()
, for it to set the value of amountToReload
.
As for the generating URLs the easiest way is to use url()
:
url('foo/bar', $parameters = array(), $secure = null);
For more info check the Laravel Helpers Docs.
来源:https://stackoverflow.com/questions/26722454/set-value-for-amounttorelad-var-so-it-can-be-accessed-on-any-method-on-the-class