Баланс пользователя не обновляется после депозита
Я использую https://coinpayments.net/ api для своего веб-сайта laravel, и когда пользователь вносит депозит, он должен отображаться в его ( $user->profile->deposit_balance), но это не работает. Как я могу решить свою проблему? Прокомментируйте, если я предоставляю меньше информации. Это мой первый пост:)
Это мой контролер депозита:
public function create()
{
$gateways = Gateway::whereStatus(1)->get();
$local_gateways = Offline::whereStatus(1)->get();
$user = Auth::user();
$settings = Settings::first();
return view('user.deposit.create', compact('gateways', 'user', 'settings', 'local_gateways'));
}
public function cryptoConfirm(Request $request)
{
$gateway = Gateway::find(3);
$user = User::findOrFail($request->nothing);
$percentage = $gateway->percent;
$fixed = $gateway->fixed;
$charge = (($percentage / 100) * $request->amount) + $fixed;
$new_amount = $request->amount - $charge;
$publicKey=$gateway->val1;
$privateKey=$gateway->val2;
$cps = new CoinPayments();
$cps->Setup($privateKey, $publicKey);
$req = array(
'amount' => $request->amount,
'currency1' => 'USD',
'currency2' => $request->currency,
'buyer_email' => $user->email,
'buyer_name' => $user->name,
'item_name' => 'Instant Deposit',
'custom' => $request->nothing,
'item_number' => $request->code.$user->id,
'address' => '',
'ipn_url' => route('userDepositCrypto'),
);
$result = $cps->CreateTransaction($req);
if ($result['error'] == 'ok') {
$deposit = Crypto::create([
'amount' => $request->amount,
'currency1' => 'USD',
'currency2' => $request->currency,
'details' => 'Instant Deposit Via Crypto Gateways',
'transaction_id' =>$request->code.$user->id,
'user_id' => $user->id,
'gateway_id' => $gateway->id,
'charge' => $charge,
'amount2'=>$result['result']['amount'],
'net_amount' => $new_amount,
'status' => 0,
'payment' => 0,
]);
return redirect($result['result']['status_url']);
} else {
print 'Error: '.$result['error']."\n";
}
}
public function cryptoStatus(Request $request)
{
$gateway = Gateway::find(3);
$settings = Settings::first();
$cp_merchant_id = $gateway->account;
$cp_ipn_secret = $gateway->val3;
$cp_debug_email = $settings->contact_email;
function errorAndDie($error_msg) {
global $cp_debug_email;
if (!empty($cp_debug_email)) {
$report = 'Error: '.$error_msg."\n\n";
$report .= "POST Data\n\n";
foreach ($_POST as $k => $v) {
$report .= "|$k| = |$v|\n";
}
mail($cp_debug_email, 'CoinPayments IPN Error', $report);
}
die('IPN Error: '.$error_msg);
}
if (!isset($_POST['ipn_mode']) || $_POST['ipn_mode'] != 'hmac') {
errorAndDie('IPN Mode is not HMAC');
}
if (!isset($_SERVER['HTTP_HMAC']) || empty($_SERVER['HTTP_HMAC'])) {
errorAndDie('No HMAC signature sent.');
}
$request = file_get_contents('php://input');
if ($request === FALSE || empty($request)) {
errorAndDie('Error reading POST data');
}
if (!isset($_POST['merchant']) || $_POST['merchant'] != trim($cp_merchant_id)) {
errorAndDie('No or incorrect Merchant ID passed');
}
$hmac = hash_hmac("sha512", $request, trim($cp_ipn_secret));
if (!hash_equals($hmac, $_SERVER['HTTP_HMAC'])) {
errorAndDie('HMAC signature does not match');
}
$txn_id = $_POST['txn_id'];
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$amount1 = floatval($_POST['amount1']);
$amount2 = floatval($_POST['amount2']);
$currency1 = $_POST['currency1'];
$currency2 = $_POST['currency2'];
$status = intval($_POST['status']);
$status_text = $_POST['status_text'];
$crypto = Crypto::whereTransaction_id($item_number)->first();
$user = $crypto->user;
$gateway = $crypto->gateway;
$order_currency = $crypto->currency1;
$order_total = $crypto->amount;
if ($currency1 != $order_currency) {
errorAndDie('Original currency mismatch!');
}
if ($amount1 < $order_total) {
errorAndDie('Amount is less than order total!');
}
if ($status >= 100 || $status == 2) {
if ($crypto->payment == 0 ){
$crypto->status = $status;
$crypto->payment = 1;
$crypto->save();
$deposit = Deposit::create([
'transaction_id' => $item_number,
'user_id' => $user->id,
'gateway_name' => $gateway->name,
'amount' => $request->amount,
'charge' => $crypto->charge,
'net_amount' => $crypto->charge,
'status' => 1,
'details' => 'Crypto Instant Deposit',
]);
$user->profile->deposit_balance = $user->profile->deposit_balance + $crypto->amount;
$user->profile->save();
}
} else if ($status < 0) {
$crypto->status = $status;
$crypto->save();
} else {
$crypto->status = $status;
$crypto->save();
}
die('IPN OK');
}
CoinPayments.php:
<?php
namespace App\Robi;
class CoinPayments
{
private $private_key = '***';
private $public_key = '***';
private $ch = null;
public function Setup($private_key, $public_key) {
$this->private_key = $private_key;
$this->public_key = $public_key;
$this->ch = null;
}
public function GetRates($short = TRUE) {
$short = $short ? 1:0;
return $this->api_call('rates', array('short' => $short));
}
public function GetBalances($all = FALSE) {
return $this->api_call('balances', array('all' => $all ? 1:0));
}
public function CreateTransactionSimple($amount, $currency1, $currency2, $buyer_email, $address='', $ipn_url='https://www.sampleplatform.com/ipn-handler-script
') {
$req = array(
'amount' => $amount,
'currency1' => $currency1,
'currency2' => $currency2,
'buyer_email' => $buyer_email,
'address' => $address,
'ipn_url' => $ipn_url,
);
return $this->api_call('create_transaction', $req);
}
public function CreateTransaction($req) {
// See https://www.coinpayments.net/apidoc-create-transaction for parameters
return $this->api_call('create_transaction', $req);
}
public function GetCallbackAddress($currency, $ipn_url = '') {
$req = array(
'currency' => $currency,
'ipn_url' => $ipn_url,
);
return $this->api_call('get_callback_address', $req);
}
public function CreateWithdrawal($amount, $currency, $address, $auto_confirm = FALSE, $ipn_url = 'https://www.sampleplatform.com/ipn-handler-script
') {
$req = array(
'amount' => $amount,
'currency' => $currency,
'address' => $address,
'auto_confirm' => $auto_confirm ? 1:0,
'ipn_url' => $ipn_url,
);
return $this->api_call('create_withdrawal', $req);
}
public function CreateTransfer($amount, $currency, $merchant, $auto_confirm = FALSE) {
$req = array(
'amount' => $amount,
'currency' => $currency,
'merchant' => $merchant,
'auto_confirm' => $auto_confirm ? 1:0,
);
return $this->api_call('create_transfer', $req);
}
public function SendToPayByName($amount, $currency, $pbntag, $auto_confirm = TRUE) {
$req = array(
'amount' => $amount,
'currency' => $currency,
'pbntag' => $pbntag,
'auto_confirm' => $auto_confirm ? 1:0,
);
return $this->api_call('create_transfer', $req);
}
private function is_setup() {
return (!empty($this->private_key) && !empty($this->public_key));
}
private function api_call($cmd, $req = array()) {
if (!$this->is_setup()) {
return array('error' => 'You have not called the Setup function with your private and public keys!');
}
// Set the API command and required fields
$req['version'] = 1;
$req['cmd'] = $cmd;
$req['key'] = $this->public_key;
$req['format'] = 'json'; //supported values are json and xml
// Generate the query string
$post_data = http_build_query($req, '', '&');
// Calculate the HMAC signature on the POST data
$hmac = hash_hmac('sha512', $post_data, $this->private_key);
// Create cURL handle and initialize (if needed)
if ($this->ch === null) {
$this->ch = curl_init('https://www.coinpayments.net/api.php');
curl_setopt($this->ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
}
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('HMAC: '.$hmac));
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post_data);
$data = curl_exec($this->ch);
if ($data !== FALSE) {
if (PHP_INT_SIZE < 8 && version_compare(PHP_VERSION, '5.4.0') >= 0) {
// 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
$dec = json_decode($data, TRUE, 512, JSON_BIGINT_AS_STRING);
} else {
$dec = json_decode($data, TRUE);
}
if ($dec !== NULL && count($dec)) {
return $dec;
} else {
// If you are using PHP 5.5.0 or higher you can use json_last_error_msg() for a better error message
return array('error' => 'Unable to parse JSON result ('.json_last_error().')');
}
} else {
return array('error' => 'cURL error: '.curl_error($this->ch));
}
}
}