diff --git a/src/Driver.php b/src/Driver.php index 0e4f6d9..4bd27bf 100644 --- a/src/Driver.php +++ b/src/Driver.php @@ -40,7 +40,9 @@ class Driver extends BaseDriver public function start(): Response { - $this->init(); + $initialized = $this->init(); + + $this->initialized($initialized); $request = GetQR::make($this->model); @@ -67,4 +69,11 @@ protected function init(): Response return $this->request($request, Responses\Init::class); } + + protected function initialized(Response $initialized): void + { + $external_id = $initialized->getExternalId(); + + $this->payment->cashier()->updateOrCreate(compact('external_id')); + } } diff --git a/src/Requests/Cancel.php b/src/Requests/Cancel.php index b85af19..456b2a5 100644 --- a/src/Requests/Cancel.php +++ b/src/Requests/Cancel.php @@ -29,8 +29,6 @@ public function getRawBody(): array 'PaymentId' => $this->model->getExternalId(), 'Amount' => $this->model->getSum(), - - 'Currency' => $this->model->getCurrency(), ]; } } diff --git a/tests/DriverTest.php b/tests/DriverTest.php index 0646d6c..4fb1bfa 100644 --- a/tests/DriverTest.php +++ b/tests/DriverTest.php @@ -21,6 +21,7 @@ use Helldar\CashierDriver\Tinkoff\QrCode\Driver as QR; use Helldar\Contracts\Cashier\Driver as DriverContract; use Helldar\Contracts\Cashier\Http\Response as ResponseContract; +use Helldar\Support\Facades\Http\Url; use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\database\seeders\DatabaseSeeder; use Tests\Fixtures\Models\RequestPayment; @@ -37,22 +38,47 @@ public function testStart() $this->assertInstanceOf(Response::class, $response); $this->assertInstanceOf(ResponseContract::class, $response); + + $this->assertIsString($response->getExternalId()); + $this->assertMatchesRegularExpression('/^(\d+)$/', $response->getExternalId()); + + $this->assertNull($response->getStatus()); + + $this->assertTrue(Url::is($response->getUrl())); } public function testCheck() { + $this->driver()->start(); + $response = $this->driver()->check(); $this->assertInstanceOf(Response::class, $response); $this->assertInstanceOf(ResponseContract::class, $response); + + $this->assertIsString($response->getExternalId()); + $this->assertMatchesRegularExpression('/^(\d+)$/', $response->getExternalId()); + + $this->assertSame('FORM_SHOWED', $response->getStatus()); + + $this->assertSame([ + 'status' => 'FORM_SHOWED', + ], $response->toArray()); } public function testRefund() { + $this->driver()->start(); + $response = $this->driver()->refund(); $this->assertInstanceOf(Response::class, $response); $this->assertInstanceOf(ResponseContract::class, $response); + + $this->assertIsString($response->getExternalId()); + $this->assertMatchesRegularExpression('/^(\d+)$/', $response->getExternalId()); + + $this->assertSame('CANCELED', $response->getStatus()); } protected function setUp(): void @@ -73,7 +99,7 @@ protected function driver(): DriverContract protected function payment(): RequestPayment { - return RequestPayment::findOrFail(self::PAYMENT_ID); + return RequestPayment::firstOrFail(); } protected function runSeeders() diff --git a/tests/Fixtures/Models/RequestPayment.php b/tests/Fixtures/Models/RequestPayment.php index c4b540e..9428de7 100644 --- a/tests/Fixtures/Models/RequestPayment.php +++ b/tests/Fixtures/Models/RequestPayment.php @@ -19,7 +19,7 @@ class RequestPayment extends Model protected $table = 'payments'; - protected $fillable = ['id', 'type_id', 'status_id', 'sum', 'currency', 'created_at']; + protected $fillable = ['id', 'type_id', 'status_id', 'sum', 'currency']; protected $casts = [ 'id' => 'integer', diff --git a/tests/Requests/CancelTest.php b/tests/Requests/CancelTest.php index ece2516..3a3c438 100644 --- a/tests/Requests/CancelTest.php +++ b/tests/Requests/CancelTest.php @@ -76,11 +76,10 @@ public function testBody() $this->assertSame([ 'PaymentId' => self::PAYMENT_EXTERNAL_ID, 'Amount' => self::PAYMENT_SUM_FORMATTED, - 'Currency' => self::CURRENCY_FORMATTED, 'TerminalKey' => $this->getTerminalKey(), - 'Token' => '668f52d9fb6f6ff75b4a319b7bc34552cf39c82cc0cf2e7a2146f54b8977cb01', + 'Token' => '8473d8d4cafb9ec63071e9050c05e0dd1178fc8e1f3c765ffef7ec7fb5fcb758', ], $request->body()); } @@ -93,7 +92,6 @@ public function testGetRawBody() $this->assertSame([ 'PaymentId' => self::PAYMENT_EXTERNAL_ID, 'Amount' => self::PAYMENT_SUM_FORMATTED, - 'Currency' => self::CURRENCY_FORMATTED, ], $request->getRawBody()); } } diff --git a/tests/database/seeders/PaymentSeeder.php b/tests/database/seeders/PaymentSeeder.php index 3fe49ac..fd28e30 100644 --- a/tests/database/seeders/PaymentSeeder.php +++ b/tests/database/seeders/PaymentSeeder.php @@ -11,12 +11,13 @@ class PaymentSeeder extends Seeder public function run() { RequestPayment::create([ - 'id' => TestCase::PAYMENT_ID, - 'type_id' => TestCase::MODEL_TYPE_ID, - 'status_id' => TestCase::MODEL_STATUS_ID, - 'sum' => TestCase::PAYMENT_SUM, - 'currency' => TestCase::CURRENCY, - 'created_at' => TestCase::PAYMENT_DATE, + 'id' => rand(1, 99999999), + + 'type_id' => TestCase::MODEL_TYPE_ID, + 'status_id' => TestCase::MODEL_STATUS_ID, + + 'sum' => TestCase::PAYMENT_SUM, + 'currency' => TestCase::CURRENCY, ]); } }