1: <?php
2:
3: namespace Mypos\IPC;
4:
5: /**
6: * Process IPC method: IPCIAPurchaseWithToken.
7: * Collect, validate and send API params
8: */
9: class IAPurchaseWithToken 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 $note;
21: private $orderID;
22: private $accountSettlement;
23: private $allowPaymentWithoutTdsReferenceID = false;
24: private $tdsReferenceID;
25: private $managerSid;
26:
27: /**
28: * Return purchase object
29: *
30: * @param Config $cnf
31: */
32: public function __construct(Config $cnf)
33: {
34: $this->setCnf($cnf);
35: }
36:
37: /**
38: * Purchase identifier - must be unique
39: *
40: * @param string $orderID
41: *
42: * @return IAPurchaseWithToken
43: */
44: public function setOrderID($orderID)
45: {
46: $this->orderID = $orderID;
47:
48: return $this;
49: }
50:
51: /**
52: * Optional note to purchase
53: *
54: * @param string $note
55: *
56: * @return IAPurchaseWithToken
57: */
58: public function setNote($note)
59: {
60: $this->note = $note;
61:
62: return $this;
63: }
64:
65: /**
66: * Account for payment settlement
67: *
68: * @param string $accountSettlement
69: */
70: public function setAccountSettlement($accountSettlement)
71: {
72: $this->accountSettlement = $accountSettlement;
73: }
74:
75: /**
76: * ISO-4217 Three letter currency code
77: *
78: * @return string
79: */
80: public function getCurrency()
81: {
82: return $this->currency;
83: }
84:
85: /**
86: * ISO-4217 Three letter currency code
87: *
88: * @param string $currency
89: *
90: * @return IAPurchaseWithToken
91: */
92: public function setCurrency($currency)
93: {
94: $this->currency = $currency;
95:
96: return $this;
97: }
98:
99: /**
100: * Cart object
101: *
102: * @return Cart
103: */
104: public function getCart()
105: {
106: return $this->cart;
107: }
108:
109: /**
110: * Cart object
111: *
112: * @param Cart $cart
113: *
114: * @return IAPurchaseWithToken
115: */
116: public function setCart(Cart $cart)
117: {
118: $this->cart = $cart;
119:
120: return $this;
121: }
122:
123: /**
124: * Card object
125: *
126: * @return Card
127: */
128: public function getCard()
129: {
130: return $this->card;
131: }
132:
133: /**
134: * Card object
135: *
136: * @param Card $card
137: */
138: public function setCard($card)
139: {
140: $this->card = $card;
141: }
142:
143: /**
144: * @return bool
145: */
146: public function isPaymentWithoutTdsReferenceIDAllowed()
147: {
148: return $this->allowPaymentWithoutTdsReferenceID;
149: }
150:
151: /**
152: * @param bool $allowPaymentWithoutTdsReferenceID
153: */
154: public function allowPaymentWithoutTdsReferenceID($allowPaymentWithoutTdsReferenceID)
155: {
156: $this->allowPaymentWithoutTdsReferenceID = $allowPaymentWithoutTdsReferenceID;
157: }
158:
159: /**
160: * @return string
161: */
162: public function getTdsReferenceID()
163: {
164: return $this->tdsReferenceID;
165: }
166:
167: /**
168: * @param string $tdsReferenceID
169: */
170: public function setTdsReferenceID($tdsReferenceID)
171: {
172: $this->tdsReferenceID = $tdsReferenceID;
173: }
174:
175: /**
176: * @return mixed
177: */
178: public function getManagerSid()
179: {
180: return $this->managerSid;
181: }
182:
183: /**
184: * @param mixed $managerSid
185: */
186: public function setManagerSid($managerSid)
187: {
188: $this->managerSid = $managerSid;
189: }
190:
191: /**
192: * Purchase identifier
193: *
194: * @return string
195: */
196: public function getOrderID()
197: {
198: return $this->orderID;
199: }
200:
201: /**
202: * Account for payment settlement
203: *
204: * @return string
205: */
206: public function getAccountSettlement()
207: {
208: return $this->accountSettlement;
209: }
210:
211: /**
212: * Optional note to purchase
213: *
214: * @return string
215: */
216: public function getNote()
217: {
218: return $this->note;
219: }
220:
221: /**
222: * Initiate API request
223: *
224: * @return Response
225: * @throws IPC_Exception
226: */
227: public function process()
228: {
229: $this->validate();
230:
231: $this->_addPostParam('IPCmethod', 'IPCIAPurchaseWithToken');
232: $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
233: $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
234: $this->_addPostParam('SID', $this->getCnf()->getSid());
235: $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
236: $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
237: $this->_addPostParam('Source', $this->getCnf()->getSource());
238:
239: $this->_addPostParam('OrderID', $this->getOrderID());
240: $this->_addPostParam('Amount', $this->getCart()->getTotal());
241: $this->_addPostParam('Currency', $this->getCurrency());
242:
243: $this->_addPostParam('CardToken', $this->getCard()->getCardToken());
244:
245: $this->_addPostParam('AllowWithoutTDSReferenceID', $this->isPaymentWithoutTdsReferenceIDAllowed() ? 1 : 0);
246: $this->_addPostParam('TDSReferenceID', $this->getTdsReferenceID());
247:
248: $this->_addPostParam('ManagerSID', $this->getManagerSid());
249:
250: $this->_addPostParam('AccountSettlement', $this->getAccountSettlement());
251: $this->_addPostParam('Note', $this->getNote());
252: $this->_addPostParam('OutputFormat', $this->getOutputFormat());
253:
254: $this->_addPostParam('ApplicationID', $this->getCnf()->getApplicationID());
255: $this->_addPostParam('PartnerID', $this->getCnf()->getPartnerID());
256:
257: $this->_addPostParam('CartItems', $this->getCart()->getItemsCount());
258: $items = $this->getCart()->getCart();
259: $i = 1;
260:
261: foreach ($items as $v) {
262: $this->_addPostParam('Article_'.$i, $v['name']);
263: $this->_addPostParam('Quantity_'.$i, $v['quantity']);
264: $this->_addPostParam('Price_'.$i, $v['price']);
265: $this->_addPostParam('Amount_'.$i, $v['price'] * $v['quantity']);
266: $this->_addPostParam('Currency_'.$i, $this->getCurrency());
267: $i++;
268: }
269:
270: return $this->_processPost();
271: }
272:
273: /**
274: * Validate all set purchase details
275: *
276: * @return boolean
277: * @throws IPC_Exception
278: */
279: public function validate()
280: {
281: if ($this->getCurrency() === null) {
282: throw new IPC_Exception('Invalid currency');
283: }
284:
285: try {
286: $this->getCnf()->validate();
287: } catch (\Exception $ex) {
288: throw new IPC_Exception('Invalid Config details: '.$ex->getMessage());
289: }
290:
291: if ($this->getCart() === null) {
292: throw new IPC_Exception('Missing Cart details');
293: }
294:
295: try {
296: $this->getCart()->validate();
297: } catch (\Exception $ex) {
298: throw new IPC_Exception('Invalid Cart details: '.$ex->getMessage());
299: }
300:
301: if ($this->getCard() === null) {
302: throw new IPC_Exception('Missing card details');
303: }
304:
305: try {
306: $this->getCard()->validate();
307: } catch (\Exception $ex) {
308: throw new IPC_Exception('Invalid Card details: '.$ex->getMessage());
309: }
310:
311: if ($this->isPaymentWithoutTDSReferenceIDAllowed() === false && $this->getTdsReferenceID() === null) {
312: throw new IPC_Exception('Missing TdsReferenceID');
313: }
314:
315: if ($this->getCnf()->getVersion() === '1.4.1'){
316: if ($this->getCnf()->getPartnerID() == null){
317: throw new IPC_Exception('Required parameter: Partner ID');
318: }
319:
320: if ($this->getCnf()->getApplicationID() == null){
321: throw new IPC_Exception('Required parameter: Application ID');
322: }
323: }
324:
325: return true;
326: }
327: }
328: