Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Modules\Commerce\Controllers;
- use Phalcon\Mvc\Controller;
- use App\Models\{
- Deposit,
- Plan,
- Plan as PlanRecord,
- Replenishment,
- User as UserRecord,
- Replenishment as ReplenishmentRecord,
- Payout as PayoutRecord,
- User
- };
- use App\Plugins\Hyip\Modules\{
- Affiliate as AffliateModule,
- Replenishment as ReplenishmentModule,
- Deposit as DepositModule
- };
- class StatusController extends Controller
- {
- public function testAction()
- {
- $merchant_id = env('COINPAYMENTS_MERCHANT_ID');
- $secret = env('COINPAYMENTS_IPN_SECRET');
- do {
- if (!isset($_SERVER['HTTP_HMAC']) || empty($_SERVER['HTTP_HMAC'])) {
- echo "No HMAC signature sent";
- exit;
- }
- $merchant = isset($_POST['merchant']) ? $_POST['merchant']:'';
- if (empty($merchant)) {
- echo "No Merchant ID passed";
- exit;
- }
- if ($merchant != $merchant_id) {
- echo "Invalid Merchant ID";
- exit;
- }
- $request = file_get_contents('php://input');
- if ($request === FALSE || empty($request)) {
- echo "Error reading POST data";
- exit;
- }
- $hmac = hash_hmac("sha512", $request, $secret);
- if ($hmac != $_SERVER['HTTP_HMAC']) {
- echo "HMAC signature does not match";
- exit;
- }
- if ($_REQUEST['currency'] !== 'BTC') {
- echo "Only Bitcoin";
- exit;
- }
- if ($_REQUEST['ipn_type'] === 'deposit') {
- $this->db->begin();
- /**
- * @var $isConfirmed bool
- */
- $isConfirmed = (int)$_REQUEST['status'] >= 100;
- /**
- * @var $replenishmentModule ReplenishmentModule
- */
- $replenishmentModule = $this->hyip->getReplenishment();
- /**
- * @var $depositModule DepositModule
- */
- $depositModule = $this->hyip->getDeposit();
- /**
- * @var $affiliateModule AffliateModule
- */
- $affiliateModule = $this->hyip->getAffiliate();
- /**
- * @var $userRecord UserRecord
- */
- $userRecord = UserRecord::findFirstByReceiver($_REQUEST['address']);
- if (!$userRecord) {
- $this->db->rollback();
- break;
- }
- /**
- * @var $replenishmentRecord ReplenishmentRecord
- */
- //TODO: FOR UPDATE
- $replenishmentRecord = ReplenishmentRecord::findFirst(
- [
- '[batch] = :batch:',
- 'bind' => [
- 'batch' => $_REQUEST['txn_id']
- ],
- 'for_update' => true
- ]
- );
- if (\mathComp($_REQUEST['amount'], '0.00005', 8) === -1) {
- $this->db->rollback();
- break;
- }
- if (
- $replenishmentRecord && $replenishmentRecord->status === $replenishmentModule->getValueByConst(
- ReplenishmentModule::REPLENISHMENT_STATUS_SUCCESS
- )
- ) {
- $this->db->rollback();
- break;
- } elseif (!$replenishmentRecord) {
- try {
- $replenishmentRecord = $replenishmentModule->createReplenishment(
- [
- 'user' => $userRecord,
- 'paysystem' => 'Bitcoin',
- 'amount' => $_REQUEST['amount'],
- 'currency' => 'BTC',
- 'batch' => $_REQUEST['txn_id'],
- 'status' => $replenishmentModule->getValueByConst(
- ReplenishmentModule::REPLENISHMENT_STATUS_WAITING
- ),
- ]
- );
- if (!$replenishmentRecord) {
- $this->db->rollback();
- break;
- }
- } catch (\Throwable $throwable) {
- $this->db->rollback();
- break;
- }
- }
- if ($isConfirmed) {
- $replenishmentRecord->status = $replenishmentModule->getValueByConst(
- ReplenishmentModule::REPLENISHMENT_STATUS_SUCCESS
- );
- if (!$replenishmentRecord->save()) {
- $this->db->rollback();
- break;
- }
- try {
- $depositRecord = $depositModule->createDeposit(
- [
- 'user' => $userRecord,
- 'plan' => 2,
- 'paysystem' => 'Bitcoin',
- 'amount' => $_REQUEST['amount'],
- 'currency' => 'BTC',
- ]
- );
- } catch (\Throwable $throwable) {
- $this->db->rollback();
- break;
- }
- if (!$depositRecord) {
- $this->db->rollback();
- break;
- }
- /**
- * @var $planRecord PlanRecord
- */
- $planRecord = Plan::findFirstById(2);
- switch ($planRecord->affiliate_type) {
- case $affiliateModule->getValueByConst(
- AffliateModule::AFFILIATE_TYPE_FROM_ANY_DEPOSIT
- ):
- case $affiliateModule->getValueByConst(
- AffliateModule::AFFILIATE_TYPE_FROM_ACCRUALS_AND_ORIGINAL_DEPOSIT
- ):
- case $affiliateModule->getValueByConst(
- AffliateModule::AFFILIATE_TYPE_FROM_ACCRUALS_AND_ANY_DEPOSIT
- ):
- case $affiliateModule->getValueByConst(
- AffliateModule::AFFILIATE_TYPE_FROM_THE_ORIGINAL_DEPOSIT
- ):
- try {
- $addFundsToUpline = $affiliateModule->addFundsToUplines(
- [
- 'user' => $userRecord,
- 'amount' => $_REQUEST['amount'],
- 'paysystem' => 'Bitcoin',
- 'currency' => 'BTC',
- 'plan' => $planRecord,
- 'affiliate_type' => $planRecord->affiliate_type
- ]
- );
- } catch (\Throwable $throwable) {
- $this->db->rollback();
- break 2;
- }
- break;
- }
- if (isset($addFundsToUpline)) {
- if ($addFundsToUpline === false) {
- $this->db->rollback();
- break;
- }
- }
- }
- $this->db->commit();
- } elseif ($_REQUEST['ipn_type'] === 'withdrawal') {
- $this->db->begin();
- /**
- * @var $payout PayoutRecord
- */
- $payout = PayoutRecord::findFirstByCpId($_REQUEST['id']);
- if (!$payout) {
- $this->db->rollback();
- break;
- }
- $payout->batch = $_REQUEST['txn_id'];
- if (!$payout->save()) {
- $this->db->rollback();
- break;
- }
- $this->db->rollback();
- }
- } while(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement