PHP code example of abheranj / iyzipay
1. Go to this page and download the library: Download abheranj/iyzipay library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
abheranj / iyzipay example snippets
'providers' => [
...
Abheranj\Iyzipay\IyzipayServiceProvider::class,
];
'aliases' => [
...
'Iyzipay' => Abheranj\Iyzipay\Facades\IyzipayService::class,
];
# IYZIPAY_PAYMENT_MODE for sanbox will be "test" and for live will be "live"
IYZIPAY_PAYMENT_MODE="test"
SANDBOX_IYZIPAY_BASE_URL="https://sandbox-api.iyzipay.com"
SANDBOX_IYZIPAY_API_KEY="Sanbox Api Key"
SANDBOX_IYZIPAY_SECRET_KEY="Sanbox Secret Key"
LIVE_IYZIPAY_BASE_URL="Live url"
LIVE_IYZIPAY_API_KEY="Live Api Key"
LIVE_IYZIPAY_SECRET_KEY="Live Secret Key"
// Add Credit Card
$card_details = [];
$card_details['alias'] = 'Card'.Auth::user()->id;
$card_details['holder'] = $request->card_name;
$card_details['number'] = $request->card_number;
$card_details['month'] = $request->exp_month;
$card_details['year'] = $request->exp_year;
$iyzipay_key = '';
$CardRes = Iyzipay::addCreditCard(Auth::user()->email, $iyzipay_key, $card_details);
if($CardRes->getStatus() != 'success'){
Session::flash('message', $CardRes->getErrorMessage());
Session::flash('icon', 'warning');
return redirect()->back()->withInput($request->input());
}
// Store in MySQL Database
$Card = $this->CardsRep->AddEditCards(
$request->card_name,
$CardRes->getCardAlias(),
$request->card_number,
$CardRes->getCardToken(),
$CardRes->getCardUserKey(), //iyzipay_key
$CardRes->getCardBankName()
);
// Remove credit card
$CardData = $this->CardsRep->GetCardById($id); // From MySQL Database
$IyzipayCard = Iyzipay::removeCreditCard($CardData->iyzipay_key, $CardData->token);
if ($IyzipayCard->getStatus() != 'success') {
Session::flash('message', $IyzipayCard->getErrorMessage());
Session::flash('icon', 'warning');
return redirect()->back();
}
$CardData->delete(); // Delete From MySQL Database
use Iyzipay\Model\BasketItemType;
use Iyzipay\Model\Currency;
use Illuminate\Support\Facades\DB;
DB::beginTransaction();
$ProductData = $this->ProductRep->GetProductById($request->pid); //From MySQL Database
$CardArr = [];
$CardArr['iyzipay_key'] = $Card->iyzipay_key; // From MySQL Database
$CardArr['token'] = $Card->token;
$BuyerArr = [];
$BuyerArr['id'] = 'B'.Auth::user()->id;
$BuyerArr['firstName'] = Auth::user()->name;
$BuyerArr['lastName'] = Auth::user()->surname;
$BuyerArr['email'] = Auth::user()->email;
$BuyerArr['mobileNumber'] = Auth::user()->phone_no;
$BuyerArr['identityNumber'] = Auth::user()->phone_no;
$BuyerArr['city'] = $request->city;
$BuyerArr['country'] = $request->country;
$BuyerArr['address'] = $request->address;
$AddressArr = [];
$AddressArr['name'] = $request->name;
$AddressArr['city'] = $request->city;
$AddressArr['country'] = $request->country;
$AddressArr['address'] = $request->address;
$AddressArr['zipcode'] = $request->postcode;
$ShipAddressArr = $AddressArr;
$ProductArr = [];
$ProductArr[0]['id'] = $ProductData['id'];
$ProductArr[0]['name'] = $ProductData['name'];
$ProductArr[0]['category'] = 'Cloth';
$ProductArr[0]['type'] = BasketItemType::VIRTUAL;
$ProductArr[0]['price'] = $ProductData['price'];
$Currency = Currency::TL;
$Installment = 1;
$Transaction = Iyzipay::singlePayment($CardArr, $BuyerArr, $AddressArr, $ShipAddressArr, $ProductArr, $Currency, $Installment);
if($Transaction->getStatus() == 'success'){
$Products = [];
$PayoutMerchant = [];
foreach ($Transaction->getPaymentItems() as $paymentItem) {
$Products[] = [
'iyzipay_key' => $paymentItem->getPaymentTransactionId(),
'paidPrice' => $paymentItem->getPaidPrice(),
'product' => [$ProductData]
];
$PayoutMerchant[] = [
'merchantCommissionRate' => $paymentItem->getMerchantCommissionRate(),
'merchantCommissionRateAmount' => $paymentItem->getMerchantCommissionRateAmount(),
'merchantCommissionRateAmount' => $paymentItem->getMerchantCommissionRateAmount(),
'iyziCommissionRateAmount' => $paymentItem->getIyziCommissionRateAmount(),
'iyziCommissionFee' => $paymentItem->getIyziCommissionFee(),
'merchantPayoutAmount' => $paymentItem->getMerchantPayoutAmount()
];
}
// $Transaction->getPaymentId() // iyzipay_key
$Payment = $this->TransactionsRep->AddTransaction($Transaction->getPaidPrice(), $Transaction->getPaymentId(), $Transaction->getCurrency(), $Card->id, $Products, $PayoutMerchant, $AddressArr); // Store in MySQL Database
DB::commit();
Session::flash('message', 'Order placed successfully.');
Session::flash('icon', 'success');
return redirect()->back();
} else {
DB::rollback();
Session::flash('message', $Transaction->getErrorMessage());
Session::flash('icon', 'warning');
return redirect()->back()->withInput($request->input());
}
$transactionObj = $this->TransactionsRep->GetTransactionById($id); // Get From MySQL Database
$IyzipayCancel = Iyzipay::cancelPayment($transactionObj->iyzipay_key);
if ($IyzipayCancel->getStatus() != 'success') {
Session::flash('message', $IyzipayCancel->getErrorMessage());
Session::flash('icon', 'warning');
return redirect()->back();
}
$Cancel[] = [
'type' => 'cancel',
'amount' => $IyzipayCancel->getPrice(),
'iyzipay_key' => $IyzipayCancel->getPaymentId()
];
$this->TransactionsRep->refundTransaction($transactionObj, $Cancel); // Update record in MySQL Database
$transactionObj = $this->TransactionsRep->GetTransactionById($id); // Get From MySQL Database
$Currency = Currency::TL;
$Products = json_decode($transactionObj->products);
$Refunds = [];
foreach ($Products as $Product) {
$IyzipayRefund = Iyzipay::refundPayment($Product->iyzipay_key, $Product->paidPrice, $Currency);
if ($IyzipayRefund->getStatus() != 'success') {
Session::flash('message', $IyzipayRefund->getErrorMessage());
Session::flash('icon', 'warning');
return redirect()->back();
}
$Refunds[] = [
'type' => 'refund',
'amount' => $IyzipayRefund->getPrice(),
'iyzipay_payment_key' => $IyzipayRefund->getPaymentId(),
'iyzipay_transaction_key' => $IyzipayRefund->getPaymentTransactionId(),
'currency' => $IyzipayRefund->getCurrency()
];
}
$this->TransactionsRep->refundTransaction($transactionObj, $Refunds); // Update record in MySQL Database
$Amount = $transactionObj->currency.' '.$IyzipayRefund->getPrice();
Session::flash('message', $Amount.' Amount Refunded successfully.');
Session::flash('icon', 'success');
return redirect()->back();
php
try{
if($request->mdStatus!="1"){
Session::flash('message', __('messages.transactions.mdStatus'.$request->mdStatus));
Session::flash('icon', 'warning');
Session::flash('heading', __('messages.common.warning'));
return redirect()->route('dissubscriptions', ['page_name'=>'Subscription Plans']);
}
if ($request->status != 'success') {
Session::flash('message', __('messages.common.somthing_wrong'));
Session::flash('icon', 'warning');
Session::flash('heading', __('messages.common.warning'));
return redirect()->route('dissubscriptions', ['page_name'=>'Subscription Plans']);
}
if(empty($request->paymentId)){
Session::flash('message', __('messages.common.somthing_wrong'));
Session::flash('icon', 'warning');
Session::flash('heading', __('messages.common.warning'));
return redirect()->route('dissubscriptions', ['page_name'=>'Subscription Plans']);
}
$requestArr = array('paymentId'=>$request->paymentId, 'conversationData'=>$request->conversationData, 'conversationId'=>$request->conversationId);
$Transaction = Iyzipay::PayThreedsPayment($requestArr);
if($Transaction->getStatus() == 'success'){
$Transactions = $Transaction->getPaymentItems();
$SubscriptionData = $this->SubscriptionsRep->GetSubscriptionById($Transactions[0]->getItemId());
$CardToken = $Transaction->getCardToken();
$Card = $this->CardsRep->GetCardByToken($CardToken);
if(empty($SubscriptionData)){
Session::flash('message', __('messages.subscriptions.not_found'));
Session::flash('icon', 'warning');
Session::flash('heading', __('messages.common.warning'));
return redirect()->route('dissubscriptions', ['page_name'=>'Subscription Plans']);
}
$Products = [];
$PayoutMerchant = [];
foreach ($Transaction->getPaymentItems() as $paymentItem) {
$Products[] = [
'iyzipay_key' => $paymentItem->getPaymentTransactionId(),
'paidPrice' => $paymentItem->getPaidPrice(),
'product' => [$SubscriptionData]
];
$PayoutMerchant[] = [
'merchantCommissionRate' => $paymentItem->getMerchantCommissionRate(),
'merchantCommissionRateAmount' => $paymentItem->getMerchantCommissionRateAmount(),
'iyziCommissionRateAmount' => $paymentItem->getIyziCommissionRateAmount(),
'iyziCommissionFee' => $paymentItem->getIyziCommissionFee(),
'merchantPayoutAmount' => $paymentItem->getMerchantPayoutAmount()
];
}
$Payment = $this->TransactionsRep->AddTransaction($Transaction->getPaidPrice(), $Transaction->getPaymentId(), $Transaction->getCurrency(), $Card->id, $Products, $PayoutMerchant, json_decode($Card->billing_info));
DB::commit();
Session::flash('message', __('messages.subscriptions.buy_success'));
Session::flash('icon', 'success');
Session::flash('heading', __('messages.common.success'));
return redirect()->route('dissubscriptions', ['page_name'=>'Subscription Plans']);
} else {
DB::rollback();
Session::flash('message', $Transaction->getErrorMessage());
Session::flash('icon', 'warning');
Session::flash('heading', __('messages.common.warning'));
return redirect()->back()->withInput($request->input());
}
} catch (Exception $e) {
DB::rollback();
return response()->view('error.500', ['message'=>$e->getMessage()], 500);
}