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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| <?php
session_start();
try
{
$db = new PDO('mysql:host=localhost;dbname=site-e-commerce', 'root','');
$db ->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); //les noms des champs seront en caractère minuscule.
$db ->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);// les erreurs lanceront des exceptions.
}
catch(Exception $e){
die('Une erreur est survenue');
}
require_once('includes/functions_panier.php');
require_once('includes/paypal.php');
$totaltva = MontantGlobalTva();
$paypal = new Paypal();
$response = $paypal->request('GetExpressCheckoutDetails', array(
'TOKEN' => $_GET['token']
));
if($response){
if($response['CHECKOUTSTATUS'] =='PaymentActionCompleted'){
header('Location: error.php');
}
}else{
var_dump($paypal->errors);
die();
}
$response = $paypal->request('DoExpressCheckoutPayment', array(
'TOKEN' => $_GET['token'],
'PAYERID' => $_GET['PayerID'],
'PAYMENTACTION'=>'Sale',
'PAYMENTREQUEST_0_AMT'=>$totaltva,
'PAYMENTREQUEST_0_CURRENCYCODE'=>'EUR'
));
if($response){
$response2 = $paypal->request('GetTransactionDetails', array(
'TRANSACTIONID' => $response['PAYMENTINFO_0_TRANSACTIONID']
));
$name = $response2['SHIPTONAME'];
$street = $response2['SHIPTOSTREET'];
$city = $response2['SHIPTOCITY'];
$country = $response2['SHIPTOSTATE'];
$date = $response2['ORDERTIME'];
$transaction_id = $response2['TRANSACTIONID'];
$amount = $response2['AMT'];
$shipping = $response2['FEEAMT'];
$currency_code = $response2['CURRENCYCODE'];
$user_id = $_SESSION['user_id'];
$db->query("INSERT INTO transactions(name,street,city,country,date,transaction_id,amount,shipping,currency_code,user_id) VALUES('$name', '$street', '$city', '$country', '$date', '$transaction_id', '$amount', '$shipping', '$currency_code', '$user_id')");
for ($i=0; $i <count($_SESSION['panier']['libelleProduit']); $i++) {
$product = $_SESSION['panier']['libelleProduit'][$i];
$quantity = $_SESSION['panier']['qteProduit'][$i];
$insert = $db->query("INSERT INTO products_transactions(product,quantity,transaction_id) VALUES('$product','$quantity','$transaction_id')");
$select = $db->query("SELECT * FROM products WHERE title='$product'");
$r = $select->fetch(PDO::FETCH_OBJ);
$stock = $r->stock;
$stock = $stock-$quantity;
$update = $db->query("UPDATE products SET stock='$stock' WHERE title='$product'");
}
header('Location: success.php');
}else{
var_dump($paypal->errors);
die();
}
?> |
Partager