function FullPaypalProcess() { $isIPN = $_SERVER["SCRIPT_NAME"] != '/api/paypal.php'; $txnId = isset($_POST['txn_id']) ? $_POST['txn_id'] : 'unknown'; LogPaypalMessage("Received " . ($isIPN ? "IPN" : "API") . " txn: {$txnId}"); $postResult = CheckPaypalPost(); if ($postResult === 'Ignore') { if (!$isIPN) { LogPaypalMessage("Forwarding post to paidfinish"); header('Location: /#subscription/paidfinish'); } return; } elseif ($postResult !== true) { if (!$isIPN) { LogPaypalMessage("Forwarding errored post to paidpending"); header('Location: /#subscription/paidpending'); } elseif (!isset($_POST['txn_id'])) { // ignore IPN txns without IDs } else { if (!is_string($postResult)) { $postResult = 'HTTP/1.0 500 Internal Server Error'; } LogPaypalMessage("Setting errored post header: {$postResult}"); header($postResult); } return; } $operation = ProcessPaypalPost(); if ($operation === false) { if (!$isIPN) { LogPaypalMessage("Forwarding errored process to paiderror"); header('Location: /#subscription/paiderror'); } else { LogPaypalMessage("Returning errored process header HTTP 500"); header('HTTP/1.0 500 Internal Server Error'); } return; } if (isset($operation['addTime'])) { $newPaidUntil = AddPaidTime($operation['addTime'], SUBSCRIPTION_PAID_ADDS_SECONDS); LogPaypalMessage("Added time, paid until: {$newPaidUntil}"); PaypalResultForUser($operation['addTime'], $newPaidUntil, false); } if (isset($operation['delTime'])) { $newPaidUntil = AddPaidTime($operation['delTime'], -1 * SUBSCRIPTION_PAID_ADDS_SECONDS); LogPaypalMessage("Removed time, paid until: {$newPaidUntil}"); PaypalResultForUser($operation['delTime'], $newPaidUntil, true); } if (!$isIPN) { LogPaypalMessage("Forwarding to paidfinish"); header('Location: /#subscription/paidfinish'); } }
function AddTheTime($user, $seconds, $message) { global $LANG; $paidUntil = AddPaidTime($user, $seconds); if ($paidUntil === false) { echo "Error adding {$seconds} to {$user}\n"; return false; } if ($paidUntil > time()) { $message .= "<br><br>" . sprintf(preg_replace('/\\{(\\d+)\\}/', '%$1$s', $LANG['paidExpires']), date('Y-m-d H:i:s e', $paidUntil)); } SendUserMessage($user, 'Subscription', $LANG['paidSubscription'], $message); echo "{$user}: {$message}\n"; return true; }
function UpdateBitPayTransaction($bitPayJson, $isNew = false) { $isNew = !!$isNew; // force boolean if (!is_array($bitPayJson)) { $jsonString = $bitPayJson; $bitPayJson = json_decode($jsonString, true); if (json_last_error() != JSON_ERROR_NONE) { DebugMessage("Invalid BitPay JSON\n" . print_r($jsonString, true), E_USER_ERROR); return false; } } $requiredFields = ['btcDue', 'btcPaid', 'btcPrice', 'currency', 'currentTime', 'exceptionStatus', 'expirationTime', 'id', 'invoiceTime', 'posData', 'price', 'rate', 'status', 'url']; foreach ($requiredFields as $fieldName) { if (!array_key_exists($fieldName, $bitPayJson)) { DebugMessage("BitPay JSON missing required field: {$fieldName}\n" . print_r($bitPayJson, true), E_USER_ERROR); return false; } } $invoiced = floor($bitPayJson['invoiceTime'] / 1000); $expired = floor($bitPayJson['expirationTime'] / 1000); $db = DBConnect(); $stmt = $db->prepare('select id, user, subextended from tblBitPayTransactions where id = ?'); $stmt->bind_param('s', $bitPayJson['id']); $stmt->execute(); $txnId = $userId = $subExtended = null; $stmt->bind_result($txnId, $userId, $subExtended); if (!$stmt->fetch()) { $txnId = $userId = $subExtended = null; } $stmt->close(); if (is_null($txnId) != $isNew) { if ($isNew) { DebugMessage("New BitPay transaction already found in database:\n" . print_r($bitPayJson, true), E_USER_ERROR); } else { DebugMessage("Existing BitPay transaction not found in database:\n" . print_r($bitPayJson, true), E_USER_ERROR); } return false; } if ($isNew) { $userId = GetUserFromPublicHMAC($bitPayJson['posData'], $invoiced); if (is_null($userId)) { DebugMessage("Could not get valid user from BitPay posData:\n" . print_r($bitPayJson, true), E_USER_ERROR); return false; } } $sql = <<<'EOF' insert into tblBitPayTransactions ( id, user, price, currency, rate, btcprice, btcpaid, btcdue, status, exception, url, posdata, invoiced, expired, updated ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, from_unixtime(?), from_unixtime(?), now()) on duplicate key update user=ifnull(user,values(user)), price=values(price), currency=values(currency), rate=values(rate), btcprice=values(btcprice), btcpaid=values(btcpaid), btcdue=values(btcdue), status=values(status), exception=values(exception), url=values(url), posdata=values(posdata), invoiced=values(invoiced), expired=values(expired), updated=values(updated) EOF; $stmt = $db->prepare($sql); $exceptionStatus = $bitPayJson['exceptionStatus'] ?: null; $stmt->bind_param('sissssssssssii', $bitPayJson['id'], $userId, $bitPayJson['price'], $bitPayJson['currency'], $bitPayJson['rate'], $bitPayJson['btcPrice'], $bitPayJson['btcPaid'], $bitPayJson['btcDue'], $bitPayJson['status'], $exceptionStatus, $bitPayJson['url'], $bitPayJson['posData'], $invoiced, $expired); if (!$stmt->execute()) { DebugMessage("Error updating BitPay transaction record: " . print_r($bitPayJson, true), E_USER_ERROR); return false; } $stmt->close(); if ($userId && !$subExtended) { $stmt = $db->prepare('select locale from tblUser where id = ?'); $locale = null; $stmt->bind_param('i', $userId); $stmt->execute(); $stmt->bind_result($locale); if (!$stmt->fetch()) { $locale = 'enus'; } $stmt->close(); $LANG = GetLang($locale); if (in_array($bitPayJson['status'], ['confirmed', 'complete'])) { $paidUntil = AddPaidTime($userId, SUBSCRIPTION_PAID_ADDS_SECONDS); if ($paidUntil === false) { LogBitPayError($bitPayJson, "Failed to add paid time to {$userId}"); SendUserMessage($userId, 'Subscription', $LANG['paidSubscription'], $LANG['SubscriptionErrors']['paiderror']); return false; } else { $stmt = $db->prepare('update tblBitPayTransactions set subextended=1 where id=?'); $stmt->bind_param('s', $bitPayJson['id']); if (!$stmt->execute()) { LogBitPayError($bitPayJson, "Failed to mark transaction as extending the sub for {$userId}"); } else { $message = $LANG['subscriptionTimeAddedMessage']; $message .= "<br><br>" . sprintf(preg_replace('/\\{(\\d+)\\}/', '%$1$s', $LANG['paidExpires']), date('Y-m-d H:i:s e', $paidUntil)); SendUserMessage($userId, 'Subscription', $LANG['paidSubscription'], $message); } $stmt->close(); } } elseif (in_array($bitPayJson['status'], ['invalid']) || $bitPayJson['exceptionStatus']) { LogBitPayError($bitPayJson, "BitPay exception for {$userId}"); SendUserMessage($userId, 'Subscription', $LANG['paidSubscription'], $LANG['SubscriptionErrors']['paiderror']); } } return true; }