Last active
November 20, 2015 12:21
-
-
Save ostashevdv/7c80708627b0f5c44eab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\modules\shop\components; | |
use yii\base\InvalidConfigException; | |
class Merchant extends \yii\base\Object | |
{ | |
public $baseUrl = 'http://test.robokassa.ru/Index.aspx'; | |
/** @var string Идентефикатор магазина */ | |
public $login; | |
/** @var string password1 */ | |
public $passwordPayment; | |
/** @var string password2 */ | |
public $passwordValidate; | |
public $options = []; | |
/** | |
* @var array $_options | |
* | |
* MrchLogin - Идентификатор магазина в ROBOKASSA | |
* OutSum - Номер счета в магазине | |
* InvId - Стоимость заказа, сделанного клиентом | |
* InvDesc - Описание покупки | |
* SignatureValue - Контрольная сумма | |
* IncCurrLabel - Предлагаемая валюта платежа | |
* Culture - Язык общения с клиентом | |
* Encoding - Кодировка, в которой возвращается HTML-код кассы | |
* Email - E-Mail покупателя | |
* ExpirationDate - Срок действия счета | |
* OutSumCurrency - Способ указать валюту | |
*/ | |
protected $_options = [ | |
'MrchLogin' => null, | |
'OutSum' => null, | |
'InvId' => null, | |
'InvDesc' => null, | |
'SignatureValue' => null, | |
'IncCurrLabel' => null, | |
'Culture' => null, | |
'Encoding' => null, | |
'Email' => null, | |
'ExpirationDate' => null, | |
'OutSumCurrency' => null, | |
]; | |
public function init() | |
{ | |
if (!isset($this->login)) { | |
throw new InvalidConfigException("\$login not set."); | |
} | |
$this->options['MrchLogin'] = $this->login; | |
if (!isset($this->passwordPayment)) { | |
throw new InvalidConfigException("\$passwordPayment not set."); | |
} | |
if (!isset($this->passwordValidate)) { | |
throw new InvalidConfigException("\$passwordValidete not set."); | |
} | |
$this->_options = array_merge($this->_options, $this->options); | |
} | |
public function getPaymentUrl($outSum, $invId, $invDesc, $shp) | |
{ | |
if (!empty($shp)) { | |
$shp = $this->prepareParams($shp); | |
} | |
$outSum = vsprintf('%01.2f', $outSum); | |
$signature = "{$this->login}:{$outSum}:{$invId}:{$this->passwordPayment}"; | |
$signature .= empty($shp) ? null : ':'.$this->paramsToString($shp); | |
$signature = md5($signature); | |
$this->_options = array_merge($this->_options, ['OutSum' => $outSum, 'InvId' => $invId, 'InvDesc' => $invDesc, 'SignatureValue' => $signature], $shp); | |
$query = array_filter($this->_options, function($val){return $val===null ? false : true;} ); | |
return $this->baseUrl .'?'. http_build_query($query); | |
} | |
public function validate($outSum, $invId, $signature, $shp, $passwordType = 'Validate') | |
{ | |
$password = $this->{'password'.$passwordType}; | |
$crc = "{$outSum}:{$invId}:{$password}"; | |
if (!empty($shp)) { | |
ksort($shp); | |
$crc .= ':'.$this->paramsToString($shp); | |
} | |
$crc = md5($crc); | |
return strtolower($signature) === strtolower($crc); | |
} | |
/** | |
* @param array $params | |
* @return array | |
*/ | |
protected function prepareParams(array $params) | |
{ | |
$return = []; | |
foreach($params as $key=>$val) { | |
if (stripos($key, 'shp_')===false) { | |
$return["shp_{$key}"] = $val; | |
} else { | |
$return[$key] = $val; | |
} } | |
ksort($return); | |
return $return; | |
} | |
protected function paramsToString(array $params) | |
{ | |
$str = ''; | |
foreach($params as $k=>$v){ | |
$str .= ":$k=$v"; | |
} | |
return ltrim($str, ':'); | |
} | |
public function filterParams(array $params, $needle) | |
{ | |
return array_filter($params, function($val, $key) use ($needle) { | |
return stripos($key, $needle)===false ? false : true; | |
}, ARRAY_FILTER_USE_BOTH); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment