Skip to content

Instantly share code, notes, and snippets.

@umidjons
Created March 7, 2018 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umidjons/31f67173b2e1532a2baefd25c93145fe to your computer and use it in GitHub Desktop.
Save umidjons/31f67173b2e1532a2baefd25c93145fe to your computer and use it in GitHub Desktop.
CancelTransaction example on DLE
<?php
class MerchantAPI {
public function CancelTransaction($id, $reason)
{
if (is_array($this->Authorize())) {
return $this->Authorize();
}
foreach ($this->errors as $k => $v) {
$this->errorMessages[$k] = $v;
}
$errorcode = null;
do {
$res = mysql_query(" select * from dle_paypayme where external_id = '$id' ");
if (!mysql_num_rows($res)) {
$errorcode = self::TransactionNotFound;
break;
}
$_transfer = mysql_fetch_assoc($res);
// If the transaction is already cancelled, just return its current state
if ($_transfer["state"] < 0) {
$result = new CancelTransactionResult();
$result->cancel_time = time() * 1000;
$result->transaction = "{$_transfer['id']}";
$result->state = 1 * $_transfer["state"];
break;
}
// If the transaction is not cancelled yet, then proceed cancellation
$updateState = 0 - ($_transfer["state"] * 1);
mysql_query("update dle_paypayme set canceled = true, canceledDate = now(), state=$updateState, reason=$reason where external_id = '$id'");
$userid = $_transfer['user_id'];
$usernamestatement = mysql_query("SELECT name FROM dle_users where user_id={$userid}");
if (!mysql_num_rows($usernamestatement)) {
$errorcode = self::UserNotFound;
break;
}
$username = mysql_fetch_assoc($usernamestatement);
if (!$_transfer['canceled']) {
$money = 1 * $_transfer['summa'];
$username = $username['name'];
$is_success = mysql_query("UPDATE dle_users SET user_balance=user_balance-{$money} where name='{$username}'");
if ($is_success === false) {
$errorcode = self::CantCancel;
break;
}
$balancestatement = mysql_query("SELECT user_balance FROM dle_users where user_id={$userid}");
$balance = mysql_fetch_assoc($balancestatement);
$balance = $balance['user_balance'];
$hist_date = time();
mysql_query("INSERT INTO dle_billing_history (history_plugin, history_plugin_id, history_user_name, history_plus, history_minus, history_balance, history_currency, history_text, history_date) values ('pay', {$userid}, '{$username}', '0', '{$money}', '{$balance}', 'sum', 'payme.uz#{$id} canceled', '{$hist_date}')");
}
$result = new CancelTransactionResult();
$result->cancel_time = time() * 1000;
$result->transaction = "{$_transfer['id']}";
$result->state = $updateState;
break;
} while (false);
if ($errorcode) {
return $this->getError($errorcode, null, $errorField);
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment