PaymentProviderController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 31
c 3
b 0
f 0
dl 0
loc 61
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A paymentSuccessful() 0 12 2
A paymentTimedOut() 0 3 1
A paymentAborted() 0 4 1
A payPalExecutePayment() 0 30 4
1
<?php
2
3
namespace App\Http\Controllers\TicketShop;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use App\Purchase;
8
use App\Exceptions\PaymentProviderException;
9
use Illuminate\Support\Facades\Mail;
10
use App\Mail\TicketsPaid;
11
12
use PayPal\Exception\PayPalConnectionException;
13
use Illuminate\Support\Facades\Log;
14
15
class PaymentProviderController extends Controller
16
{
17
18
    /**
19
     * Success-Function for users that paid at a payment provider for their purchase
20
     */
21
    public function paymentSuccessful(Purchase $purchase, string $secret)
22
    {
23
        try {
24
            $purchase->setStateToPaid($secret);
25
        } catch (PaymentProviderException $e) {
26
            return redirect()->route('ts.overview')->with('status', $e->getMessage());
27
        }
28
29
        Mail::to($purchase->customer)->send(new TicketsPaid($purchase));
30
31
        return redirect()->route('ticket.purchase', ['purchase' => $purchase])
32
            ->with('status', 'Purchase successful - Please download your tickets.');
33
    }
34
35
    public function paymentAborted(Purchase $purchase)
36
    {
37
        $purchase->deleteWithAllData();
38
        return redirect()->route('ts.events')->with('status', 'Purchase aborted - Your previously selected tickets have been deleted.');
39
    }
40
41
    public function paymentTimedOut(Purchase $purchase)
42
    {
43
        return $this->paymentAborted($purchase);
44
    }
45
46
    public function payPalExecutePayment(Request $request, Purchase $purchase, string $secret)
47
    {
48
        $payerId = $request->query('PayerID');
49
        $paymentId = $request->query('paymentId');
50
51
        $returnable = redirect()->route('ts.overview');
52
        try {
53
            $purchase->setStateToPaid($secret);
54
55
            Mail::to($purchase->customer)->send(new TicketsPaid($purchase));
56
57
            $returnable = redirect()->route('ticket.purchase', ['purchase' => $purchase])
58
                ->with('status', 'Purchase successful - Please download your tickets.');
59
60
            $payPal = resolve('App\PaymentProvider\PayPal');
61
            // Might get risky to call --> set purchase to paid, but log the error!
62
            $payPal->executePayment($paymentId, $payerId);
63
            Log::info('[PayPal] [paymentId=' . $paymentId . '|payerId=' . $payerId . '] Payment successfully executed');
64
        } catch (PaymentProviderException $e) {
65
            $returnable->with('status', $e->getMessage());
66
            Log::warning($e);
67
        } catch (PayPalConnectionException $e) {
68
            Log::warning('[PayPal] [paymentId='. $paymentId . '|payerId=' . $payerId . '] ' . $e->getMessage());
69
            // This will print the detailed information on the exception.
70
            //REALLY HELPFUL FOR DEBUGGING
71
            Log::error($e->getData());
72
        } catch (\Exception $e) {
73
            Log::warning('[General] [paymentId='. $paymentId . '|payerId=' . $payerId . '] ' . $e->getMessage());
74
        }
75
        return $returnable;
76
    }
77
}
78