138 lines
4.7 KiB
PHP
138 lines
4.7 KiB
PHP
<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Cms\Classes\Controller;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use October\Rain\Support\Facades\Event;
|
|
use TPS\Birzha\Models\Payment;
|
|
use TPS\Birzha\Classes\Payment as PaymentAPI;
|
|
use TPS\Birzha\Classes\TransactionResource;
|
|
|
|
class TransactionsApiController extends KabinetAPIController
|
|
{
|
|
|
|
public function index(Request $request) {
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'transactions_per_page' => 'numeric',
|
|
]);
|
|
|
|
if($validator->fails()) {
|
|
return response()->json($validator->errors(), 400);
|
|
}
|
|
|
|
// $transactions = $this->user->transactions()
|
|
// ->orderBy('id', 'desc')
|
|
// ->paginate($request->transactions_per_page ? $request->transactions_per_page : 5);
|
|
|
|
$transactions = TransactionResource::collection($this->user->transactions()
|
|
->orderBy('id', 'desc')
|
|
->paginate($request->transactions_per_page ? $request->transactions_per_page : 5))->response()->getData();
|
|
|
|
return response()->json($transactions, 200);
|
|
}
|
|
|
|
/**
|
|
* Get user's balance
|
|
*/
|
|
public function myBalance() {
|
|
|
|
return response()->json($this->user->getBalance() . ' TMT', 200);
|
|
}
|
|
|
|
public function updateBalance(Request $request){
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'type' => 'required|in:bank,online',
|
|
'amount' => 'required_if:type,online|numeric|gt:0',
|
|
'bank_file' => 'required_if:type,bank|mimes:pdf,jpg,png',
|
|
]);
|
|
|
|
if($validator->fails()) {
|
|
return response()->json($validator->errors(), 400);
|
|
}
|
|
|
|
$payment = $this->createNewPayment(false, $request->all());
|
|
|
|
if($payment->payment_type == 'online'){
|
|
|
|
$payment->amount = $request->get('amount');
|
|
|
|
$url = url('bank_result', ['payment_id' => $payment->id]);
|
|
|
|
try {
|
|
$response = PaymentAPI::registerOrder($payment, $url);
|
|
|
|
$result = json_decode($response->body, true);
|
|
|
|
if($result['errorCode'] == 0) {
|
|
$payment->order_id = $result['orderId'];
|
|
|
|
$payment->save();
|
|
|
|
return response()->json(['formUrl' => $result['formUrl']], 200);
|
|
} else {
|
|
return response()->json(['error' => [
|
|
'ru' => trans('validation.api.bank_services_unavailable', [], 'ru'),
|
|
'en' => trans('validation.api.bank_services_unavailable', [], 'en'),
|
|
'tm' => trans('validation.api.bank_services_unavailable', [], 'tm'),
|
|
]], 200);
|
|
}
|
|
|
|
} catch(\Throwable $th){
|
|
$payment->status = 'failed';
|
|
$payment->save();
|
|
|
|
Log::info($th->getMessage());
|
|
|
|
return response()->json(['error' => [
|
|
'ru' => trans('validation.api.online_payment_failed', [], 'ru'),
|
|
'en' => trans('validation.api.online_payment_failed', [], 'en'),
|
|
'tm' => trans('validation.api.online_payment_failed', [], 'tm'),
|
|
]], 500);
|
|
}
|
|
}
|
|
elseif($payment->payment_type == 'bank'){
|
|
$payment->amount = 0;
|
|
$payment->bank_file = \Input::file('bank_file');
|
|
|
|
if($payment->save()){
|
|
Event::fire('tps.payment.received',$payment);
|
|
return response()->json(['success' => [
|
|
'ru' => trans('validation.api.bank_payment_saved', [], 'ru'),
|
|
'en' => trans('validation.api.bank_payment_saved', [], 'en'),
|
|
'tm' => trans('validation.api.bank_payment_saved', [], 'tm'),
|
|
]], 200);
|
|
} else {
|
|
return response()->json(['error' => [
|
|
'ru' => trans('validation.api.bank_payment_failed', [], 'ru'),
|
|
'en' => trans('validation.api.bank_payment_failed', [], 'en'),
|
|
'tm' => trans('validation.api.bank_payment_failed', [], 'tm'),
|
|
]], 500);
|
|
}
|
|
}
|
|
|
|
return response()->json('The payment saving failed.', 500);
|
|
|
|
}
|
|
|
|
|
|
protected function createNewPayment($bank_file, $formData) {
|
|
$newPayment = new Payment;
|
|
$newPayment->user_id = $this->user->id;
|
|
$newPayment->amount = $formData['amount'] ?? 0;
|
|
$newPayment->payment_type = $formData['type'];
|
|
$newPayment->save();
|
|
|
|
// attach file to payment
|
|
if($bank_file) {
|
|
$newPayment->bank_file = $bank_file;
|
|
$newPayment->save();
|
|
}
|
|
|
|
return $newPayment;
|
|
}
|
|
|
|
}
|