CartController::checkout()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 39
rs 8.8571
cc 2
eloc 25
nc 2
nop 0
1
<?php
2
3
// chemin relatif ou se trouve la classe
4
5
namespace App\Http\Controllers;
6
7
use App\Http\Models\Movies;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\App;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Redirect;
12
use Netshell\Paypal\Facades\Paypal;
13
14
/**
15
 * Class CartController
16
 * To handle checkout.
17
 */
18
class CartController extends Controller
19
{
20
    /**
21
     * @var Api Paypal
22
     */
23
    private $_apiContext;
24
25
    /**
26
     * @var
27
     */
28
    private $cart;
29
30
    /**
31
     * Constructor for initialize Paypal.
32
     */
33
    public function __construct()
34
    {
35
        // Get Cart in Container
36
        $this->cart = App::make('App\Http\Cart\Cart');
37
38
        $this->_apiContext = Paypal::ApiContext(
39
            config('services.paypal.client_id'),
40
            config('services.paypal.secret')
41
        );
42
43
        $this->_apiContext->setConfig([
44
            'mode'                   => 'sandbox',
45
            'service.EndPoint'       => 'https://api.sandbox.paypal.com',
46
            'http.ConnectionTimeOut' => 30,
47
            'log.LogEnabled'         => true,
48
            'log.FileName'           => storage_path('logs/paypal.log'),
49
            'log.LogLevel'           => 'FINE',
50
        ]
51
        );
52
    }
53
54
    /**
55
     * Payments.
56
     */
57
    public function checkout()
58
    {
59
        $ids = session('likes', []);
60
61
        $total = 0;
62
        foreach ($ids as $id) {
63
            $movie = Movies::find($id);
64
            $total = $total + $movie->price;
65
        }
66
67
        $payer = PayPal::Payer();
68
        $payer->setPaymentMethod('paypal');
69
70
        $amount = PayPal::Amount();
71
        $amount->setCurrency('EUR');
72
        $amount->setTotal($total);
73
74
        $transaction = PayPal::Transaction();
75
        $transaction->setAmount($amount);
76
        $transaction->setDescription('Récapitulatif total des '.count($ids).' films commandés');
77
78
        $redirectUrls = PayPal::RedirectUrls();
79
        $redirectUrls->setReturnUrl(route('cart_done'));
80
        $redirectUrls->setCancelUrl(route('cart_cancel'));
81
82
        $payment = PayPal::Payment();
83
        $payment->setIntent('sale');
84
        $payment->setPayer($payer);
85
        $payment->setRedirectUrls($redirectUrls);
86
        $payment->setTransactions([$transaction]);
87
88
        //response de Paypal
89
        $response = $payment->create($this->_apiContext);
90
91
        $redirectUrl = $response->links[1]->href;
92
93
        //redirect to Plateform Paypal
94
        return Redirect::to($redirectUrl);
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
111
        //je recupere les informations de retour de Paypal
112
        $id = $request->get('paymentId');
113
//        $token = $request->get('token');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
        $payer_id = $request->get('PayerID');
115
        $payment = PayPal::getById($id, $this->_apiContext);
116
117
        $paymentExecution = PayPal::PaymentExecution();
118
        //execution du paiment a partir du Payer
119
        //Requete à Paypal: débit du montant de a transaction au Payer
120
        $paymentExecution->setPayerId($payer_id);
121
        $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...
122
123
        // Clear the shopping cart,
124
        $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...
125
126
        //write log
127
        Log::info('Un client vient de passer uen commande via Paypal'.$payer_id);
128
129
        // Write database
130
131
        // Thank the user for the purchase
132
        return view('Cart/success');
133
    }
134
135
    /**
136
     * Cancel.
137
     */
138
    public function cancel()
139
    {
140
    }
141
}
142