1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers;
|
---|
4 |
|
---|
5 | use App\Enum\OfferStatus;
|
---|
6 | use App\Enum\UserType;
|
---|
7 | use App\Models\Offer;
|
---|
8 | use App\Models\Transaction;
|
---|
9 | use Illuminate\Contracts\Foundation\Application;
|
---|
10 | use Illuminate\Contracts\View\Factory;
|
---|
11 | use Illuminate\Contracts\View\View;
|
---|
12 | use Illuminate\Http\Request;
|
---|
13 | use Illuminate\Support\Facades\Auth;
|
---|
14 | use Illuminate\Support\Str;
|
---|
15 |
|
---|
16 | class TransactionController extends Controller
|
---|
17 | {
|
---|
18 | //
|
---|
19 | public function __construct()
|
---|
20 | {
|
---|
21 | $this->middleware(['auth', 'verified', 'onboarding']);
|
---|
22 | $this->middleware(['role:artist,organizer'])->only('index');
|
---|
23 | $this->middleware(['role:organizer'])->except('index');
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * @return Application|Factory|View
|
---|
28 | */
|
---|
29 | public function index()
|
---|
30 | {
|
---|
31 | $payments = null;
|
---|
32 |
|
---|
33 | switch (Auth::user()->type) {
|
---|
34 | case UserType::ORGANIZER->value:
|
---|
35 | $organizerPayments = Transaction::select('transactions.*')
|
---|
36 | ->join('offers', 'offers.id', '=', 'transactions.offer_id')
|
---|
37 | ->join('events', 'events.id', '=', 'offers.event_id')
|
---|
38 | ->join('organizers', 'organizers.user_id', '=', 'events.organizer_id')
|
---|
39 | ->where('organizers.user_id', Auth::id())
|
---|
40 | ->orderBy('transactions.created_at', 'desc')
|
---|
41 | ->get();
|
---|
42 |
|
---|
43 | $payments = $organizerPayments;
|
---|
44 | break;
|
---|
45 |
|
---|
46 | case UserType::ARTIST->value:
|
---|
47 | $artistPayments = Transaction::select('transactions.*')
|
---|
48 | ->join('offers', 'offers.id', '=', 'transactions.offer_id')
|
---|
49 | ->where('offers.artist_id', Auth::id())
|
---|
50 | ->orderBy('transactions.created_at', 'desc')
|
---|
51 | ->get();
|
---|
52 |
|
---|
53 | $payments = $artistPayments;
|
---|
54 | break;
|
---|
55 | }
|
---|
56 |
|
---|
57 | return view('web.payments.payments')
|
---|
58 | ->with('payments', $payments);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public function purchase($id, Request $request)
|
---|
62 | {
|
---|
63 | $user = Auth::user();
|
---|
64 | $paymentMethod = $request->input('payment_method');
|
---|
65 | $offer = Offer::where('id', $id)->firstOrFail();
|
---|
66 |
|
---|
67 | try {
|
---|
68 | $user->createOrGetStripeCustomer();
|
---|
69 | $user->updateDefaultPaymentMethod($paymentMethod);
|
---|
70 |
|
---|
71 | // Use the line below to charge the card
|
---|
72 | //$user->charge($offer->price * 100, $paymentMethod);
|
---|
73 |
|
---|
74 | // Use the line below to make payment and issue invoice
|
---|
75 | $invoice = $user->invoiceFor($offer->event->title .' - ' . $offer->artist->user->name, $offer->price * 100);
|
---|
76 | } catch (\Exception $e) {
|
---|
77 | return back()
|
---|
78 | ->with('error', $e->getMessage());
|
---|
79 | }
|
---|
80 |
|
---|
81 | $transaction = new Transaction();
|
---|
82 | $transaction->name = 't-' . Str::random(25);
|
---|
83 | $transaction->stripe_id = $user->asStripeCustomer()->id;
|
---|
84 | $transaction->stripe_price = $offer->price;
|
---|
85 | $transaction->invoice_id = $invoice->id;
|
---|
86 | $transaction->offer()->associate($offer);
|
---|
87 |
|
---|
88 | try {
|
---|
89 | $transaction->save();
|
---|
90 | } catch (\Exception $e) {
|
---|
91 | return back()
|
---|
92 | ->with('error', $e->getMessage());
|
---|
93 | }
|
---|
94 |
|
---|
95 | $offer->status = OfferStatus::COMPLETED;
|
---|
96 | $offer->save();
|
---|
97 |
|
---|
98 |
|
---|
99 | return back()
|
---|
100 | ->with('success', 'Successfully paid! Hope you have a great event!');
|
---|
101 |
|
---|
102 | }
|
---|
103 | }
|
---|