public function save(Recipient $recipient) { $now = new \DateTime(); if ($recipient->getId()) { $sql = sprintf("UPDATE %s SET earning = :earning, referral_earning = :referral_earning, " . "updated_at = :updated_at WHERE address = :address", self::TABLE_NAME); $params = array(':address' => $recipient->getAddress(), ':earning' => $recipient->getEarning(), ':referral_earning' => $recipient->getReferralEarning(), ':updated_at' => $now->format('Y-m-d H:i:s')); } else { $sql = sprintf("INSERT INTO %s (address, earning, referral_earning, created_at, updated_at) " . "VALUES (:address, :earning, :referral_earning, :created_at, :updated_at)", self::TABLE_NAME); $params = array(':address' => $recipient->getAddress(), ':earning' => $recipient->getEarning(), ':referral_earning' => $recipient->getReferralEarning(), ':created_at' => $recipient->getCreatedAt()->format('Y-m-d H:i:s'), ':updated_at' => $recipient->getUpdatedAt()->format('Y-m-d H:i:s')); } $results = $this->database->run($sql, $params); if (empty($results)) { return null; } $recipient->setId($results[0]); }
/** * 1. Captcha check * 2. Timeout check * 3. Reward generation * 4. Payout creation * 5. Payment process * 6. Referral payout creation * 7. Referral payment process */ public function reward() { $address = $this->app->request->post('address'); $challenge = $this->app->request->post($this->captchaService->getChallengeName()); $response = $this->app->request->post($this->captchaService->getResponseName()); if (empty($address) || empty($challenge) || empty($response)) { $this->app->flash('warning', 'Missing address or captcha.'); $this->app->redirect($this->app->urlFor('index')); } $remoteIp = $this->app->request->getIp(); // Captcha Check try { $captchaResponse = $this->captchaService->checkAnswer($remoteIp, $challenge, $response); } catch (CaptchaException $e) { $this->app->flash('error', 'Unable to complete request.'); return $this->app->redirect($this->app->urlFor('index')); } if (!$captchaResponse->getSuccess()) { $this->app->flash('error', 'Invalid Captcha'); return $this->app->redirect($this->app->urlFor('index')); } try { if (!$this->database->beginTransaction()) { } } catch (PaytoshiException $e) { $this->app->flash('error', 'Unable to complete request.'); $this->app->redirect($this->app->urlFor('index')); } $recipient = $this->recipientRepository->findOneByAddress($address); if (!$recipient) { $recipient = new Recipient(); $recipient->setAddress($address); } // Timeout check $lastPayout = $this->payoutRepository->findLastByRecipientAndIp($recipient, $remoteIp); $now = new DateTime(); $waitingInterval = $this->settingRepository->getWaitingInterval(); if ($lastPayout) { $nextPayoutTime = $lastPayout->getCreatedAt()->add(new DateInterval('PT' . $waitingInterval . 'S')); if ($nextPayoutTime > $now) { $this->database->rollBack(); $waitingTime = $nextPayoutTime->diff($now); $this->app->flash('warning', sprintf('You can get a reward again in %s.', $this->formatTime($waitingTime))); return $this->app->redirect($this->app->urlFor('index')); } } // Reward Generation $earning = $this->rewardService->getReward(); // Payout Creation $payout = new Payout(); $payout->setIp($remoteIp); $payout->setRecipientAddress($recipient->getAddress()); $payout->setEarning($earning); // Payment process try { /* @var $apiResponse ApiResponse */ $apiResponse = $this->apiService->send($payout->getRecipientAddress(), $payout->getEarning(), $remoteIp); } catch (PaytoshiException $e) { $this->database->rollback(); $this->app->flash('error', $e->getMessage()); return $this->app->redirect($this->app->urlFor('index')); } if (!$apiResponse->getSuccess()) { $this->database->rollback(); $this->app->flash('error', $apiResponse->getError()); return $this->app->redirect($this->app->urlFor('index')); } $view = $this->app->view(); $view->setData(array('amount' => $apiResponse->getAmount(), 'recipient' => $apiResponse->getRecipient(), 'balanceUrl' => str_replace('_ADDRESS_', $address, $this->app->config('balance_url')))); $this->recipientRepository->save($recipient); $this->app->flash('success', $view->render($this->themeService->getTemplate('balance.html.twig'))); $_SESSION['address'] = $recipient->getAddress(); $referral = $this->app->request->post('referral'); $referralPercentage = $this->settingRepository->getReferralPercentage(); if ($referral && $referral != $address && $referralPercentage > 0) { // Referral Payout Creation $referralRecipient = $this->recipientRepository->findOneByAddress($referral); if (!$referralRecipient) { $referralRecipient = new Recipient(); $referralRecipient->setAddress($referral); } $referralEarning = ceil($earning * $referralPercentage / 100); $payout->setReferralRecipientAddress($referralRecipient->getAddress()); $payout->setReferralEarning($referralEarning); $referralRecipient->setReferralEarning($referralEarning + $referralRecipient->getReferralEarning()); // Referral payment process try { $apiResponse = $this->apiService->send($referralRecipient->getAddress(), $referralRecipient->getReferralEarning(), $remoteIp, true); } catch (PaytoshiException $e) { $this->payoutRepository->save($payout); $this->recipientRepository->save($referralRecipient); $this->database->commit(); $this->app->flash('error', $e->getMessage()); return $this->app->redirect($this->app->urlFor('index')); } if ($apiResponse->getSuccess()) { $referralRecipient->setReferralEarning(0); } $this->recipientRepository->save($referralRecipient); } $this->payoutRepository->save($payout); $this->database->commit(); return $this->app->redirect($this->app->urlFor('index')); }