Skip to content

Commit

Permalink
Merge pull request #1 from sch-group/HBSDEV-401
Browse files Browse the repository at this point in the history
Hbsdev 401
  • Loading branch information
ainurqa95 authored Jul 7, 2021
2 parents 05dd727 + e084f9e commit ccf15b5
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 121 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
4 changes: 2 additions & 2 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
}
],
"require": {
"php": ">=7.2",
"guzzlehttp/guzzle": "^6.2"
"php": ">=7.4",
"guzzlehttp/guzzle": "^6.2|^7"
},
"require-dev": {
"phpunit/phpunit": "^6.3"
Expand Down
47 changes: 47 additions & 0 deletions src/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace SchGroup\IpsPayment;

class Callback
{
private ?string $success;
private ?string $failed;
private ?string $callback;

/**
* ShopSettings constructor.
* @param string|null $success
* @param string|null $failed
* @param string|null $callback
*/
public function __construct(string $success = null, string $failed = null, string $callback = null)
{
$this->success = $success;
$this->failed = $failed;
$this->callback = $callback;
}

/**
* @return string|null
*/
public function getSuccess(): ?string
{
return $this->success;
}

/**
* @return string|null
*/
public function getFailed(): ?string
{
return $this->failed;
}

/**
* @return string|null
*/
public function getCallback(): ?string
{
return $this->callback;
}
}
57 changes: 28 additions & 29 deletions src/Customer.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,55 @@

class Customer
{
private string $firstName;
private string $name;
private ?string $email;
private ?string $phone;

/**
* @var string
*/
private $customerName;
/**
* @var string
*/
private $customerEmail;
/**
* @var string
* Customer constructor.
* @param string $firstName
* @param string $name
* @param string|null $email
* @param string|null $phone
*/
private $customerPhone;
public function __construct(string $name, string $firstName, ?string $email = null, ?string $phone = null)
{
$this->firstName = $firstName;
$this->name = $name;
$this->email = $email;
$this->phone = $phone;
}

/**
* Customer constructor.
* @param string $customerName
* @param string|null $customerEmail
* @param string|null $customerPhone
* @return string
*/
public function __construct(
string $customerName,
?string $customerEmail = null,
?string $customerPhone = null
) {
$this->customerName = $customerName;
$this->customerEmail = $customerEmail;
$this->customerPhone = $customerPhone;
public function getFirstName(): string
{
return $this->firstName;
}

/**
* @return string
*/
public function getCustomerName(): string
public function getName(): string
{
return $this->customerName;
return $this->name;
}

/**
* @return string
*/
public function getCustomerEmail(): ?string
public function getEmail(): ?string
{
return $this->customerEmail;
return $this->email;
}

/**
* @return string
*/
public function getCustomerPhone(): ?string
public function getPhone(): ?string
{
return $this->customerPhone;
return $this->phone;
}
}
}
83 changes: 70 additions & 13 deletions src/IpsPayment.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,89 @@

namespace SchGroup\IpsPayment;

use GuzzleHttp\ClientInterface;
use Exception;
use JsonException;

class IpsPayment
{
private const HEADERS = ['Content-Type: application/x-www-form-urlencoded'];

/**
* @var ClientInterface
* @param Transaction $transaction
* @return string
* @throws JsonException
* @throws Exception
*/
private $client;
public function buildLink(Transaction $transaction): string
{
$requestBody = $this->prepareParams($transaction);
$requestLink = $transaction->getShopSettings()->getTransitionPath();
$response = $this->sendRequest($requestLink, $requestBody);

$this->prepareResponse($response);

return $response['DirectLinkIs'];
}

/**
* IpsPayment constructor.
* @param ClientInterface $client
* @param Transaction $transaction
* @return array
*/
public function __construct(ClientInterface $client)
private function prepareParams(Transaction $transaction): array
{
$this->client = $client;
$paramsToString = implode('!', array_values($transaction->toArray()));
$paramsToString .= '!' . $transaction->getShopSettings()->getSecretKey();
$paramsToString .= '|' . $transaction->getShopSettings()->getSecretKey();

return array_merge($transaction->toArray(), [
'SHA' => hash('sha512', base64_encode($paramsToString))
]);
}

/**
* @param string $link
* @return array
* ```php
* [
* 'Code' => '200',
* 'Method' => 'GET",
* 'Url_To_Redirect_Customer' => '<url-payment-form-web>',
* 'SACS' => '<token>',
* 'DirectLinkIs' => '<full-uri-redirect>',
* ]
* [
* 'ErrorCode' => '<error-code>',
* 'ErrorDescription' => '<error-description>',
* ]
* ```
* @throws Exception
* @throws JsonException
*/
private function sendRequest(string $link, array $body): array
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link . '/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, self::HEADERS);
$result = curl_exec($ch);

if (curl_errno($ch)) {
throw new Exception('Curl request error: ' . $ch);
}

return json_decode($result, true, 512, JSON_THROW_ON_ERROR);
}

/**
* @param Transaction $transaction
* @return string
* @param array $response
* @throws Exception
*/
public function buildLink(Transaction $transaction): string
private function prepareResponse(array $response): void
{
return $transaction->getShopSettings()->getApiHost() .'?'. http_build_query($transaction->toArray());
if (!isset($response['Code'])) {
throw new Exception('ips request error: ' . $response['ErrorDescription']);
}
}

}
}
27 changes: 6 additions & 21 deletions src/Order.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,10 @@

class Order
{
/**
* @var float
*/
private $amount;
/**
* @var string
*/
private $subscribePeriod;
/**
* @var string
*/
private $refOrder;
/**
* @var string
*/
private $subscribe;
private float $amount;
private ?string $subscribePeriod;
private string $refOrder;
private ?string $subscribe;

/**
* Order constructor.
Expand All @@ -33,8 +21,7 @@ public function __construct(
float $amount,
?string $subscribe = null,
?string $subscribePeriod = null
)
{
) {
$this->refOrder = $refOrder;
$this->subscribe = $subscribe;
$this->subscribePeriod = $subscribePeriod;
Expand Down Expand Up @@ -72,6 +59,4 @@ public function getSubscribe(): ?string
{
return $this->subscribe;
}


}
}
58 changes: 27 additions & 31 deletions src/ShopSettings.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,26 @@

class ShopSettings
{
private const TRANSACTION_PATH = 'init_transactions';

/**
* @var string
*/
private $Integrated;

/**
* @var string
*/
private $lang;
/**
* @var string
*/
private $merchantKey;
/**
* @var string
*/
private $apiHost;
private ?string $lang;
private string $merchantKey;
private string $apiHost;
private string $secretKey;

/**
* ShopSettings constructor.
* @param string $merchantKey
* @param string $apiHost
* @param string|null $Integrated
* @param string $merchantKey
* @param string $apiHost
* @param string $secretKey
* @param string|null $lang
*/
public function __construct(string $merchantKey, string $apiHost, string $Integrated = "NO", string $lang = null)
public function __construct(string $merchantKey, string $apiHost, string $secretKey, string $lang = null)
{
$this->merchantKey = $merchantKey;
$this->Integrated = $Integrated;
$this->lang = $lang;
$this->apiHost = $apiHost;
}

/**
* @return string
*/
public function getIntegrated(): string
{
return $this->Integrated;
$this->secretKey = $secretKey;
}

/**
Expand All @@ -69,4 +49,20 @@ public function getApiHost(): string
{
return $this->apiHost;
}
}

/**
* @return string
*/
public function getTransitionPath(): string
{
return $this->apiHost . '/' . self::TRANSACTION_PATH;
}

/**
* @return string
*/
public function getSecretKey(): string
{
return $this->secretKey;
}
}
Loading

0 comments on commit ccf15b5

Please sign in to comment.