mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 11:14:06 +00:00
65 lines
1.5 KiB
PHP
65 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Billing;
|
|
|
|
use Stripe\Charge;
|
|
use Stripe\Error\InvalidRequest;
|
|
|
|
class StripePaymentGateway implements PaymentGateway
|
|
{
|
|
private $apiKey;
|
|
|
|
public function __construct($apiKey)
|
|
{
|
|
$this->apiKey = $apiKey;
|
|
}
|
|
|
|
public function charge($amount, $token)
|
|
{
|
|
try {
|
|
Charge::create([
|
|
'amount' => $amount,
|
|
'source' => $token,
|
|
'currency' => 'usd',
|
|
], ['api_key' => $this->apiKey]);
|
|
} catch (InvalidRequest $e) {
|
|
throw new PaymentFailedException;
|
|
}
|
|
}
|
|
|
|
public function getValidTestToken()
|
|
{
|
|
return \Stripe\Token::create([
|
|
"card" => [
|
|
"number" => "4242424242424242",
|
|
"exp_month" => 1,
|
|
"exp_year" => date('Y') + 1,
|
|
"cvc" => "123"
|
|
]
|
|
], ['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);
|
|
}
|
|
}
|