Completed
Push — master ( 8bb85e...49a3ee )
by Harish
04:35
created

WebhookController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 67
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B handleWebhook() 0 64 5
1
<?php
2
3
namespace Lubusin\Mojo\Controllers;
4
5
use Exception;
6
use App\User;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Controller;
9
use Lubusin\Mojo\Models\MojoPaymentDetails;
10
11
class WebhookController extends Controller
12
{
13
	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...
14
	{		
15
		$data = $_POST;
16
		$mac_provided = $data['mac'];
17
		unset($data['mac']);
18
		$ver = explode('.', phpversion());
19
		$major = (int) $ver[0];
20
		$minor = (int) $ver[1];
21
		if($major >= 5 and $minor >= 4){
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
22
		     ksort($data, SORT_STRING | SORT_FLAG_CASE);
23
		}
24
		else{
25
		     uksort($data, 'strcasecmp');
26
		}
27
		
28
		$mac_calculated = hash_hmac("sha1", implode("|", $data), config('laravelmojo.salt'));
29
		if($mac_provided == $mac_calculated){
30
		    if($data['status'] == "Credit"){
31
32
		    $user = User::where('email',$data['buyer'])->first();
33
			$user_id = $user->id;
34
	    	MojoPaymentDetails::create(['user_id' => $user_id,
35
									   'buyer_email' => $data['buyer'],
36
									   'buyer_name' => $data['buyer_name'],
37
									   'buyer_phone' => $data['buyer_phone'],
38
									   'currency' => $data['currency'],
39
									   'amount' => $data['amount'],
40
									   'fees' => $data['fees'],
41
									   'longurl' => $data['longurl'],
42
									   'payment_id' => $data['payment_id'],
43
									   'payment_request_id' => $data['payment_request_id'],
44
									   'purpose' => $data['purpose'],
45
									   'shorturl' => $data['shorturl'],
46
									   'request_status' => 'completed',
47
									   'payment_status' => $data['status'],
48
									 ]);
49
		    }
50
51
		    else{
52
		        
53
		    	$user = User::where('email',$data['buyer'])->first();
54
				$user_id = $user->id;
55
		    	MojoPaymentDetails::create(['user_id' => $user_id,
56
										   'buyer_email' => $data['buyer'],
57
										   'buyer_name' => $data['buyer_name'],
58
										   'buyer_phone' => $data['buyer_phone'],
59
										   'currency' => $data['currency'],
60
										   'amount' => $data['amount'],
61
										   'fees' => $data['fees'],
62
										   'longurl' => $data['longurl'],
63
										   'payment_id' => $data['payment_id'],
64
										   'payment_request_id' => $data['payment_request_id'],
65
										   'purpose' => $data['purpose'],
66
										   'shorturl' => $data['shorturl'],
67
										   'request_status' => 'completed',
68
										   'payment_status' => $data['status'],
69
										 ]);
70
71
		    }
72
		}
73
		else{
74
		    echo "MAC mismatch";
75
		}
76
	}
77
}
78