Overview
  • Namespace
  • Class

Namespaces

  • Mypos
    • IPC

Classes

  • Mypos\IPC\Base
  • Mypos\IPC\Card
  • Mypos\IPC\CardStore
  • Mypos\IPC\Cart
  • Mypos\IPC\Config
  • Mypos\IPC\Customer
  • Mypos\IPC\Defines
  • Mypos\IPC\GetTxnStatus
  • Mypos\IPC\Helper
  • Mypos\IPC\IAPurchase
  • Mypos\IPC\IAStoreCard
  • Mypos\IPC\IAStoredCardUpdate
  • Mypos\IPC\IPCGetTxnLog
  • Mypos\IPC\Loader
  • Mypos\IPC\MandateManagement
  • Mypos\IPC\Purchase
  • Mypos\IPC\Refund
  • Mypos\IPC\RequestMoney
  • Mypos\IPC\Response
  • Mypos\IPC\Reversal

Exceptions

  • Mypos\IPC\IPC_Exception
  1 <?php
  2 
  3 namespace Mypos\IPC;
  4 
  5 /**
  6  * Process IPC method: IPCPurchase.
  7  * Collect, validate and send API params
  8  */
  9 class Purchase extends Base
 10 {
 11 
 12     const PURCHASE_TYPE_FULL = 1;
 13     const PURCHASE_TYPE_SIMPLIFIED_CALL = 2;
 14     const PURCHASE_TYPE_SIMPLIFIED_PAYMENT_PAGE = 3;
 15 
 16     const CARD_TOKEN_REQUSET_NONE = 0;
 17     const CARD_TOKEN_REQUSET_ONLY_STORE = 1;
 18     const CARD_TOKEN_REQUSET_PAY_AND_STORE = 2;
 19 
 20     /**
 21      * @var Cart
 22      */
 23     private $cart;
 24 
 25     /**
 26      * @var Customer
 27      */
 28     private $customer;
 29     private $url_ok, $url_cancel, $url_notify;
 30     private $currency = 'EUR', $note, $orderID, $cardTokenRequest, $paymentParametersRequired;
 31 
 32     /**
 33      * Return purchase object
 34      * @param Config $cnf
 35      */
 36     public function __construct(Config $cnf)
 37     {
 38         $this->setCnf($cnf);
 39     }
 40 
 41     /**
 42      * Purchase identifier - must be unique
 43      * @param string $orderID
 44      * @return Purchase
 45      */
 46     public function setOrderID($orderID)
 47     {
 48         $this->orderID = $orderID;
 49         return $this;
 50     }
 51 
 52     /**
 53      * Purchase identifier
 54      * @return string
 55      */
 56     public function getOrderID()
 57     {
 58         return $this->orderID;
 59     }
 60 
 61     /**
 62      * Optional note to purchase
 63      * @param string $note
 64      * @return Purchase
 65      */
 66     public function setNote($note)
 67     {
 68         $this->note = $note;
 69         return $this;
 70     }
 71 
 72     /**
 73      * Optional note to purchase
 74      * @return string
 75      */
 76     public function getNote()
 77     {
 78         return $this->note;
 79     }
 80 
 81     /**
 82      * ISO-4217 Three letter currency code
 83      * @param string $currency
 84      * @return Purchase
 85      */
 86     public function setCurrency($currency)
 87     {
 88         $this->currency = $currency;
 89         return $this;
 90     }
 91 
 92     /**
 93      * ISO-4217 Three letter currency code
 94      * @return string
 95      */
 96     public function getCurrency()
 97     {
 98         return $this->currency;
 99     }
100 
101     /**
102      * Cart object
103      * @param Cart $cart
104      * @return Purchase
105      */
106     public function setCart(Cart $cart)
107     {
108         $this->cart = $cart;
109         return $this;
110     }
111 
112     /**
113      * Cart object
114      * @return Cart
115      */
116     public function getCart()
117     {
118         return $this->cart;
119     }
120 
121     /**
122      * Customer object
123      * @param Customer $customer
124      * @return Purchase
125      */
126     public function setCustomer(Customer $customer)
127     {
128         $this->customer = $customer;
129         return $this;
130     }
131 
132     /**
133      * @return Customer
134      */
135     public function getCustomer()
136     {
137         return $this->customer;
138     }
139 
140     /**
141      * Merchant Site URL where client comes after successful payment
142      * @param string $urlOk
143      * @return Config
144      */
145     public function setUrlOk($urlOk)
146     {
147         $this->url_ok = $urlOk;
148         return $this;
149     }
150 
151     /**
152      * Merchant Site URL where client comes after successful payment
153      * @return string
154      */
155     public function getUrlOk()
156     {
157         return $this->url_ok;
158     }
159 
160     /**
161      * Merchant Site URL where client comes after unsuccessful payment
162      * @param string $urlCancel
163      * @return Config
164      */
165     public function setUrlCancel($urlCancel)
166     {
167         $this->url_cancel = $urlCancel;
168         return $this;
169     }
170 
171     /**
172      * Merchant Site URL where client comes after unsuccessful payment
173      * @return string
174      */
175     public function getUrlCancel()
176     {
177         return $this->url_cancel;
178     }
179 
180     /**
181      * Merchant Site URL where IPC posts Purchase Notify requests
182      * @param string $urlNotify
183      * @return Config
184      */
185     public function setUrlNotify($urlNotify)
186     {
187         $this->url_notify = $urlNotify;
188         return $this;
189     }
190 
191     /**
192      * Merchant Site URL where IPC posts Purchase Notify requests
193      * @var string
194      */
195     public function getUrlNotify()
196     {
197         return $this->url_notify;
198     }
199 
200     /**
201      * Whether to return Card Token for current client card
202      * @return integer
203      */
204     public function getCardTokenRequest()
205     {
206         return $this->cardTokenRequest;
207     }
208 
209     /**
210      * Whether to return Card Token for current client card
211      * @param integer $cardTokenRequest
212      */
213     public function setCardTokenRequest($cardTokenRequest)
214     {
215         $this->cardTokenRequest = $cardTokenRequest;
216     }
217 
218     /**
219      * Defines the packet of details needed from merchant and client to make payment
220      * @return integer
221      */
222     public function getPaymentParametersRequired()
223     {
224         return $this->paymentParametersRequired;
225     }
226 
227     /**
228      * Defines the packet of details needed from merchant and client to make payment
229      * @param integer $paymentParametersRequired
230      */
231     public function setPaymentParametersRequired($paymentParametersRequired)
232     {
233         $this->paymentParametersRequired = $paymentParametersRequired;
234     }
235 
236 
237     /**
238      * Initiate API request
239      * @return boolean
240      */
241     public function process()
242     {
243         $this->validate();
244 
245         $this->_addPostParam('IPCmethod', 'IPCPurchase');
246         $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
247         $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
248         $this->_addPostParam('SID', $this->getCnf()->getSid());
249         $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
250         $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
251         $this->_addPostParam('Source', Defines::SOURCE_PARAM);
252 
253         $this->_addPostParam('Currency', $this->getCurrency());
254         if (!$this->isNoCartPurchase()) {
255             $this->_addPostParam('Amount', $this->cart->getTotal());
256         }
257 
258         $this->_addPostParam('OrderID', $this->getOrderID());
259         $this->_addPostParam('URL_OK', $this->getUrlOk());
260         $this->_addPostParam('URL_Cancel', $this->getUrlCancel());
261         $this->_addPostParam('URL_Notify', $this->getUrlNotify());
262 
263         if ($this->getPaymentParametersRequired() == self::PURCHASE_TYPE_FULL) {
264             $this->_addPostParam('customeremail', $this->getCustomer()->getEmail());
265             $this->_addPostParam('customerphone', $this->getCustomer()->getPhone());
266             $this->_addPostParam('customerfirstnames', $this->getCustomer()->getFirstName());
267             $this->_addPostParam('customerfamilyname', $this->getCustomer()->getLastName());
268             $this->_addPostParam('customercountry', $this->getCustomer()->getCountry());
269             $this->_addPostParam('customercity', $this->getCustomer()->getCity());
270             $this->_addPostParam('customerzipcode', $this->getCustomer()->getZip());
271             $this->_addPostParam('customeraddress', $this->getCustomer()->getAddress());
272         }
273 
274 
275         if ($this->getPaymentParametersRequired() != self::PURCHASE_TYPE_SIMPLIFIED_CALL) {
276             $this->_addPostParam('Note', $this->getNote());
277             if (!$this->isNoCartPurchase()) {
278                 $this->_addPostParam('CartItems', $this->cart->getItemsCount());
279                 $items = $this->cart->getCart();
280 
281                 $i = 1;
282                 foreach ($items as $v) {
283                     $this->_addPostParam('Article_' . $i, $v['name']);
284                     $this->_addPostParam('Quantity_' . $i, $v['quantity']);
285                     $this->_addPostParam('Price_' . $i, $v['price']);
286                     $this->_addPostParam('Amount_' . $i, $v['price'] * $v['quantity']);
287                     $this->_addPostParam('Currency_' . $i, $this->getCurrency());
288                     $i++;
289                 }
290             }
291         }
292 
293         $this->_addPostParam('CardTokenRequest', $this->getCardTokenRequest());
294         $this->_addPostParam('PaymentParametersRequired', $this->getPaymentParametersRequired());
295 
296         $this->_processHtmlPost();
297         return true;
298     }
299 
300     /**
301      * Validate all set purchase details
302      * @return boolean
303      * @throws IPC_Exception
304      */
305     public function validate()
306     {
307 
308         if ($this->getUrlCancel() === null || !Helper::isValidURL($this->getUrlCancel())) {
309             throw new IPC_Exception('Invalid Cancel URL');
310         }
311 
312         if ($this->getUrlNotify() === null || !Helper::isValidURL($this->getUrlNotify())) {
313             throw new IPC_Exception('Invalid Notify URL');
314         }
315 
316         if ($this->getUrlOk() === null || !Helper::isValidURL($this->getUrlOk())) {
317             throw new IPC_Exception('Invalid Success URL');
318         }
319 
320         if ($this->getCardTokenRequest() === null || !in_array($this->getCardTokenRequest(), array(self::CARD_TOKEN_REQUSET_NONE, self::CARD_TOKEN_REQUSET_ONLY_STORE, self::CARD_TOKEN_REQUSET_PAY_AND_STORE))) {
321             throw new IPC_Exception('Invalid value provided for CardTokenRequest params');
322         }
323 
324         if ($this->getPaymentParametersRequired() === null || !in_array($this->getPaymentParametersRequired(), array(self::PURCHASE_TYPE_FULL, self::PURCHASE_TYPE_SIMPLIFIED_CALL, self::PURCHASE_TYPE_SIMPLIFIED_PAYMENT_PAGE))) {
325             throw new IPC_Exception('Invalid value provided for PaymentParametersRequired params');
326         }
327 
328         if ($this->getCurrency() === null || strpos(Defines::AVL_CURRENCIES, $this->getCurrency()) === false) {
329             throw new IPC_Exception('Invalid currency');
330         }
331 
332         try {
333             $this->getCnf()->validate();
334         } catch (Exception $ex) {
335             throw new IPC_Exception('Invalid Config details: ' . $ex->getMessage());
336         }
337 
338         if (!$this->isNoCartPurchase()) {
339             try {
340                 $this->getCart()->validate();
341             } catch (Exception $ex) {
342                 throw new IPC_Exception('Invalid Cart details: ' . $ex->getMessage());
343             }
344         }
345 
346         if ($this->getPaymentParametersRequired() == self::PURCHASE_TYPE_FULL) {
347             try {
348                 $this->getCustomer()->validate($this->getPaymentParametersRequired());
349             } catch (Exception $ex) {
350                 throw new IPC_Exception('Invalid Customer details: ' . $ex->getMessage());
351             }
352         }
353 
354         return true;
355     }
356 
357     /**
358      * If request is only for card token request without payment, the Amount and Cart params are not required
359      * @return bool
360      */
361     private function isNoCartPurchase()
362     {
363         return $this->getCardTokenRequest() == self::CARD_TOKEN_REQUSET_ONLY_STORE;
364     }
365 }
366 
API documentation generated by ApiGen