Merge branch 'feature/stripe-idempotenz' into develop
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
cc890d9fc7
2 files changed
+32
-3
No files matched your search
@@ -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<string, mixed> $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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user