Skip to content

Commit 55406d1

Browse files
authored
Merge pull request #16 from yabhq/fix/checkout-deletion
Automatically delete cart after payment, better checkout 404 response
2 parents b4bc638 + e6e9e75 commit 55406d1

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

src/Checkout.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Yab\ShoppingCart\Payments\FailedPaymentProvider;
2020
use Yab\ShoppingCart\Payments\StripePaymentProvider;
2121
use Yab\ShoppingCart\Exceptions\PaymentFailedException;
22+
use Yab\ShoppingCart\Exceptions\CheckoutNotFoundException;
2223
use Yab\ShoppingCart\Exceptions\PurchaserInvalidException;
2324
use Yab\ShoppingCart\Exceptions\ItemNotPurchaseableException;
2425
use Yab\ShoppingCart\Exceptions\PaymentProviderInvalidException;
@@ -51,7 +52,13 @@ public function __construct(protected Cart $cart)
5152
*/
5253
public static function findById(string $checkoutId) : Checkout
5354
{
54-
return new Checkout(Cart::findOrFail($checkoutId));
55+
$checkout = Cart::find($checkoutId);
56+
57+
if (!$checkout) {
58+
throw new CheckoutNotFoundException;
59+
}
60+
61+
return new Checkout($checkout);
5562
}
5663

5764
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Yab\ShoppingCart\Exceptions;
4+
5+
use Exception;
6+
use Illuminate\Http\Response;
7+
8+
class CheckoutNotFoundException extends Exception
9+
{
10+
/**
11+
* Report or log an exception.
12+
*
13+
* @return void
14+
*/
15+
public function report()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Render an exception into an HTTP response.
22+
*
23+
* @param \Illuminate\Http\Request $request
24+
* @return \Illuminate\Http\Response
25+
*/
26+
public function render($request)
27+
{
28+
return response()->json([
29+
'message' => 'The specified checkout does not exist',
30+
], Response::HTTP_NOT_FOUND);
31+
}
32+
}

src/Http/Controllers/Checkout/CheckoutStripeController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function store(CheckoutStripeRequest $request, string $checkoutId)
2222
$checkout = Checkout::findById($checkoutId);
2323

2424
$checkout->setPaymentProvider('stripe')->charge([ 'token' => $request->token ]);
25+
$checkout->getCart()->delete();
2526

2627
return new CheckoutResource($checkout);
2728
}

tests/Feature/Api/CheckoutTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ public function an_existing_checkout_can_be_retrieved_via_the_api()
6565
]);
6666
}
6767

68+
/** @test */
69+
public function a_non_existent_checkout_returns_a_404_not_found_response()
70+
{
71+
$response = $this->get(route('checkout.show', [ 'invalid-uuid' ]));
72+
73+
$response->assertNotFound();
74+
75+
$response->assertJson([
76+
'message' => 'The specified checkout does not exist',
77+
]);
78+
}
79+
6880
/** @test */
6981
public function a_checkout_shipping_address_can_be_updated_via_the_api()
7082
{

tests/Feature/CheckoutTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Yab\ShoppingCart\Tests\Models\NonPurchaser;
1717
use Illuminate\Foundation\Testing\RefreshDatabase;
1818
use Yab\ShoppingCart\Tests\Models\NonPurchaseable;
19-
use Yab\ShoppingCart\Payments\LocalPaymentProvider;
2019
use Yab\ShoppingCart\Payments\StripePaymentProvider;
2120
use Yab\ShoppingCart\Exceptions\PurchaserInvalidException;
2221
use Yab\ShoppingCart\Exceptions\ItemNotPurchaseableException;

0 commit comments

Comments
 (0)