我正在使用Braintree为使用Laravel 5.2构建的SaaS订阅网站收取付款,并使用 ngrok.io 在本地主机上对其进行测试。注册和初始费用工作正常。 我也可以取消和恢复订阅。但是,我无法让自定义网络钩子工作。 我正在关注拉拉维尔文档。这是我的代码:
路线.php
Route::post('/webhook/braintree', 'BraintreeWebhookController@handleWebhook');
BraintreeWebhookController.php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use BraintreeWebhookNotification;
use LaravelCashierHttpControllersWebhookController;
use AppTransaction;
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)
{
//
}
}
Braintree Webhooks Test URL 链接返回"成功!服务器响应了 200。我使用测试表单测试了事务模型,它可以很好地创建数据库记录。Subscription_Charged_Successfully webhook 是否无法通过控制器,或者句柄函数中缺少某些内容?这里的任何帮助将不胜感激。
我做的和你在这里做的差不多。我正在使用 ngrok 使用 braintree 测试我的本地设置,我已经发现您的代码有一个例外。
ErrorException: Undefined property on BraintreeSubscription: name in file /var/www/vendor/braintree/braintree_php/lib/Braintree/Base.php on line 49
看起来名称不是订阅对象的属性
我发现你可以像这样获得本地订阅
$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,
]);
Subscription
在哪里LaravelCashierSubscription
.
编辑
此外,next_bill_date
也应该如上所示nextBillingDate
。