Skip to content

Commit

Permalink
Merge pull request #17 from yabhq/fix/cart-payment-status-codes
Browse files Browse the repository at this point in the history
Update HTTP response codes for failed payments, readme for cart.js in…
  • Loading branch information
jimhlad authored Feb 17, 2021
2 parents 55406d1 + c13e405 commit 921091c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

A simple yet customizable Laravel shopping cart API. Provides RESTful API endpoints out of the box to help with your next e-commerce build. Designed specifically with single page application (SPA) requirements in mind. Currently supports payment processing with **Stripe**.

Works best with [cart.js](https://github.com/yabhq/cart.js) for front-end integration.

## Table of Contents
[Requirements](#requirements)
[Installation](#installation)
Expand Down
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Yab\ShoppingCart\Payments\StripePaymentProvider;

return [
/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -34,6 +36,7 @@
|
*/
'stripe' => [
'provider' => StripePaymentProvider::class,
'secret_key' => env('STRIPE_SECRET'),
'public_key' => env('STRIPE_KEY'),
],
Expand Down
3 changes: 2 additions & 1 deletion src/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public function setPaymentProvider(string $provider) : Checkout
{
$supported = self::getSupportedPaymentProviders();

$class = $supported[$provider] ?? '';
$class = $supported[$provider] ?? $provider;

if(!class_exists($class) || !(new $class instanceof PaymentProvider)) {
throw new PaymentProviderInvalidException;
Expand Down Expand Up @@ -373,6 +373,7 @@ public function charge(array $chargeable) : void
}
catch(PaymentFailedException $e) {
app(CartLogistics::class)->afterFailedCheckout($this, $e);
throw $e;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/Checkout/CheckoutStripeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Yab\ShoppingCart\Checkout;
use Yab\ShoppingCart\Http\Controllers\Controller;
use Yab\ShoppingCart\Payments\StripePaymentProvider;
use Yab\ShoppingCart\Http\Resources\CheckoutResource;
use Yab\ShoppingCart\Http\Requests\CheckoutStripeRequest;

Expand All @@ -21,7 +22,7 @@ public function store(CheckoutStripeRequest $request, string $checkoutId)
{
$checkout = Checkout::findById($checkoutId);

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

return new CheckoutResource($checkout);
Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/Api/CheckoutStripeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Yab\ShoppingCart\Tests\Feature\Api;

use Illuminate\Http\Response;
use Yab\ShoppingCart\Models\Cart;
use Yab\ShoppingCart\Tests\TestCase;
use Illuminate\Support\Facades\Config;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Yab\ShoppingCart\Payments\LocalPaymentProvider;
use Yab\ShoppingCart\Payments\FailedPaymentProvider;
use Yab\ShoppingCart\Payments\StripePaymentProvider;

class CheckoutStripeTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function a_failed_stripe_checkout_returns_an_error_response()
{
Config::set('stripe.provider', FailedPaymentProvider::class);

$cart = factory(Cart::class)->create();

$response = $this->post(route('checkout.stripe', [ $cart->id ]), [
'token' => 'tok_123456',
]);

$response->assertStatus(Response::HTTP_INTERNAL_SERVER_ERROR);
$response->assertJson([
'message' => 'There was a problem processing the payment',
]);
}

/** @test */
public function a_successful_stripe_checkout_returns_a_successful_response()
{
Config::set('stripe.provider', LocalPaymentProvider::class);

$cart = factory(Cart::class)->create();

$response = $this->post(route('checkout.stripe', [ $cart->id ]), [
'token' => 'tok_123456',
]);

$response->assertSuccessful();
}
}

0 comments on commit 921091c

Please sign in to comment.