1 <?php
2
3 namespace Satabank\IPC;
4
5 6 7 8
9 class Purchase extends Base{
10
11 12 13
14 private $cart;
15
16 17 18
19 private $customer;
20 private $url_ok, $url_cancel, $url_notify;
21 private $currency = 'EUR', $note, $orderID;
22
23 24 25 26
27 public function __construct(Config $cnf){
28 $this->setCnf($cnf);
29 }
30
31 32 33 34 35
36 public function setOrderID($orderID){
37 $this->orderID = $orderID;
38 return $this;
39 }
40
41 42 43 44
45 public function getOrderID(){
46 return $this->orderID;
47 }
48
49 50 51 52 53
54 public function setNote($note){
55 $this->note = $note;
56 return $this;
57 }
58
59 60 61 62
63 public function getNote(){
64 return $this->note;
65 }
66
67 68 69 70 71
72 public function setCurrency($currency){
73 $this->currency = $currency;
74 return $this;
75 }
76
77 78 79 80
81 public function getCurrency(){
82 return $this->currency;
83 }
84
85 86 87 88 89
90 public function setCart(Cart $cart){
91 $this->cart = $cart;
92 return $this;
93 }
94
95 96 97 98
99 public function getCart(){
100 return $this->cart;
101 }
102
103 104 105 106 107
108 public function setCustomer(Customer $customer){
109 $this->customer = $customer;
110 return $this;
111 }
112
113 114 115
116 public function getCustomer(){
117 return $this->customer;
118 }
119
120 121 122 123 124
125 public function setUrlOk($urlOk){
126 $this->url_ok = $urlOk;
127 return $this;
128 }
129
130 131 132 133
134 public function getUrlOk(){
135 return $this->url_ok;
136 }
137
138 139 140 141 142
143 public function setUrlCancel($urlCancel){
144 $this->url_cancel = $urlCancel;
145 return $this;
146 }
147
148 149 150 151
152 public function getUrlCancel(){
153 return $this->url_cancel;
154 }
155
156 157 158 159 160
161 public function setUrlNotify($urlNotify){
162 $this->url_notify = $urlNotify;
163 return $this;
164 }
165
166 167 168 169
170 public function getUrlNotify(){
171 return $this->url_notify;
172 }
173
174 175 176 177
178 public function process(){
179 $this->validate();
180
181 $this->_addPostParam('IPCmethod', 'IPCPurchase');
182 $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
183 $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
184 $this->_addPostParam('SID', $this->getCnf()->getSid());
185 $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
186 $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
187 $this->_addPostParam('Source', Defines::SOURCE_PARAM);
188
189 $this->_addPostParam('Currency', $this->getCurrency());
190 $this->_addPostParam('Amount', $this->cart->getTotal());
191
192 $this->_addPostParam('OrderID', $this->getOrderID());
193 $this->_addPostParam('URL_OK', $this->getUrlOk());
194 $this->_addPostParam('URL_Cancel', $this->getUrlCancel());
195 $this->_addPostParam('URL_Notify', $this->getUrlNotify());
196
197 $this->_addPostParam('customeremail', $this->getCustomer()->getEmail());
198 $this->_addPostParam('customerphone', $this->getCustomer()->getPhone());
199 $this->_addPostParam('customerfirstnames', $this->getCustomer()->getFirstName());
200 $this->_addPostParam('customerfamilyname', $this->getCustomer()->getLastName());
201 $this->_addPostParam('customercountry', $this->getCustomer()->getCountry());
202 $this->_addPostParam('customercity', $this->getCustomer()->getCity());
203 $this->_addPostParam('customerzipcode', $this->getCustomer()->getZip());
204 $this->_addPostParam('customeraddress', $this->getCustomer()->getAddress());
205
206 $this->_addPostParam('Note', $this->getNote());
207 $this->_addPostParam('CartItems', $this->cart->getItemsCount());
208 $items = $this->cart->getCart();
209
210 $i = 1;
211 foreach($items as $v){
212 $this->_addPostParam('Article_' . $i, $v['name']);
213 $this->_addPostParam('Quantity_' . $i, $v['quantity']);
214 $this->_addPostParam('Price_' . $i, $v['price']);
215 $this->_addPostParam('Amount_' . $i, $v['price'] * $v['quantity']);
216 $this->_addPostParam('Currency_' . $i, $this->getCurrency());
217 $i++;
218 }
219
220 $this->_processHtmlPost();
221 return true;
222 }
223
224 225 226 227 228
229 public function validate(){
230
231 if($this->getUrlCancel() == null || !Helper::isValidURL($this->getUrlCancel())){
232 throw new IPC_Exception('Invalid Cancel URL');
233 }
234
235 if($this->getUrlNotify() == null || !Helper::isValidURL($this->getUrlNotify())){
236 throw new IPC_Exception('Invalid Notify URL');
237 }
238
239 if($this->getUrlOk() == null || !Helper::isValidURL($this->getUrlOk())){
240 throw new IPC_Exception('Invalid Success URL');
241 }
242
243 try{
244 $this->getCnf()->validate();
245 }catch(Exception $ex){
246 throw new IPC_Exception('Invalid Config details: ' . $ex->getMessage());
247 }
248
249 try{
250 $this->getCart()->validate();
251 }catch(Exception $ex){
252 throw new IPC_Exception('Invalid Cart details: ' . $ex->getMessage());
253 }
254
255 try{
256 $this->getCustomer()->validate();
257 }catch(Exception $ex){
258 throw new IPC_Exception('Invalid Customer details: ' . $ex->getMessage());
259 }
260
261
262 return true;
263 }
264
265 }
266