63 - Capturing Charges with Callbacks

This commit is contained in:
Adam Wathan
2017-01-18 09:47:54 -05:00
parent f1a57e27f6
commit 0d7720cdc0
2 changed files with 24 additions and 2 deletions

View File

@@ -38,4 +38,27 @@ class StripePaymentGateway implements PaymentGateway
]
], ['api_key' => $this->apiKey])->id;
}
public function newChargesDuring($callback)
{
$latestCharge = $this->lastCharge();
$callback($this);
return $this->newChargesSince($latestCharge)->pluck('amount');
}
private function lastCharge()
{
return array_first(\Stripe\Charge::all([
'limit' => 1
], ['api_key' => $this->apiKey])['data']);
}
private function newChargesSince($charge = null)
{
$newCharges = \Stripe\Charge::all([
'ending_before' => $charge ? $charge->id : null,
], ['api_key' => $this->apiKey])['data'];
return collect($newCharges);
}
}

View File

@@ -24,8 +24,7 @@ class StripePaymentGatewayTest extends TestCase
{
$paymentGateway = $this->getPaymentGateway();
// How could we make this API work?
$newCharges = $paymentGateway->newChargesDuring(function () {
$newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) {
$paymentGateway->charge(2500, $paymentGateway->getValidTestToken());
});