PaymentController.php 6.3 KB

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