-
Notifications
You must be signed in to change notification settings - Fork 9
/
FreeKassa.php
195 lines (162 loc) · 4.66 KB
/
FreeKassa.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Maksa988\FreeKassa;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Maksa988\FreeKassa\Traits\CallerTrait;
use Maksa988\FreeKassa\Traits\ValidateTrait;
class FreeKassa
{
use ValidateTrait;
use CallerTrait;
//
/**
* FreeKassa constructor.
*/
public function __construct()
{
//
}
/**
* @param $amount
* @param $order_id
* @param null $phone
* @param null $email
* @param array $user_parameters
* @return string
*/
public function getPayUrl($amount, $order_id, $phone = null, $email = null, $user_parameters = [])
{
// Url to init payment on FreeKassa
$url = config('freekassa.pay_url');
// Array of url query
$query = [];
// If user parameters array doesn`t empty
// add parameters to payment query
if (! empty($user_parameters)) {
foreach ($user_parameters as $parameter => $value) {
$query['us_'.$parameter] = $value;
}
}
// Project id (merchat id)
$query['m'] = config('freekassa.project_id');
// Amount of payment
$query['oa'] = $amount;
// Order id
$query['o'] = $order_id;
// User phone (optional)
if (! is_null($phone)) {
$query['phone'] = $phone;
}
// User email (optional)
if (! is_null($email)) {
$query['email'] = $email;
}
// Locale for payment form
$query['lang'] = config('freekassa.locale', 'ru');
// Payment currency
if (! is_null(config('freekassa.currency'))) {
$query['i'] = config('freekassa.currency');
}
$query['s'] = $this->getFormSignature(
config('freekassa.project_id'),
$amount,
config('freekassa.secret_key'), $order_id
);
// Merge url ang query and return
return $url.'?'.http_build_query($query);
}
/**
* @param $amount
* @param $order_id
* @param null $phone
* @param null $email
* @param array $user_parameters
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectToPayUrl($amount, $order_id, $phone = null, $email = null, $user_parameters = [])
{
return redirect()->away($this->getPayUrl($amount, $order_id, $phone, $email, $user_parameters));
}
/**
* @param string $ip
* @return bool
*/
public function allowIP($ip)
{
// Allow local ip
if ($ip == '127.0.0.1') {
return true;
}
return in_array($ip, config('freekassa.allowed_ips'));
}
/**
* @param $project_id
* @param $amount
* @param $secret
* @param $order_id
* @return string
*/
public function getFormSignature($project_id, $amount, $secret, $order_id)
{
$hashStr = $project_id.':'.$amount.':'.$secret.':'.$order_id;
return md5($hashStr);
}
/**
* @param $project_id
* @param $amount
* @param $secret
* @param $order_id
* @return string
*/
public function getSignature($project_id, $amount, $secret, $order_id)
{
$hashStr = $project_id.':'.$amount.':'.$secret.':'.$order_id;
return md5($hashStr);
}
/**
* @param Request $request
* @return string
* @throws Exceptions\InvalidPaidOrder
* @throws Exceptions\InvalidSearchOrder
*/
public function handle(Request $request)
{
// Validate request from FreeKassa
if (! $this->validateOrderFromHandle($request)) {
return $this->responseError('validateOrderFromHandle');
}
// Search and get order
$order = $this->callSearchOrder($request);
if (! $order) {
return $this->responseError('searchOrder');
}
// If order already paid return success
if (Str::lower($order['_orderStatus']) === 'paid') {
return $this->responseYES();
}
// PaidOrder - update order info
// if return false then return error
if (! $this->callPaidOrder($request, $order)) {
return $this->responseError('paidOrder');
}
// Order is paid and updated, return success
return $this->responseYES();
}
/**
* @param $error
* @return string
*/
public function responseError($error)
{
return config('freekassa.errors.'.$error, $error);
}
/**
* @return string
*/
public function responseYES()
{
// Must return 'YES' if paid successful
// https://www.free-kassa.ru/docs/api.php#step3
return 'YES';
}
}