1: <?php
2:
3: namespace Mypos\IPC;
4:
5: /**
6: * Process IPC method: IPCTDSProcess.
7: * Collect, validate and send API params
8: */
9: class TDSProcess extends Base
10: {
11: /**
12: * @var Cart
13: */
14: private $cart;
15: /**
16: * @var Card
17: */
18: private $card;
19: private $currency = 'EUR';
20: private $managerSid;
21: private $returnUrl;
22: private $useRedirect = true;
23:
24:
25: /**
26: * Return purchase object
27: *
28: * @param Config $cnf
29: */
30: public function __construct(Config $cnf)
31: {
32: $this->setCnf($cnf);
33: }
34:
35: /**
36: * ISO-4217 Three letter currency code
37: *
38: * @return string
39: */
40: public function getCurrency()
41: {
42: return $this->currency;
43: }
44:
45: /**
46: * ISO-4217 Three letter currency code
47: *
48: * @param string $currency
49: *
50: * @return TDSProcess
51: */
52: public function setCurrency($currency)
53: {
54: $this->currency = $currency;
55:
56: return $this;
57: }
58:
59: /**
60: * Cart object
61: *
62: * @return Cart
63: */
64: public function getCart()
65: {
66: return $this->cart;
67: }
68:
69: /**
70: * Cart object
71: *
72: * @param Cart $cart
73: *
74: * @return TDSProcess
75: */
76: public function setCart(Cart $cart)
77: {
78: $this->cart = $cart;
79:
80: return $this;
81: }
82:
83: /**
84: * Card object
85: *
86: * @return Card
87: */
88: public function getCard()
89: {
90: return $this->card;
91: }
92:
93: /**
94: * Card object
95: *
96: * @param Card $card
97: */
98: public function setCard($card)
99: {
100: $this->card = $card;
101: }
102:
103: /**
104: * @return mixed
105: */
106: public function getManagerSid()
107: {
108: return $this->managerSid;
109: }
110:
111: /**
112: * @param mixed $managerSid
113: */
114: public function setManagerSid($managerSid)
115: {
116: $this->managerSid = $managerSid;
117: }
118:
119: /**
120: * @return mixed
121: */
122: public function getReturnUrl()
123: {
124: return $this->returnUrl;
125: }
126:
127: /**
128: * @param mixed $returnUrl
129: */
130: public function setReturnUrl($returnUrl)
131: {
132: $this->returnUrl = $returnUrl;
133: }
134:
135: /**
136: * @return bool
137: */
138: public function isUseRedirect()
139: {
140: return $this->useRedirect;
141: }
142:
143: /**
144: * @param bool $useRedirect
145: */
146: public function setUseRedirect($useRedirect)
147: {
148: $this->useRedirect = $useRedirect;
149: }
150:
151:
152: /**
153: * Initiate API request
154: *
155: * @return bool
156: * @throws IPC_Exception
157: */
158: public function process($submitForm = true)
159: {
160: $this->validate();
161:
162: $this->_addPostParam('IPCmethod', 'IPCTDSProcess');
163: $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
164: $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
165: $this->_addPostParam('SID', $this->getCnf()->getSid());
166: $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
167: $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
168: $this->_addPostParam('Source', $this->getCnf()->getSource());
169:
170: $this->_addPostParam('Amount', $this->getCart()->getTotal());
171: $this->_addPostParam('Currency', $this->getCurrency());
172:
173: $this->_addPostParam('CardToken', $this->getCard()->getCardToken());
174:
175: $this->_addPostParam('UseRedirect', $this->isUseRedirect() ? 1 : 0);
176: $this->_addPostParam('ReturnUrl', $this->getReturnUrl());
177:
178: $this->_addPostParam('ManagerSID', $this->getManagerSid());
179:
180: $this->_addPostParam('OutputFormat', $this->getOutputFormat());
181:
182: $this->_addPostParam('CartItems', $this->getCart()->getItemsCount());
183: $items = $this->getCart()->getCart();
184: $i = 1;
185:
186: foreach ($items as $v) {
187: $this->_addPostParam('Article_'.$i, $v['name']);
188: $this->_addPostParam('Quantity_'.$i, $v['quantity']);
189: $this->_addPostParam('Price_'.$i, $v['price']);
190: $this->_addPostParam('Amount_'.$i, $v['price'] * $v['quantity']);
191: $this->_addPostParam('Currency_'.$i, $this->getCurrency());
192: $i++;
193: }
194: if ($submitForm) {
195: $this->_processHtmlPost();
196: }
197:
198: return true;
199: }
200:
201: /**
202: * Validate all set purchase details
203: *
204: * @return boolean
205: * @throws IPC_Exception
206: */
207: public function validate()
208: {
209: if ($this->getCurrency() === null) {
210: throw new IPC_Exception('Invalid currency');
211: }
212:
213: try {
214: $this->getCnf()->validate();
215: } catch (\Exception $ex) {
216: throw new IPC_Exception('Invalid Config details: '.$ex->getMessage());
217: }
218:
219: if ($this->getCart() === null) {
220: throw new IPC_Exception('Missing Cart details');
221: }
222:
223: try {
224: $this->getCart()->validate();
225: } catch (\Exception $ex) {
226: throw new IPC_Exception('Invalid Cart details: '.$ex->getMessage());
227: }
228:
229: if ($this->getCard() === null) {
230: throw new IPC_Exception('Missing card details');
231: }
232:
233: if ($this->getReturnUrl() == null || !Helper::isValidURL($this->getReturnUrl())) {
234: throw new IPC_Exception('Invalid IPC URL');
235: }
236:
237: try {
238: $this->getCard()->validate();
239: } catch (\Exception $ex) {
240: throw new IPC_Exception('Invalid Card details: '.$ex->getMessage());
241: }
242:
243: return true;
244: }
245: }
246: