Advertisement
Guest User

Untitled

a guest
Oct 1st, 2019
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.19 KB | None | 0 0
  1. <?php
  2. /*
  3. CoinPayments.net API Class - v1.0
  4. Copyright 2014-2016 CoinPayments.net. All rights reserved.
  5. License: GPLv2 - http://www.gnu.org/licenses/gpl-2.0.txt
  6. */
  7. namespace App\Http\Controllers;
  8. namespace App\Http\Traits;
  9.  
  10. use App\agents;
  11. use App\users;
  12. use App\settings;
  13. use App\confirmations;
  14. use App\gh;
  15. use App\ph;
  16. use App\plans;
  17. use App\account;
  18. use App\cp_transactions;
  19. use App\tp_transactions;
  20. use App\user_plans;
  21. use App\deposits;
  22. use App\withdrawals;
  23. use DB;
  24. //use Illuminate\Foundation\Bus\DispatchesJobs;
  25. //use Illuminate\Routing\Controller as BaseController;
  26. //use Illuminate\Foundation\Validation\ValidatesRequests;
  27. //use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  28.  
  29. use App\Http\Controllers\Controller;
  30. use Illuminate\Support\Facades\Auth;
  31.  
  32. use Illuminate\Http\Request;
  33.  
  34. trait CPTrait{
  35. private $private_key = '78FD5C8634471B9cC38418A37F208d35214fe708FFAE02e234A044F35601606f';
  36. private $public_key = '1a514a9a95550eb33661ba43e7c6ff590c8e28c66b9a9d6098297e3a1cf6c05c';
  37. private $ch = null;
  38.  
  39. public function Setup($private_key, $public_key) {
  40. $this->private_key = $private_key;
  41. $this->public_key = $public_key;
  42. $this->ch = null;
  43. }
  44.  
  45. /**
  46. * Gets the current CoinPayments.net exchange rate. Output includes both crypto and fiat currencies.
  47. * @param short If short == TRUE (the default), the output won't include the currency names and confirms needed to save bandwidth.
  48. */
  49. public function GetRates($short = TRUE) {
  50. $short = $short ? 1:0;
  51. return $this->api_call('rates', array('short' => $short));
  52. }
  53.  
  54. /**
  55. * Gets your current coin balances (only includes coins with a balance unless all = TRUE).<br />
  56. * @param all If all = TRUE then it will return all coins, even those with a 0 balance.
  57. */
  58. public function GetBalances($all = FALSE) {
  59. return $this->api_call('balances', array('all' => $all ? 1:0));
  60. }
  61.  
  62. /**
  63. * Creates a basic transaction with minimal parameters.<br />
  64. * See CreateTransaction for more advanced features.
  65. * @param amount The amount of the transaction (floating point to 8 decimals).
  66. * @param currency1 The source currency (ie. USD), this is used to calculate the exchange rate for you.
  67. * @param currency2 The cryptocurrency of the transaction. currency1 and currency2 can be the same if you don't want any exchange rate conversion.
  68. * @param address Optionally set the payout address of the transaction. If address is empty then it will follow your payout settings for that coin.
  69. * @param ipn_url Optionally set an IPN handler to receive notices about this transaction. If ipn_url is empty then it will use the default IPN URL in your account.
  70. * @param buyer_email Optionally (recommended) set the buyer's email so they can automatically claim refunds if there is an issue with their payment.
  71. */
  72. public function CreateTransactionSimple($amount, $currency1, $currency2, $address='1B7aczSxaMbRsPJXx22TP1foaHQ6FENwTA', $ipn_url='', $buyer_email='') {
  73. $req = array(
  74. 'amount' => $amount,
  75. 'currency1' => $currency1,
  76. 'currency2' => $currency2,
  77. 'address' => $address,
  78. 'ipn_url' => $ipn_url,
  79. 'buyer_email' => $buyer_email,
  80. );
  81. return $this->api_call('create_transaction', $req);
  82. }
  83.  
  84. public function GetWithdrawalInfo($txId) {
  85. $req = array(
  86. 'txid' => $txId,
  87. );
  88. return $this->api_call('get_withdrawal_info', $req);
  89. }
  90.  
  91. public function CreateTransaction($req) {
  92. // See https://www.coinpayments.net/apidoc-create-transaction for parameters
  93. return $this->api_call('create_transaction', $req);
  94. }
  95.  
  96. /**
  97. * Creates an address for receiving payments into your CoinPayments Wallet.<br />
  98. * @param currency The cryptocurrency to create a receiving address for.
  99. * @param ipn_url Optionally set an IPN handler to receive notices about this transaction. If ipn_url is empty then it will use the default IPN URL in your account.
  100. */
  101. public function GetCallbackAddress($currency, $ipn_url = '') {
  102. $req = array(
  103. 'currency' => $currency,
  104. 'ipn_url' => $ipn_url,
  105. );
  106. return $this->api_call('get_callback_address', $req);
  107. }
  108.  
  109.  
  110. public function GetTransactionInformation($txId) {
  111. $req = array(
  112. 'txid' => $txId,
  113. );
  114. return $this->api_call('get_tx_info', $req);
  115. }
  116.  
  117.  
  118.  
  119. /**
  120. * Creates a withdrawal from your account to a specified address.<br />
  121. * @param amount The amount of the transaction (floating point to 8 decimals).
  122. * @param currency The cryptocurrency to withdraw.
  123. * @param address The address to send the coins to.
  124. * @param auto_confirm If auto_confirm is TRUE, then the withdrawal will be performed without an email confirmation.
  125. * @param ipn_url Optionally set an IPN handler to receive notices about this transaction. If ipn_url is empty then it will use the default IPN URL in your account.
  126. */
  127. public function CreateWithdrawal($amount, $add_tx_fee, $currency, $currency2, $address, $auto_confirm = FALSE, $ipn_url = '') {
  128. $req = array(
  129. 'amount' => $amount,
  130. 'add_tx_fee' => $add_tx_fee,
  131. 'currency' => $currency,
  132. 'currency2' => $currency2,
  133. 'address' => $address,
  134. 'auto_confirm' => $auto_confirm ? 1:0,
  135. 'ipn_url' => $ipn_url,
  136. );
  137. return $this->api_call('create_withdrawal', $req);
  138. }
  139.  
  140. /**
  141. * Creates a transfer from your account to a specified merchant.<br />
  142. * @param amount The amount of the transaction (floating point to 8 decimals).
  143. * @param currency The cryptocurrency to withdraw.
  144. * @param merchant The merchant ID to send the coins to.
  145. * @param auto_confirm If auto_confirm is TRUE, then the transfer will be performed without an email confirmation.
  146. */
  147. public function CreateTransfer($amount, $currency, $merchant, $auto_confirm = FALSE) {
  148. $req = array(
  149. 'amount' => $amount,
  150. 'currency' => $currency,
  151. 'merchant' => $merchant,
  152. 'auto_confirm' => $auto_confirm ? 1:0,
  153. );
  154. return $this->api_call('create_transfer', $req);
  155. }
  156.  
  157. /**
  158. * Creates a transfer from your account to a specified $PayByName tag.<br />
  159. * @param amount The amount of the transaction (floating point to 8 decimals).
  160. * @param currency The cryptocurrency to withdraw.
  161. * @param pbntag The $PayByName tag to send funds to.
  162. * @param auto_confirm If auto_confirm is TRUE, then the transfer will be performed without an email confirmation.
  163. */
  164. public function SendToPayByName($amount, $currency, $pbntag, $auto_confirm = FALSE) {
  165. $req = array(
  166. 'amount' => $amount,
  167. 'currency' => $currency,
  168. 'pbntag' => $pbntag,
  169. 'auto_confirm' => $auto_confirm ? 1:0,
  170. );
  171. return $this->api_call('create_transfer', $req);
  172. }
  173.  
  174. private function is_setup() {
  175. return (!empty($this->private_key) && !empty($this->public_key));
  176. }
  177.  
  178. private function api_call($cmd, $req = array()) {
  179. if (!$this->is_setup()) {
  180. return array('error' => 'You have not called the Setup function with your private and public keys!');
  181. }
  182.  
  183. // Set the API command and required fields
  184. $req['version'] = 1;
  185. $req['cmd'] = $cmd;
  186. $req['key'] = $this->public_key;
  187. $req['format'] = 'json'; //supported values are json and xml
  188.  
  189. // Generate the query string
  190. $post_data = http_build_query($req, '', '&');
  191.  
  192. // Calculate the HMAC signature on the POST data
  193. $hmac = hash_hmac('sha512', $post_data, $this->private_key);
  194.  
  195. // Create cURL handle and initialize (if needed)
  196. if ($this->ch === null) {
  197. $this->ch = curl_init('https://www.coinpayments.net/api.php');
  198. curl_setopt($this->ch, CURLOPT_FAILONERROR, TRUE);
  199. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
  200. curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
  201. }
  202. curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('HMAC: '.$hmac));
  203. curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post_data);
  204.  
  205. $data = curl_exec($this->ch);
  206. if ($data !== FALSE) {
  207. if (PHP_INT_SIZE < 8 && version_compare(PHP_VERSION, '5.4.0') >= 0) {
  208. // We are on 32-bit PHP, so use the bigint as string option. If you are using any API calls with Satoshis it is highly NOT recommended to use 32-bit PHP
  209. $dec = json_decode($data, TRUE, 512, JSON_BIGINT_AS_STRING);
  210. } else {
  211. $dec = json_decode($data, TRUE);
  212. }
  213. if ($dec !== NULL && count($dec)) {
  214. return $dec;
  215. } else {
  216. // If you are using PHP 5.5.0 or higher you can use json_last_error_msg() for a better error message
  217. return array('error' => 'Unable to parse JSON result ('.json_last_error().')');
  218. }
  219. } else {
  220. return array('error' => 'cURL error: '.curl_error($this->ch));
  221. }
  222. }
  223.  
  224.  
  225. public function getAncestors($array, $parent, $deposit_amount, $level = 1) {
  226. $referedMembers = '';
  227. //$array=users::all();
  228. foreach ($array as $entry) {
  229. if ($parent->ref_by == $entry->id) {
  230. // return "$entry->id <br>";
  231. $referedMembers .= '- ' . $entry->name . '<br/>';
  232. //get settings
  233. $settings=settings::where('id', '=', '1')->first();
  234.  
  235. if($level == 1){
  236. $earnings=$settings->referral_commission1*$deposit_amount/100;
  237. //add earnings to ancestor balance
  238. users::where('id',$entry->id)
  239. ->update([
  240. 'account_bal' => $entry->account_bal + $earnings,
  241. ]);
  242. //increment in agent (ref) table
  243. agents::where('agent',$entry->id)->increment('earnings', $earnings);
  244. }elseif($level == 2){
  245. $earnings=$settings->referral_commission2*$deposit_amount/100;
  246. //add earnings to ancestor balance
  247. users::where('id',$entry->id)
  248. ->update([
  249. 'account_bal' => $entry->account_bal + $earnings,
  250. ]);
  251. //increment in agent (ref) table
  252. agents::where('agent',$entry->id)->increment('earnings', $earnings);
  253. }elseif($level == 3){
  254. $earnings=$settings->referral_commission3*$deposit_amount/100;
  255. //add earnings to ancestor balance
  256. users::where('id',$entry->id)
  257. ->update([
  258. 'account_bal' => $entry->account_bal + $earnings,
  259. ]);
  260. //increment in agent (ref) table
  261. agents::where('agent',$entry->id)->increment('earnings', $earnings);
  262. }elseif($level == 4){
  263. $earnings=$settings->referral_commission4*$deposit_amount/100;
  264. //add earnings to ancestor balance
  265. users::where('id',$entry->id)
  266. ->update([
  267. 'account_bal' => $entry->account_bal + $earnings,
  268. ]);
  269. //increment in agent (ref) table
  270. agents::where('agent',$entry->id)->increment('earnings', $earnings);
  271. }elseif($level == 5){
  272. $earnings=$settings->referral_commission5*$deposit_amount/100;
  273. //add earnings to ancestor balance
  274. users::where('id',$entry->id)
  275. ->update([
  276. 'account_bal' => $entry->account_bal + $earnings,
  277. ]);
  278. //increment in agent (ref) table
  279. agents::where('agent',$entry->id)->increment('earnings', $earnings);
  280. }
  281.  
  282. $referedMembers .= $this->getAncestors($array, $entry, $level+1);
  283.  
  284. }
  285. }
  286. //return $referedMembers;
  287. }
  288.  
  289.  
  290.  
  291.  
  292.  
  293. //COINPAYMENT TRANSACTIONS
  294.  
  295. // pay with coinpayment option
  296. // pay with coinpayment option
  297. public function paywithcp($amount, $coin, $ui, $msg){
  298.  
  299. //fetch settings
  300. $settings=settings::where('id','1')->first();
  301.  
  302. $user=users::where('id',$ui)->first();
  303.  
  304. //create transaction
  305. if($msg=='new')
  306. {
  307.  
  308. //fetch Coinpayment merchant credentials
  309. $cpd=cp_transactions::where('id','1')->first();
  310.  
  311. $this->Setup($cpd->cp_pv_key, $cpd->cp_p_key);
  312.  
  313. if($coin=='ETH'){
  314. $address=$settings->eth_address;
  315. }elseif($coin=='BTC'){
  316. $address=$settings->btc_address;
  317. }elseif($coin=='LTC'){
  318. $address=$settings->ltc_address;
  319. }
  320.  
  321. $req = array(
  322. 'amount' => $amount,
  323. 'currency1' => 'USD',
  324. 'currency2' => $coin,
  325. 'buyer_email' => $user->email,
  326. 'buyer_name' => $user->name,
  327. 'address' => $address, // leave blank send to follow your settings on the Coin Settings page
  328. 'item_name' => 'Account deposit',
  329. 'ipn_url' => $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'_p',
  330. );
  331. // See https://www.coinpayments.net/apidoc-create-transaction for all of the available fields
  332.  
  333. $result = $this->CreateTransaction($req);
  334. if ($result['error'] == 'ok') {
  335.  
  336. //if transacton created successful
  337.  
  338. $txn_id = $result['result']['txn_id'];
  339.  
  340. //store transacton details in DB
  341. $txn=new cp_transactions();
  342.  
  343. $txn->txn_id= $txn_id;
  344. $txn->item_name= 'Account deposit';
  345. $txn->type= 'Deposit';
  346. $txn->amount_paid= $amount;
  347. $txn->user_id= $ui;
  348. $txn->currency1= 'USD';
  349. $txn->currency2= $coin;
  350.  
  351. $txn->save();
  352.  
  353. $res = $this->GetTransactionInformation($txn_id);
  354. //get the array info of transaction
  355. if ($res['error'] == 'ok') {
  356. //$msg = $res['result']['payment_address']." \n\n Amount: ".$res['result']['amountf']."\n Coin: ".$res['result']['coin'] ;
  357. } else {
  358. return "Transaction error!";
  359. }
  360.  
  361. //if transacton created successful
  362.  
  363. //return $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'_p';
  364.  
  365. return view('payment')
  366. ->with(array(
  367. 'title'=>'Complete Payment',
  368. 'amount'=> $result['result']['amount'],
  369. 'coin'=> $coin,
  370. 'p_address'=> $result['result']['address'],
  371. 'p_qrcode'=> $result['result']['qrcode_url'],
  372. 'settings' => settings::where('id','1')->first(),
  373. ));
  374. echo'<link href="'.asset('css/bootstrap.css').'" rel="stylesheet">
  375. <script src="https://code.jquery.com/jquery.js"></script>
  376. <script src="'.asset('js/bootstrap.min.js').'"></script>';
  377. return('<div style="border:1px solid #f2f2f2; padding:10px; margin:150px; color:#d0d0d0; text-align:center;"><h1>You will be redirected to your payment page!</h1>
  378.  
  379. <h4 style="color:#222;">Click on the button below to proceed.</h4>
  380.  
  381. <form action="'.$result['result']['status_url'].'" method="post">
  382. <input type="hidden" name="_token" value="'.csrf_token().'">
  383. <input type="submit" class="btn btn-default" name="submit" value="Proceed">
  384. <a href="'.route('deposits').'" class="btn btn-default">Cancel</a>
  385. </form>
  386. </div>
  387.  
  388. ');
  389.  
  390. //$le = php_sapi_name() == 'cli' ? "\n" : '<br />';
  391. //print 'Transaction created with ID: '.$result['result']['txn_id'].$le;
  392. //print 'Buyer should send '.sprintf('%.08f', $result['result']['amount']).$coin.$le;
  393. //print 'Status URL: '.$result['result']['status_url'].$le;
  394. } else {
  395. print 'Error: '.$result['error']."\n";
  396. }
  397.  
  398. }elseif($msg=='new_p')
  399. {
  400. //credit fund to user account
  401. users::where('id',$ui)->update([
  402. 'account_bal' => $user->account_bal + $amount,
  403. ]);
  404. return redirect()->route('dashboard')->with('message','Payment successful!');
  405. }
  406.  
  407. }
  408.  
  409. public function cpaywithcp(){
  410.  
  411. //fetch settings
  412. $settings=settings::where('id','1')->first();
  413.  
  414. //fetch Coinpayment merchant credentials
  415. $cpd=cp_transactions::where('id','1')->first();
  416.  
  417. $this->Setup($cpd->cp_pv_key, $cpd->cp_p_key);
  418.  
  419. //fetch Coinpayment unconfirmed txns
  420. $cp_txns=cp_transactions::where('status',NULL)->get();
  421. if(count($cp_txns) > 0){
  422.  
  423. foreach($cp_txns as $txn){
  424.  
  425. //check if it is withdrawal transaction
  426. /*if($txn->type=="Withdrawal"){
  427. $txId=$txn->txn_id;
  428. $result = $this->GetWithdrawalInfo($txId);
  429.  
  430. //get the array info of transaction
  431. if ($result['error'] == 'ok') {
  432.  
  433. $status = $result['result']['status'];
  434.  
  435. if ($status == 2) {
  436. // payment is complete
  437.  
  438. //Update txn info on cp_txns
  439. cp_transactions::where('id',$txn->id)
  440. ->update([
  441. 'status' => $result['result']['status'],
  442. 'status_text' => $result['result']['status_text'],
  443. ]);
  444.  
  445. //get user
  446. $user=users::where('tele_id',$txn->user_tele_id)->first();
  447.  
  448.  
  449. //save withdrawal info
  450. $dp=new withdrawals();
  451.  
  452. $dp->amount= $txn->amount_paid;
  453. $dp->payment_mode= 'CoinPayments';
  454. $dp->status= 'processed';
  455. $dp->user= $user->id;
  456. $dp->save();
  457.  
  458. }
  459. }else {
  460. print 'Error: '.$result['error']."\n";
  461. }
  462. }*/
  463.  
  464. //else{
  465. $txn_id=$txn->txn_id;
  466. $result = $this->GetTransactionInformation($txn_id);
  467.  
  468. //get the array info of transaction
  469. if ($result['error'] == 'ok') {
  470.  
  471. $status = $result['result']['status'];
  472.  
  473. if ($status >= 100 || $status == 2) {
  474. // payment is complete or queued for nightly payout, success
  475.  
  476. //Update txn info on cp_txns
  477. cp_transactions::where('id',$txn->id)
  478. ->update([
  479. 'status' => $result['result']['status'],
  480. 'status_text' => $result['result']['status_text'],
  481. ]);
  482.  
  483. //get user
  484. $user=users::where('id',$txn->user_id)->first();
  485.  
  486. //add funds to user's account
  487. users::where('id',$user->id)
  488. ->update([
  489. 'account_bal' => $user->account_bal + $txn->amount_paid,
  490. ]);
  491.  
  492. //save deposit info
  493. $dp=new deposits();
  494.  
  495. $dp->txn_id= $txn_id;
  496. $dp->amount= $txn->amount_paid;
  497. $dp->payment_mode= 'CoinPayments';
  498. $dp->status= 'processed';
  499. $dp->proof= 'CoinPayments';
  500. $dp->plan= "Account deposit";
  501. $dp->user= $user->id;
  502. $dp->save();
  503.  
  504. //give % to referral
  505. if(!empty($user->ref_by) || $user->ref_by !="0"){
  506. //start to Process referral
  507.  
  508. //get the referral
  509. $duser=users::where('id',$user->ref_by)->first();
  510. //fetch site settings
  511. $settings=settings::where('id','1')->first();
  512. //increment the user's earnings
  513. $earnings=$settings->referral_commission*$txn->amount_paid/100;
  514. agents::where('agent',$duser->id)->increment('earnings', $earnings);
  515.  
  516.  
  517. //Add ref earning to the users account balance
  518. users::where('id', $duser->id)
  519. ->update([
  520. 'account_bal'=>$duser->account_bal+$earnings,
  521. ]);
  522.  
  523. //credit commission to ancestors
  524. $deposit_amount = $txn->amount_paid;
  525. $array=users::all();
  526. $parent=users::where('id',$user->ref_by)->first();;
  527. $this->getAncestors($array, $parent, $deposit_amount);
  528.  
  529. }
  530.  
  531.  
  532.  
  533. } else if ($status < 0) {
  534. //payment error, this is usually final but payments will sometimes be reopened if there was no exchange rate conversion or with seller consent
  535. //Update txn info on cp_txns
  536. cp_transactions::where('id',$txn->id)
  537. ->update([
  538. 'status' => $result['result']['status'],
  539. 'status_text' => $result['result']['status_text'],
  540. ]);
  541. } else {
  542. //payment is pending, you can optionally add a note to the order page
  543. //Update txn info on cp_txns
  544. cp_transactions::where('id',$txn->id)
  545. ->update([
  546. //'status' => $result['result']['status'],
  547. 'status_text' => $result['result']['status_text'],
  548. ]);
  549. }
  550. print_r ($result);
  551.  
  552.  
  553.  
  554. }
  555. else {
  556. print 'Error: '.$result['error']."\n";
  557. }
  558. //}
  559. }//end foreach
  560.  
  561. }else{
  562. echo "No new transactions!";
  563. }
  564.  
  565. }
  566.  
  567.  
  568.  
  569.  
  570.  
  571. public function cpwithdraw($amount, $coin, $wallet, $ui, $to_withdraw){
  572. //fetch Coinpayment merchant credentials
  573. $cpd=cp_transactions::where('id','1')->first();
  574. //get settings
  575. $settings=settings::where('id','1')->first();
  576. $this->Setup($cpd->cp_pv_key, $cpd->cp_p_key);
  577.  
  578. $amount=$amount;
  579. $add_tx_fee=1;
  580. $currency=$coin;
  581. $currency2=$settings->s_currency;
  582. $address=$wallet;
  583.  
  584. $result = $this->CreateWithdrawal($amount, $add_tx_fee, $currency, $currency2, $address);
  585. if ($result['error'] == 'ok') {
  586. //print 'Withdrawal created with ID: '.$result['result']['id'];
  587. $txn_id = $result['result']['id'];
  588. //if transacton created successful
  589. $stts=$result['result']['status'];
  590.  
  591. //fetch user
  592. $user=users::where('id',$ui)->first();
  593. //save withdrawal info
  594. $dp=new withdrawals();
  595.  
  596. $dp->txn_id= $txn_id;
  597. $dp->amount= $amount;
  598. $dp->to_deduct= $to_withdraw;
  599. $dp->payment_mode= "$coin CoinPayments";
  600. $dp->status= 'processed';
  601. $dp->user= $user->id;
  602. $dp->save();
  603.  
  604. //store transacton details in DB
  605. $txn=new cp_transactions();
  606.  
  607. $txn->txn_id= $txn_id;
  608. $txn->item_name= 'Funds withdrawal';
  609. $txn->type= 'Withdrawal';
  610. $txn->amount_paid= $amount;
  611. $txn->user_id= $ui;
  612. $txn->status= $stts;
  613. $txn->currency1= 'USD';
  614. $txn->currency2= $coin;
  615.  
  616. $txn->save();
  617.  
  618. return redirect()->back()
  619. ->with('message', 'Withrawal created successful! You will recieve your funds soon.');
  620. } else {
  621. //print 'Error: '.$result['error']."\n";
  622. return($result['error']);
  623. }
  624. }
  625.  
  626.  
  627.  
  628.  
  629. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement