84 - Leveraging Our Contract Tests

This commit is contained in:
Adam Wathan
2017-03-17 12:36:43 -04:00
parent 6e983c593c
commit 8c8d05a353
3 changed files with 21 additions and 8 deletions

View File

@@ -4,6 +4,8 @@ namespace App\Billing;
class FakePaymentGateway implements PaymentGateway class FakePaymentGateway implements PaymentGateway
{ {
const TEST_CARD_NUMBER = '4242424242424242';
private $charges; private $charges;
private $tokens; private $tokens;
private $beforeFirstChargeCallback; private $beforeFirstChargeCallback;
@@ -14,7 +16,7 @@ class FakePaymentGateway implements PaymentGateway
$this->tokens = collect(); $this->tokens = collect();
} }
public function getValidTestToken($cardNumber = '4242424242424242') public function getValidTestToken($cardNumber = self::TEST_CARD_NUMBER)
{ {
$token = 'fake-tok_'.str_random(24); $token = 'fake-tok_'.str_random(24);
$this->tokens[$token] = $cardNumber; $this->tokens[$token] = $cardNumber;

View File

@@ -2,11 +2,12 @@
namespace App\Billing; namespace App\Billing;
use Stripe\Charge;
use Stripe\Error\InvalidRequest; use Stripe\Error\InvalidRequest;
class StripePaymentGateway implements PaymentGateway class StripePaymentGateway implements PaymentGateway
{ {
const TEST_CARD_NUMBER = '4242424242424242';
private $apiKey; private $apiKey;
public function __construct($apiKey) public function __construct($apiKey)
@@ -17,21 +18,26 @@ class StripePaymentGateway implements PaymentGateway
public function charge($amount, $token) public function charge($amount, $token)
{ {
try { try {
Charge::create([ $stripeCharge = \Stripe\Charge::create([
'amount' => $amount, 'amount' => $amount,
'source' => $token, 'source' => $token,
'currency' => 'usd', 'currency' => 'usd',
], ['api_key' => $this->apiKey]); ], ['api_key' => $this->apiKey]);
return new Charge([
'amount' => $stripeCharge['amount'],
'card_last_four' => $stripeCharge['source']['last4'],
]);
} catch (InvalidRequest $e) { } catch (InvalidRequest $e) {
throw new PaymentFailedException; throw new PaymentFailedException;
} }
} }
public function getValidTestToken() public function getValidTestToken($cardNumber = self::TEST_CARD_NUMBER)
{ {
return \Stripe\Token::create([ return \Stripe\Token::create([
"card" => [ "card" => [
"number" => "4242424242424242", "number" => $cardNumber,
"exp_month" => 1, "exp_month" => 1,
"exp_year" => date('Y') + 1, "exp_year" => date('Y') + 1,
"cvc" => "123" "cvc" => "123"
@@ -43,7 +49,12 @@ class StripePaymentGateway implements PaymentGateway
{ {
$latestCharge = $this->lastCharge(); $latestCharge = $this->lastCharge();
$callback($this); $callback($this);
return $this->newChargesSince($latestCharge)->pluck('amount'); return $this->newChargesSince($latestCharge)->map(function ($stripeCharge) {
return new Charge([
'amount' => $stripeCharge['amount'],
'card_last_four' => $stripeCharge['source']['last4'],
]);
});
} }
private function lastCharge() private function lastCharge()

View File

@@ -24,9 +24,9 @@ trait PaymentGatewayContractTests
{ {
$paymentGateway = $this->getPaymentGateway(); $paymentGateway = $this->getPaymentGateway();
$charge = $paymentGateway->charge(2500, $paymentGateway->getValidTestToken('0000000000004242')); $charge = $paymentGateway->charge(2500, $paymentGateway->getValidTestToken($paymentGateway::TEST_CARD_NUMBER));
$this->assertEquals('4242', $charge->cardLastFour()); $this->assertEquals(substr($paymentGateway::TEST_CARD_NUMBER, -4), $charge->cardLastFour());
$this->assertEquals(2500, $charge->amount()); $this->assertEquals(2500, $charge->amount());
} }