From 0a45cead7670cb328c30ecb0228375475a37242b Mon Sep 17 00:00:00 2001 From: Margulan Baimbet Date: Fri, 30 Mar 2018 12:37:30 +0600 Subject: [PATCH] Change public API --- README.md | 37 ++++++++++++++++++++++-------- composer.json | 2 +- src/Epay.php | 4 ++-- src/Requests/AbstractRequest.php | 2 +- src/Requests/PaymentRequest.php | 8 +++++++ src/Responses/AbstractResponse.php | 19 ++++++++++++--- src/Responses/PaymentResponse.php | 2 +- src/Responses/StatusResponse.php | 9 +++++--- 8 files changed, 62 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 4dad54c..04683a0 100644 --- a/README.md +++ b/README.md @@ -27,28 +27,45 @@ $request = $epay->buildRequest('payment', [ 'order_id' => 1234, 'amount' => 1000, 'currency' => 398, - fields => [ + 'fields' => [ 'RL' => 1234567, 'abonent_id' => 1234567, ... ] -])->getXML(); +]); // Статус платежа -$request = $epay->buildRequest('status', ['order_id' => 1234])->getXML(); - +$request = $epay->buildRequest('status', ['order_id' => 1234]); +``` +```php //Обработка ответов Epay -$response = $epay->parseResponse('payment', $_POST['response']); // $_POST только для примера, не делайте так в реальной жизни -$client = new GuzzleHttp\Client(); -$resp = $client->get('https://testpay.kkb.kz/jsp/remote/checkOrdern.jsp?' . urlencode($request)); +// PostLink +$response = $epay->parseResponse('payment', $_POST['response']); + +// Ответ на запрос о статусе платежа +$curl = curl_init('https://testpay.kkb.kz/jsp/remote/checkOrdern.jsp?' . urlencode($request)); + +curl_setopt_array($curl, array( + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_POST => 1, + CURLOPT_POSTFIELDS => [ + 'Signed_Order_B64' => $request, + 'BackLink' => 'asdads', + 'PostLink' => 'asdads', + 'FailurePostLink' => 'asdads', + ] +)); + +$result = curl_exec($curl); +curl_close($curl); -$response = $epay->parseResponse('status', $resp->getBody()->getContent()); +$response = $epay->parseResponse('status', $result); //verify() проверяет подпись ответа. if($response->verify()) { - print_r($response->getProps()); - // getProps возвращает массив с параметрами ответа + print_r($response->get()); + // get возвращает массив с параметрами ответа } else { echo 'Response not valid'; } diff --git a/composer.json b/composer.json index f2c5053..7eb6b21 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "A PHP client for the Qazkom Epay payment gateway", "type": "library", "license": "MIT", - "version": "0.0.4", + "version": "0.0.5", "authors": [ { "name": "Margulan Baimbet", diff --git a/src/Epay.php b/src/Epay.php index cc1e03b..403efed 100644 --- a/src/Epay.php +++ b/src/Epay.php @@ -20,9 +20,9 @@ public function __construct($params = []) $this->certManager = new CertManager($params); } - public function buildRequest(string $type, array $params): Requests\AbstractRequest + public function buildRequest(string $type, array $params) { - return RequestFactory::create($type, array_merge($params, $this->params), $this->certManager); + return RequestFactory::create($type, array_merge($params, $this->params), $this->certManager)->getXML(); } public function parseResponse(string $type, string $body) diff --git a/src/Requests/AbstractRequest.php b/src/Requests/AbstractRequest.php index 3785059..9001a84 100644 --- a/src/Requests/AbstractRequest.php +++ b/src/Requests/AbstractRequest.php @@ -30,7 +30,7 @@ public function getXML() { return preg_replace('/^.+\n/', '', $this->xml->saveXML()); } - private function signXML() + protected function signXML() { $merchant = $this->xml->xpath('/document/merchant')[0]; diff --git a/src/Requests/PaymentRequest.php b/src/Requests/PaymentRequest.php index 07c0f47..0e541d2 100644 --- a/src/Requests/PaymentRequest.php +++ b/src/Requests/PaymentRequest.php @@ -30,4 +30,12 @@ public function buildXML() return $document; } + + public function getXML() + { + $this->signXML(); + $xml = preg_replace('/^.+\n/', '', $this->xml->saveXML()); + + return base64_encode($xml); + } } \ No newline at end of file diff --git a/src/Responses/AbstractResponse.php b/src/Responses/AbstractResponse.php index 46bfdb2..a6ccea3 100644 --- a/src/Responses/AbstractResponse.php +++ b/src/Responses/AbstractResponse.php @@ -16,7 +16,10 @@ abstract class AbstractResponse /** @var array $props */ protected $props = []; + /** @var string $merchantPath */ protected $merchantPath = ''; + + /** @var string $merchantSignPath */ protected $merchantSignPath = ''; public function __construct(string $body, CertManager $certManager) @@ -27,6 +30,15 @@ public function __construct(string $body, CertManager $certManager) $this->props = $this->fillProps($this->xml); } + /** + * @param \SimpleXMLElement $xml + * @return mixed + */ + abstract protected function parse(\SimpleXMLElement $xml); + + /** + * @return int + */ public function verify() { $signature = strrev( @@ -38,9 +50,10 @@ public function verify() return $this->certManager->verify($data, $signature); } - abstract protected function fillProps(\SimpleXMLElement $sxi); - - public function getProps() + /** + * @return array + */ + public function get() { return $this->props; } diff --git a/src/Responses/PaymentResponse.php b/src/Responses/PaymentResponse.php index 988318c..06689aa 100644 --- a/src/Responses/PaymentResponse.php +++ b/src/Responses/PaymentResponse.php @@ -8,7 +8,7 @@ class PaymentResponse extends AbstractResponse protected $merchantPath = '/document/bank/customer/merchant'; protected $merchantSignPath = '/document/bank/customer/merchant_sign'; - protected function fillProps(\SimpleXMLElement $sxi) + protected function fillProps(\SimpleXMLElement $sxi) : array { $props = []; diff --git a/src/Responses/StatusResponse.php b/src/Responses/StatusResponse.php index f10ceca..b6c4c33 100644 --- a/src/Responses/StatusResponse.php +++ b/src/Responses/StatusResponse.php @@ -8,7 +8,10 @@ class StatusResponse extends AbstractResponse protected $merchantPath = '/document/bank/merchant'; protected $merchantSignPath = '/document/bank/merchant_sign'; - protected function fillProps(\SimpleXMLElement $sxi) + /** + * @inheritdoc + */ + protected function parse(\SimpleXMLElement $xml) { $props = []; @@ -17,11 +20,11 @@ protected function fillProps(\SimpleXMLElement $sxi) $response = $this->xml->xpath('/document/bank/response')[0]; foreach ($order->attributes() as $name => $value) { - $props['order_' . $name] = $value; + $props['order'][$name] = (string)$value; } foreach ($response->attributes() as $name => $value) { - $props['response_' . $name] = $value; + $props['response'][$name] = (string)$value; } return $props;