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.
Result of php executing
Full code of Bintime.php
- <?php
- class UserService {
- public function createNewUser($userData)
- {
- //check if user exists
- if (User::find()
- ->where(['email' => $userData['contactEmail']])
- ->count()) {
- throw new Exception('Unable to add user: user with the same email exists');
- }
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $user = new User();
- $user->name = $userData['firstName'];
- $user->surname = $userData['lastName'];
- $user->email = $userData['contactEmail'];
- if ($user->save()) {
- $buyPointsAmount = 0;
- $currencyRate = 100;
- //calculate buy points
- if ($userData['deposit'] > 1000) {
- //convert money to points and add +50% as a bonus
- $buyPointsAmount = $buyPointsAmount*$currencyRate*1.5;
- } elseif ($userData['deposit'] > 500) {
- //convert money to points and add +30% as a bonus
- $buyPointsAmount = $buyPointsAmount*$currencyRate*1.3;
- } elseif ($userData['deposit'] > 200) {
- //convert money to points and add +10% as a bonus
- $buyPointsAmount = $buyPointsAmount*$currencyRate*1.1;
- } else {
- $buyPointsAmount = $buyPointsAmount*$currencyRate;
- }
- //save bonus points transaction
- $buyPointsTransaction = new BuyPointsTransaction();
- $buyPointsTransaction->user_id = $user->id;
- $buyPointsTransaction->amount = $buyPointsAmount;
- if (!$buyPointsTransaction->save()) {
- throw new Exception('Unable to save transaction'.$transaction->getErrors());
- }
- //create user's wallet and add points to it
- $wallet = new Wallet();
- $wallet->user_id = $user->id;
- $wallet->points = $buyPointsAmount;
- if (!$wallet->save()) {
- throw new Exception('Unable to save wallet'.$wallet->getErrors());
- }
- //prepare billing data for a report
- $reportData = [];
- $reportData['name'] = $userData['firstName'];
- $reportData['surname'] = $userData['lastName'];
- $reportData['paid'] = $userData['deposit'];
- $reportData['points'] = $buyPointsAmount;
- $reportData['transactionId'] = $buyPointsTransaction->id;
- $billingReport = new TxtBillingReport();
- $billingReport->addData($reportData);
- $billingReport->createReport($reportData);
- } else {
- throw new Exception('Unable to create user');
- }
- $transaction->commit();
- } catch (Exception $exception) {
- $transaction->rollBack();
- throw $exception;
- }
- }
- }
- class TxtBillingReport
- {
- private $output;
- public function addData($reportData)
- {
- $credentialsModel = OrganizationCredentials::find()->where(['organization_id' => OrganizationCredentials::PAY_TO_WIN])->one();
- $credentials = $credentialsModel->name. " ". $credentialsModel->credentials;
- $output = "Organization: ". $credentials."\n";
- $output .= "User: ".$reportData['name']. " ".$reportData['surname']."\n";
- $output .= "Paid: ".$reportData['paid']. "\n";
- $output .= "Recieved points: ".$reportData['points']."\n";
- $output .= "Date: ".date('Y-m-d H:i:s');
- $this->output = $output;
- }
- public function createReport($reportData)
- {
- file_put_contents($reportData['transactionId'].'txt', $this->output);
- }
- }
- class Main {
- public function run() {
- $userService = new UserService();
- try {
- $userService->createNewUserAndDonate([
- 'firstName' => 'John',
- 'lastName' => 'Doe',
- 'deposit' => 100
- ]);
- } catch (Exception $exception) {
- Yii::getLogger()->log($exception->getMessage(), Logger::LEVEL_ERROR);
- }
- }
- }
- $main = (new Main())->run();
File Description
- Bintime
- PHP Code
- 02 Apr-2021
- 4.62 Kb
You can Share it:
Latest PHP Pastes