diff --git a/app/Billing/StripePaymentGateway.php b/app/Billing/StripePaymentGateway.php index 6f5a988..0022d26 100644 --- a/app/Billing/StripePaymentGateway.php +++ b/app/Billing/StripePaymentGateway.php @@ -3,6 +3,7 @@ namespace App\Billing; use Stripe\Charge; +use Stripe\Error\InvalidRequest; class StripePaymentGateway implements PaymentGateway { @@ -15,10 +16,14 @@ class StripePaymentGateway implements PaymentGateway public function charge($amount, $token) { - Charge::create([ - 'amount' => $amount, - 'source' => $token, - 'currency' => 'usd', - ], ['api_key' => $this->apiKey]); + try { + Charge::create([ + 'amount' => $amount, + 'source' => $token, + 'currency' => 'usd', + ], ['api_key' => $this->apiKey]); + } catch (InvalidRequest $e) { + throw new PaymentFailedException; + } } } diff --git a/tests/unit/Billing/StripePaymentGatewayTest.php b/tests/unit/Billing/StripePaymentGatewayTest.php index a237bcc..7722700 100644 --- a/tests/unit/Billing/StripePaymentGatewayTest.php +++ b/tests/unit/Billing/StripePaymentGatewayTest.php @@ -1,6 +1,7 @@ assertEquals(2500, $this->lastCharge()->amount); } + /** @test */ + function charges_with_an_invalid_payment_token_fail() + { + try { + $paymentGateway = new StripePaymentGateway(config('services.stripe.secret')); + $paymentGateway->charge(2500, 'invalid-payment-token'); + } catch (PaymentFailedException $e) { + $this->assertCount(0, $this->newCharges()); + return; + } + + $this->fail("Charging with an invalid payment token did not throw a PaymentFailedException."); + } + private function lastCharge() { return array_first(\Stripe\Charge::all(