From b42913f1c69bd6e246334f41a243d75b2388b60f Mon Sep 17 00:00:00 2001 From: baibaratsky Date: Thu, 11 Aug 2016 21:04:37 +0200 Subject: [PATCH] Refactoring and code-formatting of the new ATM and WMC interfaces --- Api/ATM/ATM1/Request.php | 42 ++++++++++---------- Api/ATM/ATM1/Response.php | 26 ++++++------- Api/ATM/ATM2/Request.php | 55 +++++++++++++------------- Api/ATM/ATM2/Response.php | 28 +++++++------- Api/ATM/Request.php | 55 ++++++++++++++------------ Api/WMC/Request.php | 60 ++++++++++++++++------------- Api/WMC/WMC1/Request.php | 48 +++++++++++------------ Api/WMC/WMC1/Response.php | 16 ++++---- Api/WMC/WMC2/Request.php | 54 ++++++++++++-------------- Api/WMC/WMC2/Response.php | 26 ++++++------- Api/WMC/WMC3/Payment.php | 24 ++++++------ Api/WMC/WMC3/Request.php | 45 +++++++++++----------- Api/WMC/WMC3/Response.php | 36 ++++++++--------- Api/X/Request.php | 41 ++++++++++++-------- README.md | 12 +++++- Request/AbstractRequest.php | 46 +++++++++++----------- Request/Requester/CurlRequester.php | 18 ++++----- 17 files changed, 328 insertions(+), 304 deletions(-) diff --git a/Api/ATM/ATM1/Request.php b/Api/ATM/ATM1/Request.php index 5240203..87ca9f8 100644 --- a/Api/ATM/ATM1/Request.php +++ b/Api/ATM/ATM1/Request.php @@ -43,9 +43,7 @@ public function __construct($authType = self::AUTH_CLASSIC) protected function getValidationRules() { return array( - RequestValidator::TYPE_REQUIRED => array( - 'price', 'payeePurse', 'currency' - ) + RequestValidator::TYPE_REQUIRED => array('price', 'payeePurse', 'currency'), ); } @@ -73,23 +71,23 @@ public function getResponseClassName() { return Response::className(); } - - /** - * @param string $lightCertificate - * @param string $lightKey - * @param string $lightPass - */ - public function cert($lightCertificate, $lightKey, $lightPass = '') { + + /** + * @inheritdoc + */ + public function lightAuth($certificate, $key, $keyPassword = '') + { if ($this->authType === self::AUTH_LIGHT) { $this->setSignature( - $this->signLight( - $this->getSignerWmid() . $this->getCurrency() . - $this->getPayeePurse() . $this->getPrice(), - $lightKey, $lightPass - ) + $this->signLight( + $this->getSignerWmid() . $this->getCurrency() . $this->getPayeePurse() . $this->getPrice(), + $key, + $keyPassword + ) ); } - parent::cert($lightCertificate, $lightKey, $lightPass); + + parent::lightAuth($certificate, $key, $keyPassword); } /** @@ -98,12 +96,12 @@ public function cert($lightCertificate, $lightKey, $lightPass = '') { public function sign(Signer $requestSigner = null) { if ($this->authType === self::AUTH_CLASSIC) { - $this->setSignature( - $requestSigner->sign( - $this->getSignerWmid() . $this->getCurrency() . - $this->getPayeePurse() . $this->getPrice() - ) - ); + $this->setSignature( + $requestSigner->sign( + $this->getSignerWmid() . $this->getCurrency() . + $this->getPayeePurse() . $this->getPrice() + ) + ); } } diff --git a/Api/ATM/ATM1/Response.php b/Api/ATM/ATM1/Response.php index 5fd168d..66f361c 100644 --- a/Api/ATM/ATM1/Response.php +++ b/Api/ATM/ATM1/Response.php @@ -49,22 +49,22 @@ public function __construct($response) parent::__construct($response); $responseObject = new \SimpleXMLElement($response); - - $this->returnCode = (int) $responseObject->retval; - $this->returnDescription = (string) $responseObject->retdesc . '. '. (string) $responseObject->description; + + $this->returnCode = (int)$responseObject->retval; + $this->returnDescription = (string)$responseObject->retdesc . '. ' . (string)$responseObject->description; if (isset($responseObject->payment)) { $payment = $responseObject->payment; - $this->currency = (string) $payment['currency']; - $this->exchange = (string) $payment['exchange']; - $this->payeePurse = (string) $payment->purse; - $this->upExchange = (float) $payment->upexchange; - $this->course = (float) $payment->course; - $this->price = (float) $payment->price; - $this->amount = (float) $payment->amount; - $this->rest = (float) $payment->rest; - $this->dayLimit = (float) $payment->limit->day; - $this->monthLimit = (float) $payment->limit->month; + $this->currency = (string)$payment['currency']; + $this->exchange = (string)$payment['exchange']; + $this->payeePurse = (string)$payment->purse; + $this->upExchange = (float)$payment->upexchange; + $this->course = (float)$payment->course; + $this->price = (float)$payment->price; + $this->amount = (float)$payment->amount; + $this->rest = (float)$payment->rest; + $this->dayLimit = (float)$payment->limit->day; + $this->monthLimit = (float)$payment->limit->month; } } diff --git a/Api/ATM/ATM2/Request.php b/Api/ATM/ATM2/Request.php index 2b9baa3..1a4eafd 100644 --- a/Api/ATM/ATM2/Request.php +++ b/Api/ATM/ATM2/Request.php @@ -61,9 +61,9 @@ public function __construct($authType = self::AUTH_CLASSIC) protected function getValidationRules() { return array( - RequestValidator::TYPE_REQUIRED => array( - 'lang', 'transactionId', 'currency', 'test', 'price', 'date', 'point' - ) + RequestValidator::TYPE_REQUIRED => array( + 'lang', 'transactionId', 'currency', 'test', 'price', 'date', 'point' + ) ); } @@ -93,25 +93,24 @@ public function getResponseClassName() { return Response::className(); } - - /** - * @param string $lightCertificate - * @param string $lightKey - * @param string $lightPass - */ - public function cert($lightCertificate, $lightKey, $lightPass = '') { - if ($this->authType === self::AUTH_LIGHT) { - $this->setSignature( - $this->signLight( - $this->getSignerWmid() . $this->getTransactionId() . - $this->getCurrency() . $this->getTest() . - $this->getPayeePurse() . $this->getPrice() . - $this->getDate()->format('Ymd H:i:s') . $this->getPoint(), - $lightKey, $lightPass - ) - ); - } - parent::cert($lightCertificate, $lightKey, $lightPass); + + /** + * @inheritdoc + */ + public function lightAuth($certificate, $key, $keyPassword = '') + { + if ($this->authType === self::AUTH_LIGHT) { + $this->setSignature( + $this->signLight( + $this->getSignerWmid() . $this->getTransactionId() . $this->getCurrency() . $this->getTest() + . $this->getPayeePurse() . $this->getPrice() . $this->getDate()->format('Ymd H:i:s') + . $this->getPoint(), + $key, + $keyPassword + ) + ); + } + parent::lightAuth($certificate, $key, $keyPassword); } /** @@ -121,12 +120,12 @@ public function sign(Signer $requestSigner = null) { if ($this->authType === self::AUTH_CLASSIC) { $this->setSignature( - $requestSigner->sign( - $this->getSignerWmid() . $this->getTransactionId() . - $this->getCurrency() . $this->getTest() . - $this->getPayeePurse() . $this->getPrice() . - $this->getDate()->format('Ymd H:i:s') . $this->getPoint() - ) + $requestSigner->sign( + $this->getSignerWmid() . $this->getTransactionId() . + $this->getCurrency() . $this->getTest() . + $this->getPayeePurse() . $this->getPrice() . + $this->getDate()->format('Ymd H:i:s') . $this->getPoint() + ) ); } } diff --git a/Api/ATM/ATM2/Response.php b/Api/ATM/ATM2/Response.php index 4180842..949e177 100644 --- a/Api/ATM/ATM2/Response.php +++ b/Api/ATM/ATM2/Response.php @@ -67,20 +67,20 @@ public function __construct($response) if (isset($responseObject->payment)) { $payment = $responseObject->payment; - $this->id = (int) $payment['id']; - $this->currency = (string) $payment['currency']; - $this->test = (int) $payment['test']; - $this->payeePurse = (string) $payment->purse; - $this->price = (float) $payment->price; - $this->amount = (float) $payment->amount; - $this->comiss = (float) $payment->comiss; - $this->rest = (float) $payment->rest; - $this->date = self::createDateTime((string)$payment->date); - $this->point = (int) $payment->point; - $this->wmtranid = (int) $payment->wmtranid; - $this->dateupd = self::createDateTime((string)$payment->dateupd); - $this->dayLimit = (float) $payment->limit->day; - $this->monthLimit = (float) $payment->limit->month; + $this->id = (int)$payment['id']; + $this->currency = (string)$payment['currency']; + $this->test = (int)$payment['test']; + $this->payeePurse = (string)$payment->purse; + $this->price = (float)$payment->price; + $this->amount = (float)$payment->amount; + $this->comiss = (float)$payment->comiss; + $this->rest = (float)$payment->rest; + $this->date = self::createDateTime((string)$payment->date); + $this->point = (int)$payment->point; + $this->wmtranid = (int)$payment->wmtranid; + $this->dateupd = self::createDateTime((string)$payment->dateupd); + $this->dayLimit = (float)$payment->limit->day; + $this->monthLimit = (float)$payment->limit->month; } } diff --git a/Api/ATM/Request.php b/Api/ATM/Request.php index 75680be..2fbc52f 100644 --- a/Api/ATM/Request.php +++ b/Api/ATM/Request.php @@ -11,12 +11,13 @@ abstract class Request extends XmlRequest const AUTH_SHA256 = 'sha256'; const AUTH_MD5 = 'md5'; const AUTH_SECRET_KEY = 'secret_key'; + const AUTH_SIGN_TYPE_CLASSIC = 1; const AUTH_SIGN_TYPE_LIGHT = 2; - - CONST CURRENCY_EUR = 'EUR'; - CONST CURRENCY_USD = 'USD'; - CONST CURRENCY_RUB = 'RUB'; + + const CURRENCY_EUR = 'EUR'; + const CURRENCY_USD = 'USD'; + const CURRENCY_RUB = 'RUB'; /** @var string request/@lang */ protected $lang; @@ -38,9 +39,9 @@ abstract class Request extends XmlRequest /** @var string Light auth key file name (PEM) */ protected $lightKey; - + /** @var string Light auth key password */ - protected $lightPass; + protected $lightPassword; /** * @param string $authType @@ -53,28 +54,29 @@ public function __construct($authType = self::AUTH_CLASSIC, $lang = 'en') if (self::AUTH_CLASSIC == $authType) { $this->authTypeNum = self::AUTH_SIGN_TYPE_CLASSIC; - } elseif(self::AUTH_LIGHT == $authType) { + } elseif (self::AUTH_LIGHT == $authType) { $this->authTypeNum = self::AUTH_SIGN_TYPE_LIGHT; } $this->setLang($lang); } - + /** - * @param string $lightCertificate Light auth certificate file name (PEM) - * @param string $lightKey Light auth key file name (PEM) - * @param string $lightPass Light auth key pass + * @param string $certificate Light auth certificate file name (PEM) + * @param string $key Light auth key file name (PEM) + * @param string $keyPassword Light auth key password */ - public function cert($lightCertificate, $lightKey, $lightPass = '') + public function lightAuth($certificate, $key, $keyPassword = '') { - $this->lightCertificate = $lightCertificate; - $this->lightKey = $lightKey; - $this->lightPass = $lightPass; + $this->lightCertificate = $certificate; + $this->lightKey = $key; + $this->lightPassword = $keyPassword; } - + /** * @param $data */ - public function setSignature($data) { + public function setSignature($data) + { $this->signature = $data; } @@ -101,12 +103,13 @@ public function getLightKey() { return $this->lightKey; } - + /** * @return string */ - public function getLightPass() { - return $this->lightPass; + public function getLightPassword() + { + return $this->lightPassword; } /** @@ -153,28 +156,32 @@ protected function generateRequestNumber() /** * @param int $authTypeNum */ - public function setAuthTypeNum($authTypeNum) { + public function setAuthTypeNum($authTypeNum) + { $this->authTypeNum = $authTypeNum; } /** * @return int|string */ - public function getAuthTypeNum() { + public function getAuthTypeNum() + { return $this->authTypeNum; } /** * @param string $lang */ - public function setLang($lang) { + public function setLang($lang) + { $this->lang = $lang; } /** * @return string */ - public function getLang() { + public function getLang() + { return $this->lang; } } diff --git a/Api/WMC/Request.php b/Api/WMC/Request.php index f1d9f86..b20daf4 100644 --- a/Api/WMC/Request.php +++ b/Api/WMC/Request.php @@ -13,10 +13,10 @@ abstract class Request extends XmlRequest const AUTH_SECRET_KEY = 'secret_key'; const AUTH_SIGN_TYPE_CLASSIC = 1; const AUTH_SIGN_TYPE_LIGHT = 2; - - CONST CURRENCY_EUR = 'EUR'; - CONST CURRENCY_USD = 'USD'; - CONST CURRENCY_RUB = 'RUB'; + + const CURRENCY_EUR = 'EUR'; + const CURRENCY_USD = 'USD'; + const CURRENCY_RUB = 'RUB'; /** @var string request/@lang */ protected $lang; @@ -38,9 +38,9 @@ abstract class Request extends XmlRequest /** @var string Light auth key file name (PEM) */ protected $lightKey; - + /** @var string Light auth key password */ - protected $lightPass; + protected $lightPassword; /** * @param string $authType @@ -53,28 +53,29 @@ public function __construct($authType = self::AUTH_CLASSIC, $lang = 'en') if (self::AUTH_CLASSIC == $authType) { $this->authTypeNum = self::AUTH_SIGN_TYPE_CLASSIC; - } elseif(self::AUTH_LIGHT == $authType) { + } elseif (self::AUTH_LIGHT == $authType) { $this->authTypeNum = self::AUTH_SIGN_TYPE_LIGHT; } $this->setLang($lang); } - + /** - * @param string $lightCertificate Light auth certificate file name (PEM) - * @param string $lightKey Light auth key file name (PEM) - * @param string $lightPass Light auth key pass + * @param string $certificate Light auth certificate file name (PEM) + * @param string $key Light auth key file name (PEM) + * @param string $keyPassword Light auth key password */ - public function cert($lightCertificate, $lightKey, $lightPass = '') + public function lightAuth($certificate, $key, $keyPassword = '') { - $this->lightCertificate = $lightCertificate; - $this->lightKey = $lightKey; - $this->lightPass = $lightPass; + $this->lightCertificate = $certificate; + $this->lightKey = $key; + $this->lightPassword = $keyPassword; } - + /** * @param $data */ - public function setSignature($data) { + public function setSignature($data) + { $this->signature = $data; } @@ -93,20 +94,21 @@ public function getLightCertificate() { return $this->lightCertificate; } - + /** * @return string */ public function getLightKey() { - return $this->lightKey; + return $this->lightKey; } - + /** * @return string */ - public function getLightPass() { - return $this->lightPass; + public function getLightPassword() + { + return $this->lightPassword; } /** @@ -153,28 +155,32 @@ protected function generateRequestNumber() /** * @param int $authTypeNum */ - public function setAuthTypeNum($authTypeNum) { + public function setAuthTypeNum($authTypeNum) + { $this->authTypeNum = $authTypeNum; } /** * @return int|string */ - public function getAuthTypeNum() { + public function getAuthTypeNum() + { return $this->authTypeNum; } /** * @param string $lang */ - public function setLang($lang) { + public function setLang($lang) + { $this->lang = $lang; } /** * @return string */ - public function getLang() { - return $this->lang; + public function getLang() + { + return $this->lang; } } diff --git a/Api/WMC/WMC1/Request.php b/Api/WMC/WMC1/Request.php index d342141..d519e20 100644 --- a/Api/WMC/WMC1/Request.php +++ b/Api/WMC/WMC1/Request.php @@ -54,9 +54,7 @@ public function __construct($authType = self::AUTH_CLASSIC) protected function getValidationRules() { return array( - RequestValidator::TYPE_REQUIRED => array( - 'price', 'currency' - ) + RequestValidator::TYPE_REQUIRED => array('price', 'currency'), ); } @@ -72,7 +70,7 @@ public function getData() $xml .= self::xmlElement('purse', $this->getPayeePurse()); $phone = $this->getPhone(); if (empty($phone)) { - $xml .= self::xmlElement('phone', $phone); + $xml .= self::xmlElement('phone', $phone); } $xml .= self::xmlElement('price', $this->getPrice()); $xml .= ''; @@ -88,24 +86,24 @@ public function getResponseClassName() { return Response::className(); } - - /** - * @param string $lightCertificate - * @param string $lightKey - * @param string $lightPass - */ - public function cert($lightCertificate, $lightKey, $lightPass = '') { - if ($this->authType === self::AUTH_LIGHT) { - $this->setSignature( - $this->signLight( - $this->getSignerWmid() . $this->getCurrency() . - $this->getPayeePurse() . $this->getPhone() . - $this->getPrice(), - $lightKey, $lightPass - ) - ); - } - parent::cert($lightCertificate, $lightKey, $lightPass); + + /** + * @inheritdoc + */ + public function lightAuth($certificate, $key, $keyPassword = '') + { + if ($this->authType === self::AUTH_LIGHT) { + $this->setSignature( + $this->signLight( + $this->getSignerWmid() . $this->getCurrency() . $this->getPayeePurse() . $this->getPhone() + . $this->getPrice(), + $key, + $keyPassword + ) + ); + } + + parent::lightAuth($certificate, $key, $keyPassword); } /** @@ -115,9 +113,9 @@ public function sign(Signer $requestSigner = null) { if ($this->authType === self::AUTH_CLASSIC) { $this->setSignature($requestSigner->sign( - $this->getSignerWmid() . $this->getCurrency() . - $this->getPayeePurse() . $this->getPhone() . - $this->getPrice() + $this->getSignerWmid() . $this->getCurrency() . + $this->getPayeePurse() . $this->getPhone() . + $this->getPrice() )); } } diff --git a/Api/WMC/WMC1/Response.php b/Api/WMC/WMC1/Response.php index 26e247e..d2427d1 100644 --- a/Api/WMC/WMC1/Response.php +++ b/Api/WMC/WMC1/Response.php @@ -48,14 +48,14 @@ public function __construct($response) if (isset($responseObject->payment)) { $payment = $responseObject->payment; - $this->currency = (string) $payment['currency']; - $this->merchant = (int) $payment['merchant']; - $this->status = (string) $payment['status']; - $this->payeePurse = (string) $payment->purse; - $this->phone = (string) $payment->phone; - $this->price = (float) $payment->price; - $this->amount = (float) $payment->amount; - $this->limit = (float) $payment->limit; + $this->currency = (string)$payment['currency']; + $this->merchant = (int)$payment['merchant']; + $this->status = (string)$payment['status']; + $this->payeePurse = (string)$payment->purse; + $this->phone = (string)$payment->phone; + $this->price = (float)$payment->price; + $this->amount = (float)$payment->amount; + $this->limit = (float)$payment->limit; } } diff --git a/Api/WMC/WMC2/Request.php b/Api/WMC/WMC2/Request.php index a2b8113..64c1537 100644 --- a/Api/WMC/WMC2/Request.php +++ b/Api/WMC/WMC2/Request.php @@ -71,9 +71,9 @@ public function __construct($authType = self::AUTH_CLASSIC) protected function getValidationRules() { return array( - RequestValidator::TYPE_REQUIRED => array( - 'lang', 'transactionId', 'currency', 'test', 'price', 'date', 'point' - ) + RequestValidator::TYPE_REQUIRED => array( + 'lang', 'transactionId', 'currency', 'test', 'price', 'date', 'point' + ) ); } @@ -107,28 +107,26 @@ public function getResponseClassName() { return Response::className(); } - - /** - * @param string $lightCertificate - * @param string $lightKey - * @param string $lightPass - */ - public function cert($lightCertificate, $lightKey, $lightPass = '') { - if ($this->authType === self::AUTH_LIGHT) { - $this->setSignature( - $this->signLight( - $this->getSignerWmid() . $this->getTransactionId() . - $this->getCurrency() . $this->getTest() . - $this->getPayeePurse() . $this->getPhone() . - $this->getPrice() . $this->getDate()->format('Ymd H:i:s') . - $this->getPoint(), - $lightKey, $lightPass - ) - ); - } - parent::cert($lightCertificate, $lightKey, $lightPass); + + /** + * @inheritdoc + */ + public function lightAuth($certificate, $key, $keyPassword = '') + { + if ($this->authType === self::AUTH_LIGHT) { + $this->setSignature( + $this->signLight( + $this->getSignerWmid() . $this->getTransactionId() . $this->getCurrency() + . $this->getTest() . $this->getPayeePurse() . $this->getPhone() . $this->getPrice() + . $this->getDate()->format('Ymd H:i:s') . $this->getPoint(), + $key, + $keyPassword + ) + ); + } + parent::lightAuth($certificate, $key, $keyPassword); } - + /** * @param Signer $requestSigner */ @@ -136,11 +134,9 @@ public function sign(Signer $requestSigner = null) { if ($this->authType === self::AUTH_CLASSIC) { $this->setSignature($requestSigner->sign( - $this->getSignerWmid() . $this->getTransactionId() . - $this->getCurrency() . $this->getTest() . - $this->getPayeePurse() . $this->getPhone() . - $this->getPrice() . $this->getDate()->format('Ymd H:i:s') . - $this->getPoint() + $this->getSignerWmid() . $this->getTransactionId() . $this->getCurrency() . $this->getTest() + . $this->getPayeePurse() . $this->getPhone() . $this->getPrice() + . $this->getDate()->format('Ymd H:i:s') . $this->getPoint() )); } } diff --git a/Api/WMC/WMC2/Response.php b/Api/WMC/WMC2/Response.php index 57c3bbc..4698315 100644 --- a/Api/WMC/WMC2/Response.php +++ b/Api/WMC/WMC2/Response.php @@ -64,19 +64,19 @@ public function __construct($response) if (isset($responseObject->payment)) { $payment = $responseObject->payment; - $this->id = (int) $payment['id']; - $this->currency = (string) $payment['currency']; - $this->test = (int) $payment['test']; - $this->payeePurse = (string) $payment->purse; - $this->phone = (string) $payment->phone; - $this->price = (float) $payment->price; - $this->amount = (float) $payment->amount; - $this->comiss = (float) $payment->comiss; - $this->rest = (float) $payment->rest; - $this->date = self::createDateTime((string)$payment->date); - $this->point = (int) $payment->point; - $this->wmtranid = (int) $payment->wmtranid; - $this->dateupd = self::createDateTime((string)$payment->dateupd); + $this->id = (int)$payment['id']; + $this->currency = (string)$payment['currency']; + $this->test = (int)$payment['test']; + $this->payeePurse = (string)$payment->purse; + $this->phone = (string)$payment->phone; + $this->price = (float)$payment->price; + $this->amount = (float)$payment->amount; + $this->comiss = (float)$payment->comiss; + $this->rest = (float)$payment->rest; + $this->date = self::createDateTime((string)$payment->date); + $this->point = (int)$payment->point; + $this->wmtranid = (int)$payment->wmtranid; + $this->dateupd = self::createDateTime((string)$payment->dateupd); } } diff --git a/Api/WMC/WMC3/Payment.php b/Api/WMC/WMC3/Payment.php index 06ea958..3bf728d 100644 --- a/Api/WMC/WMC3/Payment.php +++ b/Api/WMC/WMC3/Payment.php @@ -50,19 +50,19 @@ class Payment public function __construct(array $data) { - $this->id = $data['id']; - $this->currency = $data['currency']; - $this->test = $data['test']; + $this->id = $data['id']; + $this->currency = $data['currency']; + $this->test = $data['test']; $this->payeePurse = $data['purse']; - $this->phone = $data['phone']; - $this->price = $data['price']; - $this->amount = $data['amount']; - $this->comiss = $data['comiss']; - $this->rest = $data['rest']; - $this->date = $data['date']; - $this->point = $data['point']; - $this->wmtranid = $data['wmtranid']; - $this->dateupd = $data['dateupd']; + $this->phone = $data['phone']; + $this->price = $data['price']; + $this->amount = $data['amount']; + $this->comiss = $data['comiss']; + $this->rest = $data['rest']; + $this->date = $data['date']; + $this->point = $data['point']; + $this->wmtranid = $data['wmtranid']; + $this->dateupd = $data['dateupd']; } /** diff --git a/Api/WMC/WMC3/Request.php b/Api/WMC/WMC3/Request.php index 37f1ce7..6f6ef82 100644 --- a/Api/WMC/WMC3/Request.php +++ b/Api/WMC/WMC3/Request.php @@ -51,9 +51,9 @@ public function __construct($authType = self::AUTH_CLASSIC) protected function getValidationRules() { return array( - RequestValidator::TYPE_REQUIRED => array( - 'datestart', 'dateend', 'transactionId' - ) + RequestValidator::TYPE_REQUIRED => array( + 'datestart', 'dateend', 'transactionId' + ) ); } @@ -80,23 +80,24 @@ public function getResponseClassName() { return Response::className(); } - - /** - * @param string $lightCertificate - * @param string $lightKey - * @param string $lightPass - */ - public function cert($lightCertificate, $lightKey, $lightPass = '') { - if ($this->authType === self::AUTH_LIGHT) { - $this->setSignature( - $this->signLight( - $this->getSignerWmid() . $this->getStartDateTime()->format('Ymd H:i:s') . - $this->getEndDateTime()->format('Ymd H:i:s') . $this->getTransactionId(), - $lightKey, $lightPass - ) - ); - } - parent::cert($lightCertificate, $lightKey, $lightPass); + + /** + * @inheritdoc + */ + public function lightAuth($certificate, $key, $keyPassword = '') + { + if ($this->authType === self::AUTH_LIGHT) { + $this->setSignature( + $this->signLight( + $this->getSignerWmid() . $this->getStartDateTime()->format('Ymd H:i:s') + . $this->getEndDateTime()->format('Ymd H:i:s') . $this->getTransactionId(), + $key, + $keyPassword + ) + ); + } + + parent::lightAuth($certificate, $key, $keyPassword); } /** @@ -106,8 +107,8 @@ public function sign(Signer $requestSigner = null) { if ($this->authType === self::AUTH_CLASSIC) { $this->setSignature($requestSigner->sign( - $this->getSignerWmid() . $this->getStartDateTime()->format('Ymd H:i:s') . - $this->getEndDateTime()->format('Ymd H:i:s') . $this->getTransactionId() + $this->getSignerWmid() . $this->getStartDateTime()->format('Ymd H:i:s') + . $this->getEndDateTime()->format('Ymd H:i:s') . $this->getTransactionId() )); } } diff --git a/Api/WMC/WMC3/Response.php b/Api/WMC/WMC3/Response.php index 3ea4299..a6aa63a 100644 --- a/Api/WMC/WMC3/Response.php +++ b/Api/WMC/WMC3/Response.php @@ -32,13 +32,13 @@ public function __construct($response) parent::__construct($response); $responseObject = new \SimpleXMLElement($response); - $this->returnCode = (int) $responseObject->retval; - $this->returnDescription = (string) $responseObject->retdesc; - $this->datestart = self::createDateTime((string)$responseObject->datestart); - $this->dateend = self::createDateTime((string)$responseObject->dateend); + $this->returnCode = (int)$responseObject->retval; + $this->returnDescription = (string)$responseObject->retdesc; + $this->datestart = self::createDateTime((string)$responseObject->datestart); + $this->dateend = self::createDateTime((string)$responseObject->dateend); if (isset($responseObject->history)) { - $this->count = (int) $responseObject->history['count']; + $this->count = (int)$responseObject->history['count']; foreach ($responseObject->history->children() as $payment) { $this->payments[] = new Payment($this->operationSimpleXmlToArray($payment)); } @@ -56,19 +56,19 @@ public function getPayments() protected function operationSimpleXmlToArray($simpleXml) { $data = array( - 'id' => (int) $simpleXml['id'], - 'currency' => (string) $simpleXml['currency'], - 'test' => (int) $simpleXml['test'], - 'payeePurse' => (string) $simpleXml->purse, - 'phone' => (string) $simpleXml->phone, - 'price' => (float) $simpleXml->price, - 'amount' => (float) $simpleXml->amount, - 'comiss' => (float) $simpleXml->comiss, - 'rest' => (float) $simpleXml->rest, - 'date' => self::createDateTime((string)$simpleXml->date), - 'point' => (int) $simpleXml->point, - 'wmtranid' => (int) $simpleXml->wmtranid, - 'dateupd' => self::createDateTime((string)$simpleXml->dateupd), + 'id' => (int)$simpleXml['id'], + 'currency' => (string)$simpleXml['currency'], + 'test' => (int)$simpleXml['test'], + 'payeePurse' => (string)$simpleXml->purse, + 'phone' => (string)$simpleXml->phone, + 'price' => (float)$simpleXml->price, + 'amount' => (float)$simpleXml->amount, + 'comiss' => (float)$simpleXml->comiss, + 'rest' => (float)$simpleXml->rest, + 'date' => self::createDateTime((string)$simpleXml->date), + 'point' => (int)$simpleXml->point, + 'wmtranid' => (int)$simpleXml->wmtranid, + 'dateupd' => self::createDateTime((string)$simpleXml->dateupd), ); return $data; diff --git a/Api/X/Request.php b/Api/X/Request.php index 449b9ab..a1b81cb 100644 --- a/Api/X/Request.php +++ b/Api/X/Request.php @@ -26,26 +26,36 @@ abstract class Request extends XmlRequest /** @var string Light auth key file name (PEM) */ protected $lightKey; - + /** @var string Light auth key password */ - protected $lightPass; + protected $lightPassword; public function __construct($authType = self::AUTH_CLASSIC) { $this->authType = $authType; $this->requestNumber = $this->generateRequestNumber(); } - + + /** + * @param string $certificate Light auth certificate file name (PEM) + * @param string $key Light auth key file name (PEM) + * @param string $keyPassword Light auth key password + */ + public function lightAuth($certificate, $key, $keyPassword = '') + { + $this->lightCertificate = $certificate; + $this->lightKey = $key; + $this->lightPassword = $keyPassword; + } + /** - * @param string $lightCertificate Light auth certificate file name (PEM) - * @param string $lightKey Light auth key file name (PEM) - * @param string $lightPass Light auth key pass + * @deprecated Use lightAuth() instead + * @param string $certificate Light auth certificate file name (PEM) + * @param string $key Light auth key file name (PEM) */ - public function cert($lightCertificate, $lightKey, $lightPass = '') + public function cert($certificate, $key) { - $this->lightCertificate = $lightCertificate; - $this->lightKey = $lightKey; - $this->lightPass = $lightPass; + $this->lightAuth($certificate, $key); } /** @@ -63,20 +73,21 @@ public function getLightCertificate() { return $this->lightCertificate; } - + /** * @return string */ public function getLightKey() { - return $this->lightKey; + return $this->lightKey; } - + /** * @return string */ - public function getLightPass() { - return $this->lightPass; + public function getLightPassword() + { + return $this->lightPassword; } /** diff --git a/README.md b/README.md index 9eb5fcb..f26526a 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,14 @@ XML-interfaces supported - [X21](https://github.com/baibaratsky/php-webmoney/wiki/X21): setting trust for merchant payments by SMS - [X22](https://github.com/baibaratsky/php-webmoney/wiki/X22): receiving the ticket of prerequest payment form at merchant.webmoney +XML-interfaces to top-up WMZ and WME +------------------------------------ +- [ATM1](https://github.com/baibaratsky/php-webmoney/wiki/ATM1): preliminary request on the payment possibility +- [ATM2](https://github.com/baibaratsky/php-webmoney/wiki/ATM2): payment request +- [WMC1](https://github.com/baibaratsky/php-webmoney/wiki/WMC1): preliminary request on the payment possibility (WebMoney.Check) +- [WMC2](https://github.com/baibaratsky/php-webmoney/wiki/WMC2): payment request (WebMoney.Check) +- [WMC3](https://github.com/baibaratsky/php-webmoney/wiki/WMC3): payments log (WebMoney.Check) + Megastock interfaces supported ------------------------------ - Interface for [adding Payment Integrator's merchants](https://github.com/baibaratsky/php-webmoney/wiki/Adding-Payment-Integrator%27s-merchant) @@ -96,7 +104,7 @@ if ($request->validate()) { Authentication with a Light certificate --------------------------------------- In case of authentication with a Light certificate, pass `Request::AUTH_LIGHT` to the request constructor -and use `cert()` instead of `sign()`. +and use `lightAuth()` instead of `sign()`. ```php require_once(__DIR__ . '/vendor/autoload.php'); // Require autoload file generated by composer @@ -111,7 +119,7 @@ $webMoney = new WebMoney\WebMoney(new CurlRequester); $request = new Request(Request::AUTH_LIGHT); $request->setRequestedWmid('REQUESTED WMID'); -$request->cert('FULL PATH TO THE CERTIFICATE FILE', 'FULL PATH TO THE CERTIFICATE KEY', '(OPTIONAL) PASSWORD'); +$request->lightAuth('FULL PATH TO THE CERTIFICATE FILE', 'FULL PATH TO THE CERTIFICATE KEY', '(OPTIONAL) PASSWORD'); // You can access the request XML: $request->getData() diff --git a/Request/AbstractRequest.php b/Request/AbstractRequest.php index 29df5be..6eabecc 100644 --- a/Request/AbstractRequest.php +++ b/Request/AbstractRequest.php @@ -16,6 +16,26 @@ abstract class AbstractRequest /** @var string sign|signstr */ protected $signature; + /** + * Sign data for light authentication keys (for the ATM and WMC interfaces) + * @param string $data data to sign + * @param string $key full path to the private key file + * @param string $keyPassword (optional) password for the private key + * + * @return string + * @throws Exception + */ + protected static function signLight($data, $key, $keyPassword = '') + { + if (!is_file($key)) { + throw new Exception('Cannot access private key'); + } + + $privateKey = openssl_get_privatekey(file_get_contents($key), $keyPassword); + openssl_sign($data, $signature, $privateKey, OPENSSL_ALGO_SHA1); + return base64_encode($signature); + } + /** * @return bool */ @@ -26,25 +46,6 @@ public function validate() return count($this->errors) == 0; } - - /** - * Sign data for light authentication keys - needed for ATM and WMC interface - * @param string $data data to sign - * @param string $lightKey filepath to private key - * @param string $lightPass (optional) password for private key - * - * @return string - * @throws Exception - */ - public function signLight($data, $lightKey, $lightPass = '') { - if (!is_file($lightKey)) { - throw new Exception('Cannot access private key'); - } - - $pkeyid = openssl_get_privatekey(file_get_contents($lightKey), $lightPass); - openssl_sign($data, $sig, $pkeyid, OPENSSL_ALGO_SHA1); - return base64_encode($sig); - } /** * @return string @@ -67,12 +68,13 @@ public function getErrors() { return $this->errors; } - + /** * @return string */ - public function getSignature() { - return $this->signature; + public function getSignature() + { + return $this->signature; } /** diff --git a/Request/Requester/CurlRequester.php b/Request/Requester/CurlRequester.php index 46b7ace..10c9af9 100644 --- a/Request/Requester/CurlRequester.php +++ b/Request/Requester/CurlRequester.php @@ -34,18 +34,16 @@ protected function request(AbstractRequest $request) curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, $this->verifyCertificate); curl_setopt($handler, CURLOPT_SSLVERSION, 1); - if (($request instanceof XRequest || - $request instanceof ATMRequest || - $request instanceof WMCRequest) && - ($request->getAuthType() === XRequest::AUTH_LIGHT || - $request->getAuthType() === ATMRequest::AUTH_LIGHT || - $request->getAuthType() === WMCRequest::AUTH_LIGHT)) { + if (($request instanceof XRequest && $request->getAuthType() === XRequest::AUTH_LIGHT) + || ($request instanceof ATMRequest && $request->getAuthType() === ATMRequest::AUTH_LIGHT) + || ($request instanceof WMCRequest && $request->getAuthType() === WMCRequest::AUTH_LIGHT) + ) { curl_setopt($handler, CURLOPT_SSLCERT, $request->getLightCertificate()); curl_setopt($handler, CURLOPT_SSLKEY, $request->getLightKey()); - - $pass = $request->getLightPass(); - if (!empty($pass)) { - curl_setopt($handler, CURLOPT_KEYPASSWD, $request->getLightPass()); + + $password = $request->getLightPassword(); + if (!empty($password)) { + curl_setopt($handler, CURLOPT_KEYPASSWD, $password); } }