PaymentController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Events\PaymentEvent;
  4. use App\Events\UserUpdateCreditsEvent;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\PartnerDiscount;
  7. use App\Models\Payment;
  8. use App\Models\User;
  9. use App\Models\ShopProduct;
  10. use Exception;
  11. use Illuminate\Contracts\Foundation\Application;
  12. use Illuminate\Contracts\View\Factory;
  13. use Illuminate\Contracts\View\View;
  14. use Illuminate\Http\JsonResponse;
  15. use Illuminate\Http\RedirectResponse;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use App\Helpers\ExtensionHelper;
  19. use App\Settings\GeneralSettings;
  20. use App\Settings\LocaleSettings;
  21. class PaymentController extends Controller
  22. {
  23. /**
  24. * @return Application|Factory|View
  25. */
  26. public function index(LocaleSettings $locale_settings)
  27. {
  28. return view('admin.payments.index')->with([
  29. 'payments' => Payment::paginate(15),
  30. 'locale_datatables' => $locale_settings->datatables
  31. ]);
  32. }
  33. /**
  34. * @param Request $request
  35. * @param ShopProduct $shopProduct
  36. * @return Application|Factory|View
  37. */
  38. public function checkOut(ShopProduct $shopProduct, GeneralSettings $general_settings)
  39. {
  40. $discount = PartnerDiscount::getDiscount();
  41. $price = $shopProduct->price - ($shopProduct->price * $discount / 100);
  42. $paymentGateways = [];
  43. if ($price > 0) {
  44. $extensions = ExtensionHelper::getAllExtensionsByNamespace('PaymentGateways');
  45. // 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
  46. foreach ($extensions as $extension) {
  47. $extensionName = basename($extension);
  48. $extensionSettings = ExtensionHelper::getExtensionSettings($extensionName);
  49. if ($extensionSettings->enabled == false) continue;
  50. $payment = new \stdClass();
  51. $payment->name = ExtensionHelper::getExtensionConfig($extensionName, 'name');
  52. $payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png');
  53. $paymentGateways[] = $payment;
  54. }
  55. }
  56. return view('store.checkout')->with([
  57. 'product' => $shopProduct,
  58. 'discountpercent' => $discount,
  59. 'discountvalue' => $discount * $shopProduct->price / 100,
  60. 'discountedprice' => $shopProduct->getPriceAfterDiscount(),
  61. 'taxvalue' => $shopProduct->getTaxValue(),
  62. 'taxpercent' => $shopProduct->getTaxPercent(),
  63. 'total' => $shopProduct->getTotalPrice(),
  64. 'paymentGateways' => $paymentGateways,
  65. 'productIsFree' => $price <= 0,
  66. 'credits_display_name' => $general_settings->credits_display_name
  67. ]);
  68. }
  69. /**
  70. * @param Request $request
  71. * @param ShopProduct $shopProduct
  72. * @return RedirectResponse
  73. */
  74. public function handleFreeProduct(ShopProduct $shopProduct)
  75. {
  76. /** @var User $user */
  77. $user = Auth::user();
  78. //create a payment
  79. $payment = Payment::create([
  80. 'user_id' => $user->id,
  81. 'payment_id' => uniqid(),
  82. 'payment_method' => 'free',
  83. 'type' => $shopProduct->type,
  84. 'status' => 'paid',
  85. 'amount' => $shopProduct->quantity,
  86. 'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
  87. 'tax_value' => $shopProduct->getTaxValue(),
  88. 'tax_percent' => $shopProduct->getTaxPercent(),
  89. 'total_price' => $shopProduct->getTotalPrice(),
  90. 'currency_code' => $shopProduct->currency_code,
  91. 'shop_item_product_id' => $shopProduct->id,
  92. ]);
  93. event(new UserUpdateCreditsEvent($user));
  94. event(new PaymentEvent($user, $payment, $shopProduct));
  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. // on free products, we don't need to use a payment gateway
  104. $realPrice = $product->price - ($product->price * PartnerDiscount::getDiscount() / 100);
  105. if ($realPrice <= 0) {
  106. return $this->handleFreeProduct($product);
  107. }
  108. return redirect()->route('payment.' . $paymentGateway . 'Pay', ['shopProduct' => $product->id]);
  109. }
  110. /**
  111. * @param Request $request
  112. */
  113. public function Cancel(Request $request)
  114. {
  115. return redirect()->route('store.index')->with('info', 'Payment was Canceled');
  116. }
  117. /**
  118. * @return JsonResponse|mixed
  119. *
  120. * @throws Exception
  121. */
  122. public function dataTable()
  123. {
  124. $query = Payment::with('user');
  125. return datatables($query)
  126. ->addColumn('user', function (Payment $payment) {
  127. return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
  128. })
  129. ->editColumn('price', function (Payment $payment) {
  130. return $payment->formatToCurrency($payment->price);
  131. })
  132. ->editColumn('tax_value', function (Payment $payment) {
  133. return $payment->formatToCurrency($payment->tax_value);
  134. })
  135. ->editColumn('tax_percent', function (Payment $payment) {
  136. return $payment->tax_percent . ' %';
  137. })
  138. ->editColumn('total_price', function (Payment $payment) {
  139. return $payment->formatToCurrency($payment->total_price);
  140. })
  141. ->editColumn('created_at', function (Payment $payment) {
  142. return [
  143. 'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
  144. 'raw' => $payment->created_at ? strtotime($payment->created_at) : ''
  145. ];
  146. })
  147. ->addColumn('actions', function (Payment $payment) {
  148. 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>';
  149. })
  150. ->rawColumns(['actions', 'user'])
  151. ->make(true);
  152. }
  153. }