1: <?php
2:
3: namespace Mypos\IPC;
4:
5: /**
6: * Process IPC method: IPCPurchaseByIcard.
7: * Collect, validate and send API params
8: */
9: class PurchaseByIcard extends Base
10: {
11: /**
12: * @var Cart
13: */
14: private $cart;
15:
16: private $url_ok, $url_cancel, $url_notify, $phone, $email;
17: private $currency = 'EUR', $orderID;
18:
19: /**
20: * Return purchase object
21: *
22: * @param Config $cnf
23: */
24: public function __construct(Config $cnf)
25: {
26: $this->setCnf($cnf);
27: }
28:
29: /**
30: * Purchase identifier - must be unique
31: *
32: * @param string $orderID
33: *
34: * @return PurchaseByIcard
35: */
36: public function setOrderID($orderID)
37: {
38: $this->orderID = $orderID;
39:
40: return $this;
41: }
42:
43: /**
44: * Customer Phone number
45: *
46: * @return string
47: */
48: public function getPhone()
49: {
50: return $this->phone;
51: }
52:
53: /**
54: * Customer Phone number
55: *
56: * @param string $phone
57: *
58: * @return PurchaseByIcard
59: */
60: public function setPhone($phone)
61: {
62: $this->phone = $phone;
63:
64: return $this;
65: }
66:
67: /**
68: * Customer Email address
69: *
70: * @return string
71: */
72: public function getEmail()
73: {
74: return $this->email;
75: }
76:
77: /**
78: * Customer Email address
79: *
80: * @param string $email
81: *
82: * @return PurchaseByIcard
83: */
84: public function setEmail($email)
85: {
86: $this->email = $email;
87:
88: return $this;
89: }
90:
91: /**
92: * Merchant Site URL where client comes after unsuccessful payment
93: *
94: * @param string $urlCancel
95: *
96: * @return PurchaseByIcard
97: */
98: public function setUrlCancel($urlCancel)
99: {
100: $this->url_cancel = $urlCancel;
101:
102: return $this;
103: }
104:
105: /**
106: * Merchant Site URL where IPC posts Purchase Notify requests
107: *
108: * @param string $urlNotify
109: *
110: * @return PurchaseByIcard
111: */
112: public function setUrlNotify($urlNotify)
113: {
114: $this->url_notify = $urlNotify;
115:
116: return $this;
117: }
118:
119:
120: /**
121: * Initiate API request
122: *
123: * @return boolean
124: * @throws IPC_Exception
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: * Validate all set purchase details
167: *
168: * @return boolean
169: * @throws IPC_Exception
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: * Merchant Site URL where client comes after unsuccessful payment
219: *
220: * @return string
221: */
222: public function getUrlCancel()
223: {
224: return $this->url_cancel;
225: }
226:
227: /**
228: * Merchant Site URL where IPC posts Purchase Notify requests
229: *
230: * @var string
231: */
232: public function getUrlNotify()
233: {
234: return $this->url_notify;
235: }
236:
237: /**
238: * Merchant Site URL where client comes after successful payment
239: *
240: * @return string
241: */
242: public function getUrlOk()
243: {
244: return $this->url_ok;
245: }
246:
247: /**
248: * Merchant Site URL where client comes after successful payment
249: *
250: * @param string $urlOk
251: *
252: * @return PurchaseByIcard
253: */
254: public function setUrlOk($urlOk)
255: {
256: $this->url_ok = $urlOk;
257:
258: return $this;
259: }
260:
261: /**
262: * ISO-4217 Three letter currency code
263: *
264: * @return string
265: */
266: public function getCurrency()
267: {
268: return $this->currency;
269: }
270:
271: /**
272: * ISO-4217 Three letter currency code
273: *
274: * @param string $currency
275: *
276: * @return PurchaseByIcard
277: */
278: public function setCurrency($currency)
279: {
280: $this->currency = $currency;
281:
282: return $this;
283: }
284:
285: /**
286: * Cart object
287: *
288: * @return Cart
289: */
290: public function getCart()
291: {
292: return $this->cart;
293: }
294:
295: /**
296: * Cart object
297: *
298: * @param Cart $cart
299: *
300: * @return PurchaseByIcard
301: */
302: public function setCart(Cart $cart)
303: {
304: $this->cart = $cart;
305:
306: return $this;
307: }
308:
309: /**
310: * Purchase identifier
311: *
312: * @return string
313: */
314: public function getOrderID()
315: {
316: return $this->orderID;
317: }
318: }
319: