Skip to content

Commit

Permalink
Add get shop
Browse files Browse the repository at this point in the history
  • Loading branch information
regdos committed Apr 7, 2020
1 parent 3bda673 commit 45aca9f
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ Remember: All keys in "order array" must be in lowercase.
);
```

### Get Shop

File with working example: [examples/v2/shops/Get.php](examples/v2/shops/Get.php)

You can retrieve shop data.

```php
$refund = OpenPayU_Shop::get(
'PUBLIC_SHOP_ID' // Shop ID from Merchant Panel
);
```

## Contributing

1. Fork it
Expand Down
72 changes: 72 additions & 0 deletions examples/v2/shops/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* OpenPayU Examples
*
* @copyright Copyright (c) PayU
* @license http://opensource.org/licenses/LGPL-3.0 Open Software License (LGPL 3.0)
* http://www.payu.com
* http://developers.payu.com
*/

require_once realpath(dirname(__FILE__)) . '/../../../lib/openpayu.php';
require_once realpath(dirname(__FILE__)) . '/../../config.php';

if (isset($_POST['publicShopId'])) {
$publicShopId = trim($_POST['publicShopId']);
}
?>
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Retrieving shop data</title>
<link rel="stylesheet" href="../../layout/css/bootstrap.min.css">
<link rel="stylesheet" href="../../layout/css/style.css">
<script type="text/javascript" src="../../layout/js/jquery.min.js"></script>
</head>

<body>
<div class="container">
<div class="page-header">
<h1>Retrieving shop data</h1>
</div>

<?php
if (!empty($publicShopId)) {
try {
$shop = OpenPayU_Shop::get($publicShopId);
echo '<pre>'.$shop.'</pre>';
} catch (OpenPayU_Exception $e) {
echo '<pre>';
echo 'Error code: ' . $e->getCode();
echo '<br>';
echo 'Error message: ' . $e->getMessage();
echo '<br>';
echo '</pre>';
}
} else {
?>
<form action="" method="post" class="form-horizontal">
<div class="control-group">
<label class="control-label" for="publicShopId">Public Shop Id</label>
<div class="controls">
<input class="span5" name="publicShopId" id="publicShopId" type="text" value=""/>
</div>
</div>

<div class="control-group">
<label class="control-label" for="pay-button"></label>

<div id="msg"></div>
<div class="controls">
<button class="btn btn-success" id="pay-button" type="submit">Get shop</button>
</div>
</div>
</form>
<?php
}
?>
</div>
</body>
</html>
100 changes: 100 additions & 0 deletions lib/OpenPayU/Model/Shop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

class Shop
{
/** @var string */
private $shopId;

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

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

/** @var Balance */
private $balance;

/**
* @return string
*/
public function getShopId()
{
return $this->shopId;
}

/**
* @param string $shopId
* @return Shop
*/
public function setShopId($shopId)
{
$this->shopId = $shopId;
return $this;
}

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

/**
* @param string $name
* @return Shop
*/
public function setName($name)
{
$this->name = $name;
return $this;
}

/**
* @return string
*/
public function getCurrencyCode()
{
return $this->currencyCode;
}

/**
* @param string $currencyCode
* @return Shop
*/
public function setCurrencyCode($currencyCode)
{
$this->currencyCode = $currencyCode;
return $this;
}

/**
* @return Balance
*/
public function getBalance()
{
return $this->balance;
}

/**
* @param Balance $balance
* @return Shop
*/
public function setBalance($balance)
{
$this->balance = $balance;
return $this;
}

/**
* @return string
*/
public function __toString()
{
return 'Shop [currencyCode=' . $this->shopId .
', name=' . $this->name .
', currencyCode=' . $this->currencyCode .
', balance=' . $this->balance .
']';
}
}
78 changes: 78 additions & 0 deletions lib/OpenPayU/Model/Shop/Balance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

class Balance
{
/** @var string */
private $currencyCode;

/** @var int */
private $total;

/** @var int */
private $available;

/**
* @return string
*/
public function getCurrencyCode()
{
return $this->currencyCode;
}

/**
* @param string $currencyCode
* @return Balance
*/
public function setCurrencyCode($currencyCode)
{
$this->currencyCode = $currencyCode;
return $this;
}

/**
* @return int
*/
public function getTotal()
{
return $this->total;
}

/**
* @param int $total
* @return Balance
*/
public function setTotal($total)
{
$this->total = $total;
return $this;
}

/**
* @return int
*/
public function getAvailable()
{
return $this->available;
}

/**
* @param int $available
* @return Balance
*/
public function setAvailable($available)
{
$this->available = $available;
return $this;
}

/**
* @return string
*/
public function __toString()
{
return 'Balance [currencyCode=' . $this->currencyCode .
', total=' . $this->total .
', available=' . $this->available .
']';
}
}
79 changes: 79 additions & 0 deletions lib/OpenPayU/v2/Shop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* OpenPayU Standard Library
*
* @copyright Copyright (c) PayU
* @license http://opensource.org/licenses/LGPL-3.0 Open Software License (LGPL 3.0)
* http://www.payu.com
* http://developers.payu.com
*/

class OpenPayU_Shop extends OpenPayU
{
const SHOPS_SERVICE = 'shops';

/**
* Retrieving shop data
* @param string $publicShopId
* @return Shop
* @throws OpenPayU_Exception
* @throws OpenPayU_Exception_Configuration
*/
public static function get($publicShopId)
{
try {
$authType = self::getAuth();
} catch (OpenPayU_Exception $e) {
throw new OpenPayU_Exception($e->getMessage(), $e->getCode());
}

if (!$authType instanceof AuthType_Oauth) {
throw new OpenPayU_Exception_Configuration('Get shop works only with OAuth');
}

$pathUrl = OpenPayU_Configuration::getServiceUrl() . self::SHOPS_SERVICE . '/' . $publicShopId;

return self::verifyResponse(OpenPayU_Http::doGet($pathUrl, $authType));
}

/**
* @param array $response
* @return Shop
* @throws OpenPayU_Exception
*/
public static function verifyResponse($response)
{
$httpStatus = $response['code'];

if ($httpStatus == 500) {
$result = (new ResultError())
->setErrorDescription($response['response']);
OpenPayU_Http::throwErrorHttpStatusException($httpStatus, $result);
}

$message = json_decode($response['response'], true);

if (json_last_error() === JSON_ERROR_SYNTAX) {
throw new OpenPayU_Exception_ServerError('Incorrect json response. Response: [' . $response['response'] . ']');
}

if ($httpStatus == 200) {
return (new Shop())
->setShopId($message['shopId'])
->setName($message['name'])
->setCurrencyCode($message['currencyCode'])
->setBalance(
(new Balance())
->setCurrencyCode($message['balance']['currencyCode'])
->setTotal($message['balance']['total'])
->setAvailable($message['balance']['available'])
);
}

$result = (new ResultError())
->setError($message['error'])
->setErrorDescription($message['error_description']);

OpenPayU_Http::throwErrorHttpStatusException($httpStatus, $result);
}
}
4 changes: 4 additions & 0 deletions lib/openpayu.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@
include_once('OpenPayU/v2/Order.php');
include_once('OpenPayU/v2/Retrieve.php');
include_once('OpenPayU/v2/Token.php');
include_once('OpenPayU/v2/Shop.php');

include_once('OpenPayU/Model/Shop.php');
include_once('OpenPayU/Model/Shop/Balance.php');

0 comments on commit 45aca9f

Please sign in to comment.