<commit_before><?php

namespace App\Http\Controllers;

use Log;
use Bugsnag;
use App\Event;
use Validator;
use App\Payment;
use App\DuesTransaction;
use Illuminate\Http\Request;
use App\Events\PaymentSuccess;
use SquareConnect\ApiException;
use SquareConnect\Configuration;
use SquareConnect\Api\CheckoutApi;
use SquareConnect\Model\Transaction;
use SquareConnect\Api\TransactionsApi;
use SquareConnect\Model\CreateOrderRequest;
use SquareConnect\Model\CreateCheckoutRequest;
use App\Notifications\Payment\ConfirmationNotification as Confirm;

class PaymentController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:read-payments', ['only' => ['index']]);
        $this->middleware('permission:create-payments|create-payments-own', ['only' => ['store']]);
        $this->middleware('permission:read-payments|read-payments-own', ['only' => ['show']]);
        $this->middleware('permission:update-payments', ['only' => ['update']]);
        $this->middleware('permission:delete-payments', ['only' => ['destroy']]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $payments = Payment::all();

        return response()->json(['status' => 'success', 'payments' => $payments]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $currentUser = auth()->user();

        if (! $request->filled('recorded_by') ||
            $currentUser->cant('update-payments')) {
            $request['recorded_by'] = $currentUser->id;
        }

        $this->validate($request, [
            'amount' => 'required|numeric',
            'method' => 'required|string',
            'recorded_by' => 'numeric|exists:users,id',
            'payable_type' => 'required|string',
            'payable_id' => 'required|numeric',
        ]);

        try {
            $payment = Payment::create($request->all());
        } catch (QueryException $e) {
            Bugsnag::notifyException($e);
            $errorMessage = $e->errorInfo[2];

            return response()->json(['status' => 'error', 'message' => $errorMessage], 500);
        }

        if (is_numeric($payment->id)) {
            $dbPayment = Payment::findOrFail($payment->id);
            $dbPayment->payable->user->notify(new Confirm($dbPayment));
            event(new PaymentSuccess($dbPayment));

            return response()->json(['status' => 'success', 'payment' => $dbPayment], 201);<commit_msg>Require permission to accept certain payment methods<commit_after><?php

namespace App\Http\Controllers;

use Log;
use Bugsnag;
use App\Event;
use Validator;
use App\Payment;
use App\DuesTransaction;
use Illuminate\Http\Request;
use App\Events\PaymentSuccess;
use SquareConnect\ApiException;
use SquareConnect\Configuration;
use SquareConnect\Api\CheckoutApi;
use SquareConnect\Model\Transaction;
use SquareConnect\Api\TransactionsApi;
use SquareConnect\Model\CreateOrderRequest;
use SquareConnect\Model\CreateCheckoutRequest;
use App\Notifications\Payment\ConfirmationNotification as Confirm;

class PaymentController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:read-payments', ['only' => ['index']]);
        $this->middleware('permission:create-payments|create-payments-own', ['only' => ['store']]);
        $this->middleware('permission:read-payments|read-payments-own', ['only' => ['show']]);
        $this->middleware('permission:update-payments', ['only' => ['update']]);
        $this->middleware('permission:delete-payments', ['only' => ['destroy']]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $payments = Payment::all();

        return response()->json(['status' => 'success', 'payments' => $payments]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $currentUser = auth()->user();

        if (! $request->filled('recorded_by') ||
            $currentUser->cant('update-payments')) {
            $request['recorded_by'] = $currentUser->id;
        }

        $this->validate($request, [
            'amount' => 'required|numeric',
            'method' => 'required|string|in:cash,check,swipe,square,squarecash',
            'recorded_by' => 'numeric|exists:users,id',
            'payable_type' => 'required|string',
            'payable_id' => 'required|numeric',
        ]);

        if ($user->cant('create-payment-' . $request->input('method'))) {
            return response()->json(['status' => 'error', 'message' =>
                'Forbidden - you do not have permission to use accept payment method'], 403);
        }

        try {
            $payment = Payment::create($request->all());
        } catch (QueryException $e) {
            Bugsnag::notifyException($e);
            $errorMessage = $e->errorInfo[2];

            return response()->json(['status' => 'error', 'message' => $errorMessage], 500);
        }

        if (is_numeric($payment->id)) {
            $dbPayment = Payment::findOrFail($payment->id);
            $dbPayment->payable->user->notify(new Confirm($dbPayment));
            event(new PaymentSuccess($dbPayment));

            return response()->json(['status' => 'success', 'payment' => $dbPayment], 201);
{'fraction_non_alphanumeric': 0.1627088830255057, 'fraction_numerical': 0.002990325417766051, 'mean_word_length': 3.0269121813031163, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, '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': '1447570', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1585, 'n_words': 304}