Files
Commerce/webhook_paypal.php
“VeLiTi” 6f1cc27ec4 Initial commit
2025-01-30 11:43:37 +01:00

100 lines
3.3 KiB
PHP

<?php
// Include the configuration file, this contains settings you can change.
include 'config.php';
// Include functions and connect to the database using PDO MySQL
include 'functions.php';
// Connect to MySQL database
$pdo = pdo_connect_mysql();
// paypal
require_once __DIR__."/lib/paypal/paypal.php";
//error reporting
if (debug){
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}
//GET PAYPAL TOKEN
$token = $_GET["token"] ?? '';
$base_url = 'https://'.$_SERVER['SERVER_NAME'].'/';
define('base_url', $base_url);
//IF TOKEN IS RETURNED PROCES IT
if($token !=''){
$base = PAYPAL_URL;
$id = PAYPAL_CLIENT_ID;
$secret = PAYPAL_CLIENT_SECRET;
//CHECK PAYPAL
$paypal = new paypalCurl();
$paypal -> init($id,$secret,$base);
$result = $paypal->verify($token);
//GET RELATED ORDER
$orderId = $result->txn ?? '';
//IF TXN_ID is not empty
if ($orderId !='' && $result->ref == 'COMPLETED'){
//CHECK IF ORDER EXISTS
$stmt = $pdo->prepare('SELECT * FROM transactions WHERE txn_id = ?');
$stmt->execute([$orderId]);
if ($stmt->fetch(PDO::FETCH_ASSOC)){
//TXN EXISTS - UPDATE TO PAID
$stmt = $pdo->prepare('UPDATE transactions SET payment_status = ? WHERE txn_id = ?');
$stmt->execute(["Paid", $orderId]);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Order is Paid. Create Giftcards when applicable
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
createGiftCart($pdo, $orderId);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Send the invoice when status is Paid
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
list($data,$customer_email,$order_id) = generateInvoice($pdo,$orderId);
$dompdf->loadHtml($data);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
$subject = 'MorvalWatches - Invoice: '.$order_id;
$attachment = $dompdf->output();
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Send to PHPMailer
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
send_mail_by_PHPMailer($customer_email, $subject, $data, $attachment, $subject);
if(invoice_bookkeeping){
send_mail_by_PHPMailer(email_bookkeeping, $subject, $data, $attachment, $subject);
}
//REDIRECT TO PLACEORDER SCREEN
header('Location: ' . url('index.php?page=placeorder'));
exit;
}
} else {
//GET TXN FROM RETURN LINK
$orderId = $_GET['txn'] ?? '';
//Cancel Order
$stmt = $pdo->prepare('UPDATE transactions SET payment_status = ? WHERE txn_id = ?');
$stmt->execute(["Cancelled", $orderId]);
//Redirect back to checkout
header('Location: ' . url('index.php?page=cart'));
exit;
}
} else {
//Redirect back to home
header('Location: ' . url('index.php?page=home'));
exit;
}
?>