tzsk /
payu
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Tzsk\Payu; |
||
| 4 | |||
| 5 | use Illuminate\Support\Facades\Session; |
||
| 6 | use Illuminate\Support\Facades\URL; |
||
| 7 | use Illuminate\Support\Facades\Validator; |
||
| 8 | use Illuminate\Validation\ValidationException; |
||
| 9 | use Illuminate\View\View; |
||
| 10 | use Throwable; |
||
| 11 | use Tzsk\Payu\Components\Form; |
||
| 12 | use Tzsk\Payu\Concerns\Transaction; |
||
| 13 | use Tzsk\Payu\Contracts\HasFormParams; |
||
| 14 | use Tzsk\Payu\Events\TransactionInitiated; |
||
| 15 | use Tzsk\Payu\Gateway\Factory; |
||
| 16 | use Tzsk\Payu\Gateway\Gateway; |
||
| 17 | use Tzsk\Payu\Models\PayuTransaction; |
||
| 18 | |||
| 19 | class Payu implements HasFormParams |
||
| 20 | { |
||
| 21 | protected ?string $destination = null; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 22 | protected ?Gateway $gateway = null; |
||
| 23 | protected ?Transaction $payment = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param string $gateway |
||
| 27 | * @return Payu |
||
| 28 | * @throws Throwable |
||
| 29 | */ |
||
| 30 | public function via(string $gateway): self |
||
| 31 | { |
||
| 32 | $this->gateway = Factory::make($gateway); |
||
| 33 | |||
| 34 | return $this; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function initiate(Transaction $payment): self |
||
| 38 | { |
||
| 39 | $this->payment = $payment; |
||
| 40 | |||
| 41 | return $this; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $url |
||
| 46 | * @return View |
||
| 47 | * @throws Throwable |
||
| 48 | */ |
||
| 49 | public function redirect(string $url): View |
||
| 50 | { |
||
| 51 | Validator::make(compact('url'), ['url' => 'required|url'])->validate(); |
||
| 52 | |||
| 53 | $this->destination = $url; |
||
| 54 | if (! $this->gateway) { |
||
| 55 | $this->via($this->defaultGateway()); |
||
| 56 | } |
||
| 57 | |||
| 58 | Form::inject($payload = $this->prepare()); |
||
| 59 | |||
| 60 | event(new TransactionInitiated($payload['transaction'])); |
||
| 61 | |||
| 62 | return view('payu::form'); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @throws ValidationException |
||
| 67 | */ |
||
| 68 | protected function prepare() |
||
| 69 | { |
||
| 70 | $this->validate(); |
||
| 71 | $fields = $this->fields(); |
||
| 72 | $hash = $this->getHash(); |
||
| 73 | |||
| 74 | $transaction = PayuTransaction::query() |
||
| 75 | ->firstOrNew([ |
||
| 76 | 'transaction_id' => $this->payment->transactionId, |
||
| 77 | ]); |
||
| 78 | $transaction->fill( |
||
| 79 | array_merge($this->morphFields(), [ |
||
| 80 | 'gateway' => $this->gateway, |
||
| 81 | 'body' => $this->payment, |
||
| 82 | 'destination' => $this->destination, |
||
| 83 | 'hash' => $hash, |
||
| 84 | ]) |
||
| 85 | )->save(); |
||
| 86 | |||
| 87 | Session::put('payuTransactionId', $this->payment->transactionId); |
||
| 88 | |||
| 89 | return [ |
||
| 90 | 'endpoint' => $this->gateway->endpoint(), |
||
| 91 | 'fields' => array_merge($fields, compact('hash')), |
||
| 92 | 'transaction' => $transaction, |
||
| 93 | ]; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function capture(): PayuTransaction |
||
| 97 | { |
||
| 98 | return PayuTransaction::locate(Session::get('payuTransactionId')); |
||
| 99 | } |
||
| 100 | |||
| 101 | protected function morphFields() |
||
| 102 | { |
||
| 103 | if (! $this->payment->model) { |
||
| 104 | return []; |
||
| 105 | } |
||
| 106 | |||
| 107 | return [ |
||
| 108 | 'paid_for_id' => $this->payment->model->getKey(), |
||
| 109 | 'paid_for_type' => $this->payment->model->getMorphClass(), |
||
| 110 | ]; |
||
| 111 | } |
||
| 112 | |||
| 113 | protected function defaultGateway() |
||
| 114 | { |
||
| 115 | return config('payu.default'); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function toArray(): array |
||
| 119 | { |
||
| 120 | return [ |
||
| 121 | 'furl' => $this->getSignedRoute('failed'), |
||
| 122 | 'surl' => $this->getSignedRoute('successful'), |
||
| 123 | ]; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function fields(): array |
||
| 127 | { |
||
| 128 | return collect($this->toArray()) |
||
| 129 | ->merge($this->gateway->fields()) |
||
| 130 | ->merge($this->payment->fields()) |
||
| 131 | ->all(); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @throws ValidationException |
||
| 136 | */ |
||
| 137 | public function validate(): array |
||
| 138 | { |
||
| 139 | $this->gateway->validate(); |
||
| 140 | $this->payment->payee->validate(); |
||
| 141 | $this->payment->params->validate(); |
||
| 142 | $this->payment->validate(); |
||
| 143 | |||
| 144 | return Validator::make($this->toArray(), [ |
||
| 145 | 'surl' => 'required|url', |
||
| 146 | 'furl' => 'required|url', |
||
| 147 | ])->validate(); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function getSignedRoute(string $urlType): string |
||
| 151 | { |
||
| 152 | return URL::temporarySignedRoute( |
||
| 153 | 'payu::redirect', |
||
| 154 | now()->addMinutes(30), |
||
| 155 | array_merge(compact('urlType'), ['transaction' => $this->payment->transactionId]) |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | protected function getHash() |
||
| 160 | { |
||
| 161 | return Checksum::with($this->gateway->salt()) |
||
| 162 | ->create($this->fields()); |
||
| 163 | } |
||
| 164 | } |
||
| 165 |