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