Webhookのペイロードから情報を取得する方法

public function handleWebhook(Request $request) {
    $payload = $request->all();

    $amount = $payload['data']['object']['amount'];
    
    // 「誰が」購入したかの情報を取得
    $user_name = $payload['data']['object']['user_name'];
    
    // 「どの商品」を購入したかの情報を取得
    $product_name = $payload['data']['object']['product_name'];

    // 上記の情報をメールに追加
    Mail::to('test@test.com')->send(new \App\Mail\NewMember($amount, $user_name, $product_name));

    return 'OK';
}

 

更に\App\Mail\NewMember のメールクラスも、新しい情報を受け取ってメール本文にそれを含めるように修正する必要があります。

 

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class NewMember extends Mailable
{
    use Queueable, SerializesModels;

    public $amount;
    public $user_name;
    public $product_name;

    public function __construct($amount, $user_name, $product_name)
    {
        $this->amount = $amount;
        $this->user_name = $user_name;
        $this->product_name = $product_name;
    }

    public function build()
    {
        return $this->view('emails.new_member')
            ->with([
                'amount' => $this->amount,
                'user_name' => $this->user_name,
                'product_name' => $this->product_name,
            ]);
    }
}