· 7 years ago · Jan 05, 2019, 04:56 PM
1<?php
2
3namespace App\Classes\Iyzico;
4
5use Iyzipay;
6use Iyzipay\Model;
7use Iyzipay\Options;
8use Iyzipay\Request;
9
10/**
11 * Class IyzicoPayment
12 * @author Siavash Habil <amirkhiz@gmail.com>
13 * @package App\IyzicoPayment
14 */
15class IyzicoPayment
16{
17 const SUCCESS_RESULT = 'success';
18 const FAILED_RESULT = 'failure';
19
20 const SUCCESS_MD_STATUS = 1;
21
22 /** Fraud Statuses */
23 const FRAUD_CONFIRMED = 1;
24 const FRAUD_CONFIRM_WAITING = 0;
25 const FRAUD_FAILED = -1;
26
27 /** Our commission for every basket item */
28 const BASKET_ITEM_COMMISSION = 0.06;
29
30 protected static $config;
31
32 public $htmlContent;
33 protected $errors;
34
35 /**
36 * @var Model\SubMerchant
37 */
38 protected $subMerchant;
39
40 public $cardToken;
41 public $cardUserKey;
42 public $installmentCount;
43 public $fraudStatus;
44 public $paymentId;
45 public $paymentTransactionId;
46
47 /**
48 * @var Model\InstallmentInfo
49 */
50 protected $installment;
51 protected $lang;
52
53 /**
54 * Discount value. This value calculate from pay price and will have the value of discount based on pay price
55 * @var string
56 */
57 protected $discountValue;
58
59 public function __construct($apiKey, $secretKey, $baseUrl)
60 {
61 self::$config = [
62 'apiKey' => $apiKey,
63 'secretKey' => $secretKey,
64 'baseUrl' => $baseUrl,
65 ];
66 }
67
68 public function apiTest()
69 {
70 $iyzipayResource = Model\ApiTest::retrieve(self::getConfig());
71
72 debug_r($iyzipayResource);
73 }
74
75 protected static function getConfig()
76 {
77 $options = new Options();
78 $options->setApiKey(self::$config['apiKey']);
79 $options->setSecretKey(self::$config['secretKey']);
80 $options->setBaseUrl(self::$config['baseUrl']);
81
82 return $options;
83 }
84
85 /**
86 * @param $subMerchantId
87 *
88 * @return bool
89 */
90 public function retrieveSubMerchant($subMerchantId)
91 {
92 if (empty($subMerchantId)) {
93 $this->errors[] = 'Sub Merchant ID could not be empty!';
94
95 return FALSE;
96 }
97
98 $request = new Request\RetrieveSubMerchantRequest();
99 $request->setLocale(Model\Locale::TR);
100 $request->setConversationId(time());
101 $request->setSubMerchantExternalId($subMerchantId);
102 $subMerchant = Model\SubMerchant::retrieve($request, self::getConfig());
103
104 if ($result = $this->_getResult($subMerchant)) {
105 $this->subMerchant = $this->_getSubMerchantFields($subMerchant);
106 }
107
108 return $result;
109 }
110
111 public function installments($price, $cardNumber = NULL)
112 {
113 $request = new Request\RetrieveInstallmentInfoRequest();
114 $request->setLocale(Model\Locale::TR);
115 $request->setConversationId(time());
116 if (!empty($cardNumber)) {
117 $request->setBinNumber(substr($cardNumber, 0, 6));
118 }
119 $request->setPrice($price);
120 $installmentInfo = Model\InstallmentInfo::retrieve($request, self::getConfig());
121
122 if ($result = $this->_getResult($installmentInfo)) {
123 $this->installment = $this->_getInstallmentFields($installmentInfo);
124 }
125
126 return $result;
127 }
128
129 /**
130 * @param $products
131 * @param $paymentInfo
132 * @param $cardInfo
133 * @param $billingInfo
134 * @param $callbackUrl
135 * @param bool $subMerchantKey
136 * @param bool $confirmProduct
137 *
138 * @return bool
139 */
140 public function payment($products, $paymentInfo, $cardInfo, $billingInfo, $callbackUrl, $subMerchantKey = FALSE, $confirmProduct = TRUE)
141 {
142 if (!isset($cardInfo['buying_policy'])) {
143 $this->errors[] = sprintf(lang('form_validation_required'), lang('buying_policy'));
144
145 return FALSE;
146 }
147
148 $this->installmentCount = (!empty($cardInfo['installment']) && !empty(intval($cardInfo['installment']))) ?
149 intval($cardInfo['installment']) : 1;
150
151 if (!empty($this->discountValue)) {
152 $paymentInfo['paidPrice'] = $paymentInfo['paidPrice'] - $this->discountValue;
153 if ($paymentInfo['paidPrice'] <= 0) return TRUE;
154 }
155
156 if ($this->installmentCount > 1) {
157 if ($this->installments($paymentInfo['paidPrice'], preg_replace("/[^\d]*/", '', $cardInfo['card_number']))) {
158 $installments = $this->getInstallment();
159 foreach ($installments[0]['installmentPrices'] as $installment) {
160 if ($this->installmentCount == $installment['installmentNumber']) {
161 $paymentInfo['paidPrice'] = $installment['totalPrice'];
162 }
163 }
164 }
165 }
166
167 $paymentInfo['registerCard'] = (!empty($paymentInfo['registerCard']) && !empty(intval($paymentInfo['registerCard']))) ?
168 intval($paymentInfo['registerCard']) : 0;
169
170 //Create payment request
171 $request = new Request\CreatePaymentRequest();
172 $request->setLocale(Model\Locale::TR);
173 $request->setConversationId(time());
174 $request->setPrice($paymentInfo['price']);
175 //$request->setPrice(1);
176 $request->setPaidPrice($paymentInfo['paidPrice']);
177 //$request->setPaidPrice(1);
178 $request->setCurrency(Model\Currency::TL);
179 $request->setInstallment($this->installmentCount);
180 $request->setBasketId(time());
181 $request->setPaymentChannel(Model\PaymentChannel::WEB);
182 $request->setPaymentGroup(Model\PaymentGroup::PRODUCT);
183
184 if (!empty($cardInfo['3d_payment'])) {
185 $request->setCallbackUrl($callbackUrl);
186 }
187
188 //Set Card info
189 $request->setPaymentCard($this->_setCardInfo($cardInfo, $paymentInfo['registerCard']));
190
191 //Set Buyer information
192 $request->setBuyer($this->_setBuyer($billingInfo));
193
194 //Set Billing information
195 $request->setBillingAddress($this->_setBillingInfo($billingInfo));
196
197 //Set Products
198 $request->setBasketItems($this->_setProducts($products, $subMerchantKey));
199 //log_message('PAYMENT details ;::: ' . print_r($request, TRUE));
200
201 //Make payment request
202 if (!empty($cardInfo['3d_payment'])) {
203 $payment = Model\ThreedsInitialize::create($request, self::getConfig());
204 $this->htmlContent = $payment->getHtmlContent();
205
206 //Set this value to session to check after success payment
207 $_SESSION['registerCard'] = $paymentInfo['registerCard'];
208 $_SESSION['confirmProduct'] = $confirmProduct;
209 } else {
210 $payment = Model\Payment::create($request, self::getConfig());
211 }
212
213 //In 3DS payment after SMS confirmation we should save card and get payment items to approve payment
214 //and this part just run when we do payment directly
215 if ($payment->getStatus() == self::SUCCESS_RESULT && empty($cardInfo['3d_payment'])) {
216 $this->fraudStatus = $payment->getFraudStatus();
217 if ($this->fraudCheck($this->fraudStatus)) {
218 $this->paymentId = $payment->getPaymentId();
219 $this->directPaymentSuccessActions($payment, $paymentInfo['registerCard'], $this->fraudStatus, $confirmProduct);
220 } else {
221 $this->errors[] = lang('fraud_payment');
222
223 return FALSE;
224 }
225 }
226
227 return $this->_getResult($payment);
228 }
229
230 /**
231 * @param Model\Payment $payment
232 * @param int $registerCard
233 * @param int $fraudStatus
234 * @param bool $confirmProduct
235 */
236 protected function directPaymentSuccessActions(Model\Payment $payment, $registerCard, $fraudStatus, $confirmProduct = TRUE)
237 {
238 if ($fraudStatus == self::FRAUD_CONFIRMED) {
239 if (!empty($registerCard)) {
240 // Update user card key *******
241 // if (empty($this->user['card_user_key'])) {
242 // }
243
244 $this->cardToken = $payment->getCardToken();
245 }
246
247 //TODO It should done with Thread
248 foreach ($payment->getPaymentItems() as $paymentItem) {
249 /** @var Model\PaymentItem $paymentItem */
250
251 //TODO if we have more than one product for each selling we should change this algorithm
252 $this->paymentTransactionId = $paymentItem->getPaymentTransactionId();
253
254 if ($confirmProduct) {
255 $this->confirmProduct($paymentItem->getPaymentTransactionId());
256 }
257 }
258 }
259 }
260
261 /**
262 * @param Model\ThreedsPayment $payment
263 * @param int $registerCard
264 * @param bool $confirmProduct
265 */
266 protected function threeDSPaymentSuccessActions(Model\ThreedsPayment $payment, $registerCard, $confirmProduct = TRUE)
267 {
268 if (!empty($registerCard)) {
269 // Update user card key *******
270 // if (empty($this->user['card_user_key'])) {
271 // }
272
273 $this->cardToken = $payment->getCardToken();
274 }
275
276 //TODO It should done with MQ
277 foreach ($payment->getPaymentItems() as $paymentItem) {
278 /** @var Model\PaymentItem $paymentItem */
279
280 //TODO if we have more than one product for each selling we should change this algorithm
281 $this->paymentTransactionId = $paymentItem->getPaymentTransactionId();
282
283 if ($confirmProduct) {
284 $this->confirmProduct($paymentItem->getPaymentTransactionId());
285 }
286 }
287 }
288
289 /**
290 * @param $paymentId
291 * @param $conversationId
292 * @param $conversationData
293 *
294 * @return bool
295 */
296 public function threeDSPayment($paymentId, $conversationId, $conversationData)
297 {
298 $request = new Request\CreateThreedsPaymentRequest();
299 $request->setLocale(Model\Locale::TR);
300 $request->setConversationId($conversationId);
301 $request->setPaymentId($paymentId);
302 $request->setConversationData($conversationData);
303 $threeDSPayment = Model\ThreedsPayment::create($request, self::getConfig());
304
305 if ($threeDSPayment->getStatus() == self::SUCCESS_RESULT) {
306 $this->fraudStatus = $threeDSPayment->getFraudStatus();
307 if ($this->fraudCheck($this->fraudStatus)) {
308 $this->paymentId = $threeDSPayment->getPaymentId();
309 $this->threeDSPaymentSuccessActions($threeDSPayment, $_SESSION['registerCard'], $_SESSION['confirmProduct']);
310 } else {
311 $this->errors[] = lang('fraud_payment');
312
313 return FALSE;
314 }
315 }
316
317 return $this->_getResult($threeDSPayment);
318 }
319
320 /**
321 * @param $products
322 * @param $paymentInfo
323 * @param $cardInfo
324 * @param $billingInfo
325 * @param $callbackUrl
326 * @param bool $subMerchantKey
327 * @param bool $confirmProduct
328 *
329 * @return bool
330 */
331 public function paymentWithRegisteredCard($products, $paymentInfo, $cardInfo, $billingInfo, $callbackUrl, $subMerchantKey = FALSE, $confirmProduct = TRUE)
332 {
333 if (!isset($cardInfo['buying_policy'])) {
334 $this->errors[] = sprintf(lang('form_validation_required'), lang('buying_policy'));
335
336 return FALSE;
337 }
338
339 $this->installmentCount = (!empty($cardInfo['installment']) && !empty(intval($cardInfo['installment']))) ?
340 intval($cardInfo['installment']) : 1;
341 $paymentInfo['registerCard'] = (!empty($paymentInfo['registerCard']) && !empty(intval($paymentInfo['registerCard']))) ?
342 intval($paymentInfo['registerCard']) : 0;
343
344 //Create payment request
345 $request = new Request\CreatePaymentRequest();
346 $request->setLocale(Model\Locale::TR);
347 $request->setConversationId(time());
348 $request->setPrice($paymentInfo['price']);
349 //$request->setPrice(1);
350 $request->setPaidPrice($paymentInfo['paidPrice']);
351 //$request->setPaidPrice(1);
352 $request->setCurrency(Model\Currency::TL);
353 $request->setInstallment($this->installmentCount);
354 $request->setBasketId(time());
355 $request->setPaymentChannel(Model\PaymentChannel::WEB);
356 $request->setPaymentGroup(Model\PaymentGroup::PRODUCT);
357
358 if (!empty($cardInfo['3d_payment'])) {
359 //TODO Test callback page for 3D payment
360 $request->setCallbackUrl($callbackUrl);
361 }
362
363 //Set Card info
364 $paymentCard = new Model\PaymentCard();
365 $paymentCard->setCardUserKey($cardInfo['card_user_key']);
366 $paymentCard->setCardToken($cardInfo['card_token']);
367 $request->setPaymentCard($paymentCard);
368
369 //Set Buyer information
370 $request->setBuyer($this->_setBuyer($billingInfo));
371
372 //Set Billing information
373 $request->setBillingAddress($this->_setBillingInfo($billingInfo));
374
375 //Set Products
376 $request->setBasketItems($this->_setProducts($products, $subMerchantKey));
377
378 //Make payment request
379 if (!empty($cardInfo['3d_payment'])) {
380 $payment = Model\ThreedsInitialize::create($request, self::getConfig());
381 $this->htmlContent = $payment->getHtmlContent();
382
383 //Set this value to session to check after success payment
384 $_SESSION['registerCard'] = $paymentInfo['registerCard'];
385 $_SESSION['confirmProduct'] = $confirmProduct;
386 } else {
387 $payment = Model\Payment::create($request, self::getConfig());
388 }
389
390 //In 3DS payment after SMS confirmation we should save card and get payment items to approve payment
391 //and this part just run when we do payment directly
392 if ($payment->getStatus() == self::SUCCESS_RESULT && empty($cardInfo['3d_payment'])) {
393 $this->fraudStatus = $payment->getFraudStatus();
394 if ($this->fraudCheck($this->fraudStatus)) {
395 $this->paymentId = $payment->getPaymentId();
396 $this->directPaymentSuccessActions($payment, $paymentInfo['registerCard'], $this->fraudStatus, $confirmProduct);
397 } else {
398 $this->errors[] = lang('fraud_payment');
399
400 return FALSE;
401 }
402 }
403
404 return $this->_getResult($payment);
405 }
406
407 protected function _setProducts($products, $subMerchantKey)
408 {
409 $basketItems = array();
410 foreach ($products as $product) {
411 $basketItem = new Model\BasketItem();
412 $basketItem->setId($product['product_id']);
413 $basketItem->setName($product['product_name']);
414 $basketItem->setCategory1($product['product_category']);
415 // $basketItem->setCategory2($product['type']);
416 $basketItem->setItemType(Model\BasketItemType::VIRTUAL);
417 $basketItem->setPrice($product['product_price']);
418 // $basketItem->setPrice(1);
419 // $basketItem->setSubMerchantKey($subMerchantKey);
420 // $basketItem->setSubMerchantPrice($this->_getBasketItemCommission($product['product_price']));
421 // $basketItem->setSubMerchantPrice(1);
422 $basketItems[] = $basketItem;
423 }
424
425 return $basketItems;
426 }
427
428 protected function _setBillingInfo($billingInfo)
429 {
430 $billingAddress = new Model\Address();
431 $billingAddress->setContactName($billingInfo['name']);
432 $billingAddress->setCity($billingInfo['city_label']);
433 $billingAddress->setCountry($billingInfo['country_label']);
434 $billingAddress->setAddress($billingInfo['address']);
435
436 //$billingAddress->setZipCode($billingInfo['zipCode']);
437
438 return $billingAddress;
439 }
440
441 protected function _setBuyer($billingInfo)
442 {
443 //TODO If below information is not filled in user information send warning to fill them
444 $buyer = new Model\Buyer();
445 $buyer->setId($billingInfo['id']);
446 //TODO Get user first name and last name separately
447 $buyer->setName($billingInfo['name']);
448 $buyer->setSurname($billingInfo['sur_name']);
449 $buyer->setGsmNumber($billingInfo['gsm_no']);
450 $buyer->setEmail($billingInfo['email']);
451 $buyer->setIdentityNumber($billingInfo['national_id']);
452 $buyer->setLastLoginDate(date('Y-m-d H:i:s'));
453 $buyer->setRegistrationDate(date('Y-m-d H:i:s'));
454 //TODO Get user address in user settings page
455 $buyer->setRegistrationAddress($billingInfo['address']);
456 $buyer->setIp(getClientIp());
457 $buyer->setCity($billingInfo['city_label']);
458 $buyer->setCountry($billingInfo['country_label']);
459
460 //$buyer->setZipCode("34732");
461
462 return $buyer;
463 }
464
465 protected function _setCardInfo($cardInfo, $registerCard = 0)
466 {
467 $expDate = explode('/', $cardInfo['expire_date']);
468 $expMonth = isset($expDate[0]) ? $expDate[0] : 00;
469 $expYear = isset($expDate[1]) ? $expDate[1] : 0000;
470 $_cardInfo = array(
471 'card_holder' => $cardInfo['card_full_name'],
472 'card_number' => preg_replace("/[^\d]*/", '', $cardInfo['card_number']),
473 'exp_month' => $expMonth,
474 'exp_year' => $expYear,
475 'cvv' => $cardInfo['cvv'],
476 );
477
478 $paymentCard = new Model\PaymentCard();
479 $paymentCard->setCardAlias($_cardInfo['card_holder']);
480 $paymentCard->setCardHolderName($_cardInfo['card_holder']);
481 $paymentCard->setCardNumber($_cardInfo['card_number']);
482 $paymentCard->setExpireMonth($_cardInfo['exp_month']);
483 $paymentCard->setExpireYear($_cardInfo['exp_year']);
484 $paymentCard->setCvc($_cardInfo['cvv']);
485 $paymentCard->setRegisterCard(intval($registerCard));
486
487 if (!empty($registerCard)) {
488 // Use card_user_key to register card in Iyzico
489 $paymentCard->setCardUserKey($cardInfo['card_user_key']);
490 }
491
492 return $paymentCard;
493 }
494
495 /**
496 * @return mixed
497 */
498 public function getErrors()
499 {
500 return $this->errors;
501 }
502
503 /**
504 * @param Iyzipay\IyzipayResource $resource
505 *
506 * @return bool
507 */
508 protected function _getResult(Iyzipay\IyzipayResource $resource)
509 {
510 if ($resource->getStatus() == self::SUCCESS_RESULT) {
511 return TRUE;
512 } else {
513 $debugBackTrace = array();
514 foreach (debug_backtrace() as $item) {
515 unset($item['object']);
516 $debugBackTrace[] = $item;
517 }
518 $this->errors[$resource->getErrorCode()] = $resource->getErrorMessage();
519
520 if ($resource->getErrorCode() == 5069) {
521 $this->errors[$resource->getErrorCode()] = lang('for_pay_with_debit_card_should_use_three_d_payment');
522 }
523
524 return FALSE;
525 }
526 }
527
528 /**
529 * @return Model\SubMerchant
530 */
531 public function getSubMerchant()
532 {
533 return $this->subMerchant;
534 }
535
536 protected function _getSubMerchantFields(Model\SubMerchant $subMerchant)
537 {
538 if (!empty($subMerchant)) {
539 $tempAddress = explode('/', $subMerchant->getAddress());
540 $address = isset($tempAddress[0]) ? trim($tempAddress[0]) : '';
541 $district = isset($tempAddress[1]) ? trim($tempAddress[1]) : '';
542 $city = isset($tempAddress[2]) ? trim($tempAddress[2]) : '';
543 $country = isset($tempAddress[3]) ? trim($tempAddress[3]) : '';
544
545 return array(
546 'name' => $subMerchant->getName(),
547 'email' => $subMerchant->getEmail(),
548 'first_name' => $subMerchant->getContactName(),
549 'last_name' => $subMerchant->getContactSurname(),
550 'company_name' => $subMerchant->getLegalCompanyTitle(),
551 'tax_office' => $subMerchant->getTaxOffice(),
552 'tax_no' => $subMerchant->getTaxNumber(),
553 'address' => $address,
554 'district' => $district,
555 'city' => $city,
556 'country' => $country,
557 'gsm' => $subMerchant->getGsmNumber(),
558 'national_id' => $subMerchant->getIdentityNumber(),
559 'iban' => $subMerchant->getIban(),
560 'type' => $subMerchant->getSubMerchantType(),
561 'subMerchantKey' => $subMerchant->getSubMerchantKey(),
562 );
563 }
564
565 return FALSE;
566 }
567
568 /**
569 * @return Model\InstallmentInfo
570 */
571 public function getInstallment()
572 {
573 return $this->installment;
574 }
575
576 /**
577 * @param Model\InstallmentInfo $installmentInfo
578 *
579 * @return array|bool
580 */
581 protected function _getInstallmentFields(Model\InstallmentInfo $installmentInfo)
582 {
583 if (!empty($installmentInfo)) {
584 $details = array();
585 foreach ($installmentInfo->getInstallmentDetails() as $installmentDetail) {
586 /** @var Model\InstallmentDetail $installmentDetail */
587
588 $prices = array();
589 foreach ($installmentDetail->getInstallmentPrices() as $installmentPrice) {
590 /** @var Model\InstallmentPrice $installmentPrice */
591
592 $prices[] = array(
593 'installmentPrice' => $installmentPrice->getInstallmentPrice(),
594 'totalPrice' => $installmentPrice->getTotalPrice(),
595 'installmentNumber' => $installmentPrice->getInstallmentNumber(),
596 );
597 }
598
599 $details[] = array(
600 'binNumber' => $installmentDetail->getBinNumber(),
601 'price' => $installmentDetail->getPrice(),
602 'cardType' => $installmentDetail->getCardType(),
603 'cardAssociation' => $installmentDetail->getCardAssociation(),
604 'cardFamilyName' => $installmentDetail->getCardFamilyName(),
605 'force3ds' => $installmentDetail->getForce3ds(),
606 'bankCode' => $installmentDetail->getBankCode(),
607 'bankName' => $installmentDetail->getBankName(),
608 'forceCvc' => $installmentDetail->getForceCvc(),
609 'installmentPrices' => $prices,
610 );
611 }
612
613 return $details;
614 }
615
616 return FALSE;
617 }
618
619 public function getThreeDMDStatusMessage($mdStatus)
620 {
621 switch ($mdStatus) {
622 case 0:
623 return lang('three_d_invalid_signature');
624 case 2:
625 return lang('three_d_not_registered_card_owner');
626 case 3:
627 return lang('three_d_not_registered_bank');
628 case 4:
629 return lang('three_d_not_registered_card_owner');
630 case 5:
631 return lang('three_d_not_verified');
632 case 6:
633 return lang('three_d_error');
634 case 7:
635 return lang('three_d_system_error');
636 case 8:
637 return lang('three_d_undefined_card_no');
638 }
639
640 return lang('error_code_not_found');
641 }
642
643 /**
644 * @param $cardUserKey
645 *
646 * @return array|bool
647 */
648 public function retrieveUserCards($cardUserKey)
649 {
650 if (empty($cardUserKey)) {
651 $this->errors[] = 'Card user key is required.';
652
653 return FALSE;
654 }
655
656 $request = new Request\RetrieveCardListRequest();
657 $request->setLocale(Model\Locale::TR);
658 $request->setConversationId(time());
659 $request->setCardUserKey($cardUserKey);
660 $cardList = Model\CardList::retrieve($request, self::getConfig());
661
662 //TODO Should set response to class public property
663
664 if ($this->_getResult($cardList)) {
665 return $cardList->getCardDetails();
666 }
667
668 return FALSE;
669 }
670
671 public function createCard($cardInfo)
672 {
673 $request = new Request\CreateCardRequest();
674 $request->setLocale(Model\Locale::TR);
675 $request->setConversationId(time());
676 $request->setEmail($cardInfo['email']);
677 $request->setExternalId($cardInfo['id']);
678
679 $cardInformation = new Model\CardInformation();
680 $cardInformation->setCardAlias($cardInfo['name']);
681 $cardInformation->setCardHolderName($cardInfo['card_holder']);
682 $cardInformation->setCardNumber($cardInfo['card_number']);
683 $cardInformation->setExpireMonth($cardInfo['exp_month']);
684 $cardInformation->setExpireYear($cardInfo['exp_year']);
685 $request->setCard($cardInformation);
686
687 $card = Model\Card::create($request, self::getConfig());
688
689 if ($result = $this->_getResult($card)) {
690 $this->cardToken = $card->getCardToken();
691 $this->cardUserKey = $card->getCardUserKey();
692
693 // Update user card key *******
694 }
695
696 return $result;
697 }
698
699 public function insertCard($cardUserKey, $cardInfo)
700 {
701 if (empty($cardUserKey) || empty($cardInfo)) {
702 return FALSE;
703 }
704
705 $request = new Request\CreateCardRequest();
706 $request->setLocale(Model\Locale::TR);
707 $request->setConversationId(time());
708 $request->setCardUserKey($cardUserKey);
709
710 $cardInformation = new Model\CardInformation();
711 $cardInformation->setCardAlias($cardInfo['card_holder']);
712 $cardInformation->setCardHolderName($cardInfo['card_holder']);
713 $cardInformation->setCardNumber($cardInfo['card_number']);
714 $cardInformation->setExpireMonth($cardInfo['exp_month']);
715 $cardInformation->setExpireYear($cardInfo['exp_year']);
716 $request->setCard($cardInformation);
717
718 $card = Model\Card::create($request, self::getConfig());
719
720 if ($result = $this->_getResult($card)) {
721 $this->cardToken = $card->getCardToken();
722 $this->cardUserKey = $card->getCardUserKey();
723 }
724
725 return $result;
726 }
727
728 public function confirmProduct($paymentTransactionId)
729 {
730 $request = new Request\CreateApprovalRequest();
731 $request->setLocale(Model\Locale::TR);
732 $request->setConversationId(time());
733 $request->setPaymentTransactionId($paymentTransactionId);
734 $approval = Model\Approval::create($request, self::getConfig());
735
736 return $this->_getResult($approval);
737 }
738
739 /**
740 * @param $fraudStatus
741 *
742 * @return bool
743 */
744 protected function fraudCheck($fraudStatus)
745 {
746 switch ($fraudStatus) {
747 case self::FRAUD_CONFIRMED:
748 case self::FRAUD_CONFIRM_WAITING:
749 return TRUE;
750 case self::FRAUD_FAILED:
751 return FALSE;
752 default:
753 return FALSE;
754 }
755 }
756
757 /**
758 * Calculate discount value from discount code
759 *
760 * @param string $discountCode
761 * @param float $price The price that we want to calculate the discount on it.
762 * @param $productType
763 */
764 public function setDiscountValue($discountCode, $price, $productType)
765 {
766 if (!empty($discountCode)) {
767 // Discount implementation
768 }
769 }
770
771 protected function _getBasketItemCommission($price)
772 {
773 if ($price <= 0) {
774 return FALSE;
775 }
776
777 return $price - ($price * self::BASKET_ITEM_COMMISSION);
778 }
779}