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