diff --git a/docs/Model/Request/Antifraud.md b/docs/Model/Request/Antifraud.md index 5083935..62f3495 100644 --- a/docs/Model/Request/Antifraud.md +++ b/docs/Model/Request/Antifraud.md @@ -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) @@ -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 ) diff --git a/docs/examples/payment.php b/docs/examples/payment.php index dfcc4c2..94ced70 100644 --- a/docs/examples/payment.php +++ b/docs/examples/payment.php @@ -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); diff --git a/src/HttpClientFactory.php b/src/HttpClientFactory.php index 6bfecfa..f6cbba8 100644 --- a/src/HttpClientFactory.php +++ b/src/HttpClientFactory.php @@ -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, ], ); } diff --git a/src/Model/Request/Antifraud.php b/src/Model/Request/Antifraud.php index c64fc8b..9d9c6b2 100644 --- a/src/Model/Request/Antifraud.php +++ b/src/Model/Request/Antifraud.php @@ -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; } diff --git a/src/Model/Response/PaymentTransaction.php b/src/Model/Response/PaymentTransaction.php index 077872a..cd6302e 100644 --- a/src/Model/Response/PaymentTransaction.php +++ b/src/Model/Response/PaymentTransaction.php @@ -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; @@ -29,7 +32,7 @@ private function __construct( string $id, string $status, PaymentTransactionAmount $amount, - string $channel, + ?string $channel, ?string $control, ?string $description, PaymentTransactionRedirect $redirects, @@ -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']), diff --git a/tests/HttpClientFactoryTest.php b/tests/HttpClientFactoryTest.php new file mode 100644 index 0000000..cc37c03 --- /dev/null +++ b/tests/HttpClientFactoryTest.php @@ -0,0 +1,23 @@ +create(); + + $this->assertSame(10, $client->getConfig('connect_timeout')); + $this->assertSame(30, $client->getConfig('timeout')); + } +} diff --git a/tests/PaymentApiTest.php b/tests/PaymentApiTest.php index 39219f2..2dc1a7d 100644 --- a/tests/PaymentApiTest.php +++ b/tests/PaymentApiTest.php @@ -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; @@ -398,7 +399,41 @@ 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') @@ -406,7 +441,7 @@ private function mockSinglePaymentTransactionResponse(string $serviceId, string ->willReturn($this->mockResponse( [ 'status' => 'success', - 'data' => [ + 'data' => $override + [ 'id' => '5a9d5a7e-4c1d-4f5c-8a8d-ebc4a5d6e0e1', 'status' => 'transaction_paid', 'amount' => [