163 - Total Charges for a Specific Account

This commit is contained in:
Adam Wathan
2018-01-29 13:13:29 -05:00
parent 54a8159a1d
commit 0db962d04c
6 changed files with 37 additions and 5 deletions

View File

@@ -20,4 +20,9 @@ class Charge
{ {
return $this->data['card_last_four']; return $this->data['card_last_four'];
} }
public function destination()
{
return $this->data['destination'];
}
} }

View File

@@ -23,7 +23,7 @@ class FakePaymentGateway implements PaymentGateway
return $token; return $token;
} }
public function charge($amount, $token) public function charge($amount, $token, $destinationAccountId)
{ {
if ($this->beforeFirstChargeCallback !== null) { if ($this->beforeFirstChargeCallback !== null) {
$callback = $this->beforeFirstChargeCallback; $callback = $this->beforeFirstChargeCallback;
@@ -38,6 +38,7 @@ class FakePaymentGateway implements PaymentGateway
return $this->charges[] = new Charge([ return $this->charges[] = new Charge([
'amount' => $amount, 'amount' => $amount,
'card_last_four' => substr($this->tokens[$token], -4), 'card_last_four' => substr($this->tokens[$token], -4),
'destination' => $destinationAccountId,
]); ]);
} }
@@ -53,6 +54,13 @@ class FakePaymentGateway implements PaymentGateway
return $this->charges->map->amount()->sum(); return $this->charges->map->amount()->sum();
} }
public function totalChargesFor($accountId)
{
return $this->charges->filter(function ($charge) use ($accountId) {
return $charge->destination() === $accountId;
})->map->amount()->sum();
}
public function beforeFirstCharge($callback) public function beforeFirstCharge($callback)
{ {
$this->beforeFirstChargeCallback = $callback; $this->beforeFirstChargeCallback = $callback;

View File

@@ -4,7 +4,7 @@ namespace App\Billing;
interface PaymentGateway interface PaymentGateway
{ {
public function charge($amount, $token); public function charge($amount, $token, $destinationAccountId);
public function getValidTestToken(); public function getValidTestToken();
public function newChargesDuring($callback); public function newChargesDuring($callback);

View File

@@ -2,6 +2,7 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\User;
use App\Concert; use App\Concert;
use Tests\TestCase; use Tests\TestCase;
use App\Facades\TicketCode; use App\Facades\TicketCode;
@@ -60,7 +61,12 @@ class PurchaseTicketsTest extends TestCase
OrderConfirmationNumber::shouldReceive('generate')->andReturn('ORDERCONFIRMATION1234'); OrderConfirmationNumber::shouldReceive('generate')->andReturn('ORDERCONFIRMATION1234');
TicketCode::shouldReceive('generateFor')->andReturn('TICKETCODE1', 'TICKETCODE2', 'TICKETCODE3'); TicketCode::shouldReceive('generateFor')->andReturn('TICKETCODE1', 'TICKETCODE2', 'TICKETCODE3');
$concert = \ConcertFactory::createPublished(['ticket_price' => 3250, 'ticket_quantity' => 3]); $user = factory(User::class)->create(['stripe_account_id' => 'test_acct_1234']);
$concert = \ConcertFactory::createPublished([
'ticket_price' => 3250,
'ticket_quantity' => 3,
'user_id' => $user,
]);
$this->orderTickets($concert, [ $this->orderTickets($concert, [
'email' => 'john@example.com', 'email' => 'john@example.com',
@@ -81,7 +87,7 @@ class PurchaseTicketsTest extends TestCase
] ]
]); ]);
$this->assertEquals(9750, $this->paymentGateway->totalCharges()); $this->assertEquals(9750, $this->paymentGateway->totalChargesFor('test_acct_1234'));
$this->assertTrue($concert->hasOrderFor('john@example.com')); $this->assertTrue($concert->hasOrderFor('john@example.com'));
$order = $concert->ordersFor('john@example.com')->first(); $order = $concert->ordersFor('john@example.com')->first();

View File

@@ -14,6 +14,18 @@ class FakePaymentGatewayTest extends TestCase
return new FakePaymentGateway; return new FakePaymentGateway;
} }
/** @test */
function can_get_total_charges_for_a_specific_account()
{
$paymentGateway = new FakePaymentGateway;
$paymentGateway->charge(1000, $paymentGateway->getValidTestToken(), 'test_acct_0000');
$paymentGateway->charge(2500, $paymentGateway->getValidTestToken(), 'test_acct_1234');
$paymentGateway->charge(4000, $paymentGateway->getValidTestToken(), 'test_acct_1234');
$this->assertEquals(6500, $paymentGateway->totalChargesFor('test_acct_1234'));
}
/** @test */ /** @test */
function running_a_hook_before_the_first_charge() function running_a_hook_before_the_first_charge()
{ {

View File

@@ -26,10 +26,11 @@ trait PaymentGatewayContractTests
{ {
$paymentGateway = $this->getPaymentGateway(); $paymentGateway = $this->getPaymentGateway();
$charge = $paymentGateway->charge(2500, $paymentGateway->getValidTestToken($paymentGateway::TEST_CARD_NUMBER)); $charge = $paymentGateway->charge(2500, $paymentGateway->getValidTestToken($paymentGateway::TEST_CARD_NUMBER), 'test_acct_1234');
$this->assertEquals(substr($paymentGateway::TEST_CARD_NUMBER, -4), $charge->cardLastFour()); $this->assertEquals(substr($paymentGateway::TEST_CARD_NUMBER, -4), $charge->cardLastFour());
$this->assertEquals(2500, $charge->amount()); $this->assertEquals(2500, $charge->amount());
$this->assertEquals('test_acct_1234', $charge->destination());
} }
/** @test */ /** @test */