1: <?php
2:
3: namespace Mypos\IPC;
4:
5: /**
6: * Process IPC method: IPCIAPurchase.
7: * Collect, validate and send API params
8: */
9: class IAPurchase extends Base
10: {
11: /**
12: * @var Cart
13: */
14: private $cart;
15: /**
16: * @var Card
17: */
18: private $card;
19: private $currency = 'EUR', $note, $orderID, $accountSettlement;
20:
21: /**
22: * Return purchase object
23: *
24: * @param Config $cnf
25: */
26: public function __construct(Config $cnf)
27: {
28: $this->setCnf($cnf);
29: }
30:
31: /**
32: * Purchase identifier - must be unique
33: *
34: * @param string $orderID
35: *
36: * @return IAPurchase
37: */
38: public function setOrderID($orderID)
39: {
40: $this->orderID = $orderID;
41:
42: return $this;
43: }
44:
45: /**
46: * Optional note to purchase
47: *
48: * @param string $note
49: *
50: * @return IAPurchase
51: */
52: public function setNote($note)
53: {
54: $this->note = $note;
55:
56: return $this;
57: }
58:
59: /**
60: * Account for payment settlement
61: *
62: * @param string $accountSettlement
63: */
64: public function setAccountSettlement($accountSettlement)
65: {
66: $this->accountSettlement = $accountSettlement;
67: }
68:
69: /**
70: * Initiate API request
71: *
72: * @return Response
73: * @throws IPC_Exception
74: */
75: public function process()
76: {
77: $this->validate();
78:
79: $this->_addPostParam('IPCmethod', 'IPCIAPurchase');
80: $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
81: $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
82: $this->_addPostParam('SID', $this->getCnf()->getSid());
83: $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
84: $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
85: $this->_addPostParam('Source', $this->getCnf()->getSource());
86:
87: $this->_addPostParam('OrderID', $this->getOrderID());
88: $this->_addPostParam('Amount', $this->getCart()->getTotal());
89: $this->_addPostParam('Currency', $this->getCurrency());
90:
91: if ($this->getCard()->getCardToken()) {
92: $this->_addPostParam('CardToken', $this->getCard()->getCardToken());
93: } else {
94: $this->_addPostParam('CardType', $this->getCard()->getCardType());
95: $this->_addPostParam('PAN', $this->getCard()->getCardNumber(), true);
96: $this->_addPostParam('CardholderName', $this->getCard()->getCardHolder());
97: $this->_addPostParam('ExpDate', $this->getCard()->getExpDate(), true);
98: $this->_addPostParam('CVC', $this->getCard()->getCvc(), true);
99: $this->_addPostParam('ECI', $this->getCard()->getEci());
100: $this->_addPostParam('AVV', $this->getCard()->getAvv());
101: $this->_addPostParam('XID', $this->getCard()->getXid());
102: }
103:
104: $this->_addPostParam('AccountSettlement', $this->getAccountSettlement());
105: $this->_addPostParam('Note', $this->getNote());
106: $this->_addPostParam('OutputFormat', $this->getOutputFormat());
107:
108: $this->_addPostParam('ApplicationID', $this->getCnf()->getApplicationID());
109: $this->_addPostParam('PartnerID', $this->getCnf()->getPartnerID());
110:
111: $this->_addPostParam('CartItems', $this->getCart()->getItemsCount());
112: $items = $this->getCart()->getCart();
113: $i = 1;
114: foreach ($items as $v) {
115: $this->_addPostParam('Article_'.$i, $v['name']);
116: $this->_addPostParam('Quantity_'.$i, $v['quantity']);
117: $this->_addPostParam('Price_'.$i, $v['price']);
118: $this->_addPostParam('Amount_'.$i, $v['price'] * $v['quantity']);
119: $this->_addPostParam('Currency_'.$i, $this->getCurrency());
120: $i++;
121: }
122:
123: return $this->_processPost();
124: }
125:
126: /**
127: * Validate all set purchase details
128: *
129: * @return boolean
130: * @throws IPC_Exception
131: */
132: public function validate()
133: {
134: if ($this->getCurrency() === null) {
135: throw new IPC_Exception('Invalid currency');
136: }
137:
138: try {
139: $this->getCnf()->validate();
140: } catch (\Exception $ex) {
141: throw new IPC_Exception('Invalid Config details: '.$ex->getMessage());
142: }
143:
144: if ($this->getCart() === null) {
145: throw new IPC_Exception('Missing Cart details');
146: }
147:
148: try {
149: $this->getCart()->validate();
150: } catch (\Exception $ex) {
151: throw new IPC_Exception('Invalid Cart details: '.$ex->getMessage());
152: }
153:
154: if ($this->getCard() === null) {
155: throw new IPC_Exception('Missing card details');
156: }
157:
158: try {
159: $this->getCard()->validate();
160: } catch (\Exception $ex) {
161: throw new IPC_Exception('Invalid Card details: '.$ex->getMessage());
162: }
163:
164: if ($this->getCnf()->getVersion() === '1.4.1'){
165: if ($this->getCnf()->getPartnerID() == null){
166: throw new IPC_Exception('Required parameter: Partner ID');
167: }
168:
169: if ($this->getCnf()->getApplicationID() == null){
170: throw new IPC_Exception('Required parameter: Application ID');
171: }
172: }
173:
174: return true;
175: }
176:
177: /**
178: * ISO-4217 Three letter currency code
179: *
180: * @return string
181: */
182: public function getCurrency()
183: {
184: return $this->currency;
185: }
186:
187: /**
188: * ISO-4217 Three letter currency code
189: *
190: * @param string $currency
191: *
192: * @return IAPurchase
193: */
194: public function setCurrency($currency)
195: {
196: $this->currency = $currency;
197:
198: return $this;
199: }
200:
201: /**
202: * Cart object
203: *
204: * @return Cart
205: */
206: public function getCart()
207: {
208: return $this->cart;
209: }
210:
211: /**
212: * Cart object
213: *
214: * @param Cart $cart
215: *
216: * @return IAPurchase
217: */
218: public function setCart(Cart $cart)
219: {
220: $this->cart = $cart;
221:
222: return $this;
223: }
224:
225: /**
226: * Card object
227: *
228: * @return Card
229: */
230: public function getCard()
231: {
232: return $this->card;
233: }
234:
235: /**
236: * Card object
237: *
238: * @param Card $card
239: */
240: public function setCard($card)
241: {
242: $this->card = $card;
243: }
244:
245: /**
246: * Purchase identifier
247: *
248: * @return string
249: */
250: public function getOrderID()
251: {
252: return $this->orderID;
253: }
254:
255: /**
256: * Account for payment settlement
257: *
258: * @return string
259: */
260: public function getAccountSettlement()
261: {
262: return $this->accountSettlement;
263: }
264:
265: /**
266: * Optional note to purchase
267: *
268: * @return string
269: */
270: public function getNote()
271: {
272: return $this->note;
273: }
274:
275: }
276: