diff --git a/app/Domain/Commerce/Stripe/StripeGateway.php b/app/Domain/Commerce/Stripe/StripeGateway.php index af84eb9..3634e30 100644 --- a/app/Domain/Commerce/Stripe/StripeGateway.php +++ b/app/Domain/Commerce/Stripe/StripeGateway.php @@ -66,7 +66,7 @@ public function checkoutFor(Order $order): array } $response = $this->request() - ->withHeaders(['Idempotency-Key' => 'luf-order-'.$order->id.'-'.$attempt]) + ->withHeaders(['Idempotency-Key' => $this->idempotencyKey($order, $attempt, $params)]) ->post('/v1/checkout/sessions', $params); $this->assertOk($response, 'angelegt'); @@ -99,6 +99,20 @@ public function assertSandbox(): void } } + /** + * Stripe bindet einen Schlüssel an die Parameter der ersten Anfrage und lehnt dieselbe Kennung mit anderen + * Parametern ab. Der Schlüssel enthält deshalb einen Fingerabdruck der Parameter und die Minute: Ein + * doppelter Klick bleibt wirkungslos, eine geänderte Anfrage oder ein Neuversuch nach einer Korrektur nicht. + * + * @param array $params + */ + private function idempotencyKey(Order $order, int $attempt, array $params): string + { + $fingerprint = substr(sha1(json_encode([$params, now()->format('YmdHi')], JSON_THROW_ON_ERROR)), 0, 12); + + return 'luf-order-'.$order->id.'-'.$attempt.'-'.$fingerprint; + } + /** Konfigurierter Steuersatz oder null; eine ungültige ID wird abgewiesen, statt geraten zu werden. */ private function taxRate(): ?string { diff --git a/tests/Feature/StripeSandboxTest.php b/tests/Feature/StripeSandboxTest.php index e107742..357042f 100644 --- a/tests/Feature/StripeSandboxTest.php +++ b/tests/Feature/StripeSandboxTest.php @@ -71,7 +71,7 @@ public function test_paying_creates_a_checkout_session_with_the_order_amounts_an return $request->url() === 'https://stripe.test/v1/checkout/sessions' && $request->hasHeader('Authorization', 'Bearer sk_test_nur_fuer_tests') - && $request->hasHeader('Idempotency-Key', 'luf-order-'.$order->id.'-1') + && preg_match('/^luf-order-'.$order->id.'-1-[0-9a-f]{12}$/', (string) ($request->header('Idempotency-Key')[0] ?? '')) === 1 && $body['mode'] === 'payment' && $body['client_reference_id'] === (string) $order->id && $body['metadata']['order_id'] === (string) $order->id @@ -108,6 +108,21 @@ public function test_a_configured_tax_rate_is_sent_with_every_line_and_otherwise }); } + public function test_changed_parameters_never_reuse_an_idempotency_key(): void + { + $order = $this->orderOf($this->tina(), [$this->privateKonflikte()]); + Http::fake(['stripe.test/v1/checkout/sessions' => Http::response(['id' => 'cs_test_k', 'url' => 'https://checkout.stripe.test/k'])]); + + $this->actingAs($this->tina())->post(route('member.order.pay', $order))->assertRedirect(); + $order->payments()->delete(); + config(['services.stripe.tax_rate' => 'txr_Testsatz123']); + $this->actingAs($this->tina())->post(route('member.order.pay', $order))->assertRedirect(); + + $keys = Http::recorded()->map(fn (array $pair): string => (string) $pair[0]->header('Idempotency-Key')[0])->values(); + $this->assertCount(2, $keys); + $this->assertNotSame($keys[0], $keys[1], 'Geänderte Parameter brauchen einen anderen Schlüssel als dieselbe Bestellung zuvor.'); + } + public function test_an_invalid_tax_rate_id_is_refused_before_any_call(): void { $order = $this->orderOf($this->tina(), [$this->privateKonflikte()]); @@ -139,7 +154,7 @@ public function test_an_expired_session_is_replaced_by_a_new_one(): void 'stripe.test/v1/checkout/sessions' => Http::response(['id' => 'cs_test_neu', 'url' => 'https://checkout.stripe.test/neu']), ]); $this->actingAs($this->tina())->post(route('member.order.pay', $order))->assertRedirect('https://checkout.stripe.test/neu'); - Http::assertSent(fn (HttpRequest $request): bool => $request->hasHeader('Idempotency-Key', 'luf-order-'.$order->id.'-2')); + Http::assertSent(fn (HttpRequest $request): bool => preg_match('/^luf-order-'.$order->id.'-2-[0-9a-f]{12}$/', (string) ($request->header('Idempotency-Key')[0] ?? '')) === 1); } public function test_only_the_buyer_pays_and_only_an_open_order(): void