1: | <?php
|
2: |
|
3: | namespace Mypos\IPC;
|
4: |
|
5: | |
6: | |
7: | |
8: |
|
9: | class PurchaseByIcard extends Base
|
10: | {
|
11: | |
12: | |
13: |
|
14: | private $cart;
|
15: |
|
16: | private $url_ok, $url_cancel, $url_notify, $phone, $email;
|
17: | private $currency = 'EUR', $orderID;
|
18: |
|
19: | |
20: | |
21: | |
22: | |
23: |
|
24: | public function __construct(Config $cnf)
|
25: | {
|
26: | $this->setCnf($cnf);
|
27: | }
|
28: |
|
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: |
|
36: | public function setOrderID($orderID)
|
37: | {
|
38: | $this->orderID = $orderID;
|
39: |
|
40: | return $this;
|
41: | }
|
42: |
|
43: | |
44: | |
45: | |
46: | |
47: |
|
48: | public function getPhone()
|
49: | {
|
50: | return $this->phone;
|
51: | }
|
52: |
|
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: |
|
60: | public function setPhone($phone)
|
61: | {
|
62: | $this->phone = $phone;
|
63: |
|
64: | return $this;
|
65: | }
|
66: |
|
67: | |
68: | |
69: | |
70: | |
71: |
|
72: | public function getEmail()
|
73: | {
|
74: | return $this->email;
|
75: | }
|
76: |
|
77: | |
78: | |
79: | |
80: | |
81: | |
82: | |
83: |
|
84: | public function setEmail($email)
|
85: | {
|
86: | $this->email = $email;
|
87: |
|
88: | return $this;
|
89: | }
|
90: |
|
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: |
|
98: | public function setUrlCancel($urlCancel)
|
99: | {
|
100: | $this->url_cancel = $urlCancel;
|
101: |
|
102: | return $this;
|
103: | }
|
104: |
|
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: |
|
112: | public function setUrlNotify($urlNotify)
|
113: | {
|
114: | $this->url_notify = $urlNotify;
|
115: |
|
116: | return $this;
|
117: | }
|
118: |
|
119: |
|
120: | |
121: | |
122: | |
123: | |
124: | |
125: |
|
126: | public function process()
|
127: | {
|
128: | $this->validate();
|
129: |
|
130: | $this->_addPostParam('IPCmethod', 'IPCPurchaseByIcard');
|
131: | $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
|
132: | $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
|
133: | $this->_addPostParam('SID', $this->getCnf()->getSid());
|
134: | $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
|
135: | $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
|
136: | $this->_addPostParam('Source', $this->getCnf()->getSource());
|
137: |
|
138: | $this->_addPostParam('Currency', $this->getCurrency());
|
139: | $this->_addPostParam('Amount', $this->cart->getTotal());
|
140: |
|
141: | $this->_addPostParam('OrderID', $this->getOrderID());
|
142: | $this->_addPostParam('URL_OK', $this->getUrlOk());
|
143: | $this->_addPostParam('URL_Cancel', $this->getUrlCancel());
|
144: | $this->_addPostParam('URL_Notify', $this->getUrlNotify());
|
145: |
|
146: | $this->_addPostParam('CustomerEmail', $this->getEmail());
|
147: | $this->_addPostParam('CustomerPhone', $this->getPhone());
|
148: |
|
149: | $this->_addPostParam('CartItems', $this->cart->getItemsCount());
|
150: | $items = $this->cart->getCart();
|
151: | $i = 1;
|
152: | foreach ($items as $v) {
|
153: | $this->_addPostParam('Article_' . $i, $v['name']);
|
154: | $this->_addPostParam('Quantity_' . $i, $v['quantity']);
|
155: | $this->_addPostParam('Price_' . $i, $v['price']);
|
156: | $this->_addPostParam('Amount_' . $i, $v['price'] * $v['quantity']);
|
157: | $this->_addPostParam('Currency_' . $i, $this->getCurrency());
|
158: | $i++;
|
159: | }
|
160: | $this->_processHtmlPost();
|
161: |
|
162: | return true;
|
163: | }
|
164: |
|
165: | |
166: | |
167: | |
168: | |
169: | |
170: |
|
171: | public function validate()
|
172: | {
|
173: |
|
174: | if ($this->getUrlCancel() === null || !Helper::isValidURL($this->getUrlCancel())) {
|
175: | throw new IPC_Exception('Invalid Cancel URL');
|
176: | }
|
177: |
|
178: | if ($this->getUrlNotify() === null || !Helper::isValidURL($this->getUrlNotify())) {
|
179: | throw new IPC_Exception('Invalid Notify URL');
|
180: | }
|
181: |
|
182: | if ($this->getUrlOk() === null || !Helper::isValidURL($this->getUrlOk())) {
|
183: | throw new IPC_Exception('Invalid Success URL');
|
184: | }
|
185: |
|
186: | if ($this->getCurrency() === null) {
|
187: | throw new IPC_Exception('Invalid currency');
|
188: | }
|
189: |
|
190: | if ($this->getEmail() == null && $this->getPhone() == null ) {
|
191: | throw new IPC_Exception('Must provide customer email either phone');
|
192: | }
|
193: |
|
194: | if ($this->getEmail() != null && !Helper::isValidEmail($this->getEmail())) {
|
195: | throw new IPC_Exception('Invalid Email');
|
196: | }
|
197: |
|
198: | try {
|
199: | $this->getCnf()->validate();
|
200: | } catch (\Exception $ex) {
|
201: | throw new IPC_Exception('Invalid Config details: ' . $ex->getMessage());
|
202: | }
|
203: |
|
204: | if ($this->getCart() === null) {
|
205: | throw new IPC_Exception('Missing Cart details');
|
206: | }
|
207: |
|
208: | try {
|
209: | $this->getCart()->validate();
|
210: | } catch (\Exception $ex) {
|
211: | throw new IPC_Exception('Invalid Cart details: ' . $ex->getMessage());
|
212: | }
|
213: |
|
214: | return true;
|
215: | }
|
216: |
|
217: | |
218: | |
219: | |
220: | |
221: |
|
222: | public function getUrlCancel()
|
223: | {
|
224: | return $this->url_cancel;
|
225: | }
|
226: |
|
227: | |
228: | |
229: | |
230: | |
231: |
|
232: | public function getUrlNotify()
|
233: | {
|
234: | return $this->url_notify;
|
235: | }
|
236: |
|
237: | |
238: | |
239: | |
240: | |
241: |
|
242: | public function getUrlOk()
|
243: | {
|
244: | return $this->url_ok;
|
245: | }
|
246: |
|
247: | |
248: | |
249: | |
250: | |
251: | |
252: | |
253: |
|
254: | public function setUrlOk($urlOk)
|
255: | {
|
256: | $this->url_ok = $urlOk;
|
257: |
|
258: | return $this;
|
259: | }
|
260: |
|
261: | |
262: | |
263: | |
264: | |
265: |
|
266: | public function getCurrency()
|
267: | {
|
268: | return $this->currency;
|
269: | }
|
270: |
|
271: | |
272: | |
273: | |
274: | |
275: | |
276: | |
277: |
|
278: | public function setCurrency($currency)
|
279: | {
|
280: | $this->currency = $currency;
|
281: |
|
282: | return $this;
|
283: | }
|
284: |
|
285: | |
286: | |
287: | |
288: | |
289: |
|
290: | public function getCart()
|
291: | {
|
292: | return $this->cart;
|
293: | }
|
294: |
|
295: | |
296: | |
297: | |
298: | |
299: | |
300: | |
301: |
|
302: | public function setCart(Cart $cart)
|
303: | {
|
304: | $this->cart = $cart;
|
305: |
|
306: | return $this;
|
307: | }
|
308: |
|
309: | |
310: | |
311: | |
312: | |
313: |
|
314: | public function getOrderID()
|
315: | {
|
316: | return $this->orderID;
|
317: | }
|
318: | }
|
319: | |