163 lines
7.2 KiB
PHP
163 lines
7.2 KiB
PHP
<?php
|
|
//Define security for webhook -> factuur
|
|
define('interface', true);
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// Includes
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
include './custom/settings/config.php';
|
|
include './functions.php';
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//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 {
|
|
/*
|
|
* 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.
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
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.
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//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);
|
|
|
|
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
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
$invoice_language = strtoupper($invoice_cust['customer']['language']!= '' ? $invoice_cust['customer']['language'] : $responses['language']);
|
|
|
|
list($data,$customer_email,$order_id) = generateInvoice($invoice_cust,$orderId,$invoice_language);
|
|
|
|
//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);
|
|
}
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
}
|
|
}
|
|
}
|
|
|
|
} elseif ($payment->isOpen()) {
|
|
/*
|
|
* The payment is open. status = Pending = 101
|
|
*/
|
|
$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.status = Pending = 101
|
|
*/
|
|
$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.status = Failed = 102
|
|
*/
|
|
$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.status = Expired= 103
|
|
*/
|
|
$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 status = Cancelled= 103
|
|
*/
|
|
$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
|
|
*/
|
|
$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);
|
|
|
|
} 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());
|
|
}
|