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
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ For more information, please visit [Cryptopay API docs](https://developers.crypt
* [Invoices](#invoicesapi)
* [Rates](#ratesapi)
* [Risks](#risksapi)
* [Subscriptions](#subscriptionsapi)
* [Transactions](#transactionsapi)
* [Callbacks](#callbacks)

Expand Down Expand Up @@ -452,6 +453,61 @@ $params = [
$result = $cryptopay->risks->score($params);
```

## Subscriptions


### List subscriptions


```php
$result = $cryptopay->subscriptions->all();
```

### Cancel a subscription


```php
$subscriptionId = '7dd7da55-2fd6-445e-8c7c-6c2c85d135d7';

$result = $cryptopay->subscriptions->cancel($subscriptionId);
```

### Create a subscription


```php
$startsAt = (new \DateTime())->add(\DateInterval::createFromDateString('7 days'));
$params = [
'name' => 'Subscription name',
'amount' => '100.0',
'currency' => 'EUR',
'period' => 'month',
'period_quantity' => 3,
'payer_email' => 'user@example.com',
'starts_at' => $startsAt->format(\DateTime::ATOM)
];

$result = $cryptopay->subscriptions->create($params);
```

### Retrieve a subscription


```php
$subscriptionId = '64249ede-8969-4d5c-a042-806f9c3e7db3';

$result = $cryptopay->subscriptions->retrieve($subscriptionId);
```

### Retrieve a subscription by custom_id


```php
$customId = 'PAYMENT-123';

$result = $cryptopay->subscriptions->retrieveByCustomId($customId);
```

## Transactions

[Transactions API docs](https://developers.cryptopay.me/guides/transactions)
Expand Down
88 changes: 88 additions & 0 deletions src/Api/SubscriptionsApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

// Auto-generated file
// DO NOT EDIT

namespace Cryptopay\Api;

use Cryptopay\AbstractApi;

class SubscriptionsApi extends AbstractApi
{
/**
* List subscriptions
*
* @param null|array $params
*
* @throws \Cryptopay\Exceptions\RequestException
* @return object
*/
public function all(array $params = null)
{
return $this->request('GET', '/api/subscriptions', $params);
}

/**
* Cancel a subscription
*
* @param string $subscriptionId
* @param null|array $params
*
* @throws \Cryptopay\Exceptions\RequestException
* @return object
*/
public function cancel(string $subscriptionId, array $params = null)
{
$path = '/api/subscriptions/{subscription_id}/cancel';
$path = str_replace('{subscription_id}', rawurlencode($subscriptionId), $path);

return $this->request('POST', $path, $params);
}

/**
* Create a subscription
*
* @param null|array $params
*
* @throws \Cryptopay\Exceptions\RequestException
* @return object
*/
public function create(array $params = null)
{
return $this->request('POST', '/api/subscriptions', $params);
}

/**
* Retrieve a subscription
*
* @param string $subscriptionId
* @param null|array $params
*
* @throws \Cryptopay\Exceptions\RequestException
* @return object
*/
public function retrieve(string $subscriptionId, array $params = null)
{
$path = '/api/subscriptions/{subscription_id}';
$path = str_replace('{subscription_id}', rawurlencode($subscriptionId), $path);

return $this->request('GET', $path, $params);
}

/**
* Retrieve a subscription by custom_id
*
* @param string $customId
* @param null|array $params
*
* @throws \Cryptopay\Exceptions\RequestException
* @return object
*/
public function retrieveByCustomId(string $customId, array $params = null)
{
$path = '/api/subscriptions/custom_id/{custom_id}';
$path = str_replace('{custom_id}', rawurlencode($customId), $path);

return $this->request('GET', $path, $params);
}
}
5 changes: 4 additions & 1 deletion src/Cryptopay.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Cryptopay\Api\InvoicesApi;
use Cryptopay\Api\RatesApi;
use Cryptopay\Api\RisksApi;
use Cryptopay\Api\SubscriptionsApi;
use Cryptopay\Api\TransactionsApi;

class Cryptopay
Expand All @@ -41,6 +42,7 @@ class Cryptopay
public InvoicesApi $invoices;
public RatesApi $rates;
public RisksApi $risks;
public SubscriptionsApi $subscriptions;
public TransactionsApi $transactions;

// Deprecated services
Expand All @@ -53,7 +55,7 @@ class Cryptopay
private RateService $rateService;
private RiskService $riskService;

private const VERSION = '2.0.0';
private const VERSION = '2.1.0';

private const USER_AGENT = 'Cryptopay-PHP/' . Cryptopay::VERSION . ' PHP/' . \PHP_VERSION;
private const USER_AGENT_DEPRECATED = Cryptopay::USER_AGENT . ' (deprecated)';
Expand All @@ -71,6 +73,7 @@ public function __construct(ConfigInterface $config)
$this->invoices = new InvoicesApi($connector);
$this->rates = new RatesApi($connector);
$this->risks = new RisksApi($connector);
$this->subscriptions = new SubscriptionsApi($connector);
$this->transactions = new TransactionsApi($connector);

$this->callbackService = new CallbackService($config->getCallbackSecret());
Expand Down
84 changes: 84 additions & 0 deletions tests/Api/SubscriptionsApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

// Auto-generated file
// DO NOT EDIT

namespace Tests;

use Cryptopay\Cryptopay;
use VCR\VCR;

class SubscriptionsApiTest extends ApiTest
{
public function testall()
{
VCR::insertCassette('subscriptions/all.yml');

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

$result = $cryptopay->subscriptions->all();

$this->assertNotNull($result);
}

public function testcancel()
{
VCR::insertCassette('subscriptions/cancel.yml');

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

$subscriptionId = '7dd7da55-2fd6-445e-8c7c-6c2c85d135d7';

$result = $cryptopay->subscriptions->cancel($subscriptionId);

$this->assertNotNull($result);
}

public function testcreate()
{
VCR::insertCassette('subscriptions/create.yml');

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

$startsAt = (new \DateTime())->add(\DateInterval::createFromDateString('7 days'));
$params = [
'name' => 'Subscription name',
'amount' => '100.0',
'currency' => 'EUR',
'period' => 'month',
'period_quantity' => 3,
'payer_email' => 'user@example.com',
'starts_at' => $startsAt->format(\DateTime::ATOM)
];

$result = $cryptopay->subscriptions->create($params);

$this->assertNotNull($result);
}

public function testretrieve()
{
VCR::insertCassette('subscriptions/retrieve.yml');

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

$subscriptionId = '64249ede-8969-4d5c-a042-806f9c3e7db3';

$result = $cryptopay->subscriptions->retrieve($subscriptionId);

$this->assertNotNull($result);
}

public function testretrieveByCustomId()
{
VCR::insertCassette('subscriptions/retrieveByCustomId.yml');

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

$customId = 'PAYMENT-123';

$result = $cryptopay->subscriptions->retrieveByCustomId($customId);

$this->assertNotNull($result);
}
}
2 changes: 1 addition & 1 deletion tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(?string $name = null, array $data = [], $dataName =
VCR::configure()
->setCassettePath('tests/cassettes')
->enableLibraryHooks(['curl'])
->enableRequestMatchers(['method', 'url', 'query_string', 'body'])
->enableRequestMatchers(['method', 'url', 'query_string'])
->setMode('once');
VCR::turnOn();
}
Expand Down
73 changes: 73 additions & 0 deletions tests/cassettes/subscriptions/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

-
request:
method: GET
url: 'https://business-sandbox.cryptopay.me/api/subscriptions'
headers:
Host: business-sandbox.cryptopay.me
Accept-Encoding: ''
Content-Type: application/json
Date: 'Mon, 17 Jul 2023 09:23:49 +0000'
Authorization: 'HMAC OtzdZAvAkmw4vAYniZ4ljw:5nH69i72dsiGa7SgCFcrRnS3V7Y='
User-Agent: 'Cryptopay-PHP/2.1.0 PHP/7.4.33'
Accept: ''
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Date: 'Mon, 17 Jul 2023 09:23:50 GMT'
Content-Type: 'application/json; charset=utf-8'
Transfer-Encoding: chunked
Connection: keep-alive
CF-Ray: 7e816dae3f560b58-AMS
CF-Cache-Status: DYNAMIC
Cache-Control: no-store
Strict-Transport-Security: 'max-age=15552000; includeSubDomains'
Vary: Accept-Encoding
content-security-policy: 'frame-ancestors ''none'''
x-content-type-options: nosniff
x-frame-options: DENY
Server: cloudflare
alt-svc: 'h3=":443"; ma=86400'
body: '{"data":[{"id":"de37fb63-986b-4f83-bf9f-612734316fdc","status":"cancelled","custom_id":null,"name":"Subscription name","amount":"100.0","currency":"EUR","period":"month","period_quantity":3,"current_period_starts_at":"2023-07-17T08:46:29+00:00","current_period_ends_at":"2023-07-24T08:46:28+00:00","current_period_paid":false,"payer_email":"user@example.com","payer_name":null,"product_name":null,"product_description":null,"success_redirect_url":null,"unsuccess_redirect_url":null,"created_at":"2023-07-17T08:46:29+00:00","cancelled_at":"2023-07-17T08:54:44+00:00"},{"id":"64249ede-8969-4d5c-a042-806f9c3e7db3","status":"active","custom_id":"PAYMENT-123","name":"Subscription name","amount":"100.0","currency":"EUR","period":"month","period_quantity":3,"current_period_starts_at":"2023-07-17T08:44:30+00:00","current_period_ends_at":"2024-01-18T10:58:28+00:00","current_period_paid":false,"payer_email":"user@example.com","payer_name":null,"product_name":null,"product_description":null,"success_redirect_url":null,"unsuccess_redirect_url":null,"created_at":"2023-07-17T08:44:30+00:00","cancelled_at":null}],"meta":{"total":2,"has_more":false}}'
curl_info:
url: 'https://business-sandbox.cryptopay.me/api/subscriptions'
content_type: 'application/json; charset=utf-8'
http_code: 200
header_size: 478
request_size: 260
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 0.767475
namelookup_time: 0.223341
connect_time: 0.425383
pretransfer_time: 0.573351
size_upload: 0.0
size_download: 1145.0
speed_download: 1492.0
speed_upload: 0.0
download_content_length: -1.0
upload_content_length: -1.0
starttransfer_time: 0.766369
redirect_time: 0.0
redirect_url: ''
primary_ip: 172.66.40.240
certinfo: { }
primary_port: 443
local_ip: 192.168.208.4
local_port: 50230
http_version: 2
protocol: 2
ssl_verifyresult: 0
scheme: HTTPS
appconnect_time_us: 573228
connect_time_us: 425383
namelookup_time_us: 223341
pretransfer_time_us: 573351
redirect_time_us: 0
starttransfer_time_us: 766369
total_time_us: 767475
index: 0
Loading