Bintime - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Bintime.php

  1. <?php
  2.     class UserService {
  3.         public function createNewUser($userData)
  4.         {
  5.             //check if user exists
  6.             if (User::find()
  7.                 ->where(['email' => $userData['contactEmail']])
  8.                 ->count()) {
  9.                 throw new Exception('Unable to add user: user with the same email exists');
  10.             }
  11.  
  12.             $transaction = Yii::$app->db->beginTransaction();
  13.  
  14.             try {
  15.                 $user = new User();
  16.                 $user->name = $userData['firstName'];
  17.                 $user->surname = $userData['lastName'];
  18.                 $user->email = $userData['contactEmail'];
  19.  
  20.                 if ($user->save()) {
  21.                     $buyPointsAmount = 0;
  22.                     $currencyRate = 100;
  23.                     //calculate buy points
  24.                     if ($userData['deposit'] > 1000) {
  25.                         //convert money to points and add +50% as a bonus
  26.                         $buyPointsAmount = $buyPointsAmount*$currencyRate*1.5;
  27.                     } elseif ($userData['deposit'] > 500) {
  28.                         //convert money to points and add +30% as a bonus
  29.                         $buyPointsAmount = $buyPointsAmount*$currencyRate*1.3;
  30.                     } elseif ($userData['deposit'] > 200) {
  31.                         //convert money to points and add +10% as a bonus
  32.                         $buyPointsAmount = $buyPointsAmount*$currencyRate*1.1;
  33.                     } else {
  34.                         $buyPointsAmount = $buyPointsAmount*$currencyRate;
  35.                     }
  36.  
  37.                     //save bonus points transaction
  38.                     $buyPointsTransaction = new BuyPointsTransaction();
  39.                     $buyPointsTransaction->user_id = $user->id;
  40.                     $buyPointsTransaction->amount = $buyPointsAmount;
  41.                     if (!$buyPointsTransaction->save()) {
  42.                         throw new Exception('Unable to save transaction'.$transaction->getErrors());
  43.                     }
  44.  
  45.                     //create user's wallet and add points to it
  46.                     $wallet = new Wallet();
  47.                     $wallet->user_id = $user->id;
  48.                     $wallet->points = $buyPointsAmount;
  49.                     if (!$wallet->save()) {
  50.                         throw new Exception('Unable to save wallet'.$wallet->getErrors());
  51.                     }
  52.  
  53.                     //prepare billing data for a report
  54.                     $reportData = [];
  55.                     $reportData['name'] = $userData['firstName'];
  56.                     $reportData['surname'] = $userData['lastName'];
  57.                     $reportData['paid'] = $userData['deposit'];
  58.                     $reportData['points'] = $buyPointsAmount;
  59.                     $reportData['transactionId'] = $buyPointsTransaction->id;
  60.                     $billingReport = new TxtBillingReport();
  61.                     $billingReport->addData($reportData);
  62.                     $billingReport->createReport($reportData);
  63.                 } else {
  64.                     throw new Exception('Unable to create user');
  65.                 }
  66.  
  67.                 $transaction->commit();
  68.             } catch (Exception $exception) {
  69.                 $transaction->rollBack();
  70.                 throw $exception;
  71.             }
  72.         }
  73.     }
  74.  
  75.     class TxtBillingReport
  76.     {
  77.         private $output;
  78.  
  79.         public function addData($reportData)
  80.         {
  81.             $credentialsModel = OrganizationCredentials::find()->where(['organization_id' => OrganizationCredentials::PAY_TO_WIN])->one();
  82.             $credentials = $credentialsModel->name. " ". $credentialsModel->credentials;
  83.             $output = "Organization: ". $credentials."\n";
  84.             $output .= "User: ".$reportData['name']. " ".$reportData['surname']."\n";
  85.             $output .= "Paid: ".$reportData['paid']. "\n";
  86.             $output .= "Recieved points: ".$reportData['points']."\n";
  87.             $output .= "Date: ".date('Y-m-d H:i:s');
  88.  
  89.             $this->output = $output;
  90.         }
  91.  
  92.         public function createReport($reportData)
  93.         {
  94.             file_put_contents($reportData['transactionId'].'txt', $this->output);
  95.         }
  96.     }
  97.  
  98.     class Main {
  99.         public function run() {
  100.             $userService = new UserService();
  101.  
  102.             try {
  103.                 $userService->createNewUserAndDonate([
  104.                     'firstName' => 'John',
  105.                     'lastName' => 'Doe',
  106.                     'contactEmail' => '[email protected]',
  107.                     'deposit' => 100
  108.                 ]);
  109.             } catch (Exception $exception) {
  110.                 Yii::getLogger()->log($exception->getMessage(), Logger::LEVEL_ERROR);
  111.             }
  112.         }
  113.     }
  114.  
  115.     $main = (new Main())->run();
  116.  
File Description
  • Bintime
  • PHP Code
  • 02 Apr-2021
  • 4.62 Kb
You can Share it: