Skip to content

Commit

Permalink
Merge pull request #16 from yabhq/fix/checkout-deletion
Browse files Browse the repository at this point in the history
Automatically delete cart after payment, better checkout 404 response
  • Loading branch information
jimhlad authored Feb 10, 2021
2 parents b4bc638 + e6e9e75 commit 55406d1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Yab\ShoppingCart\Payments\FailedPaymentProvider;
use Yab\ShoppingCart\Payments\StripePaymentProvider;
use Yab\ShoppingCart\Exceptions\PaymentFailedException;
use Yab\ShoppingCart\Exceptions\CheckoutNotFoundException;
use Yab\ShoppingCart\Exceptions\PurchaserInvalidException;
use Yab\ShoppingCart\Exceptions\ItemNotPurchaseableException;
use Yab\ShoppingCart\Exceptions\PaymentProviderInvalidException;
Expand Down Expand Up @@ -51,7 +52,13 @@ public function __construct(protected Cart $cart)
*/
public static function findById(string $checkoutId) : Checkout
{
return new Checkout(Cart::findOrFail($checkoutId));
$checkout = Cart::find($checkoutId);

if (!$checkout) {
throw new CheckoutNotFoundException;
}

return new Checkout($checkout);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/Exceptions/CheckoutNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Yab\ShoppingCart\Exceptions;

use Exception;
use Illuminate\Http\Response;

class CheckoutNotFoundException extends Exception
{
/**
* Report or log an exception.
*
* @return void
*/
public function report()
{
//
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function render($request)
{
return response()->json([
'message' => 'The specified checkout does not exist',
], Response::HTTP_NOT_FOUND);
}
}
1 change: 1 addition & 0 deletions src/Http/Controllers/Checkout/CheckoutStripeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function store(CheckoutStripeRequest $request, string $checkoutId)
$checkout = Checkout::findById($checkoutId);

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

return new CheckoutResource($checkout);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/Api/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public function an_existing_checkout_can_be_retrieved_via_the_api()
]);
}

/** @test */
public function a_non_existent_checkout_returns_a_404_not_found_response()
{
$response = $this->get(route('checkout.show', [ 'invalid-uuid' ]));

$response->assertNotFound();

$response->assertJson([
'message' => 'The specified checkout does not exist',
]);
}

/** @test */
public function a_checkout_shipping_address_can_be_updated_via_the_api()
{
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Yab\ShoppingCart\Tests\Models\NonPurchaser;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Yab\ShoppingCart\Tests\Models\NonPurchaseable;
use Yab\ShoppingCart\Payments\LocalPaymentProvider;
use Yab\ShoppingCart\Payments\StripePaymentProvider;
use Yab\ShoppingCart\Exceptions\PurchaserInvalidException;
use Yab\ShoppingCart\Exceptions\ItemNotPurchaseableException;
Expand Down

0 comments on commit 55406d1

Please sign in to comment.