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) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
|
111
|
|
|
//je recupere les informations de retour de Paypal |
112
|
|
|
$id = $request->get('paymentId'); |
113
|
|
|
// $token = $request->get('token'); |
|
|
|
|
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); |
|
|
|
|
122
|
|
|
|
123
|
|
|
// Clear the shopping cart, |
124
|
|
|
$request->session()->pull('likes', []); |
|
|
|
|
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
|
|
|
|
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.