Skip to content

Commit

Permalink
Merge pull request #4 from ChumakovAnton/refactoring
Browse files Browse the repository at this point in the history
Refactoring extension
  • Loading branch information
ChumakovAnton authored Jun 27, 2019
2 parents 9c74a05 + efb77fa commit 144948e
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 480 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ Once the extension is installed, simply use it in your code by :

```php

$paymentService = Yii::$app->paymentService;
/** @var \chumakovanton\tinkoffPay\TinkoffPay $paymentService */
$paymentService = Yii::$app->tinkoffPay;

$paymentRequest = new RequestInit('order1', 1000);
$paymentRequest->addData('user_id', Yii::$app->user->id);
$paymentRequest = $paymentService->initPay('order1', 1000);

$response = $paymentService->initPay($paymentRequest);
$paymentRequest->addData('user_id', 123);

try {
$response = $paymentRequest->send();
} catch (\chumakovanton\tinkoffPay\exceptions\HttpException $exception) {
throw new \yii\web\HttpException($exception->statusCode, $exception->getMessage());
}

$paymentUrl = $response->getPaymentUrl();

Expand Down
140 changes: 24 additions & 116 deletions TinkoffPay.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<?php
/**
* Created by PhpStorm.
* User: anton
* Date: 07.09.17
* Time: 11:24
*/

namespace chumakovanton\tinkoffPay;


use chumakovanton\tinkoffPay\request\RequestInit;
use chumakovanton\tinkoffPay\request\RequestInterface;
use chumakovanton\tinkoffPay\response\ResponseInit;
use RuntimeException;
use yii\base\Object;
use yii\base\BaseObject;

/**
* Class TinkoffPay
Expand All @@ -26,12 +17,12 @@
* @property string $secretKey
* @property string $apiUrl
*/
class TinkoffPay extends Object
class TinkoffPay extends BaseObject
{
/**
* @var string
*/
private $_api_url;
private $_apiUrl;
/**
* @var string
*/
Expand All @@ -44,14 +35,17 @@ class TinkoffPay extends Object
/**
* Initialize the payment
*
* @param RequestInit $request mixed You could use associative array or url params string
*
* @return ResponseInit
* @throws RuntimeException
* @param string $orderId
* @param int $amount
* @return RequestInit
*/
public function initPay(RequestInit $request): ResponseInit
public function initPay(string $orderId, int $amount): RequestInit
{
return new ResponseInit($this->buildQuery('Init', $request));
$request = new RequestInit($orderId, $amount);

$request->setCredentials($this->_apiUrl, $this->_terminalKey, $this->_secretKey);

return $request;
}

/**
Expand All @@ -63,7 +57,7 @@ public function initPay(RequestInit $request): ResponseInit
*/
public function getState($args)
{
return $this->buildQuery('GetState', $args);
return ;//todo $this->buildQuery('GetState', $args);
}

/**
Expand All @@ -75,7 +69,7 @@ public function getState($args)
*/
public function confirm($args)
{
return $this->buildQuery('Confirm', $args);
return ;//todo $this->buildQuery('Confirm', $args);
}

/**
Expand All @@ -88,7 +82,7 @@ public function confirm($args)
*/
public function charge($args)
{
return $this->buildQuery('Charge', $args);
return ;//todo $this->buildQuery('Charge', $args);
}

/**
Expand All @@ -100,7 +94,7 @@ public function charge($args)
*/
public function addCustomer($args)
{
return $this->buildQuery('AddCustomer', $args);
return ;//todo $this->buildQuery('AddCustomer', $args);
}

/**
Expand All @@ -112,7 +106,7 @@ public function addCustomer($args)
*/
public function getCustomer($args)
{
return $this->buildQuery('GetCustomer', $args);
return ;//todo $this->buildQuery('GetCustomer', $args);
}

/**
Expand All @@ -124,7 +118,7 @@ public function getCustomer($args)
*/
public function removeCustomer($args)
{
return $this->buildQuery('RemoveCustomer', $args);
return ;//todo $this->buildQuery('RemoveCustomer', $args);
}

/**
Expand All @@ -136,7 +130,7 @@ public function removeCustomer($args)
*/
public function getCardList($args)
{
return $this->buildQuery('GetCardList', $args);
return ;//todo $this->buildQuery('GetCardList', $args);
}

/**
Expand All @@ -148,7 +142,7 @@ public function getCardList($args)
*/
public function removeCard($args)
{
return $this->buildQuery('RemoveCard', $args);
return ;//todo $this->buildQuery('RemoveCard', $args);
}

/**
Expand All @@ -159,94 +153,16 @@ public function removeCard($args)
*/
public function resend()
{
return $this->buildQuery('Resend', null);
}

/**
* Builds a query string and call sendRequest method.
* Could be used to custom API call method.
*
* @param string $path API method name
* @param RequestInterface $request query params
*
* @return string JSON string response
* @throws RuntimeException
*/
protected function buildQuery(string $path, RequestInterface $request): string
{
$url = $this->_api_url;

$url = $this->_combineUrl($url, $path);

$postData = $request->setSecretKey($this->_secretKey)
->setTerminalKey($this->_terminalKey)
->serialize();

return $this->_sendRequest($url, $postData);
}

/**
* Combines parts of URL. Simply gets all parameters and puts '/' between
*
* @return string
*/
protected function _combineUrl(): string
{
$args = func_get_args();
$url = '';
foreach ($args as $arg) {
if (is_string($arg)) {
if ($arg[strlen($arg) - 1] !== '/') {
$arg .= '/';
}
$url .= $arg;
} else {
continue;
}
}

return $url;
}

/**
* Main method. Call API with params
*
* @param string $api_url API Url
* @param string $postData Data in JSON string
* @return string JSON string response
* @throws RuntimeException
*
*/
protected function _sendRequest(string $api_url, string $postData): string
{
if ($curl = curl_init()) {
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
if (!empty($postData)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
}
$out = curl_exec($curl);
curl_close($curl);

return $out;
}

throw new RuntimeException(
'Can not create connection to ' . $api_url . ' with args '
. $postData, 404
);
return ;//todo $this->buildQuery('Resend', null);
}

/**
* @param string $api_url
* @param string $apiUrl
* @return TinkoffPay
*/
public function setApiUrl($api_url): TinkoffPay
public function setApiUrl($apiUrl): TinkoffPay
{
$this->_api_url = $api_url;
$this->_apiUrl = $apiUrl;
return $this;
}

Expand All @@ -269,12 +185,4 @@ public function setSecretKey($secretKey): TinkoffPay
$this->_secretKey = $secretKey;
return $this;
}

/**
* @return string
*/
protected function getApiUrl(): string
{
return $this->_api_url;
}
}
2 changes: 1 addition & 1 deletion TinkoffPayDummyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TinkoffPayDummyRequest extends TinkoffPay
protected function _sendRequest(string $api_url, string $postData): string
{
$response = '<html><head><title>BadRequest</title></head><body>BadRequest</body></html>';
switch (str_replace('/', '', str_replace($this->getApiUrl(), '', $api_url))) {
switch (str_replace('/', '', str_replace($this->apiUrl, '', $api_url))) {
case 'Init': $response = '{"Success":true,"ErrorCode":"0","TerminalKey":"TestB","Status":"NEW","PaymentId":"13660","OrderId":"21050","Amount":100000,"PaymentURL":"https://securepay.tinkoff.ru/rest/Authorize/1B63Y1"}'; break;
}
return $response;
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
}
],
"require": {
"yiisoft/yii2": "*",
"roave/security-advisories": "dev-master"
"php": "^7.1.0",
"ext-curl": "*",
"ext-mbstring": "*",
"ext-json": "*",
"roave/security-advisories": "dev-master",
"yiisoft/yii2": "~2.0.14"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit 144948e

Please sign in to comment.