Hi all,
I started to cooperate with HiPCTech. We use for our webstore Zen Cart with Super Orders module. We had a problem with "Balance Due". If someone paid through PayPal, Super Orders still showed "Balance Due" to be paid. I've found a solution to this problem and I'd like to post it here so that maybe someone can also find it useful.
Whole changes are made in "zcmanager/includes/classes/super_order.php". I modified "start()" function and added one new ("check_paypal_payments()"). What it does is it basically selects PayPal payments from "TABLE_PAYPAL" where order_id equals "$this->oID" and adds "mc_gross" value to "amount_applied". It takes only payments from "paypalwpp" or "paypal 1.3.8a" or "paypal (ipn-handler)" modules. There might be a better way to do it. Maybe just to select all except for "paypaldp". But for security I wrote it like that.
Here is the code:
Code:
function start() {
global $db;
// scrape some useful info from the record in the orders table
$order_query = $db->Execute("select * from " . TABLE_ORDERS . " where orders_id = '" . $this->oID . "'");
$this->cID = $order_query->fields['customers_id'];
$this->order_total = $order_query->fields['order_total'];
if (zen_not_null($order_query->fields['date_cancelled']) ) {
$this->status_date = $order_query->fields['date_cancelled'];
$this->status = "cancelled";
}
elseif (zen_not_null($order_query->fields['date_completed']) ) {
$this->status_date = $order_query->fields['date_completed'];
$this->status = "completed";
}
else {
$this->status_date = false;
$this->status = false;
}
// build an array to translate the payment_type codes stored in so_payments
$payment_key_query = $db->Execute("select * from " . TABLE_SO_PAYMENT_TYPES . "
where language_id = '" . $_SESSION['languages_id'] . "'
order by payment_type_full asc");
while(!$payment_key_query->EOF) {
// this array is used by the full_type() function
$this->payment_key_array[$payment_key_query->fields['payment_type_code']] = $payment_key_query->fields['payment_type_full'];
// and this one can be used to build dropdown menus
$this->payment_key[] = array('id' => $payment_key_query->fields['payment_type_code'],
'text' => $payment_key_query->fields['payment_type_full']);
$payment_key_query->MoveNext();
}
// get all payments not tied to a purchase order
$payments_query = $db->Execute("select * from " . TABLE_SO_PAYMENTS . "
where orders_id = '" . $this->oID . "'
and purchase_order_id = 0
order by date_posted asc");
if (zen_not_null($payments_query->fields['orders_id'])) {
while (!$payments_query->EOF) {
$this->payment[] = array('index' => $payments_query->fields['payment_id'],
'number' => $payments_query->fields['payment_number'],
'name' => $payments_query->fields['payment_name'],
'amount' => $payments_query->fields['payment_amount'],
'type' => $payments_query->fields['payment_type'],
'posted' => $payments_query->fields['date_posted'],
'modified' => $payments_query->fields['last_modified']);
$payments_query->MoveNext();
}
}
else {
unset($this->payment);
$this->payment = false;
}
// get all the purchase orders for this order
$purchase_order_query = $db->Execute("select * from " . TABLE_SO_PURCHASE_ORDERS . "
where orders_id = '" . $this->oID . "'
order by date_posted asc");
if (zen_not_null($purchase_order_query->fields['orders_id'])) {
while (!$purchase_order_query->EOF) {
$this->purchase_order[] = array('index' => $purchase_order_query->fields['purchase_order_id'],
'number' => $purchase_order_query->fields['po_number'],
'posted' => $purchase_order_query->fields['date_posted'],
'modified' => $purchase_order_query->fields['last_modified']);
$purchase_order_query->MoveNext();
}
}
else {
unset($this->purchase_order);
$this->purchase_order = false;
}
// get any payments that are tied to a purchase order
if($this->purchase_order) { // need a po before you can have po payments
for($i = 0; $i < sizeof($this->purchase_order); $i++) {
$this_po_id = $this->purchase_order[$i]['index'];
$po_payments_query = $db->Execute("select * from " . TABLE_SO_PAYMENTS . "
where purchase_order_id = '" . $this_po_id . "'
order by date_posted asc");
if (zen_not_null($po_payments_query->fields['orders_id'])) {
while (!$po_payments_query->EOF) {
$this->po_payment[] = array('index' => $po_payments_query->fields['payment_id'],
'assigned_po' => $this_po_id,
'number' => $po_payments_query->fields['payment_number'],
'name' => $po_payments_query->fields['payment_name'],
'amount' => $po_payments_query->fields['payment_amount'],
'type' => $po_payments_query->fields['payment_type'],
'posted' => $po_payments_query->fields['date_posted'],
'modified' => $po_payments_query->fields['last_modified']);
$po_payments_query->MoveNext();
}
}
}
if (sizeof($this->po_payment) < 1) {
unset($this->po_payment);
$this->po_payment = false;
}
}
// get any refunds
if($this->payment || $this->po_payment) { // gotta have payments in order to refund them
$refunds_query = $db->Execute("select * from " . TABLE_SO_REFUNDS . "
where orders_id = '" . $this->oID . "'
order by date_posted asc");
if (zen_not_null($refunds_query->fields['orders_id'])) {
while (!$refunds_query->EOF) {
$this->refund[] = array('index' => $refunds_query->fields['refund_id'],
'payment' => $refunds_query->fields['payment_id'],
'number' => $refunds_query->fields['refund_number'],
'name' => $refunds_query->fields['refund_name'],
'amount' => $refunds_query->fields['refund_amount'],
'type' => $refunds_query->fields['refund_type'],
'posted' => $refunds_query->fields['date_posted'],
'modified' => $refunds_query->fields['last_modified']);
$refunds_query->MoveNext();
}
}
else {
unset($this->refund);
$this->refund = false;
}
}
// calculate and store the order total, amount applied, & balance due for the order
// add individual payments if they exists
if($this->payment) {
for($i = 0; $i < sizeof($this->payment); $i++) {
$this->amount_applied += $this->payment[$i]['amount'];
}
}
// next add the po payments if they exist
if ($this->po_payment) {
for($i = 0; $i < sizeof($this->po_payment); $i++) {
$this->amount_applied += $this->po_payment[$i]['amount'];
}
}
// now check PayPal payments and add if they exist
$this->check_paypal_payments();
$this->amount_applied += $this->paypal_total_payments;
// now subtract out any refunds if they exist
if($this->refund) {
for($i = 0; $i < sizeof($this->refund); $i++) {
$this->amount_applied -= $this->refund[$i]['amount'];
}
}
// subtract from the order total to get the balance due
$this->balance_due = $this->order_total - $this->amount_applied;
// compare this balance to the one stored in the orders table, update if necessary
if ($this->balance_due != $order_query->fields['balance_due']) $this->new_balance();
} // END function start
function check_paypal_payments() {
global $db;
$this->paypal_total_payments = 0;
$paypal_payments_query = $db->Execute("select * from " . TABLE_PAYPAL . "
where `order_id` = '" . $this->oID . "'
and `payment_status` = 'Completed'
and (`module_name` = 'paypalwpp'
or `module_name` = 'paypal 1.3.8a'
or `module_name` = 'paypal (ipn-handler)')");
if (zen_not_null($paypal_payments_query->fields['order_id'])) {
while (!$paypal_payments_query->EOF) {
$this->paypal_total_payments += $paypal_payments_query->fields['mc_gross'];
$paypal_payments_query->MoveNext();
}
}
}
Bookmarks