GatewayResponse::setOmnipayResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * Wrapper for omnipay responses, which allow us to customise functionality
5
 *
6
 * @package payment
7
 */
8
class GatewayResponse{
9
10
	/**
11
	 * @var Omnipay\Common\Message\AbstractResponse
12
	 */
13
	private $response;
14
	
15
	/**
16
	 * @var Payment
17
	 */
18
	private $payment;
19
	
20
	/**
21
	 * @var String Success message which can be exposed to the user
22
	 * if the payment was successful. Not persisted in database, so can't
23
	 * be used for offsite payment processing.
24
	 */
25
	private $message;
26
	
27
	/**
28
	 * @var String URL to an endpoint within SilverStripe that can process
29
	 * the response, usually {@link PaymentGatewayController}.
30
	 * This controller might further redirect the user, based on the
31
	 * $SuccessURL and $FailureURL messages in {@link GatewayRequestMessage}.
32
	 */
33
	private $redirect;
34
35
	public function __construct(Payment $payment) {
36
		$this->payment = $payment;
37
	}
38
39
	/**
40
	 * Check if the response indicates a successful gateway action
41
	 *
42
	 * @return boolean
43
	 */
44
	public function isSuccessful() {
45
		return $this->response && $this->response->isSuccessful();
46
	}
47
48
	/**
49
	 * Check if a redirect to an offsite gateway is required.
50
	 * Note that {@link redirect()} will still cause a redirect for onsite gateways,
51
	 * but in this case uses the provided {@link redirect} URL rather than asking the gateway
52
	 * on where to redirect.
53
	 * 
54
	 * @return boolean
55
	 */
56
	public function isRedirect() {
57
		return $this->response && $this->response->isRedirect();
58
	}
59
60
	/**
61
	 * @param Omnipay\Common\Message\AbstractResponse $response
62
	 */
63
	public function setOmnipayResponse(Omnipay\Common\Message\AbstractResponse $response) {
64
		$this->response = $response;
65
66
		return $this;
67
	}
68
69
	/**
70
	 * @return Omnipay\Common\Message\AbstractResponse
71
	 */
72
	public function getOmnipayResponse() {
73
		return $this->response;
74
	}
75
76
	/**
77
	 * See {@link $message}.
78
	 * 
79
	 * @param String $message
80
	 */
81
	public function setMessage($message) {
82
		$this->message = $message;
83
84
		return $this;
85
	}
86
87
	/**
88
	 * @return String
89
	 */
90
	public function getMessage() {
91
		return $this->message;
92
	}
93
94
	/**
95
	 * @return Payment
96
	 */
97
	public function getPayment() {
98
		return $this->payment;
99
	}
100
101
	/**
102
	 * See {@link $redirect}.
103
	 * 
104
	 * @param String $url
105
	 */
106
	public function setRedirectURL($url) {
107
		$this->redirect = $url;
108
109
		return $this;
110
	}
111
112
	/**
113
	 * Get the appropriate redirect url
114
	 */
115
	public function getRedirectURL() {
116
		return $this->redirect;
117
	}
118
119
	/**
120
	 * Do a straight redirect to the denoted {@link redirect} URL if the payment gateway is onsite.
121
	 * If the gateway is offsite, redirect the user to the gateway host instead.
122
	 * This redirect can take two forms: A straight URL with payment data transferred as GET parameters,
123
	 * or a self-submitting form with payment data transferred through POST.
124
	 *
125
	 * @return SS_HTTPResponse
126
	 */
127
	public function redirect() {
128
		if($this->response && $this->response->isRedirect()) {
129
			// Offsite gateway, use payment response to determine redirection,
130
			// either through GET with simep URL, or POST with a self-submitting form.
131
			$redirectOmnipayResponse = $this->response->getRedirectResponse();
132
			if($redirectOmnipayResponse instanceof Symfony\Component\HttpFoundation\RedirectResponse) {
133
				return Controller::curr()->redirect($redirectOmnipayResponse->getTargetUrl());	
134
			} else {
135
				return new SS_HTTPResponse((string)$redirectOmnipayResponse->getContent(), 200);
136
			}		
137
		} else {
138
			// Onsite gateway, redirect to application specific "completed" URL
139
			return Controller::curr()->redirect($this->getRedirectURL());
140
		}
141
		
142
	}
143
144
}
145