165 - Splitting Payments with Stripe

This commit is contained in:
Adam Wathan
2018-01-26 13:40:58 -05:00
parent 7442baa5c5
commit def3e0b6b2
3 changed files with 32 additions and 9 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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']);
}
}