<?php
namespace App\Classes;
use App\Bank\Payment;
use App\Imali\ImaliAccount;
use App\Imali\ImaliAccountConfig;
use App\Imali\MerchantAccount;
use App\PaymentGeneration;
use App\PeriodCloseStore;
use App\Refund;
use App\Store;
use App\StoreAmountGeneration;
use App\StoreConfig;
use App\User;
use App\UserClient;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use DateTime;
use DateTimeZone;
use Illuminate\Support\Str;
class PartnerKyc
{
public function checkImaliAccount(Request $request)
{
$user = User::query()
->join('imali_accounts', 'imali_accounts.user_id', '=', 'users.id')
->where('imali_accounts.account_number', $request->accountNumber)
->first();
if (!$user) {
return response()->json(['message' => 'Conta iMali Inválida'], 400);
// return response()->json([
// 'cody' => trans('error')[0]['cody'],
// 'error' => trans('error')[0]['error'],
// 'type' => trans('error')[0]['type'],
// 'message' => "Conta iMali Inválida",
// ], trans('error')[0]['http_code']);
}
}
public function checkIntegrador(Request $request)
{
$token = str_replace('Bearer ', '', $request->header('authorization'));
$userClient = UserClient::query()->where('client_key', $token)->first();
$loja = Store::query()->where('user_client_id', $userClient->id)->first();
$amountGeneration = Payment::query()
->where('partner_transaction_id', '=', $request->transactionID)
->first();
if ($request->user()->status == 0) {
//return response()->json(['message' => trans('client_blocked')], 400);
return response()->json([
'cody' => trans('error')[6]['cody'],
'error' => trans('error')[6]['error'],
'type' => trans('error')[6]['type'],
'message' => "Client blocked",
], trans('error')[6]['http_code']);
}
$stringLength = Str::length($request->transactionID);
if ($stringLength < 12) {
//return response()->json(['message' => trans('transaction_id_not_valid')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "TransactionID must be at least 12 characters long",
], trans('error')[0]['http_code']);
}
if (!is_numeric($request->amount)) {
//return response()->json(['message' => trans('invalid_amount')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Invalid numeric data",
], trans('error')[0]['http_code']);
}
if ($request->amount <= 0) {
//return response()->json(['message' => trans('invalid_amount')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Negative or zero amount not allowed",
], trans('error')[0]['http_code']);
}
if (!$loja) {
// return response()->json(['message' => trans('not_allowed_on_store')], 400);
return response()->json([
'cody' => trans('error')[3]['cody'],
'error' => trans('error')[3]['error'],
'type' => trans('error')[3]['type'],
'message' => "Not allowed to transact in this store",
], trans('error')[3]['http_code']);
}
if ($amountGeneration) {
//return response()->json(['message' => trans('transaction_in_use')], 400);
return response()->json([
'cody' => trans('error')[5]['cody'],
'error' => trans('error')[5]['error'],
'type' => trans('error')[5]['type'],
'message' => "Transaction in use",
], trans('error')[5]['http_code']);
}
}
public function checkPaymentVoucher(Request $request)
{
$sender = ImaliAccount::query()
->join('users', 'users.id', '=', 'imali_accounts.user_id')
->join('imali_account_configs', 'imali_account_configs.id', '=', 'imali_accounts.imali_account_config')
->where('imali_accounts.account_number', $request->accountNumber)
->first();
$user = User::query()
->join('imali_accounts', 'imali_accounts.user_id', '=', 'users.id')
->where('imali_accounts.account_number', $request->accountNumber)
->first();
if ($request->filled('voucher')) {
$voucherAmount = intval(preg_replace('/[^0-9]+/', '', $request->voucher), 10);
// $amountTotal = $voucherAmount + $sender->taxa;
$userBalance = $sender->balance - $sender->captive_balance;
$totalValor = $voucherAmount + $sender->taxa;
if (!is_numeric($voucherAmount)) {
return response()->json(['message' => 'Montante inválido'], 400);
}
if ($voucherAmount < 0) {
return response()->json(['message' => 'Montante negativo não permitido'], 400);
}
if ($voucherAmount == 0) {
return response()->json(['message' => 'Saldo insuficiente'], 400);
}
if ($totalValor > $userBalance) {
return response()->json(['message' => 'Saldo Insuficiente'], 400);
// return response()->json(['message' => 'Saldo Insuficiente', 'valorRecarga' => $voucherAmount, 'total' => $totalValor, 'userBalance' => $userBalance, 'balance' => $sender->balance], 400);
}
} else {
$amountTotal = $request->amount + $sender->taxa;
$userBalance = $sender->balance - $sender->captive_balance;
if ($amountTotal > $userBalance) {
return response()->json(['message' => 'Saldo Insuficiente'], 400);
// return response()->json(['message' => 'Saldo Insuficiente', 'userBalance' => $userBalance], 400);
}
}
if (!$user) {
return response()->json(['message' => 'Conta iMali Inválida'], 400);
}
if ($user->status != 1) {
return response()->json(['message' => 'Conta com Problemas, liga para 846002000'], 406);
}
if (!Hash::check($request->pin, $user->pin)) {
return response()->json(['message' => 'Pin Incorrecto'], 405);
}
$amountTotal = $request->amount + $sender->taxa;
$userBalance = $sender->balance - $sender->captive_balance;
if ($amountTotal > $userBalance) {
return response()->json(['message' => 'Saldo Insuficiente'], 409);
}
}
public function checkConfirmRefund(Request $request)
{
// $refund = Refund::query()
$refund = Payment::query()
->where('partner_transaction_id', '=', $request->partner_transaction_id)
->first();
$token = Payment::query()
->where('partner_transaction_id', '=', $request->partner_transaction_id)
->where('token', $request->token_otp)
->first();
if (!$refund) {
// return response()->json(['message' => trans('invalid_request_payment')], 404);
return response()->json([
'cody' => trans('error')[3]['cody'],
'error' => trans('error')[3]['error'],
'type' => trans('error')[3]['type'],
'message' => "Invalid request"
], trans('error')[3]['http_code']);
}
// $paymentG = Refund::query()
$paymentG = Payment::query()
->where('transaction_id', '=', $refund->transaction_id)
->where('status', '=', 'success')
->first();
if ($paymentG) {
// return response()->json(['message' => trans('request_confirmed')], 401);
return response()->json([
'cody' => trans('error')[5]['cody'],
'error' => trans('error')[5]['error'],
'type' => trans('error')[5]['type'],
'message' => "Sorry, this transaction has already been confirmed"
], trans('error')[5]['http_code']);
}
if (!$token) {
// return response()->json(['message' => trans('invalid_token')], 500);
return response()->json([
'cody' => trans('error')[3]['cody'],
'error' => trans('error')[3]['error'],
'type' => trans('error')[3]['type'],
'message' => "Invalid token"
], trans('error')[3]['http_code']);
}
if ($token) {
$start_date = new DateTime($token->created_at, new DateTimeZone('Africa/Maputo'));
$since_start = $start_date->diff(new DateTime(now(), new DateTimeZone('Africa/Maputo')));
// echo $since_start->days.' days total<br>';
// echo $since_start->y.' years<br>';
// echo $since_start->m.' months<br>';
// echo $since_start->d.' days<br>';
// echo $since_start->h.' hours<br>';
// echo $since_start->i.' minutes<br>';
// echo $since_start->s.' seconds<br>';
$minutos = 10;
if ($since_start->i >= $minutos) {
$token->update([
'estado' => 'expired',
'status' => 'expired',
]);
// return response()->json(['message' => trans('transaction_expired')], 401);
return response()->json([
'cody' => trans('error')[2]['cody'],
'error' => trans('error')[2]['error'],
'type' => trans('error')[2]['type'],
// 'message' => "Sorry, this transaction has already been confirmed"
], trans('error')[2]['http_code']);
}
}
$paymentExpired = Payment::query()
->where('partner_transaction_id', "=", $request->partner_transaction_id)
->where('status', '=', 'expired')
->first();
if ($paymentExpired) {
// return response()->json(['message' => trans('transaction_expired')], 401);
return response()->json([
'cody' => trans('error')[2]['cody'],
'error' => trans('error')[2]['error'],
'type' => trans('error')[2]['type'],
// 'message' => "Sorry, this transaction has already been confirmed"
], trans('error')[2]['http_code']);
}
$account = Store::query()->where('id', '=', $refund->store_id)->first();
if ($refund->amount <= 0) {
// return response()->json(['message' => trans('invalid_amount')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "invalid amount"
], trans('error')[0]['http_code']);
}
if (!is_numeric($refund->amount)) {
//return response()->json(['message' => trans('invalid_amount')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "invalid amount"
], trans('error')[0]['http_code']);
}
if ($account) {
$paymentsTotal = Payment::query()
->where('store_id', '=', $account->id)
->whereDate('created_at', '=', date('Y-m-d'))
->sum('amount_credited');
if ($paymentsTotal < $refund->amount) {
// return response()->json(['message' => trans('not_enough_funds')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Insufficient funds"
], trans('error')[0]['http_code']);
}
$user = User::query()
->where('id', '=', $refund->sender_id)
->first();
if ($user->status != 1) {
// return response()->json(['message' => trans('blocked_customer_account')], 400);
return response()->json([
'cody' => trans('error')[2]['cody'],
'error' => trans('error')[2]['error'],
'type' => trans('error')[2]['type'],
'message' => "Customer account blocked, contact us at 846002000"
], trans('error')[2]['http_code']);
}
}
$tokenAuth = str_replace('Bearer ', '', $request->header('authorization'));
$userClient = UserClient::query()
->where('client_key', '=', $tokenAuth)
->first();
$loja = Store::query()
->where('user_client_id', $userClient->id)
->where('id', '=', $refund->store_id)
->first();
$loja1 = Store::query()
->where('id', '=', $refund->store_id)
->first();
if (!$loja1) {
// return response()->json(['message' => trans('invalid_store')], 404);
return response()->json([
'cody' => trans('error')[3]['cody'],
'error' => trans('error')[3]['error'],
'type' => trans('error')[3]['type'],
'message' => "Invalid Store"
], trans('error')[3]['http_code']);
}
if (!$loja) {
// return response()->json(['message' => trans('not_allowed_on_store')], 400);
return response()->json([
'cody' => trans('error')[3]['cody'],
'error' => trans('error')[3]['error'],
'type' => trans('error')[3]['type'],
'message' => "Not allowed to transact in this store."
], trans('error')[3]['http_code']);
}
}
public function confirmPayment(Request $request)
{
$payment = Payment::query()
->where('partner_transaction_id', '=', $request->partner_transaction_id)
->first();
$token = Payment::query()
->where('partner_transaction_id', '=', $request->partner_transaction_id)
->where('token', '=', $request->token)
->first();
if ($request->user()->status == 0) {
return response()->json(['message' => trans('client_blocked')], 400);
}
if (!$payment) {
return response()->json(['message' => trans('invalid_request_payment')], 404);
}
$paymentG = Payment::query()
->where('transaction_id', '=', $payment->transaction_id)
->where('status', '=', 'success')
->first();
if ($paymentG) {
return response()->json(['message' => trans('request_confirmed')], 400);
}
if (!$token) {
return response()->json(['message' => trans('invalid_token')], 400);
}
if ($token) {
$start_date = new DateTime($token->created_at, new DateTimeZone('Africa/Maputo'));
$since_start = $start_date->diff(new DateTime(now(), new DateTimeZone('Africa/Maputo')));
// echo $since_start->days.' days total<br>';
// echo $since_start->y.' years<br>';
// echo $since_start->m.' months<br>';
// echo $since_start->d.' days<br>';
// echo $since_start->h.' hours<br>';
// echo $since_start->i.' minutes<br>';
// echo $since_start->s.' seconds<br>';
$minutos = 10;
if ($since_start->i >= $minutos) {
$token->update([
'status' => 'expired',
'estado' => 'expired'
]);
return response()->json(['message' => trans('transaction_expired')], 400);
}
}
$paymentExpired = Payment::query()
->where('partner_transaction_id', '=', $request->partner_transaction_id)
->where('status', '=', 'expired')
->first();
if ($paymentExpired) {
return response()->json(['message' => trans('transaction_expired')], 400);
}
$account = ImaliAccount::query()->where('account_number', $payment->customer_account_number)->first();
if ($account) {
if (!is_numeric($payment->amount)) {
return response()->json(['message' => trans('invalid_amount')], 402);
}
if ($account->balance < $payment->amount) {
return response()->json(['message' => trans('not_enough_funds')], 407);
}
$accountConfig = ImaliAccountConfig::query()
->where('id', '=', $account->imali_account_config)
->first();
$amountGeral = $accountConfig->taxa + $payment->amount;
if ($amountGeral > $account->balance) {
return response()->json(['message' => trans('not_enough_funds')], 407);
}
$user = User::query()
->where('id', $account->user_id)
->first();
if ($user->status != 1) {
return response()->json(['message' => trans('blocked_customer_account')], 408);
}
}
$storeConfig = StoreConfig::query()->where('store_id', '=', $payment->store_id)->first();
if ($storeConfig) {
if ($storeConfig->use_period == 1) {
$lastPeriod = PeriodCloseStore::query()
->join('stores', 'stores.id', '=', 'period_close_stores.store_id')
->select('period_close_stores.*')
->where('period_close_stores.store_id', '=', $payment->store_id)
->get()->last();
if ($lastPeriod) {
if ($lastPeriod->status == 'fechado') {
return response()->json(['message' => trans('store_cannot_refund_with_closed_period')], 400);
}
}
}
if ($storeConfig->use_refund == 0) {
return response()->json(['message' => trans('store_cannot_refund')], 400);
}
if ($storeConfig->accept_payment == 0) {
return response()->json(['message' => trans('store_cannot_receive_and_make_payments')], 400);
}
} else {
return response()->json(['message' => trans('store_not_configured')], 400);
}
}
public function checkPaymentGeneration(Request $request)
{
$token = str_replace('Bearer ', '', $request->header('authorization'));
$userClient = UserClient::query()
->where('client_key', '=', $token)
->first();
// $imali = ImaliAccount::query()->where('account_number', '=', $request->clientAccountNumber)->first();
//? fazer a verificacao aqui...
$imali = ImaliAccount::query()
->join('users', 'users.id', 'imali_accounts.user_id')
->where('imali_accounts.account_number', '=', $request->clientAccountNumber)
->orWhere('users.phone', '=', $request->clientAccountNumber)
->select('imali_accounts.*', 'users.phone as phone', 'imali_accounts.user_id as user_id', 'users.status', 'users.is_online')
->first();
$store = Store::query()->where('account_number', '=', $request->storeAccountNumber)->first();
// $token = str_replace('Bearer ', '', $request->header('authorization'));
$userClient2 = UserClient::query()->where('client_key', $token)->first();
$paymentG = Payment::query()
->where('partner_transaction_id', $request->transactionID)
->first();
if ($imali->status === 0) {
// return response()->json(['message' => trans('client_blocked')], 400);
return response()->json([
'cody' => trans('error')[2]['cody'],
'error' => trans('error')[2]['error'],
'type' => trans('error')[2]['type'],
'message' => trans('blocked_imali_account'),
], trans('error')[2]['http_code']);
}
if ($imali->is_online === 0) {
return response()->json([
'cody' => trans('error')[2]['cody'],
'error' => trans('error')[2]['error'],
'type' => trans('error')[2]['type'],
// 'message' => "Utilizador não está autenticado",
'message' => "Cliente iMali não está autenticado",
], trans('error')[2]['http_code']);
}
if ($request->user()->status == 0) {
// return response()->json(['message' => trans('client_blocked')], 400);
return response()->json([
'cody' => trans('error')[2]['cody'],
'error' => trans('error')[2]['error'],
'type' => trans('error')[2]['type'],
'message' => trans('blocked_imali_account'),
], trans('error')[2]['http_code']);
}
if ($paymentG) {
//return response()->json(['message' => trans('transaction_in_use')], 406);
return response()->json([
'cody' => trans('error')[5]['cody'],
'error' => trans('error')[5]['error'],
'type' => trans('error')[5]['type'],
'message' => trans('transaction_in_use'),
], trans('error')[5]['http_code']);
}
if (!is_numeric($request->amount)) {
//return response()->json(['message' => trans('invalid_amount')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('invalid_amount'),
], trans('error')[0]['http_code']);
}
///if($request->amount < 0) {
//return response()->json(['message' => trans('invalid_amount')], 402);
// }
//if ($request->amount == 0) {
//return response()->json(['message' => trans('invalid_amount')], 402);
//}
if ($request->amount <= 0) {
// return response()->json(['message' => trans('invalid_amount')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('invalid_amount'),
], trans('error')[0]['http_code']);
}
if (!$imali) {
//return response()->json(['message' => trans('invalid_imali_account')], 405);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('invalid_imali_account'),
], trans('error')[0]['http_code']);
}
if ($imali->balance < 0) {
// return response()->json(['message' => trans('negative_balance_not_allowed')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('negative_balance_not_allowed'),
], trans('error')[0]['http_code']);
}
//? Verificar se o request amount e maior que o balance do Cliente
if ($request->amount > $imali->balance) {
//return response()->json(['message' => trans('imali_low_balance')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('imali_low_balance'),
], trans('error')[0]['http_code']);
}
//? Verificar se o request amount e maior que o balance do Cliente
$loja = Store::query()
->where('user_client_id', $userClient->id)
->where('account_number', $request->storeAccountNumber)
->first();
if (!$loja) {
//return response()->json(['message' => trans('not_allowed_on_store')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('not_allowed_on_store'),
], trans('error')[0]['http_code']);
}
$log = new Record();
if (!$imali) {
$log->createLog([
'description' => 'Falha de Pagamento',
'details' => 'Conta i.Mali inválida ' . ' na conta ' . $request->clientAccountNumber,
'operation' => 'Payment',
'status' => 'Error',
'user_id' => 0
]);
//return response()->json(['message' => trans('invalid_imali_account')], 404);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('invalid_imali_account'),
], trans('error')[0]['http_code']);
}
if (!$store) {
$log->createLog([
'description' => 'Falha de Pagamento',
'details' => 'Conta da Loja inválida ' . ' na conta ' . $request->storeAccountNumber,
'operation' => 'Payment',
'status' => 'Error',
'user_id' => 0
]);
//return response()->json(['message' => trans('invalid_store_account')], 404);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('invalid_store_account'),
], trans('error')[0]['http_code']);
}
$user = User::find($imali->user_id);
$sender = ImaliAccount::query()
->join('users', 'users.id', '=', 'imali_accounts.user_id')
->join('imali_account_configs', 'imali_account_configs.id', '=', 'imali_accounts.imali_account_config')
->where('users.user_id', $user->user_id)
->select('imali_accounts.*', 'users.name', 'imali_account_configs.*')
->first();
if (!$userClient) {
$log->createLog([
'description' => 'Falha de Pagamento',
'details' => trans('invalid_key') . '-' . $store->account_number,
'operation' => 'Payment',
'status' => 'Error',
'user_id' => $user->id
]);
//return response()->json(['message' => trans('invalid_key')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('invalid_key'),
], trans('error')[0]['http_code']);
}
$storeConfig = StoreConfig::query()->where('store_id', '=', $store->id)->first();
if ($storeConfig) {
if ($storeConfig->use_period == 1) {
$lastPeriod = PeriodCloseStore::query()
->join('stores', 'stores.id', '=', 'period_close_stores.store_id')
->select('period_close_stores.*')
->where('period_close_stores.store_id', '=', $store->id)
->get()->last();
if ($lastPeriod) {
if ($lastPeriod->status == 'fechado') {
//return response()->json(['message' => trans('store_cannot_refund_with_closed_period')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('store_cannot_refund_with_closed_period'),
], trans('error')[0]['http_code']);
}
}
}
if ($storeConfig->use_refund == 0) {
//return response()->json(['message' => trans('store_cannot_refund')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('store_cannot_refund'),
], trans('error')[0]['http_code']);
}
if ($storeConfig->accept_payment == 0) {
//return response()->json(['message' => trans('store_cannot_receive_and_make_payments')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('store_cannot_receive_and_make_payments'),
], trans('error')[0]['http_code']);
}
} else {
//return response()->json(['message' => trans('store_not_configured')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => trans('store_not_configured'),
], trans('error')[0]['http_code']);
}
}
public function checkGetTransactions(Request $request, $accountNumber)
{
$token = str_replace('Bearer ', '', $request->header('authorization'));
$userClient = UserClient::query()
->where('client_key', '=', $token)
->first();
$loja = Store::query()
->where('user_client_id', '=', $userClient->id)
->where('account_number', '=', $accountNumber)
->first();
$loja1 = Store::query()
->where('account_number', '=', $accountNumber)
->first();
if ($request->user()->status == 0) {
return response()->json(['message' => trans('client_blocked')], 408);
}
if (!$loja1) {
return response()->json(['message' => trans('invalid_store')], 404);
}
if (!$loja) {
return response()->json(['message' => trans('not_allowed_on_store')], 400);
}
if ($loja1->status != 1) {
return response()->json(['message' => trans('store_blocked')], 400);
}
}
public function checkPaymentCliente(Request $request)
{
$token = str_replace('Bearer ', '', $request->header('authorization'));
$userClient = UserClient::query()
->where('client_key', '=', $token)
->first();
$imali = ImaliAccount::query()->where('account_number', '=', $request->clientAccountNumber)->first();
$store = Store::query()->where('account_number', '=', $request->storeAccountNumber)->first();
// $token = str_replace('Bearer ', '', $request->header('authorization'));
$userClient2 = UserClient::query()->where('client_key', $token)->first();
$paymentG = Payment::query()
->where('partner_transaction_id', $request->transactionID)
->first();
if ($paymentG) {
return response()->json(['message' => 'TransactionID já em uso'], 406);
}
if ($request->amount < 0) {
return response()->json(['message' => 'Montante inválido'], 402);
}
if ($request->amount == 0) {
return response()->json(['message' => 'Montante Zero não permitido'], 402);
}
if (!$imali) {
return response()->json(['message' => 'Conta do cliente inválida'], 405);
}
if ($imali->balance < 0) {
return response()->json(['message' => 'Saldo Negativo não permitido'], 402);
}
$loja = Store::query()
->where('user_client_id', $userClient->id)
->where('account_number', $request->storeAccountNumber)
->first();
if (!$loja) {
return response()->json(['message' => 'Não tem permissão de fazer operações nesta loja'], 400);
}
$log = new Record();
if (!$imali) {
$log->createLog([
'description' => 'Falha de Pagamento',
'details' => 'Conta i.Mali inválida ' . ' na conta ' . $request->clientAccountNumber,
'operation' => 'Payment',
'status' => 'Error',
'user_id' => 0
]);
return response()->json(['message' => 'Conta i.Mali inválida!'], 407);
}
if (!$store) {
$log->createLog([
'description' => 'Falha de Pagamento',
'details' => 'Conta da Loja inválida ' . ' na conta ' . $request->storeAccountNumber,
'operation' => 'Payment',
'status' => 'Error',
'user_id' => 0
]);
return response()->json(['message' => 'Conta da Loja inválida!'], 408);
}
$user = User::find($imali->user_id);
$sender = ImaliAccount::query()
->join('users', 'users.id', '=', 'imali_accounts.user_id')
->join('imali_account_configs', 'imali_account_configs.id', '=', 'imali_accounts.imali_account_config')
->where('users.user_id', $user->user_id)
->select('imali_accounts.*', 'users.name', 'imali_account_configs.*')
->first();
// if (!Hash::check($request->pin, $user->pin)) {
//
// $log->createLog([
// 'description' => 'Falha de Pagamento',
// 'details' => 'Pin Incorrecto!' . ' na conta ' . $imali->account_number,
// 'operation' => 'Payment',
// 'status' => 'Error',
// 'user_id' => $user->id
// ]);
// return response()->json(['message' => 'Pin Incorrecto!'], 405);
// }
if (!$userClient) {
$log->createLog([
'description' => 'Falha de Pagamento',
'details' => 'Chave da Api incorrecta!' . ' na conta(Loja) ' . $store->account_number,
'operation' => 'Payment',
'status' => 'Error',
'user_id' => $user->id
]);
return response()->json(['message' => 'Chave da Api incorrecta!'], 405);
}
if ($imali->balance < $request->amount + $sender->taxa) {
$log->createLog([
'description' => 'Falha de Pagamento',
'details' => 'Saldo Insuficiente' . $imali->balance . ' na conta ' . $imali->account_number,
'operation' => 'Payment',
'status' => 'Error',
'user_id' => $user->id
]);
return response()->json(['message' => 'Saldo Insuficiente, ' . ' Saldo Actual: ' . $imali->balance, 'INS-2' => 400], 400);
}
if ($sender->max_value_operation < $request->amount) {
$log->createLog([
'description' => 'Falha de Pagamento',
'details' => 'Limite por Transacção ' . $sender->max_value_operation . ' na conta ' . $imali->account_number,
'operation' => 'Payment',
'status' => 'Error',
'user_id' => $user->id
]);
return response()->json(['message' => 'Limite por Transacção ' . $sender->max_value_operation, 'INS-3' => 400], 400);
}
}
public function checkMerchantRefund(Request $request)
{
$merchantActivo = MerchantAccount::query()
->where('id', '=', $request->user()->id)
->where('status', '=', 'activa')
->first();
$merchant = MerchantAccount::query()
->join('stores', 'stores.merchant_account_id', '=', 'merchant_accounts.id')
->where('stores.account_number', '=', $request->storeAccountNumber)
->select('merchant_accounts.*', 'stores.balance as store_balance', 'stores.id as storeID')
->first();
$payment = Payment::query()
->where('transaction_id', '=', $request->paymentTransaction)
->first();
// $refundCheck = Refund::query()
// ->where('partner_transaction_id', '=', $request->partnerTransactionID)
// ->first();
$refundCheck = Payment::query()
->where('partner_transaction_id', '=', $request->partnerTransactionID)
->first();
if ($refundCheck) {
// return response()->json(['message' => trans('transaction_in_use')], 400);
return response()->json([
'cody' => trans('error')[5]['cody'],
'error' => trans('error')[5]['error'],
'type' => trans('error')[5]['type'],
'message' => "Transaction in use",
], trans('error')[5]['http_code']);
}
$checkPayment = Payment::query()
->where('transaction_id', '=', $request->paymentTransaction)
->first();
if ($checkPayment) {
$refundCheckStatus = Payment::query()
->where('transaction_id', '=', $request->paymentTransaction)
->where('status', '=', 'success')
->where('payment_type', '=', 'directo')
->first();
if (!$refundCheckStatus) {
// return response()->json(['message' => trans('transaction_cannot_refund'), 'test' => $request->paymentTransaction], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "This transaction cannot be refunded.",
'paymentTransaction' => $request->paymentTransaction
], trans('error')[0]['http_code']);
}
} else {
// return response()->json(['message' => trans('not_found_transaction')], 400);
return response()->json([
'cody' => trans('error')[3]['cody'],
'error' => trans('error')[3]['error'],
'type' => trans('error')[3]['type'],
'message' => "Transaction not found",
], trans('error')[3]['http_code']);
}
// if (!$merchant) {
// return response()->json(['message' => 'Comerciante Inválido'], 400);
// }
// if (!$merchantActivo) {
// return response()->json(['message' => 'Está conta tem problemas: Estado da conta ' . $merchantActivo->status], 400);
// }
// if (!Hash::check($request->password, $merchant->password)) {
// return response()->json(['message' => 'Password Incorrecto'], 400);
// }
// $imaliConfig = ImaliAccountConfig::find($request->user()->kyc_config_id);
if (!$merchant) {
return response()->json([
'cody' => trans('error')[3]['cody'],
'error' => trans('error')[3]['error'],
'type' => trans('error')[3]['type'],
'message' => "Invalid merchant"
], trans('error')[3]['http_code']);
}
$imaliConfig = ImaliAccountConfig::find($merchant->kyc_config_id);
// if ($request->amount + $imaliConfig->taxa_refund_mechant > $merchant->balance) {
if ($request->amount + $imaliConfig->taxa_refund_mechant > $merchant->store_balance) {
// return response()->json(['message' => trans('not_enough_funds')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Insufficient funds"
], trans('error')[0]['http_code']);
}
if (!is_numeric($request->amount)) {
// return response()->json(['message' => trans('invalid_amount')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Invalid amount"
], trans('error')[0]['http_code']);
}
if ($request->amount <= 0) {
// return response()->json(['data' => trans('invalid_amount')], 402);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Negative or zero amount not allowed",
], trans('error')[0]['http_code']);
}
if ($request->amount > ($payment->amount_credited + $imaliConfig->taxa_refund_mechant)) {
//return response()->json(['message' => trans('not_available_amount_to_refund')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Amount not available for refund",
'transaction' => $request->paymentTransaction
], trans('error')[0]['http_code']);
}
// $refund = Refund::query()
// ->where('payment_id', '=', $payment->id)
// ->where('estado', '=', 'success')
// ->first();
$refund = Payment::query()
->where('payment_id', '=', $payment->id)
->where('status', '=', 'success')
->first();
if ($refund) {
//return response()->json(['message' => trans('request_refunded')], 401);
return response()->json([
'cody' => trans('error')[5]['cody'],
'error' => trans('error')[5]['error'],
'type' => trans('error')[5]['type'],
'message' => "Sorry, this payment has already been refunded!",
], trans('error')[5]['http_code']);
}
$imaliUser = ImaliAccount::query()
->where('account_number', '=', $request->customerAccountNumber)
->first();
if (!$imaliUser) {
// return response()->json(['message' => trans('invalid_imali_account')], 404);
return response()->json([
'cody' => trans('error')[3]['cody'],
'error' => trans('error')[3]['error'],
'type' => trans('error')[3]['type'],
'message' => "Invalid iMali account",
], trans('error')[3]['http_code']);
}
// NEW CHECK OF CUSTOMER ACCOUNT
$customer = ImaliAccount::query()->where('account_number', '=', $request->customerAccountNumber)->first();
if ($payment->store_id != $merchant->storeID) {
// return response()->json(['message' => trans('store_not_in_transaction')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "This store is not part of the transaction",
], trans('error')[0]['http_code']);
}
if ($customer->user_id != $payment->sender_id) {
// return response()->json(['message' => trans('customer_not_in_transaction')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "This customer is not part of the transaction",
], trans('error')[0]['http_code']);
}
// END OF CUSTOMER CHECK
$token = str_replace('Bearer ', '', $request->header('authorization'));
$userClient = UserClient::query()
->where('client_key', '=', $token)
->first();
$loja = Store::query()
->where('user_client_id', '=', $userClient->id)
->where('account_number', '=', $request->storeAccountNumber)
->first();
if (!$loja) {
// return response()->json(['message' => trans('not_allowed_on_store')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Not allowed to transact in this store",
], trans('error')[0]['http_code']);
}
if (!$payment) {
// return response()->json(['message' => trans('invalid_payment')], 404);
return response()->json([
'cody' => trans('error')[3]['cody'],
'error' => trans('error')[3]['error'],
'type' => trans('error')[3]['type'],
'message' => "Invalid payment",
], trans('error')[3]['http_code']);
}
$storeConfig = StoreConfig::query()->where('store_id', '=', $merchant->storeID)->first();
if ($storeConfig) {
if ($storeConfig->use_period == 1) {
$lastPeriod = PeriodCloseStore::query()
->join('stores', 'stores.id', '=', 'period_close_stores.store_id')
->select('period_close_stores.*')
->where('period_close_stores.store_id', '=', $merchant->storeID)
->get()->last();
if ($lastPeriod->status == 'fechado') {
//return response()->json(['message' => trans('store_cannot_refund_with_closed_period')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "We're sorry, you can't make a refund with the closed period",
], trans('error')[0]['http_code']);
}
}
if ($storeConfig->use_refund == 0) {
// return response()->json(['message' => trans('store_cannot_refund')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Sorry, this store does not offer refunds",
], trans('error')[0]['http_code']);
}
if ($storeConfig->accept_payment == 0) {
// return response()->json(['message' => trans('store_cannot_receive_and_make_payments')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Sorry, this store does not receive or make payments",
], trans('error')[0]['http_code']);
}
} else {
//return response()->json(['message' => trans('store_not_configured')], 400);
return response()->json([
'cody' => trans('error')[0]['cody'],
'error' => trans('error')[0]['error'],
'type' => trans('error')[0]['type'],
'message' => "Sorry, this store is not set up to operate",
], trans('error')[0]['http_code']);
}
}
}