Skip to content

Commit

Permalink
Change public API
Browse files Browse the repository at this point in the history
  • Loading branch information
panda-madness committed Mar 30, 2018
1 parent 61a1bc9 commit 0a45cea
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 21 deletions.
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/Epay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
8 changes: 8 additions & 0 deletions src/Requests/PaymentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
19 changes: 16 additions & 3 deletions src/Responses/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Responses/PaymentResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
9 changes: 6 additions & 3 deletions src/Responses/StatusResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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;
Expand Down

0 comments on commit 0a45cea

Please sign in to comment.