This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

reynierpm
reynierpm

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-1 of 1

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.