diff --git a/app/Billing/FakePaymentGateway.php b/app/Billing/FakePaymentGateway.php index 2a87ddb..f30f1ef 100644 --- a/app/Billing/FakePaymentGateway.php +++ b/app/Billing/FakePaymentGateway.php @@ -4,6 +4,8 @@ namespace App\Billing; class FakePaymentGateway implements PaymentGateway { + const TEST_CARD_NUMBER = '4242424242424242'; + private $charges; private $tokens; private $beforeFirstChargeCallback; @@ -14,7 +16,7 @@ class FakePaymentGateway implements PaymentGateway $this->tokens = collect(); } - public function getValidTestToken($cardNumber = '4242424242424242') + public function getValidTestToken($cardNumber = self::TEST_CARD_NUMBER) { $token = 'fake-tok_'.str_random(24); $this->tokens[$token] = $cardNumber; diff --git a/app/Billing/StripePaymentGateway.php b/app/Billing/StripePaymentGateway.php index 14d2c83..317c9d2 100644 --- a/app/Billing/StripePaymentGateway.php +++ b/app/Billing/StripePaymentGateway.php @@ -2,11 +2,12 @@ namespace App\Billing; -use Stripe\Charge; use Stripe\Error\InvalidRequest; class StripePaymentGateway implements PaymentGateway { + const TEST_CARD_NUMBER = '4242424242424242'; + private $apiKey; public function __construct($apiKey) @@ -17,21 +18,26 @@ class StripePaymentGateway implements PaymentGateway public function charge($amount, $token) { try { - Charge::create([ + $stripeCharge = \Stripe\Charge::create([ 'amount' => $amount, 'source' => $token, 'currency' => 'usd', ], ['api_key' => $this->apiKey]); + + return new Charge([ + 'amount' => $stripeCharge['amount'], + 'card_last_four' => $stripeCharge['source']['last4'], + ]); } catch (InvalidRequest $e) { throw new PaymentFailedException; } } - public function getValidTestToken() + public function getValidTestToken($cardNumber = self::TEST_CARD_NUMBER) { return \Stripe\Token::create([ "card" => [ - "number" => "4242424242424242", + "number" => $cardNumber, "exp_month" => 1, "exp_year" => date('Y') + 1, "cvc" => "123" @@ -43,7 +49,12 @@ class StripePaymentGateway implements PaymentGateway { $latestCharge = $this->lastCharge(); $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() diff --git a/tests/unit/Billing/PaymentGatewayContractTests.php b/tests/unit/Billing/PaymentGatewayContractTests.php index 62bde6c..e1f8b37 100644 --- a/tests/unit/Billing/PaymentGatewayContractTests.php +++ b/tests/unit/Billing/PaymentGatewayContractTests.php @@ -24,9 +24,9 @@ trait PaymentGatewayContractTests { $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()); }