1 <?php
2
3 namespace Mypos\IPC;
4
5 6 7 8
9 class Purchase extends Base
10 {
11
12 const PURCHASE_TYPE_FULL = 1;
13 const PURCHASE_TYPE_SIMPLIFIED_CALL = 2;
14 const PURCHASE_TYPE_SIMPLIFIED_PAYMENT_PAGE = 3;
15
16 const CARD_TOKEN_REQUSET_NONE = 0;
17 const CARD_TOKEN_REQUSET_ONLY_STORE = 1;
18 const CARD_TOKEN_REQUSET_PAY_AND_STORE = 2;
19
20 21 22
23 private $cart;
24
25 26 27
28 private $customer;
29 private $url_ok, $url_cancel, $url_notify;
30 private $currency = 'EUR', $note, $orderID, $cardTokenRequest, $paymentParametersRequired;
31
32 33 34 35
36 public function __construct(Config $cnf)
37 {
38 $this->setCnf($cnf);
39 }
40
41 42 43 44 45
46 public function setOrderID($orderID)
47 {
48 $this->orderID = $orderID;
49 return $this;
50 }
51
52 53 54 55
56 public function getOrderID()
57 {
58 return $this->orderID;
59 }
60
61 62 63 64 65
66 public function setNote($note)
67 {
68 $this->note = $note;
69 return $this;
70 }
71
72 73 74 75
76 public function getNote()
77 {
78 return $this->note;
79 }
80
81 82 83 84 85
86 public function setCurrency($currency)
87 {
88 $this->currency = $currency;
89 return $this;
90 }
91
92 93 94 95
96 public function getCurrency()
97 {
98 return $this->currency;
99 }
100
101 102 103 104 105
106 public function setCart(Cart $cart)
107 {
108 $this->cart = $cart;
109 return $this;
110 }
111
112 113 114 115
116 public function getCart()
117 {
118 return $this->cart;
119 }
120
121 122 123 124 125
126 public function setCustomer(Customer $customer)
127 {
128 $this->customer = $customer;
129 return $this;
130 }
131
132 133 134
135 public function getCustomer()
136 {
137 return $this->customer;
138 }
139
140 141 142 143 144
145 public function setUrlOk($urlOk)
146 {
147 $this->url_ok = $urlOk;
148 return $this;
149 }
150
151 152 153 154
155 public function getUrlOk()
156 {
157 return $this->url_ok;
158 }
159
160 161 162 163 164
165 public function setUrlCancel($urlCancel)
166 {
167 $this->url_cancel = $urlCancel;
168 return $this;
169 }
170
171 172 173 174
175 public function getUrlCancel()
176 {
177 return $this->url_cancel;
178 }
179
180 181 182 183 184
185 public function setUrlNotify($urlNotify)
186 {
187 $this->url_notify = $urlNotify;
188 return $this;
189 }
190
191 192 193 194
195 public function getUrlNotify()
196 {
197 return $this->url_notify;
198 }
199
200 201 202 203
204 public function getCardTokenRequest()
205 {
206 return $this->cardTokenRequest;
207 }
208
209 210 211 212
213 public function setCardTokenRequest($cardTokenRequest)
214 {
215 $this->cardTokenRequest = $cardTokenRequest;
216 }
217
218 219 220 221
222 public function getPaymentParametersRequired()
223 {
224 return $this->paymentParametersRequired;
225 }
226
227 228 229 230
231 public function setPaymentParametersRequired($paymentParametersRequired)
232 {
233 $this->paymentParametersRequired = $paymentParametersRequired;
234 }
235
236
237 238 239 240
241 public function process()
242 {
243 $this->validate();
244
245 $this->_addPostParam('IPCmethod', 'IPCPurchase');
246 $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
247 $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
248 $this->_addPostParam('SID', $this->getCnf()->getSid());
249 $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
250 $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
251 $this->_addPostParam('Source', Defines::SOURCE_PARAM);
252
253 $this->_addPostParam('Currency', $this->getCurrency());
254 if (!$this->isNoCartPurchase()) {
255 $this->_addPostParam('Amount', $this->cart->getTotal());
256 }
257
258 $this->_addPostParam('OrderID', $this->getOrderID());
259 $this->_addPostParam('URL_OK', $this->getUrlOk());
260 $this->_addPostParam('URL_Cancel', $this->getUrlCancel());
261 $this->_addPostParam('URL_Notify', $this->getUrlNotify());
262
263 if ($this->getPaymentParametersRequired() == self::PURCHASE_TYPE_FULL) {
264 $this->_addPostParam('customeremail', $this->getCustomer()->getEmail());
265 $this->_addPostParam('customerphone', $this->getCustomer()->getPhone());
266 $this->_addPostParam('customerfirstnames', $this->getCustomer()->getFirstName());
267 $this->_addPostParam('customerfamilyname', $this->getCustomer()->getLastName());
268 $this->_addPostParam('customercountry', $this->getCustomer()->getCountry());
269 $this->_addPostParam('customercity', $this->getCustomer()->getCity());
270 $this->_addPostParam('customerzipcode', $this->getCustomer()->getZip());
271 $this->_addPostParam('customeraddress', $this->getCustomer()->getAddress());
272 }
273
274
275 if ($this->getPaymentParametersRequired() != self::PURCHASE_TYPE_SIMPLIFIED_CALL) {
276 $this->_addPostParam('Note', $this->getNote());
277 if (!$this->isNoCartPurchase()) {
278 $this->_addPostParam('CartItems', $this->cart->getItemsCount());
279 $items = $this->cart->getCart();
280
281 $i = 1;
282 foreach ($items as $v) {
283 $this->_addPostParam('Article_' . $i, $v['name']);
284 $this->_addPostParam('Quantity_' . $i, $v['quantity']);
285 $this->_addPostParam('Price_' . $i, $v['price']);
286 $this->_addPostParam('Amount_' . $i, $v['price'] * $v['quantity']);
287 $this->_addPostParam('Currency_' . $i, $this->getCurrency());
288 $i++;
289 }
290 }
291 }
292
293 $this->_addPostParam('CardTokenRequest', $this->getCardTokenRequest());
294 $this->_addPostParam('PaymentParametersRequired', $this->getPaymentParametersRequired());
295
296 $this->_processHtmlPost();
297 return true;
298 }
299
300 301 302 303 304
305 public function validate()
306 {
307
308 if ($this->getUrlCancel() === null || !Helper::isValidURL($this->getUrlCancel())) {
309 throw new IPC_Exception('Invalid Cancel URL');
310 }
311
312 if ($this->getUrlNotify() === null || !Helper::isValidURL($this->getUrlNotify())) {
313 throw new IPC_Exception('Invalid Notify URL');
314 }
315
316 if ($this->getUrlOk() === null || !Helper::isValidURL($this->getUrlOk())) {
317 throw new IPC_Exception('Invalid Success URL');
318 }
319
320 if ($this->getCardTokenRequest() === null || !in_array($this->getCardTokenRequest(), array(self::CARD_TOKEN_REQUSET_NONE, self::CARD_TOKEN_REQUSET_ONLY_STORE, self::CARD_TOKEN_REQUSET_PAY_AND_STORE))) {
321 throw new IPC_Exception('Invalid value provided for CardTokenRequest params');
322 }
323
324 if ($this->getPaymentParametersRequired() === null || !in_array($this->getPaymentParametersRequired(), array(self::PURCHASE_TYPE_FULL, self::PURCHASE_TYPE_SIMPLIFIED_CALL, self::PURCHASE_TYPE_SIMPLIFIED_PAYMENT_PAGE))) {
325 throw new IPC_Exception('Invalid value provided for PaymentParametersRequired params');
326 }
327
328 if ($this->getCurrency() === null || strpos(Defines::AVL_CURRENCIES, $this->getCurrency()) === false) {
329 throw new IPC_Exception('Invalid currency');
330 }
331
332 try {
333 $this->getCnf()->validate();
334 } catch (Exception $ex) {
335 throw new IPC_Exception('Invalid Config details: ' . $ex->getMessage());
336 }
337
338 if (!$this->isNoCartPurchase()) {
339 try {
340 $this->getCart()->validate();
341 } catch (Exception $ex) {
342 throw new IPC_Exception('Invalid Cart details: ' . $ex->getMessage());
343 }
344 }
345
346 if ($this->getPaymentParametersRequired() == self::PURCHASE_TYPE_FULL) {
347 try {
348 $this->getCustomer()->validate($this->getPaymentParametersRequired());
349 } catch (Exception $ex) {
350 throw new IPC_Exception('Invalid Customer details: ' . $ex->getMessage());
351 }
352 }
353
354 return true;
355 }
356
357 358 359 360
361 private function isNoCartPurchase()
362 {
363 return $this->getCardTokenRequest() == self::CARD_TOKEN_REQUSET_ONLY_STORE;
364 }
365 }
366