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: 2 additions & 2 deletions docs/Model/Request/Antifraud.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Without these fields the API responds with `422 Unprocessable Entity: Error vali
### Properties

- `$useragent` — buyer's `User-Agent` HTTP header (string, required for `directChannel`)
- `$steamid` — buyer's STEAMID64 (integer, optional)
- `$steamid` — buyer's STEAMID64 (string of digits or integer, optional; always sent as a string)
- `$mcusername` — Minecraft nickname (string, optional)
- `$mcid` — Minecraft player UUID (string, optional)

Expand All @@ -24,7 +24,7 @@ Without these fields the API responds with `422 Unprocessable Entity: Error vali
```php
public function __construct(
?string $useragent = null,
?int $steamid = null,
int|string|null $steamid = null,
?string $mcusername = null,
?string $mcid = null
)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
new Referer('https://example.com'),
new Antifraud(
$_SERVER['HTTP_USER_AGENT'] ?? '', // useragent — required when using DirectChannel
76561198000000000, // steamid — optional
'76561198000000000', // steamid — optional
),
);
$payment = $paymentApi->paymentTransactionCreate($serviceId, $createPayment);
Expand Down
3 changes: 3 additions & 0 deletions src/HttpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function create(): ClientInterface
[
'base_uri' => Configuration::API_BASE_URI,
RequestOptions::HEADERS => $this->configuration->getAuthHeader(),
// Without limits a stalled connection blocks the shop's checkout indefinitely.
RequestOptions::CONNECT_TIMEOUT => 10,
RequestOptions::TIMEOUT => 30,
],
);
}
Expand Down
10 changes: 7 additions & 3 deletions src/Model/Request/Antifraud.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
class Antifraud implements RequestInterface
{
private ?string $useragent;
private ?int $steamid;
private ?string $steamid;
private ?string $mcusername;
private ?string $mcid;

/**
* @param int|string|null $steamid STEAMID64. The API requires it as a string of digits,
* so integers are converted.
*/
public function __construct(
?string $useragent = null,
?int $steamid = null,
$steamid = null,
?string $mcusername = null,
?string $mcid = null
) {
$this->useragent = $useragent;
$this->steamid = $steamid;
$this->steamid = null === $steamid ? null : (string) $steamid;
$this->mcusername = $mcusername;
$this->mcid = $mcid;
}
Expand Down
13 changes: 8 additions & 5 deletions src/Model/Response/PaymentTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class PaymentTransaction
public string $id;
public string $status;
public PaymentTransactionAmount $amount;
public string $channel;
/**
* Null until the payer picks a payment channel.
*/
public ?string $channel;
public ?string $control;
public ?string $description;
public PaymentTransactionRedirect $redirects;
Expand All @@ -29,7 +32,7 @@ private function __construct(
string $id,
string $status,
PaymentTransactionAmount $amount,
string $channel,
?string $channel,
?string $control,
?string $description,
PaymentTransactionRedirect $redirects,
Expand Down Expand Up @@ -71,9 +74,9 @@ public static function createFromResponse(array $data): self
$data['id'],
$data['status'],
PaymentTransactionAmount::createFromResponse($data['amount']),
$data['channel'],
$data['control'],
$data['description'],
$data['channel'] ?? null,
$data['control'] ?? null,
$data['description'] ?? null,
PaymentTransactionRedirect::createFromResponse($data['redirect']),
PaymentTransactionCustomer::createFromResponse($data['customer']),
empty($data['billing']) ? null : PaymentTransactionAddress::createFromResponse($data['billing']),
Expand Down
23 changes: 23 additions & 0 deletions tests/HttpClientFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Simpay\Test;

use PHPUnit\Framework\TestCase;
use Simpay\Configuration;
use Simpay\HttpClientFactory;

final class HttpClientFactoryTest extends TestCase
{
/**
* @test
*/
public function should_limit_how_long_a_request_can_take(): void
{
$client = (new HttpClientFactory(new Configuration('key', 'password')))->create();

$this->assertSame(10, $client->getConfig('connect_timeout'));
$this->assertSame(30, $client->getConfig('timeout'));
}
}
39 changes: 37 additions & 2 deletions tests/PaymentApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Simpay\Test;

use Simpay\Model\Request\Antifraud;
use Simpay\Model\Request\PaymentTransactionId;
use Simpay\Model\Request\ServiceId;
use Simpay\Model\Response\Pagination;
Expand Down Expand Up @@ -398,15 +399,49 @@ private function mockCreatePaymentTransactionResponse(string $serviceId): void
));
}

private function mockSinglePaymentTransactionResponse(string $serviceId, string $transactionId): void
/**
* @test
*/
public function should_return_pending_payment_transaction_without_channel(): void
{
//GIVEN the payer has not chosen a channel yet, so the API returns channel null
$serviceId = 'd151e4f9';
$transactionId = '5a9d5a7e-4c1d-4f5c-8a8d-ebc4a5d6e0e1';
$this->mockSinglePaymentTransactionResponse($serviceId, $transactionId, [
'status' => 'transaction_new',
'channel' => null,
'paid_at' => null,
]);
$api = new PaymentApi($this->factory);

//WHEN
$response = $api->paymentGetTransaction(new ServiceId($serviceId), new PaymentTransactionId($transactionId));

//THEN
$this->assertNull($response->channel);
$this->assertNull($response->paidAt);
}

/**
* @test
*/
public function should_send_steamid_as_string(): void
{
// The API validates antifraud.steamid as a string; an integer would be rejected with 422.
$this->assertSame('76561198000000000', (new Antifraud('UA', 76561198000000000))->toArray()['antifraud']['steamid']);
$this->assertSame('76561198000000000', (new Antifraud('UA', '76561198000000000'))->toArray()['antifraud']['steamid']);
$this->assertNull((new Antifraud('UA'))->toArray()['antifraud']['steamid']);
}

private function mockSinglePaymentTransactionResponse(string $serviceId, string $transactionId, array $override = []): void
{
$this->client->expects($this->once())
->method('request')
->with('GET', \sprintf('payment/%s/transactions/%s', $serviceId, $transactionId))
->willReturn($this->mockResponse(
[
'status' => 'success',
'data' => [
'data' => $override + [
'id' => '5a9d5a7e-4c1d-4f5c-8a8d-ebc4a5d6e0e1',
'status' => 'transaction_paid',
'amount' => [
Expand Down
Loading