125 lines
4.1 KiB
PHP
125 lines
4.1 KiB
PHP
<?php
|
|
//Define security for webhook -> factuur
|
|
define('interface', true);
|
|
|
|
// Include the configuration file, this contains settings you can change.
|
|
include '/custom/settings/config.php';
|
|
// Include functions and connect to the database using PDO MySQL
|
|
include 'functions.php';
|
|
// Connect to MySQL database
|
|
$pdo = pdo_connect_mysql();
|
|
|
|
/*
|
|
* How to verify Mollie API Payments in a webhook.
|
|
*
|
|
* See: https://docs.mollie.com/guides/webhooks
|
|
*/
|
|
|
|
try {
|
|
/*
|
|
* Initialize the Mollie API library with your API key.
|
|
*
|
|
* See: https://www.mollie.com/dashboard/developers/api-keys
|
|
*/
|
|
require "initialize.php";
|
|
|
|
/*
|
|
* Retrieve the payment's current state.tr_ZFpQZZMZ76
|
|
*/
|
|
$payment = $mollie->payments->get($_POST["id"]);
|
|
$orderId = $payment->metadata->order_id;
|
|
|
|
/*
|
|
* Update the order in the database.
|
|
*/
|
|
// database_write($orderId, $payment->status);
|
|
|
|
// Update order_status to Paid
|
|
$stmt = $pdo->prepare('UPDATE transactions SET payment_status = ? WHERE txn_id = ?');
|
|
|
|
|
|
if ($payment->isPaid() && ! $payment->hasRefunds() && ! $payment->hasChargebacks()) {
|
|
/*
|
|
* The payment is paid and isn't refunded or charged back.
|
|
* At this point you'd probably want to start the process of delivering the product to the customer.
|
|
*/
|
|
$stmt->execute(["Paid", $orderId]);
|
|
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//Order is Paid. Create Giftcards when applicable
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
createGiftCart($pdo, $orderId);
|
|
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//Send the invoice when status is Paid
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
$base_url = 'https://'.$_SERVER['SERVER_NAME'].'/';
|
|
define('base_url', $base_url);
|
|
|
|
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);
|
|
}
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//
|
|
} elseif ($payment->isOpen()) {
|
|
/*
|
|
* The payment is open.
|
|
*/
|
|
$stmt->execute(["Pending", $orderId]);
|
|
} elseif ($payment->isPending()) {
|
|
/*
|
|
* The payment is pending.
|
|
*/
|
|
$stmt->execute(["Pending", $orderId]);
|
|
} elseif ($payment->isFailed()) {
|
|
/*
|
|
* The payment has failed.
|
|
*/
|
|
$stmt->execute(["Failed", $orderId]);
|
|
} elseif ($payment->isExpired()) {
|
|
/*
|
|
* The payment is expired.
|
|
*/
|
|
$stmt->execute(["Pending", $orderId]);
|
|
} elseif ($payment->isCanceled()) {
|
|
/*
|
|
* The payment has been canceled.
|
|
*/
|
|
$stmt->execute(["Cancelled", $orderId]);
|
|
} elseif ($payment->hasRefunds()) {
|
|
/*
|
|
* The payment has been (partially) refunded.
|
|
* The status of the payment is still "paid"
|
|
*/
|
|
$stmt->execute(["Refunded", $orderId]);
|
|
|
|
//Order is refunded - disable giftcards
|
|
useGiftCart($pdo, $orderId);
|
|
|
|
} elseif ($payment->hasChargebacks()) {
|
|
/*
|
|
* The payment has been (partially) charged back.
|
|
* The status of the payment is still "paid"
|
|
*/
|
|
}
|
|
} catch (\Mollie\Api\Exceptions\ApiException $e) {
|
|
echo "API call failed: " . htmlspecialchars($e->getMessage());
|
|
} |