286 lines
12 KiB
PHP
286 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Material;
|
|
use App\Models\Order;
|
|
use App\Models\Subscription;
|
|
use App\Models\User_sub;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
use GuzzleHttp\Client;
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
|
|
|
|
class OrderController extends Controller
|
|
{
|
|
public function buy($material_id){
|
|
|
|
$material = Material::findOrFail($material_id);
|
|
|
|
//todo material can be bought already
|
|
$order = Order::firstOrNew([
|
|
'material_id' => $material_id,
|
|
'user_id' => auth()->id()
|
|
]);
|
|
$order->category_id = $material->category_id;
|
|
$order->price = $material->price;
|
|
$order->title = $material->title;
|
|
$order->registered = 0;
|
|
$order->payed = 0;
|
|
$order->download_count = $material->download_count;
|
|
$order->downloaded = 0;
|
|
|
|
if($material->day_count != 0)
|
|
$order->last_date = Carbon::today()->addDays($material->day_count);
|
|
else
|
|
$order->last_date = null;
|
|
|
|
$order->oid = str_replace('-','',Str::orderedUuid());
|
|
$order->save();
|
|
//todo update order_count on category_material
|
|
return $this->register($order);
|
|
}
|
|
|
|
private function register(Order $order){
|
|
|
|
$url = config('app.gateway_url');
|
|
$user = config('app.gateway_user');
|
|
$pwd = config('app.gateway_password');
|
|
if(!($user && $url && $pwd)){
|
|
throw new \Exception('Method not allowed');
|
|
}
|
|
|
|
$client = new Client(['base_uri' => $url]);
|
|
try{
|
|
$response = $client->request('POST', 'register.do', [
|
|
'connect_timeout' => 15,
|
|
'timeout' => 15,
|
|
'verify' => true,
|
|
'form_params' => [
|
|
'userName' => $user,
|
|
'password' => $pwd,
|
|
'orderNumber' => $order->oid,
|
|
'currency' => 934,
|
|
'language' => 'ru',
|
|
'description'=> $order->title,
|
|
'amount' =>$order->price * 100,// amount w kopeykah
|
|
'returnUrl' => route('order_status', $order->id),
|
|
//'failUrl' => route('paymentFail', $order->id)
|
|
]
|
|
]);
|
|
|
|
$data = json_decode($response->getBody(), true);
|
|
//dd($data);
|
|
if($data['errorCode'] == 0){
|
|
$order->registered = 1;
|
|
$order->orderId = $data['orderId'];
|
|
$order->formUrl = $data['formUrl'];
|
|
|
|
}
|
|
else {
|
|
$order->registered = 0;
|
|
$order->errorMessage = $data['errorMessage'];
|
|
}
|
|
|
|
$order->errorCode = $data['errorCode'];
|
|
$order->save();
|
|
return view('order',compact('order'));
|
|
|
|
}catch (\Exception $ex){
|
|
request()->session()->flash('status','danger');
|
|
request()->session()->flash('status_message',trans('abonent.gateway_not_responding_message'));
|
|
Log::error($ex->getMessage());
|
|
//dd($ex);
|
|
return redirect()->back();
|
|
}
|
|
|
|
}
|
|
|
|
public function result($order_id){
|
|
$order = Order::findOrFail($order_id);
|
|
if($order->payed){
|
|
return view('order_complete')->with('order',$order);
|
|
}
|
|
$url = config('app.gateway_url');
|
|
$user = config('app.gateway_user');
|
|
$pwd = config('app.gateway_password');
|
|
|
|
if(!($user && $url && $pwd)){
|
|
throw new MethodNotAllowedException();
|
|
}
|
|
$client = new Client(['base_uri' => $url]);
|
|
try{
|
|
$response = $client->request('POST', 'getOrderStatus.do', [
|
|
'verify' => false,
|
|
'connect_timeout' => 15,
|
|
'timeout' => 15,
|
|
'form_params' => [
|
|
'userName' => $user,
|
|
'password' => $pwd,
|
|
'orderId' => $order->orderId,
|
|
]
|
|
]);
|
|
$data = json_decode($response->getBody(), true);
|
|
$order->errorCode = $data['ErrorCode'];
|
|
$order->orderStatus = $data['OrderStatus'];
|
|
Log::info($data);
|
|
if($data['ErrorCode'] == 0){
|
|
if($data['OrderStatus'] == 2){ //payment successfull
|
|
$order->payed = 1;
|
|
// $this->makePayment($order);
|
|
request()->session()->flash('status','success');
|
|
request()->session()->flash('status_message',trans('content.payment_successfull'));
|
|
//$order->errorMessage = trans('abonent.payment_successfull');
|
|
$status_message = 'Проведена полная авторизация суммы заказа';
|
|
}
|
|
// elseif($data['OrderStatus'] == 0){//order registered but not payed yet
|
|
// $status_message = 'Заказ зарегистрирован, но не оплачен';
|
|
// }
|
|
// elseif ($data['OrderStatus'] == 1){//Предавторизованная сумма захолдирована (для двухстадийных платежей)
|
|
// $status_message ='Предавторизованная сумма захолдирована (для двухстадийных платежей)';
|
|
// }
|
|
// elseif ($data['OrderStatus'] == 4){//Предавторизованная сумма захолдирована (для двухстадийных платежей)
|
|
// $status_message ='По транзакции была проведена операция возврата';
|
|
// }
|
|
// elseif ($data['OrderStatus'] == 5){//Предавторизованная сумма захолдирована (для двухстадийных платежей)
|
|
// $status_message ='Инициирована авторизация через ACS банка-эмитента';
|
|
// }
|
|
// else{//unsuccessfull Авторизация отменена
|
|
// $status_message ='Авторизация отменена';
|
|
// }
|
|
}
|
|
else{
|
|
// $order->status = 'payment_failed';
|
|
|
|
$order->errorMessage = $data['ErrorMessage'];
|
|
}
|
|
$order->save();
|
|
}
|
|
catch (\Exception $e){
|
|
request()->session()->flash('status','danger');
|
|
request()->session()->flash('status_message','Bank bilen aragatnaşyga geçip bolmady birsalymdan täzeden synanşyp görmegiňizi haýyş edýäris');
|
|
redirect()->back();
|
|
}
|
|
return view('order_complete')->with('order',$order);
|
|
}
|
|
|
|
public function subscribe(\Illuminate\Http\Request $request){
|
|
$url = config('app.gateway_url');
|
|
$user = config('app.gateway_user');
|
|
$pwd = config('app.gateway_password');
|
|
$client = new Client(['base_uri' => $url]);
|
|
$subscription = Subscription::find($request['subscription_type']);
|
|
if(!($user && $url && $pwd)){
|
|
throw new MethodNotAllowedException();
|
|
}
|
|
try{
|
|
$user_sub = User_sub::create([
|
|
'user_id' => auth()->id(),
|
|
'subscription_type' => $request['subscription_type'],
|
|
'start_time' => Carbon::now(),
|
|
'end_time' => Carbon::now()->addDays($subscription->duration),
|
|
'status' => false,
|
|
'sid' => uniqid()
|
|
]);
|
|
$response = $client->request('POST', 'register.do', [
|
|
'connect_timeout' => 300,
|
|
'timeout' => 300,
|
|
'verify' => true,
|
|
'form_params' => [
|
|
'userName' => $user,
|
|
'password' => $pwd,
|
|
'orderNumber' => $user_sub->sid,
|
|
'currency' => 934,
|
|
'language' => 'ru',
|
|
'description'=> $subscription->title,
|
|
'amount' =>$subscription->price * 100,// amount v kopeykah
|
|
'returnUrl' => route('substatus', $user_sub->id),
|
|
//'failUrl' => route('paymentFail', $order->id)
|
|
]
|
|
]);
|
|
$data = json_decode($response->getBody(), true);
|
|
if($data['errorCode'] == 0){
|
|
$user_sub->status = 0;
|
|
$user_sub->sid = $data['orderId'];
|
|
$user_sub->save();
|
|
return Redirect::to($data['formUrl']);
|
|
}
|
|
else {
|
|
Log::error($data['errorMessage']);
|
|
return "Registrasiýa amala aşyrylmady";
|
|
}
|
|
}
|
|
catch (\Exception $e){
|
|
request()->session()->flash('status','danger');
|
|
request()->session()->flash('status_message','Bank bilen aragatnaşyga geçip bolmady birsalymdan täzeden synanşyp görmegiňizi haýyş edýäris');
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
public function substatus($id){
|
|
$user_sub = User_sub::find($id);
|
|
if($user_sub->status){
|
|
return view('subscription_complete')->with('user_sub',$user_sub);
|
|
}
|
|
$url = config('app.gateway_url');
|
|
$user = config('app.gateway_user');
|
|
$pwd = config('app.gateway_password');
|
|
|
|
if(!($user && $url && $pwd)){
|
|
throw new MethodNotAllowedException();
|
|
}
|
|
$client = new Client(['base_uri' => $url]);
|
|
try{
|
|
$response = $client->request('POST', 'getOrderStatus.do', [
|
|
'verify' => false,
|
|
'connect_timeout' => 300,
|
|
'timeout' => 300,
|
|
'form_params' => [
|
|
'userName' => $user,
|
|
'password' => $pwd,
|
|
'orderId' => $user_sub->sid//$user_sub->sid,
|
|
]
|
|
]);
|
|
|
|
$data = json_decode($response->getBody(), true);
|
|
|
|
Log::info($data);
|
|
|
|
if($data['ErrorCode'] == 0 && $data['OrderStatus'] == 2){
|
|
$user_sub->status = 1;
|
|
|
|
// elseif($data['OrderStatus'] == 0){//order registered but not payed yet
|
|
// $status_message = 'Заказ зарегистрирован, но не оплачен';
|
|
// }
|
|
// elseif ($data['OrderStatus'] == 1){//Предавторизованная сумма захолдирована (для двухстадийных платежей)
|
|
// $status_message ='Предавторизованная сумма захолдирована (для двухстадийных платежей)';
|
|
// }
|
|
// elseif ($data['OrderStatus'] == 4){//Предавторизованная сумма захолдирована (для двухстадийных платежей)
|
|
// $status_message ='По транзакции была проведена операция возврата';
|
|
// }
|
|
// elseif ($data['OrderStatus'] == 5){//Предавторизованная сумма захолдирована (для двухстадийных платежей)
|
|
// $status_message ='Инициирована авторизация через ACS банка-эмитента';
|
|
// }
|
|
// else{//unsuccessfull Авторизация отменена
|
|
// $status_message ='Авторизация отменена';
|
|
// }
|
|
}
|
|
else{
|
|
// $order->status = 'payment_failed';
|
|
|
|
$user_sub->status = 0;
|
|
}
|
|
$user_sub->save();
|
|
}
|
|
catch (\Exception $e){
|
|
request()->session()->flash('status','danger');
|
|
request()->session()->flash('status_message','Bank bilen aragatnaşyga geçip bolmady birsalymdan täzeden synanşyp görmegiňizi haýyş edýäris');
|
|
return redirect()->back();
|
|
}
|
|
return view('subscription_complete')->with('user_sub',$user_sub);
|
|
}
|
|
}
|