Completed
Push — 2.0 ( 95bb32...281022 )
by Mark
8s
created

PaymentGatewayController::getEndpointUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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  string $action the intended action of the gateway
20
     * @param  string $identifier the unique payment id
21
     * @return string the resulting redirect url
22
     */
23
    public static function getEndpointUrl($action, $identifier)
24
    {
25
        return Director::absoluteURL(
26
            Controller::join_links('paymentendpoint', $identifier, $action)
27
        );
28
    }
29
30
	/**
31
	 * Generate an absolute url for gateways to return to, or send requests to.
32
	 * @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...
33
	 * @param  string             $action      the intended action of the gateway
34
	 * @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...
35
	 * @return string                          the resulting redirect url
36
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use getEndpointUrl
37
	 */
38
	public static function get_endpoint_url($action, $identifier) {
39
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0, use getEndpointUrl');
40
		return self::getEndpointUrl($action, $identifier);
41
	}
42
43
	/**
44
	 * The main action for handling all requests.
45
	 * It will redirect back to the application in all cases,
46
	 * but will not update the Payment/Transaction models if they are not found,
47
	 * or allowed to be updated.
48
	 */
49
	public function index() {
50
		$payment = $this->getPayment();
51
		if (!$payment) {
52
			return $this->httpError(404, _t("Payment.NOTFOUND", "Payment could not be found."));
53
		}
54
55
		//isolate the gateway request message containing success / failure urls
56
		$message = $payment->Messages()
57
			->filter("ClassName", array("PurchaseRequest","AuthorizeRequest"))
58
			->first();
59
60
		$service = PurchaseService::create($payment);
61
62
		//redirect if payment is already a success
63
		if ($payment->isComplete()) {
64
			return $this->redirect($this->getSuccessUrl($message));
65
		}
66
67
		//do the payment update
68
		$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...
69
		switch ($this->request->param('Status')) {
70
			case "complete":
71
				$serviceResponse = $service->completePurchase();
72
				if($serviceResponse->isSuccessful()){
73
					$response = $this->redirect($this->getSuccessUrl($message));
74
				} else {
75
					$response = $this->redirect($this->getFailureUrl($message));
76
				}
77
				break;
78
			case "notify":
79
				$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...
80
				// Allow implementations where no redirect happens,
81
				// since gateway failsafe callbacks might expect a 2xx HTTP response
82
				$response = new SS_HTTPResponse('', 200);
83
				break;
84
			case "cancel":
85
				//TODO: store cancellation message / void payment
86
				$response = $this->redirect($this->getFailureUrl($message));
87
				break;
88
			default:
89
				$response = $this->httpError(404, _t("Payment.INVALIDURL", "Invalid payment url."));
90
		}
91
92
		return $response;
93
	}
94
95
	/**
96
	 * Get the the payment according to the identifer given in the url
97
	 * @return Payament the payment
98
	 */
99
	private function getPayment() {
100
		return Payment::get()
101
				->filter('Identifier', $this->request->param('Identifier'))
102
				->filter('Identifier:not', "")
103
				->first();
104
	}
105
106
	/**
107
	 * Get the success url to redirect to.
108
	 * If a url hasn't been stored, then redirect to base url.
109
	 * @return string the url
110
	 */
111
	private function getSuccessUrl($message) {
112
		return $message->SuccessURL ? $message->SuccessURL : Director::baseURL();
113
	}
114
115
	/**
116
	 * Get the failure url to redirect to.
117
	 * If a url hasn't been stored, then redirect to base url.
118
	 * @return string the url
119
	 */
120
	private function getFailureUrl($message) {
121
		return $message->FailureURL ? $message->FailureURL : Director::baseURL();
122
	}
123
124
}
125