【问题标题】:Laravel 5.2 and Braintree Custom WebhooksLaravel 5.2 和 Braintree 自定义 Webhook
【发布时间】:2017-07-27 02:45:31
【问题描述】:

我正在使用 Braintree 为使用 Laravel 5.2 构建的 SaaS 订阅网站收取费用,并使用 ngrok.io 在我的本地主机上对其进行测试。注册和初始费用工作正常。我也可以取消和恢复订阅。但是,我无法让自定义 webhook 工作。我正在关注 Laravel 文档。这是我的代码:

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)
    {
        //
    }

}

Braintree Webhooks 测试 URL 链接返回“成功!服务器响应 200。”我使用测试表单测试了事务模型,它创建了一个数据库记录就好了。 Subscription_Charged_Successfully webhook 是没有通过控制器还是我在句柄函数中遗漏了什么?在这里的任何帮助将不胜感激。

【问题讨论】:

    标签: php laravel webhooks braintree


    【解决方案1】:

    我所做的几乎和你在这里所做的一样。我正在使用 ngrok 使用 Braintree 测试我的本地设置,并且我发现您的代码存在异常。

    ErrorException: Undefined property on Braintree\Subscription: name in file /var/www/vendor/braintree/braintree_php/lib/Braintree/Base.php on line 49
    

    看起来 name 不是订阅对象的属性

    我发现你可以像这样获得本地订阅

    $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,
    ]);
    

    SubscriptionLaravel\Cashier\Subscription

    编辑

    如上所示,next_bill_date 也应为 nextBillingDate

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 1970-01-01
      • 2017-02-28
      • 2017-08-07
      • 2016-04-18
      • 1970-01-01
      • 2017-01-02
      • 2016-09-09
      • 1970-01-01
      相关资源
      最近更新 更多