From def3e0b6b2b2b9297027dd05072668733f4b9645 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 26 Jan 2018 13:40:58 -0500 Subject: [PATCH] 165 - Splitting Payments with Stripe --- app/Billing/StripePaymentGateway.php | 7 ++++++- .../Billing/PaymentGatewayContractTests.php | 16 ++++++++-------- .../Unit/Billing/StripePaymentGatewayTest.php | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app/Billing/StripePaymentGateway.php b/app/Billing/StripePaymentGateway.php index 317c9d2..179c1f8 100644 --- a/app/Billing/StripePaymentGateway.php +++ b/app/Billing/StripePaymentGateway.php @@ -15,18 +15,23 @@ class StripePaymentGateway implements PaymentGateway $this->apiKey = $apiKey; } - public function charge($amount, $token) + public function charge($amount, $token, $destinationAccountId) { try { $stripeCharge = \Stripe\Charge::create([ 'amount' => $amount, 'source' => $token, 'currency' => 'usd', + 'destination' => [ + 'account' => $destinationAccountId, + 'amount' => $amount * .9, + ] ], ['api_key' => $this->apiKey]); return new Charge([ 'amount' => $stripeCharge['amount'], 'card_last_four' => $stripeCharge['source']['last4'], + 'destination' => $destinationAccountId, ]); } catch (InvalidRequest $e) { throw new PaymentFailedException; diff --git a/tests/Unit/Billing/PaymentGatewayContractTests.php b/tests/Unit/Billing/PaymentGatewayContractTests.php index 667a2cc..5068e5c 100644 --- a/tests/Unit/Billing/PaymentGatewayContractTests.php +++ b/tests/Unit/Billing/PaymentGatewayContractTests.php @@ -14,7 +14,7 @@ trait PaymentGatewayContractTests $paymentGateway = $this->getPaymentGateway(); $newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) { - $paymentGateway->charge(2500, $paymentGateway->getValidTestToken(), 'test_acct_1234'); + $paymentGateway->charge(2500, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID')); }); $this->assertCount(1, $newCharges); @@ -26,11 +26,11 @@ trait PaymentGatewayContractTests { $paymentGateway = $this->getPaymentGateway(); - $charge = $paymentGateway->charge(2500, $paymentGateway->getValidTestToken($paymentGateway::TEST_CARD_NUMBER), 'test_acct_1234'); + $charge = $paymentGateway->charge(2500, $paymentGateway->getValidTestToken($paymentGateway::TEST_CARD_NUMBER), env('STRIPE_TEST_PROMOTER_ID')); $this->assertEquals(substr($paymentGateway::TEST_CARD_NUMBER, -4), $charge->cardLastFour()); $this->assertEquals(2500, $charge->amount()); - $this->assertEquals('test_acct_1234', $charge->destination()); + $this->assertEquals(env('STRIPE_TEST_PROMOTER_ID'), $charge->destination()); } /** @test */ @@ -40,7 +40,7 @@ trait PaymentGatewayContractTests $newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) { try { - $paymentGateway->charge(2500, 'invalid-payment-token', 'test_acct_1234'); + $paymentGateway->charge(2500, 'invalid-payment-token', env('STRIPE_TEST_PROMOTER_ID')); } catch (PaymentFailedException $e) { return; } @@ -55,12 +55,12 @@ trait PaymentGatewayContractTests function can_fetch_charges_created_during_a_callback() { $paymentGateway = $this->getPaymentGateway(); - $paymentGateway->charge(2000, $paymentGateway->getValidTestToken(), 'test_acct_1234'); - $paymentGateway->charge(3000, $paymentGateway->getValidTestToken(), 'test_acct_1234'); + $paymentGateway->charge(2000, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID')); + $paymentGateway->charge(3000, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID')); $newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) { - $paymentGateway->charge(4000, $paymentGateway->getValidTestToken(), 'test_acct_1234'); - $paymentGateway->charge(5000, $paymentGateway->getValidTestToken(), 'test_acct_1234'); + $paymentGateway->charge(4000, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID')); + $paymentGateway->charge(5000, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID')); }); $this->assertCount(2, $newCharges); diff --git a/tests/Unit/Billing/StripePaymentGatewayTest.php b/tests/Unit/Billing/StripePaymentGatewayTest.php index 974b84f..cf8f988 100644 --- a/tests/Unit/Billing/StripePaymentGatewayTest.php +++ b/tests/Unit/Billing/StripePaymentGatewayTest.php @@ -16,4 +16,22 @@ class StripePaymentGatewayTest extends TestCase { return new StripePaymentGateway(config('services.stripe.secret')); } + + /** @test */ + function ninety_percent_of_the_payment_is_transferred_to_the_destination_account() + { + $paymentGateway = new StripePaymentGateway(config('services.stripe.secret')); + + $paymentGateway->charge(5000, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID')); + + $lastStripeCharge = array_first(\Stripe\Charge::all([ + 'limit' => 1 + ], ['api_key' => config('services.stripe.secret')])['data']); + + $this->assertEquals(5000, $lastStripeCharge['amount']); + $this->assertEquals(env('STRIPE_TEST_PROMOTER_ID'), $lastStripeCharge['destination']); + + $transfer = \Stripe\Transfer::retrieve($lastStripeCharge['transfer'], ['api_key' => config('services.stripe.secret')]); + $this->assertEquals(4500, $transfer['amount']); + } }