Completed
Push — master ( 49a3ee...2f361f )
by Harish
02:06
created

WebhookController::updateDB()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 17
nc 1
nop 1
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()
0 ignored issues
show
Coding Style introduced by
handleWebhook uses the super-global variable $_POST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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