<commit_before><?php

namespace App\Http\Controllers\Api;

use App\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Billing\GatewayInterface;

class OrdersController extends Controller
{
  public function __construct()
  {
    $this->middleware('admin', ['except' => 'complete']);
  }

  public function update(Order $order, Request $request)
  {
    $order->update($request->all());

    return $order;
  }

  /**
   * Handle a webhook payload to complete an order
   */
  public function complete(Request $request, GatewayInterface $gateway)
  {
    $sig_header = $request->header('stripe-signature');
    $payload = $request->getContent();

    if (!$sig_header) {
      abort(401);
    }
    try {
      $session = $gateway->getSessionFromEvent($payload, $sig_header);
      $order = Order::findOrFail($session->client_reference_id);
      \Log::info(sprintf("Gateway webhook recieved for order %s", $session->client_reference_id), [
        'order_id' => $session->client_reference_id,
      ]);

      event(new \App\Events\OrderWasPaid($order, $session->payment_intent));
      if ($session->customer && !$order->user->billing_id) {
        $order->user->billing_id = $session->customer;
        $order->user->save();
      }
    } catch (\UnexpectedValueException $e) {
      \Log::warning("Gateway webhook failed due to invalid payload", [
        'order_id' => $session->client_reference_id,
        'error' => $e,
      ]);
      return response()->json('Invalid payload', 400);
    } catch (\Stripe\Error\SignatureVerification $e) {
      \Log::warning("Gateway webhook failed due to invalid signature", [
        'order_id' => $session->client_reference_id,
        'error' => $e,
      ]);
      return response()->json('Invalid signature', 400);
    }

    return response()->json('Received', 200);
  }
}<commit_msg>Fix stripe webook error handling<commit_after><?php

namespace App\Http\Controllers\Api;

use App\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Billing\GatewayInterface;

class OrdersController extends Controller
{
  public function __construct()
  {
    $this->middleware('admin', ['except' => 'complete']);
  }

  public function update(Order $order, Request $request)
  {
    $order->update($request->all());

    return $order;
  }

  /**
   * Handle a webhook payload to complete an order
   */
  public function complete(Request $request, GatewayInterface $gateway)
  {
    $sig_header = $request->header('stripe-signature');
    $payload = $request->getContent();

    if (!$sig_header) {
      abort(401);
    }
    try {
      $session = $gateway->getSessionFromEvent($payload, $sig_header);
      $order = Order::findOrFail($session->client_reference_id);
      \Log::info(sprintf("Gateway webhook received for order %s", $session->client_reference_id), [
        'order_id' => $session->client_reference_id,
      ]);

      event(new \App\Events\OrderWasPaid($order, $session->payment_intent));
      if ($session->customer && !$order->user->billing_id) {
        $order->user->billing_id = $session->customer;
        $order->user->save();
      }
    } catch (\UnexpectedValueException $e) {
      \Log::warning("Gateway webhook failed due to invalid payload", [
        'error' => $e,
      ]);
      return response()->json('Invalid payload', 400);
    } catch (\Stripe\Error\SignatureVerification $e) {
      \Log::warning("Gateway webhook failed due to invalid signature", [
        'error' => $e,
      ]);
      return response()->json('Invalid signature', 400);
    }

    return response()->json('Received', 200);
  }
}
{'fraction_non_alphanumeric': 0.15718604012091233, 'fraction_numerical': 0.006595218466611707, 'mean_word_length': 3.412121212121212, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7140941', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1088, 'n_words': 228}