mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +00:00
84 - Leveraging Our Contract Tests
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user