Skip to content

Instantly share code, notes, and snippets.

@ricardo-rossi
Created September 18, 2014 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardo-rossi/547f0fe5f5724dab37ae to your computer and use it in GitHub Desktop.
Save ricardo-rossi/547f0fe5f5724dab37ae to your computer and use it in GitHub Desktop.
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