|
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
|
|
|
$this->generatePaymentObject($attributes); |
|
45
|
|
|
|
|
46
|
|
|
return redirect()->to(base64_decode($request->callback)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param Request $request |
|
51
|
|
|
* @return object |
|
52
|
|
|
* @throws \Exception |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function getPaymentFormInformation(Request $request) |
|
55
|
|
|
{ |
|
56
|
|
|
$data = Cache::get('tzsk_data'); |
|
57
|
|
|
$status_url = Cache::get('tzsk_status_url'); |
|
58
|
|
|
|
|
59
|
|
|
if (empty($status_url)) { |
|
60
|
|
|
throw new \Exception("There is no Redirect URL specified."); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$request->replace($data); |
|
64
|
|
|
$validation = $this->validateRequest($request); |
|
65
|
|
|
$hash = $this->getHashChecksum($request); |
|
66
|
|
|
|
|
67
|
|
|
$redirect = collect(config('payu.redirect'))->map(function($value) use ($request, $status_url) { |
|
68
|
|
|
$seperator = str_contains($value, '?') ? '&' : '?'; |
|
69
|
|
|
return url($value.$seperator.'callback='.urlencode(base64_encode($status_url))); |
|
70
|
|
|
})->all(); |
|
71
|
|
|
|
|
72
|
|
|
$form_fields = array_merge(['key' => config('payu.key'), 'hash' => $hash], |
|
73
|
|
|
array_merge($redirect, $validation)); |
|
74
|
|
|
|
|
75
|
|
|
$prefix = (config('payu.env') == 'secure') ? 'secure' : 'test'; |
|
76
|
|
|
$url = "https://{$prefix}.".config('payu.endpoint'); |
|
77
|
|
|
|
|
78
|
|
|
$payment = (object) ['fields' => $form_fields, 'url' => $url]; |
|
79
|
|
|
|
|
80
|
|
|
return $payment; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param Request $request |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getHashChecksum(Request $request) |
|
88
|
|
|
{ |
|
89
|
|
|
$fields = array_merge(config('payu.required_fields'), config('payu.optional_fields')); |
|
90
|
|
|
|
|
91
|
|
|
$hash_array = []; |
|
92
|
|
|
foreach (collect($fields)->flip()->except(['phone'])->flip() as $field) { |
|
93
|
|
|
$hash_array[] = $request->has($field) ? $request->get($field) : ""; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$checksum_array = array_merge([config('payu.key')], $hash_array, [config('payu.salt')]); |
|
97
|
|
|
|
|
98
|
|
|
$hash = hash('sha512', implode('|', $checksum_array)); |
|
99
|
|
|
|
|
100
|
|
|
return $hash; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param Request $request |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function validateRequest(Request $request) |
|
108
|
|
|
{ |
|
109
|
|
|
list($validation, $data) = $this->getValidationData($request); |
|
110
|
|
|
|
|
111
|
|
|
$validator = Validator::make($request->all(), $validation); |
|
112
|
|
|
|
|
113
|
|
|
if ($validator->fails()) { |
|
114
|
|
|
throw new \InvalidArgumentException($validator->errors()->first()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $data; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param Request $request |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function getValidationData(Request $request) |
|
125
|
|
|
{ |
|
126
|
|
|
$validation = []; |
|
127
|
|
|
$data = []; |
|
128
|
|
|
foreach (config('payu.required_fields') as $item) { |
|
129
|
|
|
$validation[$item] = 'required'; |
|
130
|
|
|
$request->has($item) ? $data[$item] = $request->get($item) : null; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
foreach (config('payu.optional_fields') as $item) { |
|
134
|
|
|
$request->has($item) ? $data[$item] = $request->get($item) : null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
foreach (config('payu.additional_fields') as $item) { |
|
138
|
|
|
$request->has($item) ? $data[$item] = $request->get($item) : null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return [$validation, $data]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Generate the payment model Object for use. |
|
146
|
|
|
* |
|
147
|
|
|
* @param $attributes |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function generatePaymentObject($attributes) |
|
150
|
|
|
{ |
|
151
|
|
|
$model = Cache::get('tzsk_model'); |
|
152
|
|
|
|
|
153
|
|
|
if (config('payu.driver') == 'database') { |
|
154
|
|
|
$payment_instance = PayuPayment::create($attributes); |
|
155
|
|
|
|
|
156
|
|
|
if (!empty($model)) { |
|
157
|
|
|
$payment_instance->fill([ |
|
158
|
|
|
'payable_id' => $model->id, |
|
159
|
|
|
'payable_type' => get_class($model) |
|
160
|
|
|
])->save(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$payment = $payment_instance->id; |
|
164
|
|
|
|
|
165
|
|
|
} else { |
|
166
|
|
|
$payment = $attributes; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
Cache::put('tzsk_payment', $payment, 5); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|