1: <?php
2:
3: namespace Mypos\IPC;
4:
5: /**
6: * Process IPC method: IPCPreAuthorization.
7: * Collect, validate and send API params
8: */
9: class PreAuthorization extends Base
10: {
11: /**
12: * @var Customer
13: */
14: private $url_ok, $url_cancel, $url_notify;
15: private $currency = 'EUR', $note, $orderID, $itemName, $amount;
16:
17: /**
18: * Return PreAuthorization object
19: *
20: * @param Config $cnf
21: */
22: public function __construct(Config $cnf)
23: {
24: $this->setCnf($cnf);
25: }
26:
27: /**
28: * PreAuthorization identifier - must be unique
29: *
30: * @param string $orderID
31: *
32: * @return PreAuthorization
33: */
34: public function setOrderID($orderID)
35: {
36: $this->orderID = $orderID;
37:
38: return $this;
39: }
40:
41: /**
42: * @param string $itemName
43: *
44: * @return PreAuthorization
45: */
46: public function setItemName($itemName)
47: {
48: $this->itemName = $itemName;
49:
50: return $this;
51: }
52:
53: /**
54: * Total amount of the PreAuthorization
55: *
56: * @param float $amount
57: *
58: * @return PreAuthorization
59: */
60: public function setAmount($amount)
61: {
62: $this->amount = $amount;
63:
64: return $this;
65: }
66:
67:
68: /**
69: * Optional note for PreAuthorization
70: *
71: * @param string $note
72: *
73: * @return PreAuthorization
74: */
75: public function setNote($note)
76: {
77: $this->note = $note;
78:
79: return $this;
80: }
81:
82: /**
83: * Merchant Site URL where client comes after unsuccessful payment
84: *
85: * @param string $urlCancel
86: *
87: * @return PreAuthorization
88: */
89: public function setUrlCancel($urlCancel)
90: {
91: $this->url_cancel = $urlCancel;
92:
93: return $this;
94: }
95:
96: /**
97: * Merchant Site URL where IPC posts PreAuthorization Notify requests
98: *
99: * @param string $urlNotify
100: *
101: * @return PreAuthorization
102: */
103: public function setUrlNotify($urlNotify)
104: {
105: $this->url_notify = $urlNotify;
106:
107: return $this;
108: }
109:
110: /**
111: * Initiate API request
112: *
113: * @return boolean
114: * @throws IPC_Exception
115: */
116: public function process()
117: {
118: $this->validate();
119:
120: $this->_addPostParam('IPCmethod', 'IPCPreAuthorization');
121: $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
122: $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
123: $this->_addPostParam('SID', $this->getCnf()->getSid());
124: $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
125: $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
126: $this->_addPostParam('Source', $this->getCnf()->getSource());
127:
128: $this->_addPostParam('ItemName', $this->getItemName());
129:
130: $this->_addPostParam('Currency', $this->getCurrency());
131: $this->_addPostParam('Amount', $this->getAmount());
132:
133: $this->_addPostParam('OrderID', $this->getOrderID());
134: $this->_addPostParam('URL_OK', $this->getUrlOk());
135: $this->_addPostParam('URL_Cancel', $this->getUrlCancel());
136: $this->_addPostParam('URL_Notify', $this->getUrlNotify());
137:
138: $this->_addPostParam('Note', $this->getNote());
139:
140: $this->_addPostParam('ApplicationID', $this->getCnf()->getApplicationID());
141: $this->_addPostParam('PartnerID', $this->getCnf()->getPartnerID());
142:
143: $this->_processHtmlPost();
144:
145: return true;
146: }
147:
148: /**
149: * Validate all set PreAuthorization details
150: *
151: * @return boolean
152: * @throws IPC_Exception
153: */
154: public function validate()
155: {
156: if (!Helper::versionCheck($this->getCnf()->getVersion(), '1.4')) {
157: throw new IPC_Exception('IPCVersion ' . $this->getCnf()->getVersion() . ' does not support IPCPreAuthorization method. Please use 1.4 or above.');
158: }
159:
160: if ($this->getItemName() === null || !is_string($this->getItemName())) {
161: throw new IPC_Exception('Empty or invalid item name.');
162: }
163:
164: if ($this->getUrlCancel() === null || !Helper::isValidURL($this->getUrlCancel())) {
165: throw new IPC_Exception('Invalid Cancel URL');
166: }
167:
168: if ($this->getUrlNotify() === null || !Helper::isValidURL($this->getUrlNotify())) {
169: throw new IPC_Exception('Invalid Notify URL');
170: }
171:
172: if ($this->getUrlOk() === null || !Helper::isValidURL($this->getUrlOk())) {
173: throw new IPC_Exception('Invalid Success URL');
174: }
175:
176: if ($this->getAmount() === null || !Helper::isValidAmount($this->getAmount())) {
177: throw new IPC_Exception('Empty or invalid amount');
178: }
179:
180: if ($this->getCurrency() === null) {
181: throw new IPC_Exception('Invalid currency');
182: }
183:
184: try {
185: $this->getCnf()->validate();
186: } catch (\Exception $ex) {
187: throw new IPC_Exception('Invalid Config details: ' . $ex->getMessage());
188: }
189:
190: if ($this->getCnf()->getVersion() === '1.4.1') {
191: if ($this->getCnf()->getPartnerID() == null) {
192: throw new IPC_Exception('Required parameter: Partner ID');
193: }
194:
195: if ($this->getCnf()->getApplicationID() == null) {
196: throw new IPC_Exception('Required parameter: Application ID');
197: }
198: }
199:
200: return true;
201: }
202:
203: /**
204: * Merchant Site URL where client comes after unsuccessful payment
205: *
206: * @return string
207: */
208: public function getUrlCancel()
209: {
210: return $this->url_cancel;
211: }
212:
213: /**
214: * Merchant Site URL where IPC posts PreAuthorization Notify requests
215: *
216: * @var string
217: */
218: public function getUrlNotify()
219: {
220: return $this->url_notify;
221: }
222:
223: /**
224: * Merchant Site URL where client comes after successful payment
225: *
226: * @return string
227: */
228: public function getUrlOk()
229: {
230: return $this->url_ok;
231: }
232:
233: /**
234: * Merchant Site URL where client comes after successful payment
235: *
236: * @param string $urlOk
237: *
238: * @return PreAuthorization
239: */
240: public function setUrlOk($urlOk)
241: {
242: $this->url_ok = $urlOk;
243:
244: return $this;
245: }
246:
247: /**
248: * ISO-4217 Three letter currency code
249: *
250: * @return string
251: */
252: public function getCurrency()
253: {
254: return $this->currency;
255: }
256:
257: /**
258: * ISO-4217 Three letter currency code
259: *
260: * @param string $currency
261: *
262: * @return PreAuthorization
263: */
264: public function setCurrency($currency)
265: {
266: $this->currency = $currency;
267:
268: return $this;
269: }
270:
271:
272: /**
273: * PreAuthorization identifier
274: *
275: * @return string
276: */
277: public function getOrderID()
278: {
279: return $this->orderID;
280: }
281:
282: /**
283: * @return string
284: */
285: public function getItemName()
286: {
287: return $this->itemName;
288: }
289:
290: /**
291: * Total amount of the PreAuthorization
292: *
293: * @return float
294: */
295: public function getAmount()
296: {
297: return $this->amount;
298: }
299:
300: /**
301: * Optional note to PreAuthorization
302: *
303: * @return string
304: */
305: public function getNote()
306: {
307: return $this->note;
308: }
309:
310: }
311: