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; } ?>