Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.53 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Modules\Commerce\Controllers;
  4.  
  5.  
  6. use Phalcon\Mvc\Controller;
  7. use App\Models\{
  8.     Deposit,
  9.     Plan,
  10.     Plan as PlanRecord,
  11.     Replenishment,
  12.     User as UserRecord,
  13.     Replenishment as ReplenishmentRecord,
  14.     Payout as PayoutRecord,
  15.     User
  16. };
  17. use App\Plugins\Hyip\Modules\{
  18.     Affiliate as AffliateModule,
  19.     Replenishment as ReplenishmentModule,
  20.     Deposit as DepositModule
  21. };
  22.  
  23. class StatusController extends Controller
  24. {
  25.     public function testAction()
  26.     {
  27.         $merchant_id = env('COINPAYMENTS_MERCHANT_ID');
  28.         $secret = env('COINPAYMENTS_IPN_SECRET');
  29.  
  30.         do {
  31.             if (!isset($_SERVER['HTTP_HMAC']) || empty($_SERVER['HTTP_HMAC'])) {
  32.                 echo "No HMAC signature sent";
  33.                 exit;
  34.             }
  35.  
  36.             $merchant = isset($_POST['merchant']) ? $_POST['merchant']:'';
  37.             if (empty($merchant)) {
  38.                 echo "No Merchant ID passed";
  39.                 exit;
  40.             }
  41.  
  42.             if ($merchant != $merchant_id) {
  43.                 echo "Invalid Merchant ID";
  44.                 exit;
  45.             }
  46.  
  47.             $request = file_get_contents('php://input');
  48.             if ($request === FALSE || empty($request)) {
  49.                 echo "Error reading POST data";
  50.                 exit;
  51.             }
  52.  
  53.             $hmac = hash_hmac("sha512", $request, $secret);
  54.             if ($hmac != $_SERVER['HTTP_HMAC']) {
  55.                 echo "HMAC signature does not match";
  56.                 exit;
  57.             }
  58.  
  59.             if ($_REQUEST['currency'] !== 'BTC') {
  60.                 echo "Only Bitcoin";
  61.                 exit;
  62.             }
  63.  
  64.             if ($_REQUEST['ipn_type'] === 'deposit') {
  65.                 $this->db->begin();
  66.  
  67.                 /**
  68.                  * @var $isConfirmed bool
  69.                  */
  70.                 $isConfirmed = (int)$_REQUEST['status'] >= 100;
  71.  
  72.                 /**
  73.                  * @var $replenishmentModule ReplenishmentModule
  74.                  */
  75.                 $replenishmentModule = $this->hyip->getReplenishment();
  76.  
  77.                 /**
  78.                  * @var $depositModule DepositModule
  79.                  */
  80.                 $depositModule = $this->hyip->getDeposit();
  81.  
  82.                 /**
  83.                  * @var $affiliateModule AffliateModule
  84.                  */
  85.                 $affiliateModule = $this->hyip->getAffiliate();
  86.  
  87.                 /**
  88.                  * @var $userRecord UserRecord
  89.                  */
  90.                 $userRecord = UserRecord::findFirstByReceiver($_REQUEST['address']);
  91.  
  92.                 if (!$userRecord) {
  93.                     $this->db->rollback();
  94.                     break;
  95.                 }
  96.  
  97.                 /**
  98.                  * @var $replenishmentRecord ReplenishmentRecord
  99.                  */
  100.                 //TODO: FOR UPDATE
  101.                 $replenishmentRecord = ReplenishmentRecord::findFirst(
  102.                     [
  103.                         '[batch] = :batch:',
  104.                         'bind' => [
  105.                             'batch' => $_REQUEST['txn_id']
  106.                         ],
  107.                         'for_update' => true
  108.                     ]
  109.                 );
  110.  
  111.                 if (\mathComp($_REQUEST['amount'], '0.00005', 8) === -1) {
  112.                     $this->db->rollback();
  113.                     break;
  114.                 }
  115.  
  116.                 if (
  117.                     $replenishmentRecord && $replenishmentRecord->status === $replenishmentModule->getValueByConst(
  118.                         ReplenishmentModule::REPLENISHMENT_STATUS_SUCCESS
  119.                     )
  120.                 ) {
  121.                     $this->db->rollback();
  122.                     break;
  123.                 } elseif (!$replenishmentRecord) {
  124.                     try {
  125.                         $replenishmentRecord = $replenishmentModule->createReplenishment(
  126.                             [
  127.                                 'user' => $userRecord,
  128.                                 'paysystem' => 'Bitcoin',
  129.                                 'amount' => $_REQUEST['amount'],
  130.                                 'currency' => 'BTC',
  131.                                 'batch' => $_REQUEST['txn_id'],
  132.                                 'status' => $replenishmentModule->getValueByConst(
  133.                                     ReplenishmentModule::REPLENISHMENT_STATUS_WAITING
  134.                                 ),
  135.                             ]
  136.                         );
  137.  
  138.                         if (!$replenishmentRecord) {
  139.                             $this->db->rollback();
  140.                             break;
  141.                         }
  142.                     } catch (\Throwable $throwable) {
  143.                         $this->db->rollback();
  144.                         break;
  145.                     }
  146.                 }
  147.  
  148.                 if ($isConfirmed) {
  149.                     $replenishmentRecord->status = $replenishmentModule->getValueByConst(
  150.                         ReplenishmentModule::REPLENISHMENT_STATUS_SUCCESS
  151.                     );
  152.  
  153.                     if (!$replenishmentRecord->save()) {
  154.                         $this->db->rollback();
  155.                         break;
  156.                     }
  157.  
  158.                     try {
  159.                         $depositRecord = $depositModule->createDeposit(
  160.                             [
  161.                                 'user' => $userRecord,
  162.                                 'plan' => 2,
  163.                                 'paysystem' => 'Bitcoin',
  164.                                 'amount' => $_REQUEST['amount'],
  165.                                 'currency' => 'BTC',
  166.                             ]
  167.                         );
  168.                     } catch (\Throwable $throwable) {
  169.                         $this->db->rollback();
  170.                         break;
  171.                     }
  172.  
  173.                     if (!$depositRecord) {
  174.                         $this->db->rollback();
  175.                         break;
  176.                     }
  177.  
  178.                     /**
  179.                      * @var $planRecord PlanRecord
  180.                      */
  181.                     $planRecord = Plan::findFirstById(2);
  182.                     switch ($planRecord->affiliate_type) {
  183.                         case $affiliateModule->getValueByConst(
  184.                             AffliateModule::AFFILIATE_TYPE_FROM_ANY_DEPOSIT
  185.                         ):
  186.                         case $affiliateModule->getValueByConst(
  187.                             AffliateModule::AFFILIATE_TYPE_FROM_ACCRUALS_AND_ORIGINAL_DEPOSIT
  188.                         ):
  189.                         case $affiliateModule->getValueByConst(
  190.                             AffliateModule::AFFILIATE_TYPE_FROM_ACCRUALS_AND_ANY_DEPOSIT
  191.                         ):
  192.                         case $affiliateModule->getValueByConst(
  193.                             AffliateModule::AFFILIATE_TYPE_FROM_THE_ORIGINAL_DEPOSIT
  194.                         ):
  195.                             try {
  196.                                 $addFundsToUpline = $affiliateModule->addFundsToUplines(
  197.                                     [
  198.                                         'user' => $userRecord,
  199.                                         'amount' => $_REQUEST['amount'],
  200.                                         'paysystem' => 'Bitcoin',
  201.                                         'currency' => 'BTC',
  202.                                         'plan' => $planRecord,
  203.                                         'affiliate_type' => $planRecord->affiliate_type
  204.                                     ]
  205.                                 );
  206.                             } catch (\Throwable $throwable) {
  207.                                 $this->db->rollback();
  208.                                 break 2;
  209.                             }
  210.                             break;
  211.                     }
  212.  
  213.                     if (isset($addFundsToUpline)) {
  214.                         if ($addFundsToUpline === false) {
  215.                             $this->db->rollback();
  216.                             break;
  217.                         }
  218.                     }
  219.                 }
  220.  
  221.                 $this->db->commit();
  222.             } elseif ($_REQUEST['ipn_type'] === 'withdrawal') {
  223.                 $this->db->begin();
  224.  
  225.                 /**
  226.                  * @var $payout PayoutRecord
  227.                  */
  228.                 $payout = PayoutRecord::findFirstByCpId($_REQUEST['id']);
  229.                 if (!$payout) {
  230.                     $this->db->rollback();
  231.                     break;
  232.                 }
  233.  
  234.                 $payout->batch = $_REQUEST['txn_id'];
  235.                 if (!$payout->save()) {
  236.                     $this->db->rollback();
  237.                     break;
  238.                 }
  239.  
  240.                 $this->db->rollback();
  241.             }
  242.         } while(false);
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement