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