Created
September 18, 2014 19:34
-
-
Save ricardo-rossi/547f0fe5f5724dab37ae to your computer and use it in GitHub Desktop.
Laravel Adapter for Stripe Billing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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