mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +00:00
6.5 - Hooking into Charges
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Billing;
|
||||
class FakePaymentGateway implements PaymentGateway
|
||||
{
|
||||
private $charges;
|
||||
private $beforeFirstChargeCallback;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -18,6 +19,10 @@ class FakePaymentGateway implements PaymentGateway
|
||||
|
||||
public function charge($amount, $token)
|
||||
{
|
||||
if ($this->beforeFirstChargeCallback !== null) {
|
||||
$this->beforeFirstChargeCallback->__invoke($this);
|
||||
}
|
||||
|
||||
if ($token !== $this->getValidTestToken()) {
|
||||
throw new PaymentFailedException;
|
||||
}
|
||||
@@ -28,4 +33,9 @@ class FakePaymentGateway implements PaymentGateway
|
||||
{
|
||||
return $this->charges->sum();
|
||||
}
|
||||
|
||||
public function beforeFirstCharge($callback)
|
||||
{
|
||||
$this->beforeFirstChargeCallback = $callback;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,4 +30,20 @@ class FakePaymentGatewayTest extends TestCase
|
||||
|
||||
$this->fail();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function running_a_hook_before_the_first_charge()
|
||||
{
|
||||
$paymentGateway = new FakePaymentGateway;
|
||||
$callbackRan = false;
|
||||
|
||||
$paymentGateway->beforeFirstCharge(function ($paymentGateway) use (&$callbackRan) {
|
||||
$callbackRan = true;
|
||||
$this->assertEquals(0, $paymentGateway->totalCharges());
|
||||
});
|
||||
|
||||
$paymentGateway->charge(2500, $paymentGateway->getValidTestToken());
|
||||
$this->assertTrue($callbackRan);
|
||||
$this->assertEquals(2500, $paymentGateway->totalCharges());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user