Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel Adapter for Stripe Billing
<?php namespace Endata\Billing;
use Stripe;
use Stripe_Charge;
use Stripe_Customer;
use Stripe_InvalidRequestError;
use Stripe_CardError;
use Config;
use Exception;
class StripeBilling implements BillingInterface {
public function __construct()
{
Stripe::setApiKey(Config::get('stripe.secret_key'));
}
/**
* Start a subscription
* @param array $data [description]
* @return [type] [description]
*/
public function subscribe(array $data)
{
try
{
if($data['customer'] == null)
{
$customer = Stripe_Customer::create([
'card' => $data['token'],
"plan" => $data['plan'],
"quantity" => $data['quantity'],
'email' => $data['email'],
'description' => $data['name']
]);
}
else
{
$customer = Stripe_Customer::retrieve($data['customer']);
$customer->subscriptions->create([
'card' => $data['token'],
"plan" => $data['plan'],
"quantity" => $data['quantity'],
]);
}
return $customer;
}
catch (Stripe_InvalidRequestError $e)
{
// Invalid parameters were supplied to Stripe's API
throw new Exception($e->getMessage());
}
catch(Stripe_CardError $e)
{
throw new Exception($e->getMessage());
}
}
/**
* One time charges
* @param array $data [description]
* @return [type] [description]
*/
public function charge(array $data)
{
try
{
if($data['customer'] == null)
{
$customer = Stripe_Customer::create([
'card' => $data['token'],
'email' => $data['email'],
'description' => $data['name']
]);
}
else
{
$customer = Stripe_Customer::retrieve($data['customer']);
}
Stripe_Charge::create([
'customer' => $customer->id,
'amount' => $data['amount'],
'currency' => 'usd',
'description' => $data['product']
]);
return $customer;
}
catch (Stripe_InvalidRequestError $e)
{
// Invalid parameters were supplied to Stripe's API
throw new Exception($e->getMessage());
}
catch(Stripe_CardError $e)
{
throw new Exception($e->getMessage());
}
}
public function customerInfo($customer) {
try
{
return Stripe_Customer::retrieve($customer);
}
catch (Stripe_InvalidRequestError $e)
{
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment