app.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. require_once __DIR__ . "/vendor/autoload.php";
  3. error_reporting(E_ALL);
  4. setlocale(LC_ALL, 'ru_RU.UTF-8');
  5. if(empty($_ENV['YOOMONEY_ACCESS_TOKEN']) || empty($_ENV['YOOMONEY_TRANSFER_TO'])) {
  6. throw new Exception('Environment variables `YOOMONEY_ACCESS_TOKEN` and `YOOMONEY_TRANSFER_TO` must be set');
  7. }
  8. if(empty($_ENV['METRICS_URL']) || empty($_ENV['METRICS_USER']) || empty($_ENV['METRICS_PASSWORD']) || empty($_ENV['METRICS_DATABASE'])) {
  9. throw new Exception('Environment variables `METRICS_URL`, `METRICS_USER`, `METRICS_PASSWORD` and `METRICS_DATABASE` must be set');
  10. }
  11. $yooMoney = new YooMoney($_ENV['YOOMONEY_ACCESS_TOKEN']);
  12. $metrics = new Metrics($_ENV['METRICS_URL'], $_ENV['METRICS_USER'], $_ENV['METRICS_PASSWORD'], $_ENV['METRICS_DATABASE']);
  13. $info = $yooMoney->getInfo();
  14. $metrics->send('yoomoney', ['balance' => $info['balance']], ['account' => $info['account']]);
  15. if(!empty($_ENV['YOOMONEY_TRANSFER_TO_ACCESS_TOKEN'])) {
  16. $infoTransferTo = (new YooMoney($_ENV['YOOMONEY_TRANSFER_TO_ACCESS_TOKEN']))->getInfo();
  17. $metrics->send('yoomoney', ['balance' => $infoTransferTo['balance']], ['account' => $infoTransferTo['account']]);
  18. }
  19. if($info['balance'] < 2 || $info['balance'] == floor($info['balance'])) {
  20. exit();
  21. }
  22. $transferAmount = $info['balance'] - floor($info['balance']) + 2;
  23. if($transferAmount > $info['balance']) {
  24. exit();
  25. }
  26. $response = $yooMoney->transfer($_ENV['YOOMONEY_TRANSFER_TO'], $transferAmount, 'Rounding from ' . $info['account'], 'Rounding', 'rounding');
  27. $yooMoney->confirm($response['request_id']);