9

handleCustomerSubscriptionDeleted以下を使用して、デフォルトのメソッドをオーバーライドしますapp/Http/Controllers/WebHookController.php

<?php namespace App\Http\Controllers;

use Laravel\Cashier\WebhookController;
use Symfony\Component\HttpFoundation\Response;

class StripeWebhooks extends WebhookController {

  /**
   * Handle stripe subscription webhook
   *
   * @param  array  $payload
   * @return Response
   */
  public function handleCustomerSubscriptionDeleted($payload)
  {
    $billable = $this->getBillable($payload['data']['object']['customer']);

    if ($billable && $billable->subscribed()) {
      $billable->subscription()->cancel();

    }

    return new Response('Webhook Handled', 200);
  }

}

これが実際にデフォルトを上書きするためには、ルートを拡張コントローラーに更新する必要がありますか、それともデフォルトを参照する必要がありますか? デフォルトのままにしておくと、Laravelにコントローラーの存在を知らせることさえできないと思いますが、確認したかったのです。

現時点の:

Route::post('stripe/webhook', '\Laravel\Cashier\WebhookController@handleWebhook');

これの他の部分は、handleCustomerSubscriptionDeletedこれが参照している現在のユーザーを取得して、他のアクションを実行できるようにしたいです。この場合、レコードを自動的に非公開に設定します。ユーザーを取得するための最良の方法は何ですか? $payload['data']['object']['customer']users テーブルの列を保持して関連付けることができるようstripe_idですが、確認したいと思います。助けてくれてありがとう!

アップデート

私は(ドキュメントに基づいて)、次のようになるはずだと信じています:

<?php namespace App\Http\Controllers;

use App\Models\Dashboard\Listing;
use App\User;
use Symfony\Component\HttpFoundation\Response;
use Laravel\Cashier\WebhookController as BaseController;

class WebhookController extends BaseController
{
  /**
   * Handle stripe subscription webhook
   *
   * @param  array  $payload
   * @return Response
   */
  public function handleCustomerSubscriptionDeleted($payload)
  {
    $billable = $this->getBillable($payload['data']['object']['customer']);

    if ($billable && $billable->subscribed()) {
      $billable->subscription()->cancel();

      // Get current user
      $user = User::find($billable);

      // Set each listing to draft
      foreach ($user->listings as $listing) {
        $current_listing = Listing::find($listing->id);
        if ($current_listing->status == 'published') {
          $current_listing->status = 'draft';
          $current_listing->save();
        }
      }
    }

    return new Response('Webhook Handled', 200);
  }
}

次に、ルートを次のように更新しました。

Route::post('stripe/webhook', 'WebhookController@handleWebhook');

しかし、まだ発砲していません。しかし、私が疑問に思っているのはhandleCustomerSubscriptionDeleted、「キャンセルしたとき」と呼ばれるか、猶予期間が終了して実際のサブスクリプションがキャンセルされた後に呼び出されるかということです。待機中のゲームをローカルでプレイするよりも、これをテストするためのより信頼できる方法はありますか?

アップデート #2

オーバーライド クラスを次のように更新しました。

<?php namespace App\Http\Controllers;

use App\Models\Dashboard\Listing;
use App\User;
use Symfony\Component\HttpFoundation\Response;
use Laravel\Cashier\WebhookController as BaseController;

class WebhookController extends BaseController
{

  /**
   * Handle stripe subscription webhook
   *
   * @param  array  $payload
   * @return Response
   */
  public function handleCustomerSubscriptionDeleted(array $payload)
  {
    $billable = $this->getBillable($payload['data']['object']['customer']);

    if ($billable && $billable->subscribed()) {
      $billable->subscription()->cancel();

      // Get current user
      $user = User::where('stripe_id', $billable)->firstOrFail();

      // Set each listing to draft
      foreach ($user->listings as $listing) {
        $current_listing = Listing::find($listing->id);
        if ($current_listing->status == 'published') {
          $current_listing->status = 'draft';
          $current_listing->save();
        }
      }
    }

    return new Response('Webhook Handled', 200);
  }
}

私が変更した部分は$billable、以前考えていたユーザー ID ではなく、応答が返すものであるため、ユーザーを検索するように変更することでした。@Shazが言及したように、localtunnel.meを試してみましたが、これによりリクエストを送信できましたが、影響を与えたい顧客IDを渡すことができませんでした。すべてが正しいことを確認する方法がわかりません実際に働いています。Cashier を介して手動でイベントを実行できるかどうかを調べてみることもできますが、それでも少し奇妙に思えます。

アップデート #3

聞く限り、少し簡単なことを試してみましたcustomer.subscription.created(すぐに起動するため):

<?php namespace App\Http\Controllers;

use App\Models\Dashboard\Listing;
use App\User;
use Symfony\Component\HttpFoundation\Response;
use Laravel\Cashier\WebhookController as BaseController;

class WebhookController extends BaseController
{

  /**
   * @param array $payload
   */
  public function handleCustomerSubscriptionCreated(array $payload) {
    $billable = $this->getBillable($payload['data']['object']['customer']);

    if ($billable) {
      // Get current user
      $user = User::where('stripe_id', $billable)->firstOrFail();

      $user->first_name = 'Helloooooo';
      $user->save();
    }
  }

  /**
   * Handle stripe subscription webhook
   *
   * @param  array  $payload
   * @return Response
   */
  public function handleCustomerSubscriptionDeleted(array $payload)
  {
    $billable = $this->getBillable($payload['data']['object']['customer']);

    if ($billable && $billable->subscribed()) {
      $billable->subscription()->cancel();

      // Get current user
      $user = User::where('stripe_id', $billable)->firstOrFail();

      // Set each listing to draft
      foreach ($user->listings as $listing) {
        $current_listing = Listing::find($listing->id);
        if ($current_listing->status == 'published') {
          $current_listing->status = 'draft';
          $current_listing->save();
        }
      }
    }

    return new Response('Webhook Handled', 200);
  }
}

localtunnel.me を使用して Webhook をセットアップしましたが、Webhook に正しく応答しているようには見えません。Stripe ダッシュボード (顧客 ID が設定されていないことは明らかです) は問題ありません。500エラーについて私が得ている応答は、残念ながら、Laravelが吐き出しているソースコードのごちゃごちゃした混乱の中で失われている/途切れています:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="robots" content="noindex,nofollow" />
        <style>
            /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
            html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}

            html { backgr...
4

3 に答える 3

1
$billable = $this->getBillable($payload['data']['object']['customer']);
$user = $billable; // billable is User object
于 2016-01-25T13:42:13.343 に答える