/** * Postback for Redirect Pay */ function postback($arr) { global $C_debug; if (empty($arr['invoice_id'])) { return false; } if (empty($arr['transaction_id'])) { return false; } if (empty($arr['amount'])) { return false; } if (eregi("MULTI-", $arr['invoice_id'])) { $this->postback_multiple($arr); return; } # Get the latest invoice info: $db =& DB(); $sql1 = ""; if (!empty($arr['subscription_id'])) { $sql1 = "checkout_plugin_data = " . $db->qstr(trim($arr['subscription_id'])) . " OR "; } $q = "SELECT * FROM " . AGILE_DB_PREFIX . "invoice WHERE\n\t \t\t\t( \n\t\t\t\t\t\t{$sql1}\n\t \t\t\t\tparent_id = " . $db->qstr(@$arr['invoice_id']) . "\n\t\t\t\t\t\tOR\n\t\t\t\t\t\tid = " . $db->qstr(@$arr['invoice_id']) . " \n\t\t\t\t\t) \n\t\t\t\t\tAND\n\t \t\t\tbilling_status != 1\n\t\t\t\t\tAND \n\t \t\t\tsite_id = " . $db->qstr(DEFAULT_SITE) . "\n\t \t\t\tORDER BY date_orig\n\t \t\t\tLIMIT 0,1"; $invoice = $db->Execute($q); if ($invoice === false || $invoice->RecordCount() == 0) { $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg()); } if ($invoice->RecordCount() == 0) { return false; } $invoice_id = $invoice->fields['id']; # Validate the currency $billed_currency_id = $invoice->fields['billed_currency_id']; $total_amt = $invoice->fields['total_amt']; $billed_amt = $invoice->fields['billed_amt']; $actual_billed_amt = $invoice->fields['actual_billed_amt']; $currency_iso = @$arr['currency']; if (empty($currency_iso) || !$currency_iso) { # same as billed_currency_id $this->billed_amt = $arr['amount'] + $billed_amt; $this->actual_billed_amt = $arr['amount'] + $billed_amt; $this->actual_billed_currency_id = $billed_currency_id; } else { # Get the actual billed currency id currency info: $q = "SELECT * FROM " . AGILE_DB_PREFIX . "currency WHERE\n\t \t\t\tthree_digit\t= " . $db->qstr($currency_iso) . " AND\n\t \t\t\tsite_id = " . $db->qstr(DEFAULT_SITE); $result = $db->Execute($q); if ($result === false) { $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg()); } $actual_billed_currency_id = $result->fields['id']; if (is_string($result->fields["convert_array"])) { $convert = unserialize($result->fields["convert_array"]); } else { $convert = false; } $this->format_currency[$actual_billed_currency_id] = array('symbol' => $result->fields["symbol"], 'convert' => $convert, 'iso' => $result->fields["three_digit"]); if ($result->RecordCount() == 0 || $actual_billed_currency_id == $billed_currency_id) { # same as billed_currency_id $this->billed_amt = $arr['amount'] + $billed_amt; $this->actual_billed_amt = $arr['amount'] + $billed_amt; $this->actual_billed_currency_id = $actual_billed_currency_id; } else { # Get the billed currency id currency info: $q = "SELECT * FROM " . AGILE_DB_PREFIX . "currency WHERE\n\t\t \t\t\tid \t= " . $db->qstr($billed_currency_id) . " AND\n\t\t \t\t\tsite_id = " . $db->qstr(DEFAULT_SITE); $result = $db->Execute($q); if ($result === false) { $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg()); } $this->format_currency[$billed_currency_id] = array('symbol' => $result->fields["symbol"], 'convert' => unserialize($result->fields["convert_array"]), 'iso' => $result->fields["three_digit"]); # Convert the invoice amount to the actual billed currency amount $due_amount = $invoice->fields['total_amt'] - $invoice->fields['billed_amt']; $conversion = $this->format_currency[$billed_currency_id]["convert"][$actual_billed_currency_id]["rate"]; $this->billed_amt = $billed_amt + ($arr['amount'] /= $conversion); $this->actual_billed_amt = $actual_billed_amt + $arr['amount']; $this->actual_billed_currency_id = $actual_billed_currency_id; } } # Check for any subscription_id if (!empty($arr['subscription_id'])) { $this->subscription_id = trim($arr['subscription_id']); } else { $this->subscription_id = trim($invoice->fields['checkout_plugin_data']); } # Check for the checkout_id if (!empty($arr['checkout_id'])) { $this->checkout_id = $arr['checkout_id']; } else { $this->checkout_id = $invoice->fields['checkout_plugin_id']; } # Check for the billing status: if ($this->billed_amt >= $invoice->fields['total_amt']) { $this->billing_status = '1'; } else { $this->billing_status = '0'; } # Check if this transaction_id has already been processed: $q = "SELECT id FROM " . AGILE_DB_PREFIX . "invoice_memo WHERE\n \t\t\tinvoice_id \t= " . $db->qstr($invoice_id) . " AND\n \t\t\ttype\t\t= " . $db->qstr('postback') . " AND\n \t\t\tmemo\t\t= " . $db->qstr($arr['transaction_id']) . " AND\n \t\t\tsite_id \t= " . $db->qstr(DEFAULT_SITE); $memo = $db->Execute($q); if ($memo === false) { $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg()); } if ($memo->RecordCount() > 0) { # duplicate post: $C_debug->error('Duplicate Postback', 'checkout.inc.php :: postback()', "Duplicate postback for invoice {$arr['invoice_id']} & transaction id {$arr['transaction_id']}"); } else { # Create the invoice memo: $memo_id = $db->GenID(AGILE_DB_PREFIX . 'invoice_memo_id'); $q = "INSERT INTO\n\t \t\t\t" . AGILE_DB_PREFIX . "invoice_memo \n\t \t SET\n\t \t\t\tid \t\t\t\t\t= " . $db->qstr($memo_id) . ",\n\t \t\t\tsite_id \t\t\t= " . $db->qstr(DEFAULT_SITE) . ",\n\t \t\t\tdate_orig \t\t\t= " . $db->qstr(time()) . ", \n\t \t\t\tinvoice_id\t \t\t= " . $db->qstr($invoice_id) . ", \n\t \t\t\taccount_id\t\t\t= " . $db->qstr(0) . ", \n\t \t\t\ttype\t\t\t\t= " . $db->qstr('postback') . ", \n\t \t\t\tmemo\t\t\t\t= " . $db->qstr($arr['transaction_id']); $memosql = $db->Execute($q); if ($memosql === false) { $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg()); } # Update the invoice billing info: $q = "UPDATE\n\t \t\t\t" . AGILE_DB_PREFIX . "invoice \n\t \t SET\n\t \t\t\tdate_last \t\t\t= " . $db->qstr(time()) . ", \n\t \t\t\tbilling_status \t\t= " . $db->qstr($this->billing_status) . ", \n\t \t\t\tcheckout_plugin_id\t= " . $db->qstr($this->checkout_id) . ", \n\t \t\t\tcheckout_plugin_data = " . $db->qstr($this->subscription_id) . ", \n\t \t\t\tbilled_amt\t\t\t= " . $db->qstr($this->billed_amt) . ", \n\t \t\t\tactual_billed_amt\t= " . $db->qstr($this->actual_billed_amt) . ", \n\t \t\t\tactual_billed_currency_id = " . $db->qstr($this->actual_billed_currency_id) . "\n\t \t\t WHERE\n\t \t\t\tid \t\t\t= " . $db->qstr($invoice_id) . " AND\n\t \t\t\tsite_id \t= " . $db->qstr(DEFAULT_SITE); $memosql = $db->Execute($q); if ($memosql === false) { $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg()); } # Update the invoice approval status $VAR['id'] = $invoice_id; include_once PATH_MODULES . 'invoice/invoice.inc.php'; $inv = new invoice(); if (!$arr['status']) { # void $inv->voidInvoice($VAR); # create a record of the viod in an invoice memo: $memo_id = $db->GenID(AGILE_DB_PREFIX . 'invoice_memo_id'); $q = "INSERT INTO\n\t\t \t\t\t" . AGILE_DB_PREFIX . "invoice_memo \n\t\t \t SET\n\t\t \t\t\tid \t\t\t\t\t= " . $db->qstr($memo_id) . ",\n\t\t \t\t\tsite_id \t\t\t= " . $db->qstr(DEFAULT_SITE) . ",\n\t\t \t\t\tdate_orig \t\t\t= " . $db->qstr(time()) . ", \n\t\t \t\t\tinvoice_id\t \t\t= " . $db->qstr($invoice_id) . ", \n\t\t \t\t\taccount_id\t\t\t= " . $db->qstr(0) . ", \n\t\t \t\t\ttype\t\t\t\t= " . $db->qstr('void') . ", \n\t\t \t\t\tmemo\t\t\t\t= " . $db->qstr("Voided due to postback: " . $arr['transaction_id']); $rsql = $db->Execute($q); if ($rsql === false) { $C_debug->error('checkout.inc.php', 'postback', $q . " | " . @$db->ErrorMsg()); } } else { # approve $inv->autoApproveInvoice($invoice_id); # User invoice payment confirmation include_once PATH_MODULES . 'email_template/email_template.inc.php'; $email = new email_template(); $email->send('invoice_paid_user', $invoice->fields['account_id'], $invoice_id, DEFAULT_CURRENCY, ''); # Admin alert of payment processed $email = new email_template(); $email->send('admin->invoice_paid_admin', $invoice->fields['account_id'], $invoice_id, DEFAULT_CURRENCY, ''); } } return true; }