PaymentGatewayController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 101
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSuccessUrl() 0 3 2
A getFailureUrl() 0 3 2
A get_endpoint_url() 0 5 1
C index() 0 45 7
A getPayment() 0 6 1
1
<?php
2
3
/**
4
 * Payment Gateway Controller
5
 *
6
 * This controller handles redirects from gateway servers, and also behind-the-scenes
7
 * requests that gateway servers to notify our application of successful payment.
8
 *
9
 * @package payment
10
 */
11
class PaymentGatewayController extends Controller{
12
13
	private static $allowed_actions = array(
14
		'endpoint'
15
	);
16
17
	/**
18
	 * Generate an absolute url for gateways to return to, or send requests to.
19
	 * @param  GatewayMessage $message message that redirect applies to.
0 ignored issues
show
Bug introduced by
There is no parameter named $message. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
20
	 * @param  string             $action      the intended action of the gateway
21
	 * @param  string             $returnurl   the application url to re-redirect to
0 ignored issues
show
Bug introduced by
There is no parameter named $returnurl. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
22
	 * @return string                          the resulting redirect url
23
	 */
24
	public static function get_endpoint_url($action, $identifier) {
25
		return Director::absoluteURL(
26
			Controller::join_links('paymentendpoint', $identifier, $action)
27
		);
28
	}
29
30
	/**
31
	 * The main action for handling all requests.
32
	 * It will redirect back to the application in all cases,
33
	 * but will not update the Payment/Transaction models if they are not found,
34
	 * or allowed to be updated.
35
	 */
36
	public function index() {		
37
		$payment = $this->getPayment();
38
		if (!$payment) {
39
			return $this->httpError(404, _t("Payment.NOTFOUND", "Payment could not be found."));
40
		}
41
42
		//isolate the gateway request message containing success / failure urls
43
		$message = $payment->Messages()
44
			->filter("ClassName", array("PurchaseRequest","AuthorizeRequest"))
45
			->first();
46
47
		$service = PurchaseService::create($payment);
48
		
49
		//redirect if payment is already a success
50
		if ($payment->isComplete()) {
51
			return $this->redirect($this->getSuccessUrl($message));
52
		}
53
54
		//do the payment update
55
		$response = null;
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
		switch ($this->request->param('Status')) {
57
			case "complete":
58
				$serviceResponse = $service->completePurchase();
59
				if($serviceResponse->isSuccessful()){
60
					$response = $this->redirect($this->getSuccessUrl($message));
61
				} else {
62
					$response = $this->redirect($this->getFailureUrl($message));
63
				}
64
				break;
65
			case "notify":
66
				$serviceResponse = $service->completePurchase();
0 ignored issues
show
Unused Code introduced by
$serviceResponse is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
				// Allow implementations where no redirect happens,
68
				// since gateway failsafe callbacks might expect a 2xx HTTP response
69
				$response = new SS_HTTPResponse('', 200);
70
				break;
71
			case "cancel":
72
				//TODO: store cancellation message / void payment
73
				$response = $this->redirect($this->getFailureUrl($message));
74
				break;
75
			default:
76
				$response = $this->httpError(404, _t("Payment.INVALIDURL", "Invalid payment url."));
77
		}
78
79
		return $response;
80
	}
81
82
	/**
83
	 * Get the the payment according to the identifer given in the url
84
	 * @return Payament the payment
85
	 */
86
	private function getPayment() {
87
		return Payment::get()
88
				->filter('Identifier', $this->request->param('Identifier'))
89
				->filter('Identifier:not', "")
90
				->first();
91
	}
92
93
	/**
94
	 * Get the success url to redirect to.
95
	 * If a url hasn't been stored, then redirect to base url.
96
	 * @return string the url
97
	 */
98
	private function getSuccessUrl($message) {
99
		return $message->SuccessURL ? $message->SuccessURL : Director::baseURL();
100
	}
101
102
	/**
103
	 * Get the failure url to redirect to.
104
	 * If a url hasn't been stored, then redirect to base url.
105
	 * @return string the url
106
	 */
107
	private function getFailureUrl($message) {
108
		return $message->FailureURL ? $message->FailureURL : Director::baseURL();
109
	}
110
111
}
112