2021-06-05 09:26:32 +00:00
< ? php
namespace App\Http\Controllers\Admin ;
2023-01-30 21:34:06 +00:00
use App\Events\PaymentEvent ;
2021-09-09 19:06:08 +00:00
use App\Events\UserUpdateCreditsEvent ;
2021-06-05 09:26:32 +00:00
use App\Http\Controllers\Controller ;
2022-08-27 16:59:07 +00:00
use App\Models\PartnerDiscount ;
2021-06-05 09:26:32 +00:00
use App\Models\Payment ;
2021-06-22 21:22:57 +00:00
use App\Models\User ;
2023-01-14 22:33:30 +00:00
use App\Models\ShopProduct ;
2021-06-23 09:28:57 +00:00
use Exception ;
2021-06-05 09:26:32 +00:00
use Illuminate\Contracts\Foundation\Application ;
use Illuminate\Contracts\View\Factory ;
use Illuminate\Contracts\View\View ;
2021-06-23 09:28:57 +00:00
use Illuminate\Http\JsonResponse ;
2021-06-05 09:26:32 +00:00
use Illuminate\Http\RedirectResponse ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\Auth ;
2023-01-03 20:17:25 +00:00
use App\Helpers\ExtensionHelper ;
2021-12-13 01:48:02 +00:00
2021-06-05 09:26:32 +00:00
class PaymentController extends Controller
{
2021-06-22 19:25:04 +00:00
/**
* @ return Application | Factory | View
*/
public function index ()
{
2021-06-05 09:26:32 +00:00
return view ( 'admin.payments.index' ) -> with ([
2023-01-05 17:01:42 +00:00
'payments' => Payment :: paginate ( 15 ),
2021-06-05 09:26:32 +00:00
]);
}
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
* @ param ShopProduct $shopProduct
2021-06-05 09:26:32 +00:00
* @ return Application | Factory | View
*/
2023-01-14 22:33:30 +00:00
public function checkOut ( ShopProduct $shopProduct )
2021-06-05 09:26:32 +00:00
{
2023-01-30 21:44:48 +00:00
$discount = PartnerDiscount :: getDiscount ();
$price = $shopProduct -> price - ( $shopProduct -> price * $discount / 100 );
2023-01-03 20:17:25 +00:00
$paymentGateways = [];
2023-01-15 00:41:16 +00:00
2023-01-30 21:44:48 +00:00
if ( $price > 0 ) {
$extensions = ExtensionHelper :: getAllExtensionsByNamespace ( 'PaymentGateways' );
// build a paymentgateways array that contains the routes for the payment gateways and the image path for the payment gateway which lays in public/images/Extensions/PaymentGateways with the extensionname in lowercase
foreach ( $extensions as $extension ) {
$extensionName = basename ( $extension );
if ( ! ExtensionHelper :: getExtensionConfig ( $extensionName , 'enabled' )) continue ; // skip if not enabled
$payment = new \stdClass ();
$payment -> name = ExtensionHelper :: getExtensionConfig ( $extensionName , 'name' );
$payment -> image = asset ( 'images/Extensions/PaymentGateways/' . strtolower ( $extensionName ) . '_logo.png' );
$paymentGateways [] = $payment ;
}
2023-01-03 20:17:25 +00:00
}
2023-01-15 00:41:16 +00:00
2021-06-05 09:26:32 +00:00
return view ( 'store.checkout' ) -> with ([
2023-01-05 17:01:42 +00:00
'product' => $shopProduct ,
2023-01-20 22:43:23 +00:00
'discountpercent' => $discount ,
'discountvalue' => $discount * $shopProduct -> price / 100 ,
2023-01-05 17:01:42 +00:00
'discountedprice' => $shopProduct -> getPriceAfterDiscount (),
'taxvalue' => $shopProduct -> getTaxValue (),
'taxpercent' => $shopProduct -> getTaxPercent (),
'total' => $shopProduct -> getTotalPrice (),
2023-01-03 20:17:25 +00:00
'paymentGateways' => $paymentGateways ,
2023-01-30 21:44:48 +00:00
'productIsFree' => $price <= 0 ,
2021-06-05 09:26:32 +00:00
]);
}
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
* @ param ShopProduct $shopProduct
2021-06-05 09:26:32 +00:00
* @ return RedirectResponse
*/
2023-01-30 21:34:06 +00:00
public function handleFreeProduct ( ShopProduct $shopProduct )
2023-01-04 17:59:52 +00:00
{
/** @var User $user */
$user = Auth :: user ();
2023-01-30 21:34:06 +00:00
//create a payment
2023-01-04 17:59:52 +00:00
$payment = Payment :: create ([
'user_id' => $user -> id ,
'payment_id' => uniqid (),
'payment_method' => 'free' ,
'type' => $shopProduct -> type ,
'status' => 'paid' ,
'amount' => $shopProduct -> quantity ,
2023-01-15 00:41:16 +00:00
'price' => $shopProduct -> price - ( $shopProduct -> price * PartnerDiscount :: getDiscount () / 100 ),
2023-01-04 17:59:52 +00:00
'tax_value' => $shopProduct -> getTaxValue (),
'tax_percent' => $shopProduct -> getTaxPercent (),
'total_price' => $shopProduct -> getTotalPrice (),
'currency_code' => $shopProduct -> currency_code ,
'shop_item_product_id' => $shopProduct -> id ,
2023-01-03 20:17:25 +00:00
]);
2023-01-04 17:59:52 +00:00
event ( new UserUpdateCreditsEvent ( $user ));
2023-01-30 21:34:06 +00:00
event ( new PaymentEvent ( $user , $payment , $shopProduct ));
2023-01-04 17:59:52 +00:00
//not sending an invoice
//redirect back to home
return redirect () -> route ( 'home' ) -> with ( 'success' , __ ( 'Your credit balance has been increased!' ));
}
2023-01-15 00:41:16 +00:00
public function pay ( Request $request )
{
2023-01-03 20:17:25 +00:00
$product = ShopProduct :: find ( $request -> product_id );
$paymentGateway = $request -> payment_method ;
2022-06-08 08:11:55 +00:00
2023-01-30 21:34:06 +00:00
// on free products, we don't need to use a payment gateway
$realPrice = $product -> price - ( $product -> price * PartnerDiscount :: getDiscount () / 100 );
if ( $realPrice <= 0 ) {
return $this -> handleFreeProduct ( $product );
}
2023-01-03 20:17:25 +00:00
return redirect () -> route ( 'payment.' . $paymentGateway . 'Pay' , [ 'shopProduct' => $product -> id ]);
2021-06-05 09:26:32 +00:00
}
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
2021-06-05 09:26:32 +00:00
*/
2021-12-14 01:17:19 +00:00
public function Cancel ( Request $request )
2021-06-05 09:26:32 +00:00
{
2023-01-14 23:41:57 +00:00
return redirect () -> route ( 'store.index' ) -> with ( 'info' , 'Payment was Canceled' );
2021-06-05 09:26:32 +00:00
}
2021-06-23 09:28:57 +00:00
/**
* @ return JsonResponse | mixed
2023-01-05 17:01:42 +00:00
*
2021-06-23 09:28:57 +00:00
* @ throws Exception
*/
public function dataTable ()
{
$query = Payment :: with ( 'user' );
return datatables ( $query )
2023-01-05 23:13:50 +00:00
2023-01-05 23:01:23 +00:00
-> addColumn ( 'user' , function ( Payment $payment ) {
2023-01-15 00:41:16 +00:00
return ( $payment -> user ) ? '<a href="' . route ( 'admin.users.show' , $payment -> user -> id ) . '">' . $payment -> user -> name . '</a>' : __ ( 'Unknown user' );
2021-06-23 09:28:57 +00:00
})
-> editColumn ( 'price' , function ( Payment $payment ) {
2021-11-05 08:00:18 +00:00
return $payment -> formatToCurrency ( $payment -> price );
})
-> editColumn ( 'tax_value' , function ( Payment $payment ) {
return $payment -> formatToCurrency ( $payment -> tax_value );
})
2021-12-15 11:16:30 +00:00
-> editColumn ( 'tax_percent' , function ( Payment $payment ) {
2023-01-15 00:41:16 +00:00
return $payment -> tax_percent . ' %' ;
2021-12-15 11:16:30 +00:00
})
2021-11-05 08:00:18 +00:00
-> editColumn ( 'total_price' , function ( Payment $payment ) {
return $payment -> formatToCurrency ( $payment -> total_price );
2021-06-23 09:28:57 +00:00
})
2021-12-15 11:16:30 +00:00
2021-06-23 09:28:57 +00:00
-> editColumn ( 'created_at' , function ( Payment $payment ) {
2023-01-15 00:41:16 +00:00
return [
'display' => $payment -> created_at ? $payment -> created_at -> diffForHumans () : '' ,
'raw' => $payment -> created_at ? strtotime ( $payment -> created_at ) : ''
];
2021-06-23 09:28:57 +00:00
})
2022-01-04 08:05:53 +00:00
-> addColumn ( 'actions' , function ( Payment $payment ) {
2023-01-15 00:41:16 +00:00
return '<a data-content="' . __ ( 'Download' ) . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route ( 'admin.invoices.downloadSingleInvoice' , 'id=' . $payment -> payment_id ) . '" class="btn btn-sm text-white btn-info mr-1"><i class="fas fa-file-download"></i></a>' ;
2022-01-04 08:05:53 +00:00
})
2022-08-27 17:44:27 +00:00
-> rawColumns ([ 'actions' , 'user' ])
2022-01-04 08:05:53 +00:00
-> make ( true );
2021-06-23 09:28:57 +00:00
}
2023-01-05 17:01:42 +00:00
}