From a9f1b0a7bbb383fe74cdbd62399fa09b67f71e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Wi=C4=99cek?= Date: Sun, 22 Nov 2026 17:59:23 +0100 Subject: [PATCH] Use the direct billing field names the API uses The direct billing module did not match simpay-api: - Creating a transaction sent amount_type and phone_number. The API requires amountType and reads phoneNumber, so every request with an amount type was rejected with 422 and the phone number was dropped. The response was read from transaction_id and redirect_url, but the API returns transactionId and redirectUrl. - The phone number list filter was sent as filter[phone_number]; the API allows filter[phoneNumber] and answers 400 to unknown filters. - Transactions were read from value_netto and phone_number; the API sends value_net and phoneNumber. operator, updated_at and notify.count can be null for new transactions and now map to null. The test fixtures used the same wrong names, so the suite passed. They now follow the API resources, plus a case for a new transaction with empty fields. --- src/Model/Request/AmountType.php | 2 +- src/Model/Request/PhoneNumber.php | 2 +- .../Response/DirectBillingTransaction.php | 14 ++-- .../DirectBillingTransactionCreate.php | 2 +- .../Response/DirectBillingTransactionList.php | 14 ++-- .../DirectBillingTransactionNotify.php | 6 +- tests/DirectBillingTransactionApiTest.php | 70 ++++++++++++++++--- 7 files changed, 79 insertions(+), 31 deletions(-) diff --git a/src/Model/Request/AmountType.php b/src/Model/Request/AmountType.php index 2690caa..040d0aa 100644 --- a/src/Model/Request/AmountType.php +++ b/src/Model/Request/AmountType.php @@ -37,7 +37,7 @@ public static function createGross(): self public function toArray(): array { return [ - 'amount_type' => $this->value, + 'amountType' => $this->value, ]; } } diff --git a/src/Model/Request/PhoneNumber.php b/src/Model/Request/PhoneNumber.php index 1a1ffb2..9085bc8 100644 --- a/src/Model/Request/PhoneNumber.php +++ b/src/Model/Request/PhoneNumber.php @@ -20,7 +20,7 @@ public function __construct(string $value) public function toArray(): array { return [ - 'phone_number' => $this->value, + 'phoneNumber' => $this->value, ]; } } diff --git a/src/Model/Response/DirectBillingTransaction.php b/src/Model/Response/DirectBillingTransaction.php index d163b2c..a7f4b7e 100644 --- a/src/Model/Response/DirectBillingTransaction.php +++ b/src/Model/Response/DirectBillingTransaction.php @@ -15,7 +15,7 @@ class DirectBillingTransaction public ?Operator $operator; public DirectBillingTransactionNotify $notify; public \DateTimeImmutable $createdAt; - public \DateTimeImmutable $updatedAt; + public ?\DateTimeImmutable $updatedAt; private function __construct( string $id, @@ -27,7 +27,7 @@ private function __construct( ?Operator $operator, DirectBillingTransactionNotify $notify, \DateTimeImmutable $createdAt, - \DateTimeImmutable $updatedAt + ?\DateTimeImmutable $updatedAt ) { $this->id = $id; $this->status = $status; @@ -46,14 +46,14 @@ public static function createFromResponse(array $data): self return new self( $data['id'], $data['status'], - $data['phone_number'], - $data['control'], + $data['phoneNumber'] ?? null, + $data['control'] ?? null, $data['value'], - $data['value_netto'], - new Operator($data['operator']), + $data['value_net'], + null !== ($data['operator'] ?? null) ? new Operator($data['operator']) : null, DirectBillingTransactionNotify::createFromResponse($data['notify']), new \DateTimeImmutable($data['created_at']), - new \DateTimeImmutable($data['updated_at']), + null !== ($data['updated_at'] ?? null) ? new \DateTimeImmutable($data['updated_at']) : null, ); } } diff --git a/src/Model/Response/DirectBillingTransactionCreate.php b/src/Model/Response/DirectBillingTransactionCreate.php index 55717b3..39f042d 100644 --- a/src/Model/Response/DirectBillingTransactionCreate.php +++ b/src/Model/Response/DirectBillingTransactionCreate.php @@ -17,6 +17,6 @@ private function __construct(string $transactionId, string $redirectUrl) public static function createFromResponse(array $data): self { - return new self($data['transaction_id'], $data['redirect_url'],); + return new self($data['transactionId'], $data['redirectUrl'],); } } diff --git a/src/Model/Response/DirectBillingTransactionList.php b/src/Model/Response/DirectBillingTransactionList.php index a5371e8..3cfa271 100644 --- a/src/Model/Response/DirectBillingTransactionList.php +++ b/src/Model/Response/DirectBillingTransactionList.php @@ -10,18 +10,18 @@ class DirectBillingTransactionList public string $status; public float $value; public float $valueNetto; - public string $operator; + public ?string $operator; public \DateTimeImmutable $createdAt; - public \DateTimeImmutable $updatedAt; + public ?\DateTimeImmutable $updatedAt; private function __construct( string $id, string $status, float $value, float $valueNetto, - string $operator, + ?string $operator, \DateTimeImmutable $createdAt, - \DateTimeImmutable $updatedAt + ?\DateTimeImmutable $updatedAt ) { $this->id = $id; $this->status = $status; @@ -38,10 +38,10 @@ public static function createFromResponse(array $data): self $data['id'], $data['status'], $data['value'], - $data['value_netto'], - $data['operator'], + $data['value_net'], + $data['operator'] ?? null, new \DateTimeImmutable($data['created_at']), - new \DateTimeImmutable($data['updated_at']), + null !== ($data['updated_at'] ?? null) ? new \DateTimeImmutable($data['updated_at']) : null, ); } } diff --git a/src/Model/Response/DirectBillingTransactionNotify.php b/src/Model/Response/DirectBillingTransactionNotify.php index 3901329..73d7edb 100644 --- a/src/Model/Response/DirectBillingTransactionNotify.php +++ b/src/Model/Response/DirectBillingTransactionNotify.php @@ -8,9 +8,9 @@ class DirectBillingTransactionNotify { public bool $isSend; public \DateTimeImmutable $lastSendAt; - public int $count; + public ?int $count; - private function __construct(bool $isSend, \DateTimeImmutable $lastSendAt, int $count) + private function __construct(bool $isSend, \DateTimeImmutable $lastSendAt, ?int $count) { $this->isSend = $isSend; $this->lastSendAt = $lastSendAt; @@ -19,6 +19,6 @@ private function __construct(bool $isSend, \DateTimeImmutable $lastSendAt, int $ public static function createFromResponse(array $data): self { - return new self($data['is_send'], new \DateTimeImmutable($data['last_send_at']), $data['count'],); + return new self($data['is_send'], new \DateTimeImmutable($data['last_send_at']), $data['count'] ?? null,); } } diff --git a/tests/DirectBillingTransactionApiTest.php b/tests/DirectBillingTransactionApiTest.php index e3a2c84..c1da9ff 100644 --- a/tests/DirectBillingTransactionApiTest.php +++ b/tests/DirectBillingTransactionApiTest.php @@ -164,7 +164,7 @@ private function mockDirectBillingTransactionsApiResponse(string $serviceId): vo 'id' => 'dc261d4f-31ef-4728-bfd6-97bbe2a5ef0a', 'status' => 'transaction_db_payed', 'value' => 0.34, - 'value_netto' => 0.24, + 'value_net' => 0.24, 'operator' => 'test_operator', 'created_at' => '2023-02-16T14:52:11+01:00', 'updated_at' => '2023-02-16T14:52:11+01:00', @@ -196,7 +196,7 @@ private function mockDirectBillingTransactionsWithFiltersApiResponse(string $ser 'query' => [ 'filter' => [ 'status' => 'transaction_db_new', - 'phone_number' => '48123456789', + 'phoneNumber' => '48123456789', 'control' => 'test_control', ], ], @@ -210,7 +210,7 @@ private function mockDirectBillingTransactionsWithFiltersApiResponse(string $ser 'id' => 'dc261d4f-31ef-4728-bfd6-97bbe2a5ef0a', 'status' => 'transaction_db_new', 'value' => 0.34, - 'value_netto' => 0.24, + 'value_net' => 0.24, 'operator' => 'test_operator', 'created_at' => '2023-02-16T14:52:11+01:00', 'updated_at' => '2023-02-16T14:52:11+01:00', @@ -248,13 +248,61 @@ private function mockDirectBillingTransactionNecessary(string $serviceId): void [ 'success' => true, 'data' => [ - 'transaction_id' => '1d87a1b3-18f8-4146-bcb1-c0c9f293b04f', - 'redirect_url' => 'https://db.simpay.pl/1d87a1b3-18f8-4146-bcb1-c0c9f293b04f', + 'transactionId' => '1d87a1b3-18f8-4146-bcb1-c0c9f293b04f', + 'redirectUrl' => 'https://db.simpay.pl/1d87a1b3-18f8-4146-bcb1-c0c9f293b04f', ], ], )); } + /** + * @test + */ + public function should_return_new_direct_billing_transaction_with_empty_fields(): void + { + //GIVEN + $serviceId = 'd151e4f9'; + $transactionId = '4c6aa6ba-4518-473c-a30f-1376138e699c'; + $this->client->expects($this->once()) + ->method('request') + ->with('GET', \sprintf('directbilling/%s/transactions/%s', $serviceId, $transactionId),) + ->willReturn($this->mockResponse( + [ + 'success' => true, + 'data' => [ + 'id' => $transactionId, + 'status' => 'transaction_db_new', + 'phoneNumber' => null, + 'control' => null, + 'value' => 0, + 'value_net' => 0, + 'value_created' => 5.34, + 'value_created_type' => 'NET', + 'operator' => null, + 'notify' => [ + 'is_send' => false, + 'last_send_at' => '1970-01-01T01:00:00+01:00', + 'count' => null, + ], + 'score' => null, + 'created_at' => '2023-02-16T14:52:11+01:00', + 'updated_at' => null, + ], + ], + )); + $api = new DirectBillingTransactionApi($this->factory); + + //WHEN + $response = $api->directBillingTransaction(new ServiceId($serviceId), new PaymentTransactionId($transactionId)); + + //THEN + $this->assertSame('transaction_db_new', $response->status); + $this->assertNull($response->phoneNumber); + $this->assertNull($response->operator); + $this->assertNull($response->notify->count); + $this->assertNull($response->updatedAt); + } + private function mockCreateDirectBillingTransaction(string $serviceId): void { $this->client->expects($this->once()) @@ -265,10 +313,10 @@ private function mockCreateDirectBillingTransaction(string $serviceId): void [ 'json' => [ 'amount' => 19.99, - 'amount_type' => 'required', + 'amountType' => 'required', 'description' => 'test description', 'control' => '96125f23d5494bfca845b781b5f1ad03', - 'phone_number' => '48123456789', + 'phoneNumber' => '48123456789', 'returns' => [ 'success' => 'https://www.simpay.pl/success', 'failure' => 'https://www.simpay.pl/failure', @@ -281,8 +329,8 @@ private function mockCreateDirectBillingTransaction(string $serviceId): void [ 'success' => true, 'data' => [ - 'transaction_id' => '1d87a1b3-18f8-4146-bcb1-c0c9f293b04f', - 'redirect_url' => 'https://db.simpay.pl/1d87a1b3-18f8-4146-bcb1-c0c9f293b04f', + 'transactionId' => '1d87a1b3-18f8-4146-bcb1-c0c9f293b04f', + 'redirectUrl' => 'https://db.simpay.pl/1d87a1b3-18f8-4146-bcb1-c0c9f293b04f', ], ], )); @@ -299,10 +347,10 @@ private function mockDirectBillingTransactionResponse(string $serviceId, string 'data' => [ 'id' => $transactionId, 'status' => 'transaction_db_payed', - 'phone_number' => null, + 'phoneNumber' => null, 'control' => '1d87a1b3-18f8-4146-bcb1-c0c9f293b04f', 'value' => 16.67, - 'value_netto' => 10, + 'value_net' => 10, 'operator' => 't-mobile', 'notify' => [ 'is_send' => false,