Skip to content

Commit

Permalink
Merge pull request #14 from krystianbajno/master
Browse files Browse the repository at this point in the history
Refunds support
  • Loading branch information
axotion committed Apr 9, 2019
2 parents 6469996 + 02cff2a commit 7e0abcb
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 19 deletions.
27 changes: 27 additions & 0 deletions src/DotpayApi/Client.php
Expand Up @@ -4,13 +4,35 @@

use Evilnet\Dotpay\DotpayApi\Contracts\IRequest;

/**
* Class Client
* @package Evilnet\Dotpay\DotpayApi
*/
class Client
{
/**
* @var
*/
private $username;
/**
* @var
*/
private $password;
/**
* @var
*/
private $base_url;
/**
* @var \GuzzleHttp\Client
*/
private $client;

/**
* Client constructor.
* @param $username
* @param $password
* @param $base_url
*/
public function __construct($username, $password, $base_url)
{
$this->username = $username;
Expand All @@ -19,6 +41,11 @@ public function __construct($username, $password, $base_url)
$this->client = new \GuzzleHttp\Client();
}

/**
* @param IRequest $request
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function makeRequest(IRequest $request)
{
$options = [
Expand Down
15 changes: 15 additions & 0 deletions src/DotpayApi/Contracts/IRequest.php
Expand Up @@ -2,9 +2,24 @@

namespace Evilnet\Dotpay\DotpayApi\Contracts;

/**
* Interface IRequest
* @package Evilnet\Dotpay\DotpayApi\Contracts
*/
interface IRequest
{
/**
* @return mixed
*/
public function toArray();

/**
* @return mixed
*/
public function method();

/**
* @return mixed
*/
public function path();
}
48 changes: 46 additions & 2 deletions src/DotpayApi/DotpayApi.php
Expand Up @@ -3,14 +3,35 @@
namespace Evilnet\Dotpay\DotpayApi;

use Evilnet\Dotpay\DotpayApi\Requests\CreatePaymentLink;
use Evilnet\Dotpay\DotpayApi\Requests\CreateRefund;

/**
* Class DotpayApi
* @package Evilnet\Dotpay\DotpayApi
*/
class DotpayApi
{
/**
* @var
*/
private $config;
/**
* @var Client
*/
private $client;
/**
* @var Validator
*/
private $validator;
/**
* @var UrlCreator
*/
private $url_creator;

/**
* DotpayApi constructor.
* @param $config
*/
public function __construct($config)
{
$this->config = $config;
Expand All @@ -19,11 +40,31 @@ public function __construct($config)
$this->url_creator = new UrlCreator($this->config['pin']);
}

/**
* @param $payment
* @return mixed|string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function createPayment($payment)
{
return $this->getPaymentUrl($this->client->makeRequest(new CreatePaymentLink($this->config['shop_id'], $payment)));
}

/**
* @param $payment
* @param $operation_number
* @return mixed
* @throws \Evilnet\Dotpay\Exceptions\RequestIntegrityException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function refundPayment($operation_number, $payment){
return $this->client->makeRequest(new CreateRefund($operation_number, $payment));
}

/**
* @param $payment
* @return mixed|string
*/
public function getPaymentUrl($payment)
{
switch ($this->config['api_version']) {
Expand All @@ -37,9 +78,12 @@ public function getPaymentUrl($payment)
}
}

/**
* @param $data
* @return bool
*/
public function verifyCallback($data)
{
return $this->validator->verify($data);
}

}
}
16 changes: 8 additions & 8 deletions src/DotpayApi/Requests/AbstractRequest.php
Expand Up @@ -2,17 +2,17 @@

namespace Evilnet\Dotpay\DotpayApi\Requests;

/**
* Class AbstractRequest
* @package Evilnet\Dotpay\DotpayApi\Requests
*/
class AbstractRequest
{
protected $shop_id;

public function __construct($shop_id)
{
$this->shop_id = $shop_id;
}

/**
* @return array
*/
public function toArray()
{
return get_object_vars($this);
}
}
}
62 changes: 61 additions & 1 deletion src/DotpayApi/Requests/CreatePaymentLink.php
Expand Up @@ -4,36 +4,96 @@

use Evilnet\Dotpay\DotpayApi\Contracts\IRequest;

/**
* Class CreatePaymentLink
* @package Evilnet\Dotpay\DotpayApi\Requests
*/
class CreatePaymentLink extends AbstractRequest implements IRequest
{
/**
* @var
*/
protected $amount;
/**
* @var
*/
protected $currency;
/**
* @var
*/
protected $description;
/**
* @var
*/
protected $control;
// protected $language;
/**
* @var int
*/
protected $onlinetransfer = 1;
/**
* @var int
*/
protected $ch_lock = 1;
/**
* @var int
*/
protected $redirection_type = 0;
/**
* @var string
*/
protected $buttontext = 'Return';
/**
* @var
*/
protected $url;
/**
* @var
*/
protected $urlc;
/**
* @var
*/
protected $expiration_datetime;
/**
* @var array
*/
protected $payer = [];
/**
* @var array
*/
protected $recipient = [];

/**
* @var
*/
protected $shop_id;

/**
* CreatePaymentLink constructor.
* @param $shop_id
* @param $data
*/
public function __construct($shop_id, $data)
{
parent::__construct($shop_id);
$this->shop_id = $shop_id;

foreach ($data as $key => $value) {
$this->$key = $value;
}
}

/**
* @return string
*/
public function method()
{
return 'POST';
}

/**
* @return string
*/
public function path()
{
return 'api/v1/accounts/'.$this->shop_id.'/payment_links/';
Expand Down
71 changes: 71 additions & 0 deletions src/DotpayApi/Requests/CreateRefund.php
@@ -0,0 +1,71 @@
<?php

namespace Evilnet\Dotpay\DotpayApi\Requests;

use Evilnet\Dotpay\DotpayApi\Contracts\IRequest;
use Evilnet\Dotpay\Exceptions\RequestIntegrityException;

/**
* Class CreateRefund
* @package Evilnet\Dotpay\DotpayApi\Requests
*/
class CreateRefund extends AbstractRequest implements IRequest
{
/**
* @var
*/
protected $amount;

/**
* @var
*/
protected $description;

/**
* @var
*/
protected $control;

/**
* @var
*/
protected $operation_number;

/**
* CreateRefund constructor.
* @param $operation_number
* @param $data
* @throws RequestIntegrityException
*/
public function __construct($operation_number, $data)
{
if(!$operation_number){
throw new RequestIntegrityException("Operation number is not set");
}

$this->operation_number = $operation_number;

foreach ($data as $key => $value) {
if(!$value || is_numeric($value) && $value <= 0){
throw new RequestIntegrityException();
}
$this->$key = $value;
}
}

/**
* @return string
*/
public function method()
{
return 'POST';
}

/**
* @return string
*/
public function path()
{
return 'api/v1/payments/'.$this->operation_number.'/refund/';
}
}

0 comments on commit 7e0abcb

Please sign in to comment.