mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-13 22:52:55 +00:00
83 - Promoting Charges to Objects
This commit is contained in:
23
app/Billing/Charge.php
Normal file
23
app/Billing/Charge.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Billing;
|
||||
|
||||
class Charge
|
||||
{
|
||||
private $data;
|
||||
|
||||
public function __construct($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function amount()
|
||||
{
|
||||
return $this->data['amount'];
|
||||
}
|
||||
|
||||
public function cardLastFour()
|
||||
{
|
||||
return $this->data['card_last_four'];
|
||||
}
|
||||
}
|
||||
@@ -5,16 +5,20 @@ namespace App\Billing;
|
||||
class FakePaymentGateway implements PaymentGateway
|
||||
{
|
||||
private $charges;
|
||||
private $tokens;
|
||||
private $beforeFirstChargeCallback;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->charges = collect();
|
||||
$this->tokens = collect();
|
||||
}
|
||||
|
||||
public function getValidTestToken()
|
||||
public function getValidTestToken($cardNumber = '4242424242424242')
|
||||
{
|
||||
return "valid-token";
|
||||
$token = 'fake-tok_'.str_random(24);
|
||||
$this->tokens[$token] = $cardNumber;
|
||||
return $token;
|
||||
}
|
||||
|
||||
public function charge($amount, $token)
|
||||
@@ -25,10 +29,14 @@ class FakePaymentGateway implements PaymentGateway
|
||||
$callback($this);
|
||||
}
|
||||
|
||||
if ($token !== $this->getValidTestToken()) {
|
||||
if (! $this->tokens->has($token)) {
|
||||
throw new PaymentFailedException;
|
||||
}
|
||||
$this->charges[] = $amount;
|
||||
|
||||
return $this->charges[] = new Charge([
|
||||
'amount' => $amount,
|
||||
'card_last_four' => substr($this->tokens[$token], -4),
|
||||
]);
|
||||
}
|
||||
|
||||
public function newChargesDuring($callback)
|
||||
@@ -40,7 +48,7 @@ class FakePaymentGateway implements PaymentGateway
|
||||
|
||||
public function totalCharges()
|
||||
{
|
||||
return $this->charges->sum();
|
||||
return $this->charges->map->amount()->sum();
|
||||
}
|
||||
|
||||
public function beforeFirstCharge($callback)
|
||||
|
||||
Reference in New Issue
Block a user