CMXX - Webhooks and giftcards

This commit is contained in:
“VeLiTi”
2025-02-19 12:11:44 +01:00
parent d85923c299
commit 0e9d133ff9
10 changed files with 358 additions and 335 deletions

View File

@@ -3,8 +3,16 @@
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();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
//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'];
// paypal
require_once __DIR__."/lib/paypal/paypal.php";
@@ -36,47 +44,60 @@ if($token !=''){
//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);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
//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);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
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);
// 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);
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'] ?? '';