35732

Set value for amountToRelad var so it can be accessed on any method on the class

Question:

I'm working in a <a href="http://www.octobercms.com" rel="nofollow">OctoberCMS</a> 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?

Answer1:

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 <a href="http://laravel.com/docs/4.2/helpers#urls" rel="nofollow">Laravel Helpers Docs</a>.

Recommend

  • Search and print matching string
  • Why doesn't copyTo(… PASTE_VALUES) work in the middle of a macro?
  • How to know the return address of a function?
  • How to pass props to react component through routing using react-router?
  • How to make a UserDetailsManager available as a bean
  • How to authenticate user name and password against Active Directory Federation Services (ADFS)?
  • VBScript to check for open process by user
  • How to connect localhost:3306 of mysql container via app container- docker
  • Insert Statement
  • show popover on input validation in angularjs
  • Rails 3, Custom Actions, and HTML request methods
  • Should I use composite primary keys in Grails?
  • How can I sync the html/session used between Django's 'Client' and Selenium's we
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • How can I let users share their location in Bot Framework webchat channel?
  • android Navigation Bar hiding and persantage of usable screen overlap
  • (Tcl/Expect) clear screen after exit
  • Django return user model id with L
  • Tomcat memory Leak
  • How do I signal completion of my dataflow?
  • How does this usort cmp function actually work?
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Rails Find when some params will be blank
  • MongoError: Incorrect arguments
  • Django rest serializer Breaks when data exists
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • JSON with duplicate key names losing information when parsed
  • Display Images one by one with next and previous functionality
  • How to make Safari send if-modified-since header?
  • Upload files with Ajax and Jquery
  • How to pass list parameters for each object using Spring MVC?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Getting error when using KSoap library to consume .NET web services
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Authorize attributes not working in MVC 4
  • Can't mass-assign protected attributes when import data from csv file
  • JaxB to read class hierarchy
  • UserPrincipal.Current returns apppool on IIS