-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChargeController.class.php
72 lines (53 loc) · 1.9 KB
/
ChargeController.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
class ChargeController
{
public function httpGetMethod(Http $http, array $queryFields)
{
if (empty($_SESSION) == true) {
$http->redirectTo('/');
}
}
public function httpPostMethod(Http $http, array $formFields)
{
var_dump($_POST);
$orders = json_decode($_POST['orders']);
var_dump($orders);
$totalAmount = 0;
$productsModel = new ProductsModel();
foreach ($orders as $order) {
$product = $productsModel->getOneProduct($order->id);
$order->safePrice = $product['Price'];
$totalAmount += ($order->safePrice * $order->quantity);
}
//var_dump($orders);
//var_dump($totalAmount);
$errorMessage = null;
try {
\Stripe\Stripe::setApiKey('sk_test_Dxcqjz8VWJZ0TSbg6ZtyWhx100iMEAz6SA');
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
$token = $_POST['stripeToken'];
$customer = \Stripe\Customer::create(array(
"email" => $_SESSION['email'],
"source" => $token
));
$charge = \Stripe\Charge::create(array(
"amount" => $totalAmount * 100,
"currency" => "eur",
"description" => "Commande ok",
"customer" => $customer->id
));
$orderModel = new OrderModel();
// $orderModel->saveOrder($orders, $_SESSION['id'], $totalAmount);
$number = $orderModel->saveOrder($orders, $_SESSION['id'], $totalAmount);
$http->redirectTo("/payment/charge/sucess?tid='.$charge->id.'&product='.$charge->decription.'&order='.$number");
} catch (Exception $error) {
$errorMessage = "Le paiement a échoué";
if ($error->httpStatus == 402) {
$errorMessage = "Votre carte a malheureusement été refusé merci de tester une autre carte";
} else {
$errorMessage = "le paiement a échoué a malheureusement échoué, merci de tester ultérieurment";
}
return ['errorMessage' => $errorMessage];
}
}
}