Skip to content

Instantly share code, notes, and snippets.

@DarkcoderSe
Forked from Miri92/Payment.php
Last active January 1, 2021 06:30
Show Gist options
  • Save DarkcoderSe/318c256beb8e50d778dc2c4f4bf5e95f to your computer and use it in GitHub Desktop.
Save DarkcoderSe/318c256beb8e50d778dc2c4f4bf5e95f to your computer and use it in GitHub Desktop.
KapitalBank Payment API with PHP Laravel framework - Example snippet
<?php
namespace App\Models;
use App\Database\EloquentModel as Model;
class Payment extends Model
{
protected $fillable = ['order_id', 'session_id', 'currency', 'order_status', 'order_description', 'amount', 'payment_url', 'status_code','order_check_status','language_code'];
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\{ Payment };
use Illuminate\Support\Facades\{DB, File, Hash, Storage, Validator, Config, Auth, Mail};
use SimpleXMLElement;
use App\Traits\Log;
class PaymentKapitalController extends Controller
{
protected $serviceUrl = 'https://e-commerce.kapitalbank.az:5443/Exec';
protected $cert = "kapitalbank_certificates/templ.crt";
protected $key = "kapitalbank_certificates/merchant_name2.key";
protected $merchant_id = 'E1000010';
protected $language = 'RU';
const PORT = 5443;
public function __construct()
{
if (Storage::disk('local')->exists($this->cert)) {
$this->cert = storage_path('app/'.$this->cert);
} else {
throw new \Exception("Certificate does not exists: $this->cert");
}
if (Storage::disk('local')->exists($this->key)) {
$this->key = storage_path('app/'.$this->key);
} else {
throw new \Exception("Key does not exists: $this->key");
}
}
public function index(){
return 'index';
}
public function curl($xml){
$url = $this->serviceUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, self::PORT);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLCERT, $this->cert);
curl_setopt($ch, CURLOPT_SSLKEY, $this->key);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//Error handling and return result
$data = curl_exec($ch);
if ($data === false) {
$result = curl_error($ch);
} else {
$result = $data;
}
// Close handle
curl_close($ch);
return $result;
}
public function createTestOrder(){
//echo header("Location: ");
$order_data = array(
'merchant' => $this->merchant_id,
'amount' => 1,
'currency' => 944,
'description' => 'Templateplanet Purchase',
'lang' => 'RU'
);
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<TKKPG>
<Request>
<Operation>CreateOrder</Operation>
<Language>'.$order_data['lang'].'</Language>
<Order>
<OrderType>Purchase</OrderType>
<Merchant>'.$order_data['merchant'].'</Merchant>
<Amount>'.$order_data['amount'].'</Amount>
<Currency>'.$order_data['currency'].'</Currency>
<Description>'.$order_data['description'].'</Description>
<ApproveURL>https://templateplanet.az/en/kapital/approve</ApproveURL>
<CancelURL>https://templateplanet.az/en/kapital/cancel</CancelURL>
<DeclineURL>https://templateplanet.az/en/kapital/decline</DeclineURL>
</Order>
</Request>
</TKKPG>
';
//return $xml;
$result = $this->curl($xml);
return $this->handleCurlResponse($order_data,$result);
//dd($result);
// $result;
}
public function handleCurlResponse($inital_data, $data){
$oXML = new SimpleXMLElement($data);
//dd($oXML);
$OrderID = $oXML->Response->Order->OrderID;
$SessionID = $oXML->Response->Order->SessionID;
$paymentBaseUrl = $oXML->Response->Order->URL;
Payment::create([
'amount' => $inital_data['amount'],
'order_id' => $OrderID,
'session_id' => $SessionID,
'payment_url' => $paymentBaseUrl,
'staus_code' => $oXML->Response->Status,
'order_description' => $inital_data['description'],
'currency' => $inital_data['currency'],
'language_code' => $inital_data['currency'],
]);
///
$redirectUrl = $paymentBaseUrl."?ORDERID=".$OrderID."&SESSIONID=".$SessionID."&";
//dd($redirectUrl);
//echo $redirectUrl;
return redirect()->to($redirectUrl);;
//return header("Location: ");
}
public function approveUrl(Request $request){
Log::write('approveUrl','kapitalBank',$request->all());
$xmlmsg = new SimpleXMLElement($request->xmlmsg);
$getPaymentRow = Payment::where('order_id', '=', $xmlmsg->OrderID)->first();
if($getPaymentRow){
$getPaymentRow->update([
'order_status' => $xmlmsg->OrderStatus,
]);
$this->getOrderStatus($getPaymentRow);
}
return 'approve';
}
public function cancelUrl(Request $request){
//echo $request->xmlmsg;
$xmlmsg = new SimpleXMLElement($request->xmlmsg);
Log::write('cancelUrl','kapitalBank',$request->all());
$getPaymentRow = Payment::where('order_id', '=', $xmlmsg->OrderID)->first();
if($getPaymentRow){
$getPaymentRow->update([
'order_status' => $xmlmsg->OrderStatus,
]);
}
return 'cancel';
}
public function declineUrl(Request $request){
//dd($request->all());
Log::write('declineUrl','kapitalBank',$request->all());
if ($request->filled('xmlmsg')){
$xmlmsg = new SimpleXMLElement($request->xmlmsg);
//dd($xmlmsg->OrderStatus);
$getPaymentRow = Payment::where('order_id', '=', $xmlmsg->OrderID)->first();
if($getPaymentRow){
$getPaymentRow->update([
'order_status' => $xmlmsg->OrderStatus,
]);
}
}
return 'DECLINED';
}
//Internet shop must perform the Get Order Status operation for the security purposes and decide whether to provide the service or not depending on the response.
public function getOrderStatus($data){
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<TKKPG>
<Request>
<Operation>GetOrderStatus</Operation>
<Language>'.$this->language.'</Language>
<Order>
<Merchant>'.$this->merchant_id.'</Merchant>
<OrderID>'.$data->order_id.'</OrderID>
</Order>
<SessionID>'.$data->session_id.'</SessionID>
</Request>
</TKKPG>';
$response = $this->curl($xml);
$xmlmsg = new SimpleXMLElement($response);
//dd($xmlmsg->Response->Status);
$getPaymentRow = Payment::where('order_id', '=', $xmlmsg->Response->Order->OrderID)->first();
if($getPaymentRow){
$getPaymentRow->update([
'order_check_status' => $xmlmsg->Response->Order->OrderStatus,
'status_code' => $xmlmsg->Response->Status,
]);
}
return $response;
}
}
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
protected $addHttpCookie = true;
protected $except = [
'kapital/decline',
'kapital/approve',
'kapital/cancel'
];
}
<?php
Route::prefix('kapital')->name('kapital.')->group(function(){
Route::get('index', 'KapitalPaymentController@index')->name('index');
Route::get('order', 'KapitalPaymentController@createTestOrder')->name('order');
//status
Route::post('approve', 'KapitalPaymentController@approveUrl')->name('approve');
Route::post('cancel', 'KapitalPaymentController@cancelUrl')->name('cancel');
Route::post('decline', 'KapitalPaymentController@declineUrl')->name('decline');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment