|
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. |
|
|
|
|
|
|
20
|
|
|
* @param string $action the intended action of the gateway |
|
21
|
|
|
* @param string $returnurl the application url to re-redirect to |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.