I'm using Braintree to collect payments for a SaaS subscription site built using Laravel 5.2 and testing it on my localhost using ngrok.io. The signup and initial charges work fine. I can cancel and resume subscriptions, as well. However, I can't get the custom webhooks to work. I'm following the Laravel docs. Here's my code:
routes.php
Route::post('/webhook/braintree', 'BraintreeWebhookController@handleWebhook');
BraintreeWebhookController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Braintree\WebhookNotification;
use Laravel\Cashier\Http\Controllers\WebhookController;
use App\Transaction;
class BraintreeWebhookController extends WebhookController
{
//
public function handleSubscriptionChargedSuccessfully(WebhookNotification $notification)
{
$transaction = new Transaction();
$transaction->subscript_id = $notification->subscription->id;
$transaction->name = $notification->subscription->name;
$transaction->next_bill_date = $notification->subscription->next_bill_date;
$transaction->price = $notification->subscription->price;
$transaction->save();
return new Response('Webhook Handled', 200);
}
public function handleSubscriptionChargedUnsuccessfully(WebhookNotification $notification)
{
//
}
public function handleSubscriptionTrialEnded(WebhookNotification $notification)
{
//
}
}
The Braintree Webhooks Test URL link returns "Success! Server responded with 200." I tested the Transaction model using a test form and it creates a database record just fine. Is the Subscription_Charged_Successfully webhook not making it through to the controller or am I missing something in the handle function? Any help here would be greatly appreciated.
I'm pretty much doing the same as what you've got here. I'm using ngrok to test my local setup with braintree and I've figured out there's an exception with your code.
ErrorException: Undefined property on Braintree\Subscription: name in file /var/www/vendor/braintree/braintree_php/lib/Braintree/Base.php on line 49
Looks like name is not a property on subscription object
I figured out that you can get your local subscription like so
$localSubscription = Subscription::where('braintree_id', $notification->subscription->id)
->first();
Transaction::create([
'subscription_id' => $notification->subscription->id,
'name' => $localSubscription->name,
'next_bill_date' => $notification->subscription-> nextBillingDate,
'price' => $notification->subscription->price,
'user_id' => $localSubscription->user->id,
]);
Where Subscription
is Laravel\Cashier\Subscription
.
EDIT
Also next_bill_date
should be nextBillingDate
as seen above.