Last active
January 5, 2019 17:18
-
-
Save amirkhiz/5b3707d0a6ec87175aa2bab2563212cf to your computer and use it in GitHub Desktop.
Iyzico wrapper class with example payment and callback page. Iyzico library used v2.0.43 version from this repository https://github.com/iyzico/iyzipay-php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$iyzicoPayment = new IyzicoPayment( | |
config_item('iyzicoApiKey'), | |
config_item('iyzicoSecretKey'), | |
config_item('iyzicoBaseUrl') | |
); | |
//This source used for 3DS payment callbackUrl here we control the posted fields for result | |
if ($_POST['status'] != IyzicoPayment::SUCCESS_RESULT) { | |
die('Error! :::: Payment failed'); | |
} | |
// if MDStatus equal to 1 everything is ok and we can complete the 3D payment | |
$iyzicoMDStatus = !empty($_POST['mdStatus']) ? $_POST['mdStatus'] : NULL; | |
if ($iyzicoMDStatus != IyzicoPayment::SUCCESS_MD_STATUS) { | |
die('Error! :::: ' . $iyzicoPayment->getThreeDMDStatusMessage($iyzicoMDStatus)); | |
} | |
$iyzicoPaymentId = !empty($_POST['paymentId']) ? $_POST['paymentId'] : NULL; | |
$iyzicoConversationData = !empty($_POST['conversationData']) ? $_POST['conversationData'] : NULL; | |
$iyzicoConversationId = !empty($_POST['conversationId']) ? $_POST['conversationId'] : NULL; | |
// ThreeDS payment second step if everything is ok it will response true otherwise false | |
if (!$iyzicoPayment->threeDSPayment($iyzicoPaymentId, $iyzicoConversationId, $iyzicoConversationData)) { | |
echi 'Error! :::: '; | |
print_r($iyzicoPayment->getErrors()); | |
} | |
echo 'OK ;) ::: Congraduations you did it.'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$config['iyzicoApiKey'] = '[API_KEY]'; | |
$config['iyzicoSecretKey'] = '[SECRET_KEY]'; | |
$config['iyzicoBaseUrl'] = 'https://api.iyzipay.com'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Classes\Iyzico; | |
use Iyzipay; | |
use Iyzipay\Model; | |
use Iyzipay\Options; | |
use Iyzipay\Request; | |
/** | |
* Class IyzicoPayment | |
* @author Siavash Habil <amirkhiz@gmail.com> | |
* @package App\IyzicoPayment | |
*/ | |
class IyzicoPayment | |
{ | |
const SUCCESS_RESULT = 'success'; | |
const FAILED_RESULT = 'failure'; | |
const SUCCESS_MD_STATUS = 1; | |
/** Fraud Statuses */ | |
const FRAUD_CONFIRMED = 1; | |
const FRAUD_CONFIRM_WAITING = 0; | |
const FRAUD_FAILED = -1; | |
/** Our commission for every basket item */ | |
const BASKET_ITEM_COMMISSION = 0.06; | |
protected static $config; | |
public $htmlContent; | |
protected $errors; | |
/** | |
* @var Model\SubMerchant | |
*/ | |
protected $subMerchant; | |
public $cardToken; | |
public $cardUserKey; | |
public $installmentCount; | |
public $fraudStatus; | |
public $paymentId; | |
public $paymentTransactionId; | |
/** | |
* @var Model\InstallmentInfo | |
*/ | |
protected $installment; | |
protected $lang; | |
/** | |
* Discount value. This value calculate from pay price and will have the value of discount based on pay price | |
* @var string | |
*/ | |
protected $discountValue; | |
public function __construct($apiKey, $secretKey, $baseUrl) | |
{ | |
self::$config = [ | |
'apiKey' => $apiKey, | |
'secretKey' => $secretKey, | |
'baseUrl' => $baseUrl, | |
]; | |
} | |
public function apiTest() | |
{ | |
$iyzipayResource = Model\ApiTest::retrieve(self::getConfig()); | |
debug_r($iyzipayResource); | |
} | |
protected static function getConfig() | |
{ | |
$options = new Options(); | |
$options->setApiKey(self::$config['apiKey']); | |
$options->setSecretKey(self::$config['secretKey']); | |
$options->setBaseUrl(self::$config['baseUrl']); | |
return $options; | |
} | |
/** | |
* @param $subMerchantId | |
* | |
* @return bool | |
*/ | |
public function retrieveSubMerchant($subMerchantId) | |
{ | |
if (empty($subMerchantId)) { | |
$this->errors[] = 'Sub Merchant ID could not be empty!'; | |
return FALSE; | |
} | |
$request = new Request\RetrieveSubMerchantRequest(); | |
$request->setLocale(Model\Locale::TR); | |
$request->setConversationId(time()); | |
$request->setSubMerchantExternalId($subMerchantId); | |
$subMerchant = Model\SubMerchant::retrieve($request, self::getConfig()); | |
if ($result = $this->_getResult($subMerchant)) { | |
$this->subMerchant = $this->_getSubMerchantFields($subMerchant); | |
} | |
return $result; | |
} | |
public function installments($price, $cardNumber = NULL) | |
{ | |
$request = new Request\RetrieveInstallmentInfoRequest(); | |
$request->setLocale(Model\Locale::TR); | |
$request->setConversationId(time()); | |
if (!empty($cardNumber)) { | |
$request->setBinNumber(substr($cardNumber, 0, 6)); | |
} | |
$request->setPrice($price); | |
$installmentInfo = Model\InstallmentInfo::retrieve($request, self::getConfig()); | |
if ($result = $this->_getResult($installmentInfo)) { | |
$this->installment = $this->_getInstallmentFields($installmentInfo); | |
} | |
return $result; | |
} | |
/** | |
* @param $products | |
* @param $paymentInfo | |
* @param $cardInfo | |
* @param $billingInfo | |
* @param $callbackUrl | |
* @param bool $subMerchantKey | |
* @param bool $confirmProduct | |
* | |
* @return bool | |
*/ | |
public function payment($products, $paymentInfo, $cardInfo, $billingInfo, $callbackUrl, $subMerchantKey = FALSE, $confirmProduct = TRUE) | |
{ | |
if (!isset($cardInfo['buying_policy'])) { | |
$this->errors[] = sprintf(lang('form_validation_required'), lang('buying_policy')); | |
return FALSE; | |
} | |
$this->installmentCount = (!empty($cardInfo['installment']) && !empty(intval($cardInfo['installment']))) ? | |
intval($cardInfo['installment']) : 1; | |
if (!empty($this->discountValue)) { | |
$paymentInfo['paidPrice'] = $paymentInfo['paidPrice'] - $this->discountValue; | |
if ($paymentInfo['paidPrice'] <= 0) return TRUE; | |
} | |
if ($this->installmentCount > 1) { | |
if ($this->installments($paymentInfo['paidPrice'], preg_replace("/[^\d]*/", '', $cardInfo['card_number']))) { | |
$installments = $this->getInstallment(); | |
foreach ($installments[0]['installmentPrices'] as $installment) { | |
if ($this->installmentCount == $installment['installmentNumber']) { | |
$paymentInfo['paidPrice'] = $installment['totalPrice']; | |
} | |
} | |
} | |
} | |
$paymentInfo['registerCard'] = (!empty($paymentInfo['registerCard']) && !empty(intval($paymentInfo['registerCard']))) ? | |
intval($paymentInfo['registerCard']) : 0; | |
//Create payment request | |
$request = new Request\CreatePaymentRequest(); | |
$request->setLocale(Model\Locale::TR); | |
$request->setConversationId(time()); | |
$request->setPrice($paymentInfo['price']); | |
//$request->setPrice(1); | |
$request->setPaidPrice($paymentInfo['paidPrice']); | |
//$request->setPaidPrice(1); | |
$request->setCurrency(Model\Currency::TL); | |
$request->setInstallment($this->installmentCount); | |
$request->setBasketId(time()); | |
$request->setPaymentChannel(Model\PaymentChannel::WEB); | |
$request->setPaymentGroup(Model\PaymentGroup::PRODUCT); | |
if (!empty($cardInfo['3d_payment'])) { | |
$request->setCallbackUrl($callbackUrl); | |
} | |
//Set Card info | |
$request->setPaymentCard($this->_setCardInfo($cardInfo, $paymentInfo['registerCard'])); | |
//Set Buyer information | |
$request->setBuyer($this->_setBuyer($billingInfo)); | |
//Set Billing information | |
$request->setBillingAddress($this->_setBillingInfo($billingInfo)); | |
//Set Products | |
$request->setBasketItems($this->_setProducts($products, $subMerchantKey)); | |
//log_message('PAYMENT details ;::: ' . print_r($request, TRUE)); | |
//Make payment request | |
if (!empty($cardInfo['3d_payment'])) { | |
$payment = Model\ThreedsInitialize::create($request, self::getConfig()); | |
$this->htmlContent = $payment->getHtmlContent(); | |
//Set this value to session to check after success payment | |
$_SESSION['registerCard'] = $paymentInfo['registerCard']; | |
$_SESSION['confirmProduct'] = $confirmProduct; | |
} else { | |
$payment = Model\Payment::create($request, self::getConfig()); | |
} | |
//In 3DS payment after SMS confirmation we should save card and get payment items to approve payment | |
//and this part just run when we do payment directly | |
if ($payment->getStatus() == self::SUCCESS_RESULT && empty($cardInfo['3d_payment'])) { | |
$this->fraudStatus = $payment->getFraudStatus(); | |
if ($this->fraudCheck($this->fraudStatus)) { | |
$this->paymentId = $payment->getPaymentId(); | |
$this->directPaymentSuccessActions($payment, $paymentInfo['registerCard'], $this->fraudStatus, $confirmProduct); | |
} else { | |
$this->errors[] = lang('fraud_payment'); | |
return FALSE; | |
} | |
} | |
return $this->_getResult($payment); | |
} | |
/** | |
* @param Model\Payment $payment | |
* @param int $registerCard | |
* @param int $fraudStatus | |
* @param bool $confirmProduct | |
*/ | |
protected function directPaymentSuccessActions(Model\Payment $payment, $registerCard, $fraudStatus, $confirmProduct = TRUE) | |
{ | |
if ($fraudStatus == self::FRAUD_CONFIRMED) { | |
if (!empty($registerCard)) { | |
// Update user card key ******* | |
// if (empty($this->user['card_user_key'])) { | |
// } | |
$this->cardToken = $payment->getCardToken(); | |
} | |
//TODO It should done with Thread | |
foreach ($payment->getPaymentItems() as $paymentItem) { | |
/** @var Model\PaymentItem $paymentItem */ | |
//TODO if we have more than one product for each selling we should change this algorithm | |
$this->paymentTransactionId = $paymentItem->getPaymentTransactionId(); | |
if ($confirmProduct) { | |
$this->confirmProduct($paymentItem->getPaymentTransactionId()); | |
} | |
} | |
} | |
} | |
/** | |
* @param Model\ThreedsPayment $payment | |
* @param int $registerCard | |
* @param bool $confirmProduct | |
*/ | |
protected function threeDSPaymentSuccessActions(Model\ThreedsPayment $payment, $registerCard, $confirmProduct = TRUE) | |
{ | |
if (!empty($registerCard)) { | |
// Update user card key ******* | |
// if (empty($this->user['card_user_key'])) { | |
// } | |
$this->cardToken = $payment->getCardToken(); | |
} | |
//TODO It should done with MQ | |
foreach ($payment->getPaymentItems() as $paymentItem) { | |
/** @var Model\PaymentItem $paymentItem */ | |
//TODO if we have more than one product for each selling we should change this algorithm | |
$this->paymentTransactionId = $paymentItem->getPaymentTransactionId(); | |
if ($confirmProduct) { | |
$this->confirmProduct($paymentItem->getPaymentTransactionId()); | |
} | |
} | |
} | |
/** | |
* @param $paymentId | |
* @param $conversationId | |
* @param $conversationData | |
* | |
* @return bool | |
*/ | |
public function threeDSPayment($paymentId, $conversationId, $conversationData) | |
{ | |
$request = new Request\CreateThreedsPaymentRequest(); | |
$request->setLocale(Model\Locale::TR); | |
$request->setConversationId($conversationId); | |
$request->setPaymentId($paymentId); | |
$request->setConversationData($conversationData); | |
$threeDSPayment = Model\ThreedsPayment::create($request, self::getConfig()); | |
if ($threeDSPayment->getStatus() == self::SUCCESS_RESULT) { | |
$this->fraudStatus = $threeDSPayment->getFraudStatus(); | |
if ($this->fraudCheck($this->fraudStatus)) { | |
$this->paymentId = $threeDSPayment->getPaymentId(); | |
$this->threeDSPaymentSuccessActions($threeDSPayment, $_SESSION['registerCard'], $_SESSION['confirmProduct']); | |
} else { | |
$this->errors[] = lang('fraud_payment'); | |
return FALSE; | |
} | |
} | |
return $this->_getResult($threeDSPayment); | |
} | |
/** | |
* @param $products | |
* @param $paymentInfo | |
* @param $cardInfo | |
* @param $billingInfo | |
* @param $callbackUrl | |
* @param bool $subMerchantKey | |
* @param bool $confirmProduct | |
* | |
* @return bool | |
*/ | |
public function paymentWithRegisteredCard($products, $paymentInfo, $cardInfo, $billingInfo, $callbackUrl, $subMerchantKey = FALSE, $confirmProduct = TRUE) | |
{ | |
if (!isset($cardInfo['buying_policy'])) { | |
$this->errors[] = sprintf(lang('form_validation_required'), lang('buying_policy')); | |
return FALSE; | |
} | |
$this->installmentCount = (!empty($cardInfo['installment']) && !empty(intval($cardInfo['installment']))) ? | |
intval($cardInfo['installment']) : 1; | |
$paymentInfo['registerCard'] = (!empty($paymentInfo['registerCard']) && !empty(intval($paymentInfo['registerCard']))) ? | |
intval($paymentInfo['registerCard']) : 0; | |
//Create payment request | |
$request = new Request\CreatePaymentRequest(); | |
$request->setLocale(Model\Locale::TR); | |
$request->setConversationId(time()); | |
$request->setPrice($paymentInfo['price']); | |
//$request->setPrice(1); | |
$request->setPaidPrice($paymentInfo['paidPrice']); | |
//$request->setPaidPrice(1); | |
$request->setCurrency(Model\Currency::TL); | |
$request->setInstallment($this->installmentCount); | |
$request->setBasketId(time()); | |
$request->setPaymentChannel(Model\PaymentChannel::WEB); | |
$request->setPaymentGroup(Model\PaymentGroup::PRODUCT); | |
if (!empty($cardInfo['3d_payment'])) { | |
//TODO Test callback page for 3D payment | |
$request->setCallbackUrl($callbackUrl); | |
} | |
//Set Card info | |
$paymentCard = new Model\PaymentCard(); | |
$paymentCard->setCardUserKey($cardInfo['card_user_key']); | |
$paymentCard->setCardToken($cardInfo['card_token']); | |
$request->setPaymentCard($paymentCard); | |
//Set Buyer information | |
$request->setBuyer($this->_setBuyer($billingInfo)); | |
//Set Billing information | |
$request->setBillingAddress($this->_setBillingInfo($billingInfo)); | |
//Set Products | |
$request->setBasketItems($this->_setProducts($products, $subMerchantKey)); | |
//Make payment request | |
if (!empty($cardInfo['3d_payment'])) { | |
$payment = Model\ThreedsInitialize::create($request, self::getConfig()); | |
$this->htmlContent = $payment->getHtmlContent(); | |
//Set this value to session to check after success payment | |
$_SESSION['registerCard'] = $paymentInfo['registerCard']; | |
$_SESSION['confirmProduct'] = $confirmProduct; | |
} else { | |
$payment = Model\Payment::create($request, self::getConfig()); | |
} | |
//In 3DS payment after SMS confirmation we should save card and get payment items to approve payment | |
//and this part just run when we do payment directly | |
if ($payment->getStatus() == self::SUCCESS_RESULT && empty($cardInfo['3d_payment'])) { | |
$this->fraudStatus = $payment->getFraudStatus(); | |
if ($this->fraudCheck($this->fraudStatus)) { | |
$this->paymentId = $payment->getPaymentId(); | |
$this->directPaymentSuccessActions($payment, $paymentInfo['registerCard'], $this->fraudStatus, $confirmProduct); | |
} else { | |
$this->errors[] = lang('fraud_payment'); | |
return FALSE; | |
} | |
} | |
return $this->_getResult($payment); | |
} | |
protected function _setProducts($products, $subMerchantKey) | |
{ | |
$basketItems = array(); | |
foreach ($products as $product) { | |
$basketItem = new Model\BasketItem(); | |
$basketItem->setId($product['product_id']); | |
$basketItem->setName($product['product_name']); | |
$basketItem->setCategory1($product['product_category']); | |
// $basketItem->setCategory2($product['type']); | |
$basketItem->setItemType(Model\BasketItemType::VIRTUAL); | |
$basketItem->setPrice($product['product_price']); | |
// $basketItem->setPrice(1); | |
// $basketItem->setSubMerchantKey($subMerchantKey); | |
// $basketItem->setSubMerchantPrice($this->_getBasketItemCommission($product['product_price'])); | |
// $basketItem->setSubMerchantPrice(1); | |
$basketItems[] = $basketItem; | |
} | |
return $basketItems; | |
} | |
protected function _setBillingInfo($billingInfo) | |
{ | |
$billingAddress = new Model\Address(); | |
$billingAddress->setContactName($billingInfo['name']); | |
$billingAddress->setCity($billingInfo['city_label']); | |
$billingAddress->setCountry($billingInfo['country_label']); | |
$billingAddress->setAddress($billingInfo['address']); | |
//$billingAddress->setZipCode($billingInfo['zipCode']); | |
return $billingAddress; | |
} | |
protected function _setBuyer($billingInfo) | |
{ | |
//TODO If below information is not filled in user information send warning to fill them | |
$buyer = new Model\Buyer(); | |
$buyer->setId($billingInfo['id']); | |
//TODO Get user first name and last name separately | |
$buyer->setName($billingInfo['name']); | |
$buyer->setSurname($billingInfo['sur_name']); | |
$buyer->setGsmNumber($billingInfo['gsm_no']); | |
$buyer->setEmail($billingInfo['email']); | |
$buyer->setIdentityNumber($billingInfo['national_id']); | |
$buyer->setLastLoginDate(date('Y-m-d H:i:s')); | |
$buyer->setRegistrationDate(date('Y-m-d H:i:s')); | |
//TODO Get user address in user settings page | |
$buyer->setRegistrationAddress($billingInfo['address']); | |
$buyer->setIp(getClientIp()); | |
$buyer->setCity($billingInfo['city_label']); | |
$buyer->setCountry($billingInfo['country_label']); | |
//$buyer->setZipCode("34732"); | |
return $buyer; | |
} | |
protected function _setCardInfo($cardInfo, $registerCard = 0) | |
{ | |
$expDate = explode('/', $cardInfo['expire_date']); | |
$expMonth = isset($expDate[0]) ? $expDate[0] : 00; | |
$expYear = isset($expDate[1]) ? $expDate[1] : 0000; | |
$_cardInfo = array( | |
'card_holder' => $cardInfo['card_full_name'], | |
'card_number' => preg_replace("/[^\d]*/", '', $cardInfo['card_number']), | |
'exp_month' => $expMonth, | |
'exp_year' => $expYear, | |
'cvv' => $cardInfo['cvv'], | |
); | |
$paymentCard = new Model\PaymentCard(); | |
$paymentCard->setCardAlias($_cardInfo['card_holder']); | |
$paymentCard->setCardHolderName($_cardInfo['card_holder']); | |
$paymentCard->setCardNumber($_cardInfo['card_number']); | |
$paymentCard->setExpireMonth($_cardInfo['exp_month']); | |
$paymentCard->setExpireYear($_cardInfo['exp_year']); | |
$paymentCard->setCvc($_cardInfo['cvv']); | |
$paymentCard->setRegisterCard(intval($registerCard)); | |
if (!empty($registerCard)) { | |
// Use card_user_key to register card in Iyzico | |
$paymentCard->setCardUserKey($cardInfo['card_user_key']); | |
} | |
return $paymentCard; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getErrors() | |
{ | |
return $this->errors; | |
} | |
/** | |
* @param Iyzipay\IyzipayResource $resource | |
* | |
* @return bool | |
*/ | |
protected function _getResult(Iyzipay\IyzipayResource $resource) | |
{ | |
if ($resource->getStatus() == self::SUCCESS_RESULT) { | |
return TRUE; | |
} else { | |
$debugBackTrace = array(); | |
foreach (debug_backtrace() as $item) { | |
unset($item['object']); | |
$debugBackTrace[] = $item; | |
} | |
$this->errors[$resource->getErrorCode()] = $resource->getErrorMessage(); | |
if ($resource->getErrorCode() == 5069) { | |
$this->errors[$resource->getErrorCode()] = lang('for_pay_with_debit_card_should_use_three_d_payment'); | |
} | |
return FALSE; | |
} | |
} | |
/** | |
* @return Model\SubMerchant | |
*/ | |
public function getSubMerchant() | |
{ | |
return $this->subMerchant; | |
} | |
protected function _getSubMerchantFields(Model\SubMerchant $subMerchant) | |
{ | |
if (!empty($subMerchant)) { | |
$tempAddress = explode('/', $subMerchant->getAddress()); | |
$address = isset($tempAddress[0]) ? trim($tempAddress[0]) : ''; | |
$district = isset($tempAddress[1]) ? trim($tempAddress[1]) : ''; | |
$city = isset($tempAddress[2]) ? trim($tempAddress[2]) : ''; | |
$country = isset($tempAddress[3]) ? trim($tempAddress[3]) : ''; | |
return array( | |
'name' => $subMerchant->getName(), | |
'email' => $subMerchant->getEmail(), | |
'first_name' => $subMerchant->getContactName(), | |
'last_name' => $subMerchant->getContactSurname(), | |
'company_name' => $subMerchant->getLegalCompanyTitle(), | |
'tax_office' => $subMerchant->getTaxOffice(), | |
'tax_no' => $subMerchant->getTaxNumber(), | |
'address' => $address, | |
'district' => $district, | |
'city' => $city, | |
'country' => $country, | |
'gsm' => $subMerchant->getGsmNumber(), | |
'national_id' => $subMerchant->getIdentityNumber(), | |
'iban' => $subMerchant->getIban(), | |
'type' => $subMerchant->getSubMerchantType(), | |
'subMerchantKey' => $subMerchant->getSubMerchantKey(), | |
); | |
} | |
return FALSE; | |
} | |
/** | |
* @return Model\InstallmentInfo | |
*/ | |
public function getInstallment() | |
{ | |
return $this->installment; | |
} | |
/** | |
* @param Model\InstallmentInfo $installmentInfo | |
* | |
* @return array|bool | |
*/ | |
protected function _getInstallmentFields(Model\InstallmentInfo $installmentInfo) | |
{ | |
if (!empty($installmentInfo)) { | |
$details = array(); | |
foreach ($installmentInfo->getInstallmentDetails() as $installmentDetail) { | |
/** @var Model\InstallmentDetail $installmentDetail */ | |
$prices = array(); | |
foreach ($installmentDetail->getInstallmentPrices() as $installmentPrice) { | |
/** @var Model\InstallmentPrice $installmentPrice */ | |
$prices[] = array( | |
'installmentPrice' => $installmentPrice->getInstallmentPrice(), | |
'totalPrice' => $installmentPrice->getTotalPrice(), | |
'installmentNumber' => $installmentPrice->getInstallmentNumber(), | |
); | |
} | |
$details[] = array( | |
'binNumber' => $installmentDetail->getBinNumber(), | |
'price' => $installmentDetail->getPrice(), | |
'cardType' => $installmentDetail->getCardType(), | |
'cardAssociation' => $installmentDetail->getCardAssociation(), | |
'cardFamilyName' => $installmentDetail->getCardFamilyName(), | |
'force3ds' => $installmentDetail->getForce3ds(), | |
'bankCode' => $installmentDetail->getBankCode(), | |
'bankName' => $installmentDetail->getBankName(), | |
'forceCvc' => $installmentDetail->getForceCvc(), | |
'installmentPrices' => $prices, | |
); | |
} | |
return $details; | |
} | |
return FALSE; | |
} | |
public function getThreeDMDStatusMessage($mdStatus) | |
{ | |
switch ($mdStatus) { | |
case 0: | |
return lang('three_d_invalid_signature'); | |
case 2: | |
return lang('three_d_not_registered_card_owner'); | |
case 3: | |
return lang('three_d_not_registered_bank'); | |
case 4: | |
return lang('three_d_not_registered_card_owner'); | |
case 5: | |
return lang('three_d_not_verified'); | |
case 6: | |
return lang('three_d_error'); | |
case 7: | |
return lang('three_d_system_error'); | |
case 8: | |
return lang('three_d_undefined_card_no'); | |
} | |
return lang('error_code_not_found'); | |
} | |
/** | |
* @param $cardUserKey | |
* | |
* @return array|bool | |
*/ | |
public function retrieveUserCards($cardUserKey) | |
{ | |
if (empty($cardUserKey)) { | |
$this->errors[] = 'Card user key is required.'; | |
return FALSE; | |
} | |
$request = new Request\RetrieveCardListRequest(); | |
$request->setLocale(Model\Locale::TR); | |
$request->setConversationId(time()); | |
$request->setCardUserKey($cardUserKey); | |
$cardList = Model\CardList::retrieve($request, self::getConfig()); | |
//TODO Should set response to class public property | |
if ($this->_getResult($cardList)) { | |
return $cardList->getCardDetails(); | |
} | |
return FALSE; | |
} | |
public function createCard($cardInfo) | |
{ | |
$request = new Request\CreateCardRequest(); | |
$request->setLocale(Model\Locale::TR); | |
$request->setConversationId(time()); | |
$request->setEmail($cardInfo['email']); | |
$request->setExternalId($cardInfo['id']); | |
$cardInformation = new Model\CardInformation(); | |
$cardInformation->setCardAlias($cardInfo['name']); | |
$cardInformation->setCardHolderName($cardInfo['card_holder']); | |
$cardInformation->setCardNumber($cardInfo['card_number']); | |
$cardInformation->setExpireMonth($cardInfo['exp_month']); | |
$cardInformation->setExpireYear($cardInfo['exp_year']); | |
$request->setCard($cardInformation); | |
$card = Model\Card::create($request, self::getConfig()); | |
if ($result = $this->_getResult($card)) { | |
$this->cardToken = $card->getCardToken(); | |
$this->cardUserKey = $card->getCardUserKey(); | |
// Update user card key ******* | |
} | |
return $result; | |
} | |
public function insertCard($cardUserKey, $cardInfo) | |
{ | |
if (empty($cardUserKey) || empty($cardInfo)) { | |
return FALSE; | |
} | |
$request = new Request\CreateCardRequest(); | |
$request->setLocale(Model\Locale::TR); | |
$request->setConversationId(time()); | |
$request->setCardUserKey($cardUserKey); | |
$cardInformation = new Model\CardInformation(); | |
$cardInformation->setCardAlias($cardInfo['card_holder']); | |
$cardInformation->setCardHolderName($cardInfo['card_holder']); | |
$cardInformation->setCardNumber($cardInfo['card_number']); | |
$cardInformation->setExpireMonth($cardInfo['exp_month']); | |
$cardInformation->setExpireYear($cardInfo['exp_year']); | |
$request->setCard($cardInformation); | |
$card = Model\Card::create($request, self::getConfig()); | |
if ($result = $this->_getResult($card)) { | |
$this->cardToken = $card->getCardToken(); | |
$this->cardUserKey = $card->getCardUserKey(); | |
} | |
return $result; | |
} | |
public function confirmProduct($paymentTransactionId) | |
{ | |
$request = new Request\CreateApprovalRequest(); | |
$request->setLocale(Model\Locale::TR); | |
$request->setConversationId(time()); | |
$request->setPaymentTransactionId($paymentTransactionId); | |
$approval = Model\Approval::create($request, self::getConfig()); | |
return $this->_getResult($approval); | |
} | |
/** | |
* @param $fraudStatus | |
* | |
* @return bool | |
*/ | |
protected function fraudCheck($fraudStatus) | |
{ | |
switch ($fraudStatus) { | |
case self::FRAUD_CONFIRMED: | |
case self::FRAUD_CONFIRM_WAITING: | |
return TRUE; | |
case self::FRAUD_FAILED: | |
return FALSE; | |
default: | |
return FALSE; | |
} | |
} | |
/** | |
* Calculate discount value from discount code | |
* | |
* @param string $discountCode | |
* @param float $price The price that we want to calculate the discount on it. | |
* @param $productType | |
*/ | |
public function setDiscountValue($discountCode, $price, $productType) | |
{ | |
if (!empty($discountCode)) { | |
// Discount implementation | |
} | |
} | |
protected function _getBasketItemCommission($price) | |
{ | |
if ($price <= 0) { | |
return FALSE; | |
} | |
return $price - ($price * self::BASKET_ITEM_COMMISSION); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$iyzicoPayment = new IyzicoPayment( | |
config_item('iyzicoApiKey'), | |
config_item('iyzicoSecretKey'), | |
config_item('iyzicoBaseUrl') | |
); | |
$products[] = [ | |
'product_id' => 1, | |
'product_name' => 'Test product', | |
'product_category' => 'Test Category', | |
'product_price' => 1, | |
]; | |
$paymentInfo = [ | |
'price' => 1, | |
'paidPrice' => 1, | |
'installment' => 1, | |
'registerCard' => FALSE, | |
]; | |
$cardInfo = [ | |
'card_full_name' => '', | |
'card_number' => 'xxxxxxxxxxxxxxxx', | |
'expire_date' => 'xx/xxxx', | |
'cvv' => xxx, | |
'installment' => 1, | |
'buying_policy' => TRUE, | |
'3d_payment' => TRUE, | |
]; | |
$billingInfo = [ | |
'id' => 1, | |
'name' => 'John', | |
'sur_name' => 'Doe', | |
'gsm_no' => 'xxxxxxxxxx', | |
'email' => 'john.doe@example.com', | |
'country_label' => '[Country]', | |
'city_label' => '[City]', | |
'address' => '[Address]', | |
'national_id' => '[NationalId]', | |
]; | |
$result = $iyzicoPayment->payment($products, $paymentInfo, $cardInfo, $billingInfo, '[CallbackUrl]'); | |
// Result of request | |
echo $result; | |
// Print errors if exist | |
print_r($iyzicoPayment->getErrors()); | |
// Echo html content of 3DS payment comes from iyzico | |
echo $iyzicoPayment->htmlContent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this class is really huge and is not really understandable but i added this here because yesterday one of my friends asked something like this because of iyzico docs is really confusable and not understandable. Hope this code help someone.
NOTE: I suggest you to not to use this code for production.