Skip to content

Commit

Permalink
Merge pull request #17 from yii-cms/refactor
Browse files Browse the repository at this point in the history
test fixes, cs fixes
  • Loading branch information
gonimar authored Jun 25, 2019
2 parents 9bc4380 + 9f1ade4 commit 6fb7deb
Show file tree
Hide file tree
Showing 13 changed files with 414 additions and 270 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ php:
- 7.0
- 7.1
- 7.2
- 7.3

sudo: true

Expand All @@ -20,7 +21,7 @@ install:
- travis_retry composer require php-coveralls/php-coveralls

script:
- phpunit --verbose $PHPUNIT_FLAGS
- vendor/bin/phpunit --verbose $PHPUNIT_FLAGS

after_script:
- bash <(curl -s https://codecov.io/bash)
Expand Down
16 changes: 10 additions & 6 deletions BaseAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

namespace robokassa;


use yii\base\Action;
use yii\base\InvalidConfigException;

class BaseAction extends Action {
/**
* Class BaseAction
* @package robokassa
*/
class BaseAction extends Action
{
public $merchant = 'robokassa';

public $callback;

/**
* @param Merchant $merchant Merchant.
* @param $nInvId
* @param $nOutSum
* @param $shp
* @param mixed $nInvId
* @param mixed $nOutSum
* @param array $shp
* @return mixed
* @throws \yii\base\InvalidConfigException
* @throws InvalidConfigException
*/
protected function callback($merchant, $nInvId, $nOutSum, $shp)
{
Expand Down
13 changes: 10 additions & 3 deletions FailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
namespace robokassa;

use Yii;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;


class FailAction extends BaseAction {
/**
* Class FailAction
* @package robokassa
*/
class FailAction extends BaseAction
{

/**
* Runs the action.
* @throws BadRequestHttpException
* @throws InvalidConfigException
*/
public function run()
{
if (!isset($_REQUEST['OutSum'], $_REQUEST['InvId'])) {
throw new BadRequestHttpException;
}

/** @var \robokassa\Merchant $merchant */
/** @var Merchant $merchant */
$merchant = Yii::$app->get($this->merchant);

$shp = [];
Expand Down
82 changes: 63 additions & 19 deletions Merchant.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Yii;
use yii\base\BaseObject;
use yii\base\InvalidConfigException;
use yii\web\Response;

class Merchant extends BaseObject
{
Expand All @@ -15,52 +17,90 @@ class Merchant extends BaseObject
public $isTest = false;

public $baseUrl = 'https://auth.robokassa.ru/Merchant/Index.aspx';

public $hashAlgo = 'md5';

public function payment($nOutSum, $nInvId, $sInvDesc = null, $sIncCurrLabel=null, $sEmail = null, $sCulture = null, $shp = [], $returnLink = false)
/**
* @param mixed $nOutSum Требуемая к получению сумма (буквально — стоимость заказа, сделанного клиентом).
* Формат представления — число, разделитель — точка, например: 123.45.
* @param mixed $nInvId Номер счета в магазине.
* @param null|string $sInvDesc Описание покупки,
* можно использовать только символы английского или русского алфавита,
* цифры и знаки препинания. Максимальная длина — 100 символов.
* @param null|string $sIncCurrLabel Предлагаемый способ оплаты.
* @param null|string $sEmail Email покупателя автоматически подставляется в платёжную форму ROBOKASSA.
* @param null|string $sCulture Язык общения с клиентом (в соответствии с ISO 3166-1).
* @param array $shp Дополнительные пользовательские параметры
* @param bool $returnLink
* @return string|Response
* @throws InvalidConfigException
*/
public function payment($nOutSum, $nInvId, $sInvDesc = null, $sIncCurrLabel = null, $sEmail = null, $sCulture = null, $shp = [], $returnLink = false)
{
$url = $this->baseUrl;

$signature = "{$this->sMerchantLogin}:{$nOutSum}:{$nInvId}:{$this->sMerchantPass1}";

$sSignatureValue = $this->encryptSignature($signature);
$sSignatureValue = $this->generateSignature($nOutSum, $nInvId);

$url .= '?' . http_build_query([
'MrchLogin' => $this->sMerchantLogin,
'OutSum' => $nOutSum,
'InvId' => $nInvId,
'Desc' => $sInvDesc,
'SignatureValue' => $sSignatureValue,
'IncCurrLabel' => $sIncCurrLabel,
'Email' => $sEmail,
'Culture' => $sCulture,
'IsTest' => $this->isTest ? 1 : null,
]);
'MrchLogin' => $this->sMerchantLogin,
'OutSum' => $nOutSum,
'InvId' => $nInvId,
'Desc' => $sInvDesc,
'SignatureValue' => $sSignatureValue,
'IncCurrLabel' => $sIncCurrLabel,
'Email' => $sEmail,
'Culture' => $sCulture,
'IsTest' => $this->isTest ? 1 : null,
]);

if (!empty($shp) && ($query = http_build_query($shp)) !== '') {
$url .= '&' . $query;
}
if ( !$returnLink ){

if (!$returnLink) {
Yii::$app->user->setReturnUrl(Yii::$app->request->getUrl());
return Yii::$app->response->redirect($url);
} else {
return $url;
}
}

/**
* @param $shp
* @return string
*/
private function implodeShp($shp)
{
ksort($shp);

foreach($shp as $key => $value) {
foreach ($shp as $key => $value) {
$shp[$key] = $key . '=' . $value;
}

return implode(':', $shp);
}

private function generateSignature($nOutSum, $nInvId)
{
if ($nInvId === null) {
// MerchantLogin:OutSum:Пароль#1
$signature = "{$this->sMerchantLogin}:{$nOutSum}:{$this->sMerchantPass1}";
} else {
// MerchantLogin:OutSum:InvId:Пароль#1
$signature = "{$this->sMerchantLogin}:{$nOutSum}:{$nInvId}:{$this->sMerchantPass1}";
}

return strtolower($this->encryptSignature($signature));
}

/**
* @param $sSignatureValue
* @param $nOutSum
* @param $nInvId
* @param $sMerchantPass
* @param array $shp
* @return bool
*/
public function checkSignature($sSignatureValue, $nOutSum, $nInvId, $sMerchantPass, $shp = [])
{
$signature = "{$nOutSum}:{$nInvId}:{$sMerchantPass}";
Expand All @@ -72,7 +112,11 @@ public function checkSignature($sSignatureValue, $nOutSum, $nInvId, $sMerchantPa
return strtolower($this->encryptSignature($signature)) === strtolower($sSignatureValue);

}


/**
* @param $signature
* @return string
*/
protected function encryptSignature($signature)
{
return hash($this->hashAlgo, $signature);
Expand Down
21 changes: 17 additions & 4 deletions ResultAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
namespace robokassa;

use Yii;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;


class ResultAction extends BaseAction {
/**
* Class ResultAction
* @package robokassa
*/
class ResultAction extends BaseAction
{
/**
* Runs the action.
* @throws InvalidConfigException
* @throws BadRequestHttpException
*/
public function run()
{
if (!isset($_REQUEST['OutSum'], $_REQUEST['InvId'], $_REQUEST['SignatureValue'])) {
throw new BadRequestHttpException;
}

/** @var \robokassa\Merchant $merchant */
/** @var Merchant $merchant */
$merchant = Yii::$app->get($this->merchant);

$shp = [];
Expand All @@ -26,7 +33,13 @@ public function run()
}
}

if ($merchant->checkSignature($_REQUEST['SignatureValue'], $_REQUEST['OutSum'], $_REQUEST['InvId'], $merchant->sMerchantPass2, $shp)) {
if ($merchant->checkSignature(
$_REQUEST['SignatureValue'],
$_REQUEST['OutSum'],
$_REQUEST['InvId'],
$merchant->sMerchantPass2,
$shp)
) {
return $this->callback($merchant, $_REQUEST['InvId'], $_REQUEST['OutSum'], $shp);
}

Expand Down
21 changes: 17 additions & 4 deletions SuccessAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
namespace robokassa;

use Yii;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;


class SuccessAction extends BaseAction {
/**
* Class SuccessAction
* @package robokassa
*/
class SuccessAction extends BaseAction
{
/**
* Runs the action.
* @throws InvalidConfigException
* @throws BadRequestHttpException
*/
public function run()
{
if (!isset($_REQUEST['OutSum'], $_REQUEST['InvId'], $_REQUEST['SignatureValue'])) {
throw new BadRequestHttpException;
}

/** @var \robokassa\Merchant $merchant */
/** @var Merchant $merchant */
$merchant = Yii::$app->get($this->merchant);

$shp = [];
Expand All @@ -26,7 +33,13 @@ public function run()
}
}

if ($merchant->checkSignature($_REQUEST['SignatureValue'], $_REQUEST['OutSum'], $_REQUEST['InvId'], $merchant->sMerchantPass1, $shp)) {
if ($merchant->checkSignature(
$_REQUEST['SignatureValue'],
$_REQUEST['OutSum'],
$_REQUEST['InvId'],
$merchant->sMerchantPass1,
$shp)
) {
return $this->callback($merchant, $_REQUEST['InvId'], $_REQUEST['OutSum'], $shp);
}

Expand Down
64 changes: 36 additions & 28 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
{
"name": "yii-cms/yii2-robokassa",
"description": "Robokassa payment extension.",
"keywords": ["yii2", "robokassa"],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Sergey Gonimar",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"yiisoft/yii2": "~2.0.13"
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"autoload": {
"psr-4": { "robokassa\\": "" }
},
"extra": {
"branch-alias": {
"dev-master": "1.2.x-dev"
}
"name": "yii-cms/yii2-robokassa",
"description": "Robokassa payment extension.",
"keywords": [
"yii2",
"robokassa"
],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Sergey Gonimar",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"yiisoft/yii2": "~2.0.14"
},
"require-dev": {
"phpunit/phpunit": "4.8.27|~5.7.21|^6.2"
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"autoload": {
"psr-4": {
"robokassa\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "1.2.x-dev"
}
}
}
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace robokassa\tests;

use yii\di\Container;
Expand Down Expand Up @@ -45,7 +46,7 @@ protected function mockWebApplication($config = [], $appClass = '\yii\web\Applic
'components' => [
'request' => [
'cookieValidationKey' => 'test',
'scriptFile' => __DIR__ .'/index.php',
'scriptFile' => __DIR__ . '/index.php',
'scriptUrl' => '/index.php',
'url' => '/',
],
Expand Down
Loading

0 comments on commit 6fb7deb

Please sign in to comment.