PaymentController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Events\UserUpdateCreditsEvent;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\PartnerDiscount;
  6. use App\Models\Payment;
  7. use App\Models\User;
  8. use App\Models\ShopProduct;
  9. use Exception;
  10. use Illuminate\Contracts\Foundation\Application;
  11. use Illuminate\Contracts\View\Factory;
  12. use Illuminate\Contracts\View\View;
  13. use Illuminate\Http\JsonResponse;
  14. use Illuminate\Http\RedirectResponse;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Auth;
  17. use App\Helpers\ExtensionHelper;
  18. class PaymentController extends Controller
  19. {
  20. /**
  21. * @return Application|Factory|View
  22. */
  23. public function index()
  24. {
  25. return view('admin.payments.index')->with([
  26. 'payments' => Payment::paginate(15),
  27. ]);
  28. }
  29. /**
  30. * @param Request $request
  31. * @param ShopProduct $shopProduct
  32. * @return Application|Factory|View
  33. */
  34. public function checkOut(ShopProduct $shopProduct)
  35. {
  36. // get all payment gateway extensions
  37. $extensions = glob(app_path() . '/Extensions/PaymentGateways/*', GLOB_ONLYDIR);
  38. // 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
  39. $paymentGateways = [];
  40. foreach ($extensions as $extension) {
  41. $extensionName = basename($extension);
  42. $config = ExtensionHelper::getExtensionConfig($extensionName, 'PaymentGateways');
  43. if ($config) {
  44. $payment = new \stdClass();
  45. $payment->name = $config['name'];
  46. $payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png');
  47. $paymentGateways[] = $payment;
  48. }
  49. }
  50. return view('store.checkout')->with([
  51. 'product' => $shopProduct,
  52. 'discountpercent' => PartnerDiscount::getDiscount(),
  53. 'discountvalue' => PartnerDiscount::getDiscount() * $shopProduct->price / 100,
  54. 'discountedprice' => $shopProduct->getPriceAfterDiscount(),
  55. 'taxvalue' => $shopProduct->getTaxValue(),
  56. 'taxpercent' => $shopProduct->getTaxPercent(),
  57. 'total' => $shopProduct->getTotalPrice(),
  58. 'paymentGateways' => $paymentGateways,
  59. ]);
  60. }
  61. /**
  62. * @param Request $request
  63. * @param ShopProduct $shopProduct
  64. * @return RedirectResponse
  65. */
  66. public function FreePay(ShopProduct $shopProduct)
  67. {
  68. //check if the product is really free or the discount is 100%
  69. if ($shopProduct->getTotalPrice() > 0) return redirect()->route('home')->with('error', __('An error ocured. Please try again.'));
  70. //give product
  71. /** @var User $user */
  72. $user = Auth::user();
  73. //not updating server limit
  74. //update User with bought item
  75. if ($shopProduct->type == "Credits") {
  76. $user->increment('credits', $shopProduct->quantity);
  77. } elseif ($shopProduct->type == "Server slots") {
  78. $user->increment('server_limit', $shopProduct->quantity);
  79. }
  80. //skipped the referral commission, because the user did not pay anything.
  81. //not giving client role
  82. //store payment
  83. $payment = Payment::create([
  84. 'user_id' => $user->id,
  85. 'payment_id' => uniqid(),
  86. 'payment_method' => 'free',
  87. 'type' => $shopProduct->type,
  88. 'status' => 'paid',
  89. 'amount' => $shopProduct->quantity,
  90. 'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
  91. 'tax_value' => $shopProduct->getTaxValue(),
  92. 'tax_percent' => $shopProduct->getTaxPercent(),
  93. 'total_price' => $shopProduct->getTotalPrice(),
  94. 'currency_code' => $shopProduct->currency_code,
  95. 'shop_item_product_id' => $shopProduct->id,
  96. ]);
  97. event(new UserUpdateCreditsEvent($user));
  98. //not sending an invoice
  99. //redirect back to home
  100. return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
  101. }
  102. public function pay(Request $request)
  103. {
  104. $product = ShopProduct::find($request->product_id);
  105. $paymentGateway = $request->payment_method;
  106. return redirect()->route('payment.' . $paymentGateway . 'Pay', ['shopProduct' => $product->id]);
  107. }
  108. /**
  109. * @param Request $request
  110. */
  111. public function Cancel(Request $request)
  112. {
  113. return redirect()->route('store.index')->with('info', 'Payment was Canceled');
  114. }
  115. /**
  116. * @return JsonResponse|mixed
  117. *
  118. * @throws Exception
  119. */
  120. public function dataTable()
  121. {
  122. $query = Payment::with('user');
  123. return datatables($query)
  124. ->addColumn('user', function (Payment $payment) {
  125. return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
  126. })
  127. ->editColumn('price', function (Payment $payment) {
  128. return $payment->formatToCurrency($payment->price);
  129. })
  130. ->editColumn('tax_value', function (Payment $payment) {
  131. return $payment->formatToCurrency($payment->tax_value);
  132. })
  133. ->editColumn('tax_percent', function (Payment $payment) {
  134. return $payment->tax_percent . ' %';
  135. })
  136. ->editColumn('total_price', function (Payment $payment) {
  137. return $payment->formatToCurrency($payment->total_price);
  138. })
  139. ->editColumn('created_at', function (Payment $payment) {
  140. return [
  141. 'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
  142. 'raw' => $payment->created_at ? strtotime($payment->created_at) : ''
  143. ];
  144. })
  145. ->addColumn('actions', function (Payment $payment) {
  146. 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>';
  147. })
  148. ->rawColumns(['actions', 'user'])
  149. ->make(true);
  150. }
  151. }