source: app/Http/Controllers/TransactionController.php@ dfae77e

Last change on this file since dfae77e was dfae77e, checked in by Igor Danilovski <igor_danilovski@…>, 2 years ago
  • Initial commit;
  • Property mode set to 100644
File size: 3.3 KB
Line 
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Enum\OfferStatus;
6use App\Enum\UserType;
7use App\Models\Offer;
8use App\Models\Transaction;
9use Illuminate\Contracts\Foundation\Application;
10use Illuminate\Contracts\View\Factory;
11use Illuminate\Contracts\View\View;
12use Illuminate\Http\Request;
13use Illuminate\Support\Facades\Auth;
14use Illuminate\Support\Str;
15
16class 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}
Note: See TracBrowser for help on using the repository browser.