CMXX - Webhooks and giftcards
This commit is contained in:
173
webhook.php
173
webhook.php
@@ -2,18 +2,26 @@
|
||||
//Define security for webhook -> factuur
|
||||
define('interface', true);
|
||||
|
||||
// Include the configuration file, this contains settings you can change.
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Includes
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
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
|
||||
*/
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//LOGIN TO API
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$data = json_encode(array("clientID" => clientID, "clientsecret" => clientsecret), JSON_UNESCAPED_UNICODE);
|
||||
$responses = ioAPIv2('/v2/authorization', $data,'');
|
||||
//Decode Payload
|
||||
if (!empty($responses)){$responses = json_decode($responses,true);}else{$responses = '400';}
|
||||
$clientsecret = $responses['token'];
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// BASEURL is required for invoice template
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$base_url = 'https://'.$_SERVER['SERVER_NAME'].'/';
|
||||
define('base_url', $base_url);
|
||||
|
||||
try {
|
||||
/*
|
||||
@@ -23,94 +31,121 @@ try {
|
||||
*/
|
||||
require "initialize.php";
|
||||
|
||||
/*
|
||||
* Retrieve the payment's current state.tr_ZFpQZZMZ76
|
||||
*/
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//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 = ?');
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Update the order in the database.
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
|
||||
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();
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//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.
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//UPDATE THE PAYMENT STATUS
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$payload = json_encode(array("txn_id" => $orderId, "payment_status" => 1, "giftcard_categoryID" => giftcard_id), JSON_UNESCAPED_UNICODE);
|
||||
$transaction = ioAPIv2('/v2/transactions/',$payload,$clientsecret);
|
||||
$transaction = json_decode($transaction,true);
|
||||
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//Send to PHPMailer
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
send_mail_by_PHPMailer($customer_email, $subject, $data, $attachment, $subject);
|
||||
if ($transaction !== null && !empty($transaction)) {
|
||||
|
||||
if(count($transaction) > 0) {
|
||||
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//Generate INVOICE RECORD
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$payload = json_encode(array("txn_id" => $transaction['transaction_id']), JSON_UNESCAPED_UNICODE);
|
||||
$invoice = ioAPIv2('/v2/invoice/',$payload,$clientsecret);
|
||||
$invoice = json_decode($invoice,true);
|
||||
|
||||
if ($invoice !== null && !empty($invoice)) {
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//Generate INVOICE TO CUSTOMER
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$invoice_cust = ioAPIv2('/v2/invoice/list=invoice&id='.$invoice['invoice_id'],'',$clientsecret);
|
||||
$invoice_cust = json_decode($invoice_cust,true);
|
||||
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//Send the invoice when status is Paid
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
list($data,$customer_email,$order_id) = generateInvoice($invoice_cust,$orderId);
|
||||
|
||||
//CREATE PDF
|
||||
$dompdf->loadHtml($data);
|
||||
// (Optional) Setup the paper size and orientation
|
||||
$dompdf->setPaper('A4', 'portrait');
|
||||
|
||||
// Render the HTML as PDF
|
||||
$dompdf->render();
|
||||
$subject = ($invoice_morval_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);
|
||||
if(invoice_bookkeeping){
|
||||
send_mail_by_PHPMailer(email_bookkeeping, $subject, $data, $attachment, $subject);
|
||||
}
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
}
|
||||
}
|
||||
}
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//
|
||||
|
||||
} elseif ($payment->isOpen()) {
|
||||
/*
|
||||
* The payment is open.
|
||||
* The payment is open. status = Pending = 101
|
||||
*/
|
||||
$stmt->execute(["Pending", $orderId]);
|
||||
$payload = json_encode(array("txn_id" => $orderId, "payment_status" => 101), JSON_UNESCAPED_UNICODE);
|
||||
$transaction = ioAPIv2('/v2/transactions/',$payload,$clientsecret);
|
||||
|
||||
} elseif ($payment->isPending()) {
|
||||
/*
|
||||
* The payment is pending.
|
||||
* The payment is pending.status = Pending = 101
|
||||
*/
|
||||
$stmt->execute(["Pending", $orderId]);
|
||||
$payload = json_encode(array("txn_id" => $orderId, "payment_status" => 101), JSON_UNESCAPED_UNICODE);
|
||||
$transaction = ioAPIv2('/v2/transactions/',$payload,$clientsecret);
|
||||
|
||||
} elseif ($payment->isFailed()) {
|
||||
/*
|
||||
* The payment has failed.
|
||||
* The payment has failed.status = Failed = 102
|
||||
*/
|
||||
$stmt->execute(["Failed", $orderId]);
|
||||
$payload = json_encode(array("txn_id" => $orderId, "payment_status" => 102), JSON_UNESCAPED_UNICODE);
|
||||
$transaction = ioAPIv2('/v2/transactions/',$payload,$clientsecret);
|
||||
|
||||
} elseif ($payment->isExpired()) {
|
||||
/*
|
||||
* The payment is expired.
|
||||
* The payment is expired.status = Expired= 103
|
||||
*/
|
||||
$stmt->execute(["Pending", $orderId]);
|
||||
$payload = json_encode(array("txn_id" => $orderId, "payment_status" => 103), JSON_UNESCAPED_UNICODE);
|
||||
$transaction = ioAPIv2('/v2/transactions/',$payload,$clientsecret);
|
||||
|
||||
} elseif ($payment->isCanceled()) {
|
||||
/*
|
||||
* The payment has been canceled.
|
||||
* The payment has been status = Cancelled= 103
|
||||
*/
|
||||
$stmt->execute(["Cancelled", $orderId]);
|
||||
$payload = json_encode(array("txn_id" => $orderId, "payment_status" => 999), JSON_UNESCAPED_UNICODE);
|
||||
$transaction = ioAPIv2('/v2/transactions/',$payload,$clientsecret);
|
||||
|
||||
} elseif ($payment->hasRefunds()) {
|
||||
/*
|
||||
* The payment has been (partially) refunded.
|
||||
* The status of the payment is still "paid"
|
||||
* status = Paid = 1
|
||||
*/
|
||||
$stmt->execute(["Refunded", $orderId]);
|
||||
|
||||
$payload = json_encode(array("txn_id" => $orderId, "payment_status" => 1), JSON_UNESCAPED_UNICODE);
|
||||
$transaction = ioAPIv2('/v2/transactions/',$payload,$clientsecret);
|
||||
|
||||
//Order is refunded - disable giftcards
|
||||
useGiftCart($pdo, $orderId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user