PaymentController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. $discount = PartnerDiscount::getDiscount();
  51. return view('store.checkout')->with([
  52. 'product' => $shopProduct,
  53. 'discountpercent' => $discount,
  54. 'discountvalue' => $discount * $shopProduct->price / 100,
  55. 'discountedprice' => $shopProduct->getPriceAfterDiscount(),
  56. 'taxvalue' => $shopProduct->getTaxValue(),
  57. 'taxpercent' => $shopProduct->getTaxPercent(),
  58. 'total' => $shopProduct->getTotalPrice(),
  59. 'paymentGateways' => $paymentGateways,
  60. ]);
  61. }
  62. /**
  63. * @param Request $request
  64. * @param ShopProduct $shopProduct
  65. * @return RedirectResponse
  66. */
  67. public function FreePay(ShopProduct $shopProduct)
  68. {
  69. //check if the product is really free or the discount is 100%
  70. if ($shopProduct->getTotalPrice() > 0) return redirect()->route('home')->with('error', __('An error ocured. Please try again.'));
  71. //give product
  72. /** @var User $user */
  73. $user = Auth::user();
  74. //not updating server limit
  75. //update User with bought item
  76. if ($shopProduct->type == "Credits") {
  77. $user->increment('credits', $shopProduct->quantity);
  78. } elseif ($shopProduct->type == "Server slots") {
  79. $user->increment('server_limit', $shopProduct->quantity);
  80. }
  81. //skipped the referral commission, because the user did not pay anything.
  82. //not giving client role
  83. //store payment
  84. $payment = Payment::create([
  85. 'user_id' => $user->id,
  86. 'payment_id' => uniqid(),
  87. 'payment_method' => 'free',
  88. 'type' => $shopProduct->type,
  89. 'status' => 'paid',
  90. 'amount' => $shopProduct->quantity,
  91. 'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
  92. 'tax_value' => $shopProduct->getTaxValue(),
  93. 'tax_percent' => $shopProduct->getTaxPercent(),
  94. 'total_price' => $shopProduct->getTotalPrice(),
  95. 'currency_code' => $shopProduct->currency_code,
  96. 'shop_item_product_id' => $shopProduct->id,
  97. ]);
  98. event(new UserUpdateCreditsEvent($user));
  99. //not sending an invoice
  100. //redirect back to home
  101. return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
  102. }
  103. public function pay(Request $request)
  104. {
  105. $product = ShopProduct::find($request->product_id);
  106. $paymentGateway = $request->payment_method;
  107. return redirect()->route('payment.' . $paymentGateway . 'Pay', ['shopProduct' => $product->id]);
  108. }
  109. /**
  110. * @param Request $request
  111. */
  112. public function Cancel(Request $request)
  113. {
  114. return redirect()->route('store.index')->with('info', 'Payment was Canceled');
  115. }
  116. /**
  117. * @return JsonResponse|mixed
  118. *
  119. * @throws Exception
  120. */
  121. public function dataTable()
  122. {
  123. $query = Payment::with('user');
  124. return datatables($query)
  125. ->addColumn('user', function (Payment $payment) {
  126. return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
  127. })
  128. ->editColumn('price', function (Payment $payment) {
  129. return $payment->formatToCurrency($payment->price);
  130. })
  131. ->editColumn('tax_value', function (Payment $payment) {
  132. return $payment->formatToCurrency($payment->tax_value);
  133. })
  134. ->editColumn('tax_percent', function (Payment $payment) {
  135. return $payment->tax_percent . ' %';
  136. })
  137. ->editColumn('total_price', function (Payment $payment) {
  138. return $payment->formatToCurrency($payment->total_price);
  139. })
  140. ->editColumn('created_at', function (Payment $payment) {
  141. return [
  142. 'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
  143. 'raw' => $payment->created_at ? strtotime($payment->created_at) : ''
  144. ];
  145. })
  146. ->addColumn('actions', function (Payment $payment) {
  147. 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>';
  148. })
  149. ->rawColumns(['actions', 'user'])
  150. ->make(true);
  151. }
  152. }