1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lubusin\Mojo\Controllers; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use Illuminate\Routing\Controller; |
7
|
|
|
use Lubusin\Mojo\Models\MojoPaymentDetails; |
8
|
|
|
|
9
|
|
|
class WebhookController extends Controller |
10
|
|
|
{ |
11
|
|
|
public function handleWebhook() |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
$data = $_POST; |
14
|
|
|
$mac_provided = $data['mac']; |
15
|
|
|
unset($data['mac']); |
16
|
|
|
$ver = explode('.', phpversion()); |
17
|
|
|
$major = (int) $ver[0]; |
18
|
|
|
$minor = (int) $ver[1]; |
19
|
|
|
if($major >= 5 && $minor >= 4){ |
20
|
|
|
ksort($data, SORT_STRING | SORT_FLAG_CASE); |
21
|
|
|
} |
22
|
|
|
else{ |
23
|
|
|
uksort($data, 'strcasecmp'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$mac_calculated = hash_hmac("sha1", implode("|", $data), config('laravelmojo.salt')); |
27
|
|
|
if($mac_provided == $mac_calculated){ |
28
|
|
|
if($data['status'] == "Credit") { |
29
|
|
|
$this->updateDB($data); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
else { |
33
|
|
|
$this->updateDB($data); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
else{ |
37
|
|
|
echo "MAC mismatch"; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function updateDB($data) |
42
|
|
|
{ |
43
|
|
|
$user = User::where('email',$data['buyer'])->first(); |
44
|
|
|
$user_id = $user->id; |
45
|
|
|
MojoPaymentDetails::create(['user_id' => $user_id, |
46
|
|
|
'buyer_email' => $data['buyer'], |
47
|
|
|
'buyer_name' => $data['buyer_name'], |
48
|
|
|
'buyer_phone' => $data['buyer_phone'], |
49
|
|
|
'currency' => $data['currency'], |
50
|
|
|
'amount' => $data['amount'], |
51
|
|
|
'fees' => $data['fees'], |
52
|
|
|
'longurl' => $data['longurl'], |
53
|
|
|
'payment_id' => $data['payment_id'], |
54
|
|
|
'payment_request_id' => $data['payment_request_id'], |
55
|
|
|
'purpose' => $data['purpose'], |
56
|
|
|
'shorturl' => $data['shorturl'], |
57
|
|
|
'request_status' => 'completed', |
58
|
|
|
'payment_status' => $data['status'], |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: