Skip to content

Instantly share code, notes, and snippets.

@Miri92
Created August 9, 2020 07:56
Show Gist options
  • Select an option

  • Save Miri92/758d1a3bccffd003fdde09c3c752b5e4 to your computer and use it in GitHub Desktop.

Select an option

Save Miri92/758d1a3bccffd003fdde09c3c752b5e4 to your computer and use it in GitHub Desktop.

Revisions

  1. Miri Zulfugar created this gist Aug 9, 2020.
    11 changes: 11 additions & 0 deletions Payment.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?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'];

    }
    236 changes: 236 additions & 0 deletions PaymentKapitalController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,236 @@
    <?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;

    }

    //paymentLogs in admin
    public function paymentLogs(){
    $rows = Payment::latest()->paginate(20);

    return view('back.settings.payment_logs', compact('rows'));
    }
    }