Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Erkin Çakar committed Oct 28, 2015
0 parents commit 370b5db
Show file tree
Hide file tree
Showing 42 changed files with 3,026 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script: phpunit
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Erkin Cakar (https://github.com/travijuu)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# BKM Express PHP Payment Library

Documentation is preparing.
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "travijuu/bkm-express",
"type": "library",
"description": "BKM Express Payment Library",
"keywords": ["bkm express", "payment", "e-commerce"],
"license": "MIT",
"authors": [
{
"name": "Erkin Cakar",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"psr-0": {
"Travijuu\\BkmExpress": "src/"
}
},
"minimum-stability": "stable"
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
141 changes: 141 additions & 0 deletions src/Travijuu/BkmExpress/BkmExpress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
namespace Travijuu\BkmExpress;

use Travijuu\BkmExpress\Common\IncomingResult;
use Travijuu\BkmExpress\Common\Result;
use Travijuu\BkmExpress\Exception\VerificationException;
use Travijuu\BkmExpress\Payment\InitializePayment\BKMSoapClient;
use Travijuu\BkmExpress\Payment\InitializePayment\InitializePayment;
use Travijuu\BkmExpress\Payment\InitializePayment\InitializePaymentWSRequest;
use Travijuu\BkmExpress\Payment\InitializePayment\RedirectRequest;
use Travijuu\BkmExpress\Payment\RequestMerchInfo\RequestMerchInfoServer;
use Travijuu\BkmExpress\Utility\Calculate;
use Travijuu\BkmExpress\Utility\Certificate;
use Travijuu\BkmExpress\Exception\ClassNotFoundException;
use SoapServer;
use Closure;

class BkmExpress
{
public $mid;

public $successUrl;

public $cancelUrl;

public $privateKeyPath;

public $publicKeyPath;

public $bkmPublicKeyPath;

public $bankList = [];

public $virtualPosList = [];


public function __construct($mid, $successUrl, $cancelUrl, $privateKeyPath, $publicKeyPath, $bkmPublicKeyPath)
{
$this->mid = $mid;
$this->successUrl = $successUrl;
$this->cancelUrl = $cancelUrl;
$this->privateKeyPath = $privateKeyPath;
$this->publicKeyPath = $publicKeyPath;
$this->bkmPublicKeyPath = $bkmPublicKeyPath;

$this->bankList = include('Config/BkmBankList.php');
}

public function initPayment($wsdl, $sAmount, $cAmount, $banks)
{
$bkmClient = new BKMSoapClient($wsdl, ['trace' => 1]);
$initPayment = new InitializePayment();
$initPaymentRequest = new InitializePaymentWSRequest();

$initPaymentRequest->setMerchantId($this->mid)
->setSuccessUrl($this->successUrl)
->setCancelUrl($this->cancelUrl)
->setSaleAmount($sAmount)
->setCargoAmount($cAmount)
->setBanks($banks)
->setTimestamp(date('Ymd-H:i:s'));

$dataToBeHashed = $initPaymentRequest->getDataToBeHashed();
$signature = Certificate::sign($dataToBeHashed, $this->privateKeyPath);
$verified = Certificate::verify($signature, $dataToBeHashed, $this->publicKeyPath);

if (! $verified) {
throw new VerificationException("Private / Public key does not match!");
}

$initPaymentRequest->setSignature(base64_encode($signature));
$initPayment->setRequest($initPaymentRequest);
$bkmClient->setParams($initPayment);

$response = $bkmClient->initializePayment();

$verified = $response->verify($this->bkmPublicKeyPath);

if (! $verified) {
throw new VerificationException("Response can not be verified!");
}

if ($response->success()) {
return new RedirectRequest($response, $this->privateKeyPath);
}

return null;
}

public function requestMerchInfo($wsdl, $virtualPosList, Closure $saveTokenCallback = null)
{
$this->virtualPosList = $virtualPosList;

$server = new SoapServer($wsdl, ['classmap' => RequestMerchInfoServer::$classmap]);
$server->setClass(
'Travijuu\BkmExpress\Payment\RequestMerchInfo\RequestMerchInfoServer',
$this->publicKeyPath,
$this->privateKeyPath,
$virtualPosList,
$saveTokenCallback
);
$server->handle();
}

public function confirm($data)
{
$incomingResult = new IncomingResult($data);
$verified = $incomingResult->verify($this->bkmPublicKeyPath);

if (Calculate::timeDiff($incomingResult->getTs()) > 30) {
return Result::build('REQUEST_NOT_SYNCHRONIZED');
}

if (! $verified) {
return Result::build('MAC_VERIFICATION_FAILED');
}

return $incomingResult;
}

public function getBank($bankId)
{
foreach ($this->bankList as $bank) {
if ($bank['id'] == $bankId) {
return $bank;
}
}
}

public function getPosResponse($bankId, $data)
{
$bank = $this->getBank($bankId);
$class = 'PosResponse\Response' . $bank['system'];

if (! class_exists($class)) {
throw new ClassNotFoundException("{$class} not found!");
}

return new $class($data);
}
}
34 changes: 34 additions & 0 deletions src/Travijuu/BkmExpress/BkmExpressServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php namespace Travijuu\BkmExpress;

use Illuminate\Support\ServiceProvider;

class BkmExpressServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [];
}

}
106 changes: 106 additions & 0 deletions src/Travijuu/BkmExpress/Common/Bank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
namespace Travijuu\BkmExpress\Common;

use Travijuu\BkmExpress\Exception\UnexpectedDataType;
use Travijuu\BkmExpress\Exception\UnexpectedInstance;

class Bank
{

/**
* @access private
* @var string
*/
private $id;
/**
* @access private
* @var string
*/
private $name;
/**
* @access private
* @var string
*/
private $expBank;
/**
* @access private
* @var Bin
*/
private $bins = [];
/**
* @access private
* @var bin[]
*/
private $bin;

public function getId()
{
return $this->id;
}

public function setId($id)
{
$this->id = $id;

return $this;
}

public function getName()
{
return $this->name;
}

public function setName($name)
{
$this->name = $name;

return $this;
}

public function getDescription()
{
return $this->expBank;
}

public function setDescription($description)
{
$this->expBank = $description;

return $this;
}

public function getBins()
{
return $this->bins;
}

public function setBins($bins)
{
if (! is_array($bins)) {

throw new UnexpectedDataType("Bins should be array");
}

$this->bins = [];

foreach ($bins as $bin) {

$this->addBin($bin);
}

return $this;
}

public function addBin($bin)
{
if (! $bin instanceof Bin) {

throw new UnexpectedInstance("Should be instance of Bin");
}

array_push($this->bins, $bin);

return $this;
}
}

Loading

0 comments on commit 370b5db

Please sign in to comment.