Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ $result = $cryptopay->risks->score($params);


```php
$result = $cryptopay->transactions->all();
$result = $cryptopay->transactions->all([
'reference_type' => 'Invoice'
]);
```


Expand Down
15 changes: 12 additions & 3 deletions src/Connector/AbstractConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cryptopay\Connector;

use Cryptopay\Config\ConfigInterface;
use Cryptopay\Constants\Methods;
use Exception;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
Expand All @@ -17,6 +18,8 @@ abstract class AbstractConnector implements ConnectorInterface

protected GuzzleClient $client;

protected string $userAgent;

/**
* @param string $method
* @param string $path
Expand All @@ -26,7 +29,12 @@ abstract class AbstractConnector implements ConnectorInterface
*/
public function request(string $method, string $path, array $params = null): object
{
$body = $params ? json_encode($params) : '';
$body = '';
if ($method == Methods::GET) {
$path = $params ? $path . '?' . \http_build_query($params) : $path;
} else {
$body = $params ? json_encode($params) : '';
}

try {
$headers = $this->signRequest($method, $path, $body);
Expand Down Expand Up @@ -75,8 +83,9 @@ public function signRequest(string $method, string $path, ?string $body): array

return [
'Content-Type' => $contentType,
'date' => $date,
'Authorization' => 'HMAC ' . $this->config->getApiKey() . ':' . $signature
'Date' => $date,
'Authorization' => 'HMAC ' . $this->config->getApiKey() . ':' . $signature,
'User-Agent' => $this->userAgent
];
}
}
6 changes: 4 additions & 2 deletions src/Connector/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class Connector extends AbstractConnector
* Connector constructor.
* @param ConfigInterface $config
*/
public function __construct(ConfigInterface $config)
public function __construct(ConfigInterface $config, string $userAgent)
{
$this->config = $config;

$this->userAgent = $userAgent;

$this->client = new GuzzleClient([
'base_uri' => $this->config->getBaseUrl(),
'base_uri' => $this->config->getBaseUrl(),
'timeout' => $this->config->getTimeout()
]);
}
Expand Down
1 change: 1 addition & 0 deletions src/Connector/TestConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TestConnector extends AbstractConnector
public function __construct(ConfigInterface $config, GuzzleClient $client)
{
$this->config = $config;
$this->userAgent = 'Cryptopay-PHP/Test';
$this->client = $client;
}
}
23 changes: 15 additions & 8 deletions src/Cryptopay.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ class Cryptopay
private RateService $rateService;
private RiskService $riskService;

private const VERSION = '2.0.0';

private const USER_AGENT = 'Cryptopay-PHP/' . Cryptopay::VERSION . ' PHP/' . \PHP_VERSION;
private const USER_AGENT_DEPRECATED = Cryptopay::USER_AGENT . ' (deprecated)';

public function __construct(ConfigInterface $config)
{
$connector = new Connector($config);
$connector = new Connector($config, Cryptopay::USER_AGENT);

$this->accounts = new AccountsApi($connector);
$this->channels = new ChannelsApi($connector);
Expand All @@ -71,13 +76,15 @@ public function __construct(ConfigInterface $config)
$this->callbackService = new CallbackService($config->getCallbackSecret());

// Deprecated services
$this->accountService = new AccountsService($connector);
$this->channelService = new ChannelService($connector);
$this->coinWithdrawalService = new CoinWithdrawalService($connector);
$this->invoiceService = new InvoiceService($connector);
$this->rateService = new RateService($connector);
$this->riskService = new RiskService($connector);
$this->transactionService = new TransactionService($connector);
$deprecatedConnector = new Connector($config, Cryptopay::USER_AGENT_DEPRECATED);

$this->accountService = new AccountsService($deprecatedConnector);
$this->channelService = new ChannelService($deprecatedConnector);
$this->coinWithdrawalService = new CoinWithdrawalService($deprecatedConnector);
$this->invoiceService = new InvoiceService($deprecatedConnector);
$this->rateService = new RateService($deprecatedConnector);
$this->riskService = new RiskService($deprecatedConnector);
$this->transactionService = new TransactionService($deprecatedConnector);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/Api/TransactionsApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public function testall()

$cryptopay = new Cryptopay($this->config);

$result = $cryptopay->transactions->all();
$result = $cryptopay->transactions->all([
'reference_type' => 'Invoice'
]);

$this->assertNotNull($result);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(?string $name = null, array $data = [], $dataName =
->withBaseUrl($this->baseUrl)
->withTimeout($this->timeout);

$this->connector = new Connector($this->config);
$this->connector = new Connector($this->config, 'Cryptopay-PHP/Test');

parent::__construct($name, $data, $dataName);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Services/RateServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RateServiceTest extends BaseTest
public function testServiceUnavailableShouldReturnRequestException()
{
$this->config->withBaseUrl('url_not_exists.ccc');
$connector = new Connector($this->config);
$connector = new Connector($this->config, 'Cryptopay-PHP/Test');
$rateService = new RateService($connector);

$this->expectException(RequestException::class);
Expand Down
46 changes: 23 additions & 23 deletions tests/cassettes/invoices/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
Host: business-sandbox.cryptopay.me
Expect: ''
Accept-Encoding: ''
User-Agent: GuzzleHttp/7
Content-Type: application/json
date: 'Tue, 11 Jul 2023 08:57:56 +0000'
Authorization: 'HMAC OtzdZAvAkmw4vAYniZ4ljw:B5I1lECdbL+vZfBAksMr0q2iUt0='
Date: 'Wed, 12 Jul 2023 07:50:21 +0000'
Authorization: 'HMAC OtzdZAvAkmw4vAYniZ4ljw:sDw8eoPMwdf9XsrG/k+fF7IwrXo='
User-Agent: 'Cryptopay-PHP/2.0.0 PHP/7.4.33'
Accept: ''
body: '{"price_amount":"100.0","price_currency":"EUR","pay_currency":"BTC"}'
response:
Expand All @@ -19,11 +19,11 @@
code: '201'
message: Created
headers:
Date: 'Tue, 11 Jul 2023 08:57:57 GMT'
Date: 'Wed, 12 Jul 2023 07:50:22 GMT'
Content-Type: 'application/json; charset=utf-8'
Transfer-Encoding: chunked
Connection: keep-alive
CF-Ray: 7e4fd7857b21b790-AMS
CF-Ray: 7e57b1e62cc9d0cd-AMS
CF-Cache-Status: DYNAMIC
Cache-Control: no-store
Strict-Transport-Security: 'max-age=15552000; includeSubDomains'
Expand All @@ -32,43 +32,43 @@
x-frame-options: DENY
Server: cloudflare
alt-svc: 'h3=":443"; ma=86400'
body: '{"data":{"id":"4e79ee49-f3dd-4941-a6cc-ed0d86087cff","custom_id":null,"customer_id":null,"subscription_id":null,"status":"new","status_context":null,"address":"2MwvfYSivVfn1XEsHJmyvhbrg6qf1CqznZL","network":"bitcoin","uri":"bitcoin:2MwvfYSivVfn1XEsHJmyvhbrg6qf1CqznZL?amount=0.003517","price_amount":"100.0","price_currency":"EUR","pay_amount":"0.003517","pay_currency":"BTC","fee":"1.0","fee_currency":"EUR","paid_amount":"0.0","exchange":{"pair":"BTCEUR","rate":"28439.193","fee":"0.0","fee_currency":"EUR"},"transactions":[],"name":null,"description":null,"metadata":null,"success_redirect_url":null,"unsuccess_redirect_url":null,"hosted_page_url":"https://hosted-business-sandbox.cryptopay.me/invoices/4e79ee49-f3dd-4941-a6cc-ed0d86087cff","created_at":"2023-07-11T08:57:57+00:00","expires_at":"2023-07-11T09:07:57+00:00"}}'
body: '{"data":{"id":"03692121-10b9-40bf-a79f-f9ad1f89f13d","custom_id":null,"customer_id":null,"subscription_id":null,"status":"new","status_context":null,"address":"2NGDeKppHSbBDnshnSQ3BHvXQcxCu9FvLTP","network":"bitcoin","uri":"bitcoin:2NGDeKppHSbBDnshnSQ3BHvXQcxCu9FvLTP?amount=0.00349","price_amount":"100.0","price_currency":"EUR","pay_amount":"0.00349","pay_currency":"BTC","fee":"1.0","fee_currency":"EUR","paid_amount":"0.0","exchange":{"pair":"BTCEUR","rate":"28657.5853","fee":"0.0","fee_currency":"EUR"},"transactions":[],"name":null,"description":null,"metadata":null,"success_redirect_url":null,"unsuccess_redirect_url":null,"hosted_page_url":"https://hosted-business-sandbox.cryptopay.me/invoices/03692121-10b9-40bf-a79f-f9ad1f89f13d","created_at":"2023-07-12T07:50:22+00:00","expires_at":"2023-07-12T08:00:22+00:00"}}'
curl_info:
url: 'https://business-sandbox.cryptopay.me/api/invoices'
content_type: 'application/json; charset=utf-8'
http_code: 201
header_size: 460
request_size: 326
request_size: 344
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 0.579051
namelookup_time: 0.005323
connect_time: 0.144666
pretransfer_time: 0.329312
total_time: 0.638799
namelookup_time: 0.008216
connect_time: 0.094941
pretransfer_time: 0.259097
size_upload: 68.0
size_download: 827.0
speed_download: 1428.0
speed_upload: 117.0
size_download: 826.0
speed_download: 1294.0
speed_upload: 106.0
download_content_length: -1.0
upload_content_length: 68.0
starttransfer_time: 0.578988
starttransfer_time: 0.638648
redirect_time: 0.0
redirect_url: ''
primary_ip: 172.66.43.16
certinfo: { }
primary_port: 443
local_ip: 192.168.208.2
local_port: 60932
local_ip: 192.168.208.3
local_port: 42886
http_version: 2
protocol: 2
ssl_verifyresult: 0
scheme: HTTPS
appconnect_time_us: 329271
connect_time_us: 144666
namelookup_time_us: 5323
pretransfer_time_us: 329312
appconnect_time_us: 259014
connect_time_us: 94941
namelookup_time_us: 8216
pretransfer_time_us: 259097
redirect_time_us: 0
starttransfer_time_us: 578988
total_time_us: 579051
starttransfer_time_us: 638648
total_time_us: 638799
index: 0
48 changes: 24 additions & 24 deletions tests/cassettes/transactions/all.yml

Large diffs are not rendered by default.