65 lines
2.7 KiB
PHP
65 lines
2.7 KiB
PHP
<?php
|
|
|
|
use App\Services\Stripe\HttpStripeClient;
|
|
use Illuminate\Http\Client\RequestException;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
beforeEach(function () {
|
|
config()->set('admin_access.secrets_key', 'base64:'.base64_encode(random_bytes(32)));
|
|
withStripeSecret();
|
|
Http::preventStrayRequests();
|
|
});
|
|
|
|
it('lists only the invoices the customer still owes', function () {
|
|
Http::fake(['api.stripe.com/v1/invoices*' => Http::response(['data' => [
|
|
['id' => 'in_1', 'number' => 'R-1', 'amount_due' => 21480, 'currency' => 'eur', 'created' => 1750000000],
|
|
]])]);
|
|
|
|
$open = app(HttpStripeClient::class)->openInvoices('cus_42');
|
|
|
|
expect($open)->toHaveCount(1)
|
|
->and($open[0]['amount_due_cents'])->toBe(21480)
|
|
->and($open[0]['currency'])->toBe('EUR');
|
|
|
|
// `open` ist Stripes Status für finalisiert und unbezahlt. Ohne den Filter
|
|
// käme die ganze Rechnungshistorie zurück, und der Kunde bekäme einen
|
|
// Bezahlknopf neben Rechnungen, die er längst beglichen hat.
|
|
Http::assertSent(fn ($request) => $request['customer'] === 'cus_42'
|
|
&& $request['status'] === 'open');
|
|
});
|
|
|
|
it('reports a refused charge instead of throwing', function () {
|
|
// Der häufigste Fall überhaupt: die Karte lehnt ab. Das ist kein
|
|
// Serverfehler, das ist die Antwort — und die Seite muss sie zeigen
|
|
// können, statt in eine 500 zu laufen.
|
|
Http::fake(['api.stripe.com/v1/invoices/in_1/pay' => Http::response([
|
|
'error' => ['code' => 'card_declined', 'message' => 'Your card was declined.'],
|
|
], 402)]);
|
|
|
|
$result = app(HttpStripeClient::class)->payInvoice('in_1');
|
|
|
|
expect($result['paid'])->toBeFalse()
|
|
->and($result['failure'])->toContain('declined');
|
|
});
|
|
|
|
it('still throws on an answer that says nothing about the card', function () {
|
|
// 401 oder 500 sind Aussagen über UNS, nicht über das Zahlungsmittel des
|
|
// Kunden. Sie als „Ihre Karte wurde abgelehnt" auszugeben wäre eine
|
|
// Behauptung über sein Konto, hergeleitet aus unserem Fehler.
|
|
Http::fake(['api.stripe.com/v1/invoices/in_1/pay' => Http::response([], 500)]);
|
|
|
|
// Konkrete Klasse, nicht `Throwable::class`: Pest behandelt eine
|
|
// Schnittstelle als zu enthaltende MELDUNG, nicht als Typ — der Test wäre
|
|
// sonst grün, ohne irgendetwas zu prüfen.
|
|
expect(fn () => app(HttpStripeClient::class)->payInvoice('in_1'))
|
|
->toThrow(RequestException::class);
|
|
});
|
|
|
|
it('reports a successful charge', function () {
|
|
Http::fake(['api.stripe.com/v1/invoices/in_1/pay' => Http::response([
|
|
'id' => 'in_1', 'status' => 'paid', 'paid' => true,
|
|
])]);
|
|
|
|
expect(app(HttpStripeClient::class)->payInvoice('in_1')['paid'])->toBeTrue();
|
|
});
|