Ján Ambroz
Ján Ambroz

Reputation: 168

Use constants in request method Laravel

I have this method for doing payments. I try assign all needs values to $pr:

public function request() {
    $pr = new \SporoPayPaymentRequest();
    $pr->pu_predcislo = SLSP_SPOROPAY_PU_PREDCISLO;
    $pr->pu_cislo = SLSP_SPOROPAY_PU_CISLO;
    $pr->suma = $this->amount; // suma (v €)
    $pr->vs = $this->variableSymbol; // variabilný symbol platby
    $pr->url = $this->returnUrl;
    //$pr->mail_notif_att = 3;
    //$pr->email_adr = '[email protected]';

    // ??? bez tychto dvoch parametrov to nejde
    $pr->param = urldecode('abc=defgh');
    $pr->ss = str_pad($this->specificSymbol, 10, 0, STR_PAD_LEFT);
    $pr->SetRedirectUrlBase('SLSP_SPOROPAY_REDIRECTURLBASE');

    if ($pr->Validate()) {
        $pr->SignMessage(SLSP_SPOROPAY_SHAREDSECRET);
        $paymentRequestUrl = $pr->GetRedirectUrl();
        // header("Location: " . $paymentRequestUrl);
        // pre pripad ze nas to nepresmeruje dame userovi moznost kliknut si priamo na link
        return $paymentRequestUrl;
    } else {
        return FALSE;
    }
}`

I send array to this method and this array see like:

array:6 [
  "mode" => "sandbox"
  "SLSP_SPOROPAY_PU_PREDCISLO" => "000000"
  "SLSP_SPOROPAY_PU_CISLO" => "0013662162"
  "SLSP_SPOROPAY_PU_KBANKY" => "0900"
  "SLSP_SPOROPAY_SHAREDSECRET" => "Z3qY08EpvLlAAoMZdnyUdQ=="
  "SLSP_SPOROPAY_REDIRECTURLBASE" => "http://epaymentsimulator.monogram.sk/SLSP_SporoPay.aspx"
]

How can i assign to SLSP_SPOROPAY_PU_PREDCISLO value from my array? Thank you.

Or now i see i have in libraries file "constatns.php" with :

define('SLSP_SPOROPAY_PU_PREDCISLO', '000000');
define('SLSP_SPOROPAY_PU_CISLO', '0013662162');
define('SLSP_SPOROPAY_PU_KBANKY', '0900');
define('SLSP_SPOROPAY_SHAREDSECRET', 'Z3qY08EpvLlAAoMZdnyUdQ==');
define('SLSP_SPOROPAY_REDIRECTURLBASE', 'http://epaymentsimulator.monogram.sk/SLSP_SporoPay.aspx');

but all this constant will be change frequently so maybe i need now set constants in this fill and then call method request. Or talk to me best way for this.

Thank you.

Upvotes: 0

Views: 1073

Answers (1)

user2094178
user2094178

Reputation: 9454

One way of doing it would be using config.

For example, you could place the following in a service provider:

$array = ['SLSP_SPOROPAY' =>
    [
      "mode" => "sandbox",
      "PU_PREDCISLO" => "000000",
      "PU_CISLO" => "0013662162",
      "PU_KBANKY" => "0900",
      "SHAREDSECRET" => "Z3qY08EpvLlAAoMZdnyUdQ==",
      "REDIRECTURLBASE" => "http://epaymentsimulator.monogram.sk/SLSP_SporoPay.aspx"
    ]
];

config($array);

This will allow you to access these settings anywhere in your laravel app using dot notation, such as:

config('SLSP_SPOROPAY.mode');
config('SLSP_SPOROPAY.PU_PREDCISLO'); // and so on

Then of course you can change the naming convention to your liking.

Upvotes: 1

Related Questions