mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-03-01 11:59:09 +00:00
165 - Splitting Payments with Stripe
This commit is contained in:
@@ -15,18 +15,23 @@ class StripePaymentGateway implements PaymentGateway
|
|||||||
$this->apiKey = $apiKey;
|
$this->apiKey = $apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function charge($amount, $token)
|
public function charge($amount, $token, $destinationAccountId)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$stripeCharge = \Stripe\Charge::create([
|
$stripeCharge = \Stripe\Charge::create([
|
||||||
'amount' => $amount,
|
'amount' => $amount,
|
||||||
'source' => $token,
|
'source' => $token,
|
||||||
'currency' => 'usd',
|
'currency' => 'usd',
|
||||||
|
'destination' => [
|
||||||
|
'account' => $destinationAccountId,
|
||||||
|
'amount' => $amount * .9,
|
||||||
|
]
|
||||||
], ['api_key' => $this->apiKey]);
|
], ['api_key' => $this->apiKey]);
|
||||||
|
|
||||||
return new Charge([
|
return new Charge([
|
||||||
'amount' => $stripeCharge['amount'],
|
'amount' => $stripeCharge['amount'],
|
||||||
'card_last_four' => $stripeCharge['source']['last4'],
|
'card_last_four' => $stripeCharge['source']['last4'],
|
||||||
|
'destination' => $destinationAccountId,
|
||||||
]);
|
]);
|
||||||
} catch (InvalidRequest $e) {
|
} catch (InvalidRequest $e) {
|
||||||
throw new PaymentFailedException;
|
throw new PaymentFailedException;
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ trait PaymentGatewayContractTests
|
|||||||
$paymentGateway = $this->getPaymentGateway();
|
$paymentGateway = $this->getPaymentGateway();
|
||||||
|
|
||||||
$newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) {
|
$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);
|
$this->assertCount(1, $newCharges);
|
||||||
@@ -26,11 +26,11 @@ trait PaymentGatewayContractTests
|
|||||||
{
|
{
|
||||||
$paymentGateway = $this->getPaymentGateway();
|
$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(substr($paymentGateway::TEST_CARD_NUMBER, -4), $charge->cardLastFour());
|
||||||
$this->assertEquals(2500, $charge->amount());
|
$this->assertEquals(2500, $charge->amount());
|
||||||
$this->assertEquals('test_acct_1234', $charge->destination());
|
$this->assertEquals(env('STRIPE_TEST_PROMOTER_ID'), $charge->destination());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@@ -40,7 +40,7 @@ trait PaymentGatewayContractTests
|
|||||||
|
|
||||||
$newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) {
|
$newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) {
|
||||||
try {
|
try {
|
||||||
$paymentGateway->charge(2500, 'invalid-payment-token', 'test_acct_1234');
|
$paymentGateway->charge(2500, 'invalid-payment-token', env('STRIPE_TEST_PROMOTER_ID'));
|
||||||
} catch (PaymentFailedException $e) {
|
} catch (PaymentFailedException $e) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -55,12 +55,12 @@ trait PaymentGatewayContractTests
|
|||||||
function can_fetch_charges_created_during_a_callback()
|
function can_fetch_charges_created_during_a_callback()
|
||||||
{
|
{
|
||||||
$paymentGateway = $this->getPaymentGateway();
|
$paymentGateway = $this->getPaymentGateway();
|
||||||
$paymentGateway->charge(2000, $paymentGateway->getValidTestToken(), 'test_acct_1234');
|
$paymentGateway->charge(2000, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID'));
|
||||||
$paymentGateway->charge(3000, $paymentGateway->getValidTestToken(), 'test_acct_1234');
|
$paymentGateway->charge(3000, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID'));
|
||||||
|
|
||||||
$newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) {
|
$newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) {
|
||||||
$paymentGateway->charge(4000, $paymentGateway->getValidTestToken(), 'test_acct_1234');
|
$paymentGateway->charge(4000, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID'));
|
||||||
$paymentGateway->charge(5000, $paymentGateway->getValidTestToken(), 'test_acct_1234');
|
$paymentGateway->charge(5000, $paymentGateway->getValidTestToken(), env('STRIPE_TEST_PROMOTER_ID'));
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertCount(2, $newCharges);
|
$this->assertCount(2, $newCharges);
|
||||||
|
|||||||
@@ -16,4 +16,22 @@ class StripePaymentGatewayTest extends TestCase
|
|||||||
{
|
{
|
||||||
return new StripePaymentGateway(config('services.stripe.secret'));
|
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']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user