Completed
Push — master ( d1ecb8...6399e4 )
by Julien
03:46 queued 23s
created

CartController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.4286
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
// chemin relatif ou se trouve la classe
3
namespace App\Http\Controllers;
4
5
use App\Http\Models\Administrators;
6
use App\Http\Models\Movies;
7
use App\Http\Requests\AdministratorsRequest;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Redirect;
11
use Illuminate\Support\Facades\Session;
12
use Netshell\Paypal\Facades\Paypal;
13
14
/**
15
 * Class CartController
16
 * To handle checkout
17
 */
18
class CartController extends Controller{
19
20
21
    /**
22
     * @var Api Paypal
23
     */
24
    private $_apiContext;
25
26
    /**
27
     * Constructor for initialize Paypal
28
     */
29
    public function __construct()
30
    {
31
        $this->_apiContext = Paypal::ApiContext(
32
            config('services.paypal.client_id'),
33
            config('services.paypal.secret')
34
        );
35
36
        $this->_apiContext->setConfig(array(
37
            'mode' => 'sandbox',
38
            'service.EndPoint' => 'https://api.sandbox.paypal.com',
39
            'http.ConnectionTimeOut' => 30,
40
            'log.LogEnabled' => true,
41
            'log.FileName' => storage_path('logs/paypal.log'),
42
            'log.LogLevel' => 'FINE'
43
        )
44
        );
45
46
    }
47
48
49
    /**
50
     * Payments
51
     */
52
    public function checkout(){
53
54
        $ids = session('likes', []);
55
56
        $total = 0;
57
        foreach($ids as $id){
58
            $movie = Movies::find($id);
59
            $total = $total + $movie->price;
60
        }
61
62
        $payer = PayPal::Payer();
63
        $payer->setPaymentMethod('paypal');
64
65
        $amount = PayPal::Amount();
66
        $amount->setCurrency('EUR');
67
        $amount->setTotal($total);
68
69
        $transaction = PayPal::Transaction();
70
        $transaction->setAmount($amount);
71
        $transaction->setDescription("Récapitulatif total des ".count($ids). " films commandés");
72
73
        $redirectUrls = PayPal::RedirectUrls();
74
        $redirectUrls->setReturnUrl(route('cart_done'));
75
        $redirectUrls->setCancelUrl(route('cart_cancel'));
76
77
        $payment = PayPal::Payment();
78
        $payment->setIntent('sale');
79
        $payment->setPayer($payer);
80
        $payment->setRedirectUrls($redirectUrls);
81
        $payment->setTransactions(array($transaction));
82
83
        //response de Paypal
84
        $response = $payment->create($this->_apiContext);
85
86
        $redirectUrl = $response->links[1]->href;
87
88
        //redirect to Plateform Paypal
89
        return Redirect::to( $redirectUrl);
90
91
92
93
94
    }
95
96
97
    /**
98
     * Payments Récapitilatif
99
     */
100
    public function recapitulatif(){
101
102
        return view("Cart/recapitulatif");
103
    }
104
105
    /**
106
     * Done
107
     */
108 View Code Duplication
    public function done(Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
110
        //je recupere les informations de retour de Paypal
111
        $id = $request->get('paymentId');
112
        $token = $request->get('token');
0 ignored issues
show
Unused Code introduced by
$token is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
113
        $payer_id = $request->get('PayerID');
114
115
        $payment = PayPal::getById($id, $this->_apiContext);
116
117
        $paymentExecution = PayPal::PaymentExecution();
118
119
        //execution du paiment a partir du Payer
120
        //Requete à Paypal: débit du montant de a transaction au Payer
121
        $paymentExecution->setPayerId($payer_id);
122
        $executePayment = $payment->execute($paymentExecution, $this->_apiContext);
0 ignored issues
show
Unused Code introduced by
$executePayment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
123
124
        // Clear the shopping cart,
125
        $request->session()->pull('likes', []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
127
        // Write database
128
129
130
131
132
        // Thank the user for the purchase
133
        return view('Cart/success');
134
135
    }
136
137
138
    /**
139
     * Cancel
140
     */
141
    public function cancel(){
142
143
    }
144
145
146
}
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165