Files
Commerce/webhook_paypal.php
2025-02-19 12:11:44 +01:00

121 lines
4.7 KiB
PHP

<?php
// 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';
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
//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";
//error reporting
if (debug){
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}
//GET PAYPAL TOKEN
$token = $_GET["token"] ?? '';
$base_url = 'https://'.$_SERVER['SERVER_NAME'].'/';
define('base_url', $base_url);
//IF TOKEN IS RETURNED PROCES IT
if($token !=''){
$base = PAYPAL_URL;
$id = PAYPAL_CLIENT_ID;
$secret = PAYPAL_CLIENT_SECRET;
//CHECK PAYPAL
$paypal = new paypalCurl();
$paypal -> 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'){
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
//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
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
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);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
}
}
} 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;
}
?>