mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 11:14:06 +00:00
59 - Handling Invalid Payment Tokens
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Billing;
|
namespace App\Billing;
|
||||||
|
|
||||||
use Stripe\Charge;
|
use Stripe\Charge;
|
||||||
|
use Stripe\Error\InvalidRequest;
|
||||||
|
|
||||||
class StripePaymentGateway implements PaymentGateway
|
class StripePaymentGateway implements PaymentGateway
|
||||||
{
|
{
|
||||||
@@ -15,10 +16,14 @@ class StripePaymentGateway implements PaymentGateway
|
|||||||
|
|
||||||
public function charge($amount, $token)
|
public function charge($amount, $token)
|
||||||
{
|
{
|
||||||
Charge::create([
|
try {
|
||||||
'amount' => $amount,
|
Charge::create([
|
||||||
'source' => $token,
|
'amount' => $amount,
|
||||||
'currency' => 'usd',
|
'source' => $token,
|
||||||
], ['api_key' => $this->apiKey]);
|
'currency' => 'usd',
|
||||||
|
], ['api_key' => $this->apiKey]);
|
||||||
|
} catch (InvalidRequest $e) {
|
||||||
|
throw new PaymentFailedException;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Billing\StripePaymentGateway;
|
use App\Billing\StripePaymentGateway;
|
||||||
|
use App\Billing\PaymentFailedException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group integration
|
* @group integration
|
||||||
@@ -24,6 +25,20 @@ class StripePaymentGatewayTest extends TestCase
|
|||||||
$this->assertEquals(2500, $this->lastCharge()->amount);
|
$this->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()
|
private function lastCharge()
|
||||||
{
|
{
|
||||||
return array_first(\Stripe\Charge::all(
|
return array_first(\Stripe\Charge::all(
|
||||||
|
|||||||
Reference in New Issue
Block a user