Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kharinenkov committed Apr 7, 2021
1 parent f74ccfb commit fd66360
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.1.0

First release
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021 Vladimir Kharinenkov, https://github.com/vhar <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Simple Sbarbank REST API Client
===============================

Simple PHP client for Sperbank acquiring REST API
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "vhar/sberbank",
"description": "Simple PHP client for Sperbank acquiring REST API",
"version": "0.1.0",
"type": "library",
"keywords": ["sberbank", "sberbank online", "credit card", "sberbank acquiring"],
"require": {
"php": ">=7.1",
"ext-json": "*",
"guzzlehttp/guzzle": ">=6.5.5"
},
"license": "MIT",
"authors": [
{
"name": "Vladimir Kharinenkov",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"VHar\\Sberbank\\": "src/"
}
},
"minimum-stability": "stable"
}
211 changes: 211 additions & 0 deletions src/Sberbank/SBClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php

namespace VHar\Sberbank;

use ErrorException;
use GuzzleHttp\Client;

/**
* Client for working with Sberbank REST API.
*
* @see https://securepayments.sberbank.ru/wiki/doku.php/integration:api:rest:start
*/

class SBClient
{
/**
* Http Client
*
* @var Client
*/
private $client;

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

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

/**
*
* @var int
*/
private $test_mode = 0;

/**
* URLs to access REST requests
*/
const API_PROD_URL = 'https://securepayments.sberbank.ru';
const API_TEST_URL = 'https://3dsec.sberbank.ru';

/**
* Client constructor.
*/
public function __construct(array $config = [], Client $client)
{
$this->client = $client ?? new GuzzleHttp\Client();

// Required params
$this->shopLogin = $config['shopLogin'] ?? $this->shopLogin;
if (!$this->shopLogin) {
throw new ErrorException('Please provide shopLogin');
}
$this->shopPassword = $config['shopPassword'] ?? $this->shopPassword;
if (!$this->shopPassword) {
throw new ErrorException('Please provide shopPassword');
}

$this->test_mode = $config['test_mode'] ?? $this->test_mode;

}

/**
* Creating a new order
* @see https://securepayments.sberbank.ru/wiki/doku.php/integration:api:rest:requests:register
*/
public function registerOrder($order)
{
$url = $this->getApiUrl() . '/payment/rest/register.do?%s';

// Required params
if (!$order['orderNumber']) {
throw new ErrorException('Please provide orderNumber');
}
if (!$order['amount']) {
throw new ErrorException('Please provide amount');
}
if (!$order['returnUrl']) {
throw new ErrorException('Please provide returnUrl');
}

$params = [
'userName' => $this->shopLogin,
'password' => $this->shopPassword,
'orderNumber' => $order['orderNumber'],
'amount' => $order['amount'],
'returnUrl' => $order['returnUrl'],
];

if ($order['failUrl']) $params['failUrl'] = $order['failUrl'];
if ($order['currency']) $params['currency'] = $order['currency'];
if ($order['language']) $params['language'] = $order['language'];
if ($order['pageView']) $params['pageView'] = $order['pageView'];
if (isset($order['params']) && !is_array($order['params'])) {
throw new ErrorException('params must be array');
}
if ($order['params']) $params['params'] = json_encode($order['params']);
if ($order['sessionTimeoutSecs']) $params['sessionTimeoutSecs'] = $order['sessionTimeoutSecs'];

$request = sprintf($url, http_build_query($params));
$response = $this->client->request('GET', $request);

return json_decode($response->getBody());
}

/**
* Check order status
* @see https://securepayments.sberbank.ru/wiki/doku.php/integration:api:rest:requests:getorderstatusextended
*/
public function getOrderStatusExtended($order)
{
$url = $this->getApiUrl() . '/payment/rest/getOrderStatusExtended.do?%s';

// Required params
if (!$order['orderNumber'] && !$order['orderId']) {
throw new ErrorException('Please provide orderId OR orderNumber');
}

if (isset($order['params']) && !is_array($order['params'])) {
throw new ErrorException('params must be array');
}

$params = [
'userName' => $this->shopLogin,
'password' => $this->shopPassword,
];

if ($order['orderNumber']) $params['orderNumber'] = $order['orderNumber'];
if ($order['orderId']) $params['orderId'] = $order['orderId'];
if ($order['language']) $params['language'] = $order['language'];

$request = sprintf($url, http_build_query($params));
$response = $this->client->request('GET', $request);

return json_decode($response->getBody());
}

/**
* Cancel order payment
* @see https://securepayments.sberbank.ru/wiki/doku.php/integration:api:rest:requests:reverse
*/
public function reverseOrder($order)
{
$url = $this->getApiUrl() . '/payment/rest/reverse.do?%s';

if (!$order['amount']) {
throw new ErrorException('Please provide amount');
}
if (!$order['orderId']) {
throw new ErrorException('Please provide orderId');
}

$params = [
'userName' => $this->shopLogin,
'password' => $this->shopPassword,
'amount' => $order['amount'],
'orderId' => $order['orderId'],
];

if ($order['language']) $params['language'] = $order['language'];
if ($order['params']) $params['params'] = json_encode($order['params']);

$request = sprintf($url, http_build_query($params));
$response = $this->client->request('GET', $request);

return json_decode($response->getBody());
}


/**
* Refund order payment
* @see https://securepayments.sberbank.ru/wiki/doku.php/integration:api:rest:requests:refund
*/
public function refundOrder($order)
{
$url = $this->getApiUrl() . '/payment/rest/refund.do?%s';

if (!$order['amount']) {
throw new ErrorException('Please provide amount');
}
if (!$order['orderId']) {
throw new ErrorException('Please provide orderId');
}

$params = [
'userName' => $this->shopLogin,
'password' => $this->shopPassword,
'amount' => $order['amount'],
'orderId' => $order['orderId'],
];

if ($order['params']) $params['params'] = json_encode($order['params']);

$request = sprintf($url, http_build_query($params));
$response = $this->client->request('GET', $request);

return json_decode($response->getBody());
}

private function getApiUrl()
{
if ($this->test_mode) {
return self::API_TEST_URL;
} else {
return self::API_PROD_URL;
}
}
}

0 comments on commit fd66360

Please sign in to comment.