Skip to content

Commit

Permalink
Fix #14, cs
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaoda committed Feb 27, 2020
1 parent c9b703b commit 26b8937
Showing 1 changed file with 53 additions and 46 deletions.
99 changes: 53 additions & 46 deletions src/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
*
* @package Idma\Robokassa
*/
class Payment {
class Payment
{
const CULTURE_EN = 'en';
const CULTURE_RU = 'ru';

private $baseUrl = 'https://merchant.roboxchange.com/Index.aspx?';
private $isTestMode = false;
private $valid = false;
private $baseUrl = 'https://merchant.roboxchange.com/Index.aspx?';
private $valid = false;
private $data;
private $isTestMode;
private $customParams = [];

private $login;
Expand All @@ -40,39 +41,39 @@ class Payment {
/**
* Class constructor.
*
* @param string $login login of Merchant
* @param string $paymentPassword password #1
* @param string $validationPassword password #2
* @param bool $testMode use test server
* @param string $login login of Merchant
* @param string $paymentPassword password #1
* @param string $validationPassword password #2
* @param bool $testMode use test server
*/
public function __construct($login, $paymentPassword, $validationPassword, $testMode = false)
{
$this->login = $login;
$this->paymentPassword = $paymentPassword;
$this->login = $login;
$this->paymentPassword = $paymentPassword;
$this->validationPassword = $validationPassword;
$this->isTestMode = $testMode;
$this->isTestMode = $testMode;

$this->data = [
'MerchantLogin' => $this->login,
'InvId' => null,
'OutSum' => 0,
'Desc' => null,
'MerchantLogin' => $this->login,
'InvId' => null,
'OutSum' => 0,
'Desc' => null,
'SignatureValue' => '',
'Encoding' => 'utf-8',
'Culture' => self::CULTURE_RU,
'IncCurrLabel' => '',
'IsTest' => $testMode ? 1 : 0
'Encoding' => 'utf-8',
'Culture' => self::CULTURE_RU,
'IncCurrLabel' => '',
'IsTest' => $testMode ? 1 : 0
];
}

/**
* Create payment url.
*
* @throws InvalidSumException if sum less or equals zero
* @return string the payment url
* @throws EmptyDescriptionException if description is empty or not provided
* @throws InvalidInvoiceIdException if invoice ID less or equals zero or not provided
*
* @return string the payment url
* @throws InvalidSumException if sum less or equals zero
*/
public function getPaymentUrl()
{
Expand All @@ -99,12 +100,15 @@ public function getPaymentUrl()
if ($this->customParams) {
// sort params alphabetically
ksort($this->customParams);
$signature .= ':' . implode(array_map(function($key, $value){ return $key . '=' .$value; }, array_keys($this->customParams), $this->customParams), ':');

$signature .= ':' . implode(':', array_map(static function ($key, $value) {
return $key . '=' . $value;
}, array_keys($this->customParams), $this->customParams));
}

$this->data['SignatureValue'] = md5($signature);

$data = http_build_query($this->data, null, '&');
$data = http_build_query($this->data, null, '&');
$custom = http_build_query($this->customParams, null, '&');

return $this->baseUrl . $data . ($custom ? '&' . $custom : '');
Expand All @@ -113,7 +117,7 @@ public function getPaymentUrl()
/**
* Validates on ResultURL.
*
* @param string $data query data
* @param string $data query data
*
* @return bool
*/
Expand All @@ -125,7 +129,7 @@ public function validateResult($data)
/**
* Validates on SuccessURL.
*
* @param string $data query data
* @param string $data query data
*
* @return bool
*/
Expand All @@ -137,8 +141,8 @@ public function validateSuccess($data)
/**
* Validates the Robokassa query.
*
* @param string $data query data
* @param string $passwordType type of password, 'validation' or 'payment'
* @param string $data query data
* @param string $passwordType type of password, 'validation' or 'payment'
*
* @return bool
*/
Expand Down Expand Up @@ -175,11 +179,11 @@ public function isValid()
* Adds custom parameters in payment.
* The 'shp_' prefix will be added automatically.
*
* @param array $params custom parameters array
* @param array $params custom parameters array
*
* @return Payment
* @throws InvalidParamException if params is not an array
*
* @return Payment
*/
public function addCustomParameters($params)
{
Expand All @@ -197,7 +201,8 @@ public function addCustomParameters($params)
/**
* @return string
*/
public function getSuccessAnswer() {
public function getSuccessAnswer()
{
return 'OK' . $this->getInvoiceId() . "\n";
}

Expand All @@ -212,15 +217,18 @@ private function getCustomParamsString(array $source)
}

ksort($params);
$params = implode(array_map(function($key, $value){ return $key . '=' .$value; }, array_keys($params), $params), ':');

$params = implode(':', array_map(static function ($key, $value) {
return $key . '=' . $value;
}, array_keys($params), $params));

return $params ? ':' . $params : '';
}

/**
* Get custom parameter from payment data.
*
* @param string $name parameter name without "shp_"
* @param string $name parameter name without "shp_"
*
* @return mixed
*/
Expand Down Expand Up @@ -250,7 +258,7 @@ public function getInvoiceId()
*/
public function setInvoiceId($id)
{
$this->data['InvId'] = (int) $id;
$this->data['InvId'] = (int)$id;

return $this;
}
Expand All @@ -264,11 +272,11 @@ public function getSum()
}

/**
* @param mixed $summ
* @param mixed $summ
*
* @return Payment
* @throws InvalidSumException
*
* @return Payment
*/
public function setSum($summ)
{
Expand All @@ -278,9 +286,9 @@ public function setSum($summ)
$this->data['OutSum'] = $summ;

return $this;
} else {
throw new InvalidSumException();
}

throw new InvalidSumException();
}

/**
Expand All @@ -292,13 +300,13 @@ public function getDescription()
}

/**
* @param string $description
* @param string $description
*
* @return Payment
*/
public function setDescription($description)
{
$this->data['Desc'] = (string) $description;
$this->data['Desc'] = (string)$description;

return $this;
}
Expand All @@ -312,13 +320,13 @@ public function getCulture()
}

/**
* @param string $culture
* @param string $culture
*
* @return Payment
*/
public function setCulture($culture = self::CULTURE_RU)
{
$this->data['Culture'] = (string) $culture;
$this->data['Culture'] = (string)$culture;

return $this;
}
Expand All @@ -332,13 +340,13 @@ public function getCurrencyLabel()
}

/**
* @param string $currLabel
* @param string $currLabel
*
* @return Payment
*/
public function setCurrencyLabel($currLabel)
{
$this->data['IncCurrLabel'] = (string) $currLabel;
$this->data['IncCurrLabel'] = (string)$currLabel;

return $this;
}
Expand All @@ -347,11 +355,10 @@ public function setCurrencyLabel($currLabel)
* @param $email
* @return $this
*/
public function setEmail($email)
public function setEmail($email)
{
$this->data['Email'] = $email;

return $this;
}

}

0 comments on commit 26b8937

Please sign in to comment.