1: <?php
2:
3: namespace Mypos\IPC;
4:
5: /**
6: * Process IPC method: IPCRefund.
7: * Collect, validate and send API params
8: */
9: class Refund extends Base
10: {
11: private $currency = 'EUR', $amount, $trnref, $orderID;
12:
13: /**
14: * Return Refund object
15: *
16: * @param Config $cnf
17: */
18: public function __construct(Config $cnf)
19: {
20: $this->setCnf($cnf);
21: }
22:
23: /**
24: * Refund amount
25: *
26: * @param float $amount
27: */
28: public function setAmount($amount)
29: {
30: $this->amount = $amount;
31: }
32:
33: /**
34: * Transaction reference - transaction unique identifier
35: *
36: * @param string $trnref
37: *
38: * @return Refund
39: */
40: public function setTrnref($trnref)
41: {
42: $this->trnref = $trnref;
43:
44: return $this;
45: }
46:
47: /**
48: * Request identifier - must be unique
49: *
50: * @param string $orderID
51: *
52: * @return Refund
53: */
54: public function setOrderID($orderID)
55: {
56: $this->orderID = $orderID;
57:
58: return $this;
59: }
60:
61: /**
62: * Initiate API request
63: *
64: * @return boolean
65: * @throws IPC_Exception
66: */
67: public function process()
68: {
69: $this->validate();
70:
71: $this->_addPostParam('IPCmethod', 'IPCRefund');
72: $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
73: $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
74: $this->_addPostParam('SID', $this->getCnf()->getSid());
75: $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
76: $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
77: $this->_addPostParam('Source', $this->getCnf()->getSource());
78:
79: $this->_addPostParam('Currency', $this->getCurrency());
80: $this->_addPostParam('Amount', $this->getAmount());
81:
82: $this->_addPostParam('OrderID', $this->getOrderID());
83: $this->_addPostParam('IPC_Trnref', $this->getTrnref());
84: $this->_addPostParam('OutputFormat', $this->getOutputFormat());
85:
86: $this->_addPostParam('ApplicationID', $this->getCnf()->getApplicationID());
87: $this->_addPostParam('PartnerID', $this->getCnf()->getPartnerID());
88:
89: $response = $this->_processPost()->getData(CASE_LOWER);
90: if (empty($response['ipc_trnref']) || (empty($response['amount']) || $response['amount'] != $this->getAmount()) || (empty($response['currency']) || $response['currency'] != $this->getCurrency()) || $response['status'] != Defines::STATUS_SUCCESS) {
91: return false;
92: }
93:
94: return true;
95: }
96:
97: /**
98: * Validate all set refund details
99: *
100: * @return boolean
101: * @throws IPC_Exception
102: */
103: public function validate()
104: {
105: try {
106: $this->getCnf()->validate();
107: } catch (\Exception $ex) {
108: throw new IPC_Exception('Invalid Config details: '.$ex->getMessage());
109: }
110:
111: if ($this->getAmount() == null || !Helper::isValidAmount($this->getAmount())) {
112: throw new IPC_Exception('Invalid Amount');
113: }
114:
115: if ($this->getCurrency() == null) {
116: throw new IPC_Exception('Invalid Currency');
117: }
118:
119: if ($this->getTrnref() == null || !Helper::isValidTrnRef($this->getTrnref())) {
120: throw new IPC_Exception('Invalid TrnRef');
121: }
122:
123: if ($this->getOrderID() == null || !Helper::isValidOrderId($this->getOrderID())) {
124: throw new IPC_Exception('Invalid OrderId');
125: }
126:
127: if ($this->getOutputFormat() == null || !Helper::isValidOutputFormat($this->getOutputFormat())) {
128: throw new IPC_Exception('Invalid Output format');
129: }
130:
131: if ($this->getCnf()->getVersion() === '1.4.1') {
132: if ($this->getCnf()->getPartnerID() == null) {
133: throw new IPC_Exception('Required parameter: Partner ID');
134: }
135:
136: if ($this->getCnf()->getApplicationID() == null) {
137: throw new IPC_Exception('Required parameter: Application ID');
138: }
139: }
140:
141: return true;
142: }
143:
144: /**
145: * Refund amount
146: *
147: * @return float
148: */
149: public function getAmount()
150: {
151: return $this->amount;
152: }
153:
154: /**
155: * ISO-4217 Three letter currency code
156: *
157: * @return string
158: */
159: public function getCurrency()
160: {
161: return $this->currency;
162: }
163:
164: /**
165: * ISO-4217 Three letter currency code
166: *
167: * @param string $currency
168: *
169: * @return Refund
170: */
171: public function setCurrency($currency)
172: {
173: $this->currency = $currency;
174:
175: return $this;
176: }
177:
178: /**
179: * Transaction reference - transaction unique identifier
180: *
181: * @return string
182: */
183: public function getTrnref()
184: {
185: return $this->trnref;
186: }
187:
188: /**
189: * Request identifier - must be unique
190: *
191: * @return string
192: */
193: public function getOrderID()
194: {
195: return $this->orderID;
196: }
197:
198: }