Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Models;
  4.  
  5. use Illuminate\Database\Eloquent\Model;
  6.  
  7. use Denpa\Bitcoin\Client as BitcoinClient;
  8.  
  9. use App\Models\Setting;
  10. use App\Models\User;
  11. use App\Models\Bonus;
  12. use App\Classes\BitcoinAPI;
  13.  
  14. class UserTransaction extends Model
  15. {
  16. protected $table = 'users_transactions';
  17.  
  18. protected $fillable = [
  19. 'user_id', 'wallet', 'txids', 'confirmations', 'status', 'amount', 'amount_cent', 'payment_method'
  20. ];
  21.  
  22. public static function getById($id) {
  23. return self::where('id', $id)->first();
  24. }
  25.  
  26. public function getFormattedAmount() {
  27. if(strtolower($this->getPaymentMethod()) == 'btc') {
  28. return $this->getFormattedBTC() . ' (' . $this->getFormattedPrice() . ')';
  29. }
  30.  
  31. return $this->getFormattedPrice();
  32. }
  33.  
  34. public function getFormattedBTC() {
  35. return number_format($this->amount / 100000, 5, ',', '.') . ' BTC';
  36. }
  37.  
  38. public function getFormattedPrice() {
  39. return number_format($this->amount_cent / 100, 2, ',', '.') . ' ' . Setting::getShopCurrency();
  40. }
  41.  
  42. public function isPaid() {
  43. return strtolower($this->status) == 'paid';
  44. }
  45.  
  46. public function isPending() {
  47. return strtolower($this->status) == 'pending';
  48. }
  49.  
  50. public function isWaiting() {
  51. return strtolower($this->status) == 'waiting';
  52. }
  53.  
  54. public function getPaymentMethod() {
  55. return strtolower($this->payment_method);
  56. }
  57.  
  58. public function getUsername() {
  59. $name = '-/-';
  60.  
  61. $user = User::where('id', $this->user_id)->get()->first();
  62.  
  63. if($user != null) {
  64. $name = $user->username;
  65. }
  66.  
  67. return $name;
  68. }
  69.  
  70. public function getDate() {
  71. return $this->created_at->format('d.m.Y');
  72. }
  73.  
  74. public function updateWhenPaidBtc() {
  75. if(strlen($this->wallet) && $this->isWaiting()) {
  76. $bitcoind = BitcoinAPI::getBitcoinClient();
  77. $receivedInfo = $bitcoind->listreceivedbyaddress(0, true, true, (string) decrypt($this->wallet))[0];
  78.  
  79. $rAmount = $receivedInfo['amount'];
  80. if($rAmount > 0) {
  81. $txIDs = $receivedInfo['txids'];
  82.  
  83. $amountCent = intval(BitcoinAPI::convertBtc($rAmount) * floatval(Setting::get('shop.bonus_in_percent', "1")));
  84.  
  85. $bonuses = Bonus::orderByDESC('min_amount')->get();
  86.  
  87. foreach($bonuses as $bonus) {
  88. if($amountCent >= $bonus->min_amount) {
  89. $amountCent = $amountCent * floatval($bonus->percent);
  90. break;
  91. }
  92. }
  93.  
  94. $this->update([
  95. 'status' => 'pending',
  96. 'amount' => intval($rAmount * 100000),
  97. 'amount_cent' => intval($amountCent),
  98. 'txid' => encrypt(implode(',', $txIDs))
  99. ]);
  100.  
  101. return true;
  102. }
  103. } else if(strlen($this->wallet) > 0 && $this->isPending()) {
  104. $bitcoind = BitcoinAPI::getBitcoinClient();
  105. $receivedInfo = $bitcoind->listreceivedbyaddress(Setting::get('shop.btc_confirms_needed'), true, true, (string) decrypt($this->wallet))[0];
  106.  
  107. $rAmount = $receivedInfo['amount'];
  108. if($rAmount > 0) {
  109. $this->update([
  110. 'status' => 'paid'
  111. ]);
  112.  
  113. $user = User::where('id', $this->user_id)->get()->first();
  114.  
  115. if($user != null) {
  116. $balance_in_cent = $user->balance_in_cent;
  117.  
  118. $user->update([
  119. 'balance_in_cent' => $balance_in_cent + $this->amount_cent
  120. ]);
  121. }
  122.  
  123. return true;
  124. }
  125. }
  126.  
  127. return false;
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement