Completed
Branch master (c7a76b)
by Kazi Mainuddin
03:20 queued 01:31
created

PaymentController::validateRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
namespace Tzsk\Payu\Controllers;
3
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Cache;
8
use Illuminate\Support\Facades\Validator;
9
use Tzsk\Payu\Model\PayuPayment;
10
use Tzsk\Payu\ProcessPayment;
11
12
class PaymentController extends Controller
13
{
14
    /**
15
     * Got to payment.
16
     *
17
     * @param Request $request
18
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
19
     */
20
    public function index(Request $request)
21
    {
22
        $payment = $this->getPaymentFormInformation($request);
23
24
        return view('tzsk::payment_form', compact('payment'));
25
    }
26
27
    /**
28
     * After payment it will return here.
29
     *
30
     * @param Request $request
31
     * @return \Illuminate\Http\RedirectResponse
32
     */
33
    public function payment(Request $request)
34
    {
35
        $attributes = $request->only([
36
            'txnid', 'mihpayid', 'firstname', 'email', 'phone', 'amount', 'discount',
37
            'net_amount_debit', 'data', 'status', 'unmappedstatus', 'mode', 'bank_ref_num',
38
            'bankcode', 'cardnum', 'name_on_card', 'issuing_bank', 'card_type'
39
        ]);
40
        $attributes['data'] = json_encode($request->all());
41
        $process = new ProcessPayment($request);
42
        $attributes['status'] = $process->getStatus();
43
44
        if (config('payu.driver') == 'database') {
45
            $payment = PayuPayment::create($attributes)->id;
46
        } else {
47
            $payment = $attributes;
48
        }
49
50
        Cache::put('tzsk_payment', $payment, 5);
51
52
        return redirect()->to($request->callback);
53
    }
54
55
    /**
56
     * @param  Request $request
57
     * @return object
58
     */
59
    protected  function getPaymentFormInformation(Request $request)
60
    {
61
        $data = Cache::get('tzsk_data');
62
        $status_url = Cache::get('tzsk_status_url');
63
64
        $request->replace($data);
65
        $validation = $this->validateRequest($request);
66
        $hash = $this->getHashChecksum($request);
67
68
        $redirect = collect(config('payu.redirect'))->map(function($value) use ($request, $status_url) {
69
            $seperator = str_contains($value, '?') ? '&' : '?';
70
            return url($value.$seperator.'callback='.$status_url);
71
        })->all();
72
73
        $form_fields = array_merge(['key' => config('payu.key'), 'hash' => $hash],
74
            array_merge($redirect, $validation));
75
76
        $prefix = (config('payu.env') == 'secure') ? 'secure' : 'test';
77
        $url = "https://{$prefix}.".config('payu.endpoint');
78
79
        $payment = (object) ['fields' => $form_fields, 'url' => $url];
80
81
        return $payment;
82
    }
83
84
    /**
85
     * @param Request $request
86
     * @return string
87
     */
88
    protected function getHashChecksum(Request $request)
89
    {
90
        $fields = array_merge(config('payu.required_fields'), config('payu.optional_fields'));
91
92
        $hash_array = [];
93
        foreach (collect($fields)->flip()->except(['phone'])->flip() as $field) {
94
            $hash_array[] = $request->has($field) ? $request->get($field) : "";
95
        }
96
97
        $checksum_array = array_merge([config('payu.key')], $hash_array, [config('payu.salt')]);
98
99
        $hash = hash('sha512', implode('|', $checksum_array));
100
101
        return $hash;
102
    }
103
104
    /**
105
     * @param Request $request
106
     * @return array
107
     */
108
    protected function validateRequest(Request $request)
109
    {
110
        lists($validation, $data) = $this->getValidationData($request);
0 ignored issues
show
Bug introduced by
The variable $validation does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
111
112
        $validator = Validator::make($request->all(), $validation);
113
114
        if ($validator->fails()) {
115
            throw new \InvalidArgumentException($validator->errors()->first());
116
        }
117
118
        return $data;
119
    }
120
121
    /**
122
     * @param  Request $request
123
     * @return array
124
     */
125
    protected function getValidationData(Request $request)
126
    {
127
        $validation = [];
128
        $data = [];
129
        foreach (config('payu.required_fields') as $item) {
130
            $validation[$item] = 'required';
131
            $request->has($item) ? $data[$item] = $request->get($item) : null;
132
        }
133
134
        foreach (config('payu.optional_fields') as $item) {
135
            $request->has($item) ? $data[$item] = $request->get($item) : null;
136
        }
137
138
        foreach (config('payu.additional_fields') as $item) {
139
            $request->has($item) ? $data[$item] = $request->get($item) : null;
140
        }
141
142
        return compact($validation, $data);
143
    }
144
145
}
146