Skip to content

Fixes for braintree support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ And add the subscription class(es) to the `subscription_models` property in this

These values are primarily used by the WebhookControllers to help locate the corresponding Eloquent model from the gateway's ID when a new billing event is received.

## Create a user with subscription

```php
public function subscribeNewUser($email, $paymentMethod){
$user = User::firstOrNew(array('email'=>$email));

//register user in Braintree with a credit card's payment token
if(!$c = $user->billing()){
$user->billing()->withCardToken($paymentMethod)->create();
}

// create customer
if(!$t = $user->subscriptions()->first()){
try{
$b = $user->subscriptions('basic')->withCardToken($paymentMethod)->create($user);
}catch(Exception $e){
return false;
}
}else{
throw new UserException('Subscription exists for that user');
}
return true;
}
```

## Customers

Billing customers can be created and managed separately from subscriptions or charges.
Expand Down
20 changes: 12 additions & 8 deletions src/Mmanos/Billing/CustomerBillableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ public function subscriptionModelsArray()

if (method_exists($this, 'subscriptionmodels')) {
$subscriptions = array();

foreach ($this->subscriptionmodels() as $value) {
if ($value instanceof \Illuminate\Support\Collection) {
$subscriptions = array_merge($subscriptions, $value->all());
}
else if ($value instanceof \Illuminate\Database\Eloquent\Model) {
$subscriptions[] = $value;
$subscr_models = $this->subscriptionmodels();
if($subscr_models) {
foreach ($subscr_models as $value) {
if ($value instanceof \Illuminate\Support\Collection) {
$subscriptions = array_merge($subscriptions, $value->all());
}
else if ($value instanceof \Illuminate\Database\Eloquent\Model) {
$subscriptions[] = $value;
}
}
}else{
return array();
}

return $subscriptions;
Expand All @@ -121,7 +125,7 @@ public function subscriptionModelsArray()
if (method_exists($this, 'gatewaySubscription')) {
return array($this);
}

return null;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Mmanos/Billing/Gateways/Braintree/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public function create(array $properties = array())
{
$this->braintree_customer = Braintree_Customer::create(array(
'email' => Arr::get($properties, 'email'),
'creditCard' => array(
'paymentMethodNonce' => Arr::get($properties, 'card_token') ? Arr::get($properties, 'card_token') : null,
'options' => array(
'verifyCard' => true
)
)

))->customer;

$this->id = $this->braintree_customer->id;
Expand Down
4 changes: 4 additions & 0 deletions src/Mmanos/Billing/Gateways/Braintree/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public function info()
public function create($plan, array $properties = array())
{
if (!$token = Arr::get($properties, 'card')) {
if(empty($this->braintree_customer)){
throw new \Exception('Customer not existed in Braintree');
}

$cards = $this->braintree_customer->creditCards;
$token = $cards[0]->token;
}
Expand Down