PaymentController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. const BUY_PERMISSION = 'user.shop.buy';
  24. const VIEW_PERMISSION = "admin.payments.read";
  25. /**
  26. * @return Application|Factory|View
  27. */
  28. public function index(LocaleSettings $locale_settings)
  29. {
  30. $this->checkPermission(self::VIEW_PERMISSION);
  31. return view('admin.payments.index')->with([
  32. 'payments' => Payment::paginate(15),
  33. 'locale_datatables' => $locale_settings->datatables
  34. ]);
  35. }
  36. /**
  37. * @param Request $request
  38. * @param ShopProduct $shopProduct
  39. * @return Application|Factory|View
  40. */
  41. public function checkOut(ShopProduct $shopProduct, GeneralSettings $general_settings)
  42. {
  43. $this->checkPermission(self::BUY_PERMISSION);
  44. $discount = PartnerDiscount::getDiscount();
  45. $price = $shopProduct->price - ($shopProduct->price * $discount / 100);
  46. $paymentGateways = [];
  47. if ($price > 0) {
  48. $extensions = ExtensionHelper::getAllExtensionsByNamespace('PaymentGateways');
  49. // 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
  50. foreach ($extensions as $extension) {
  51. $extensionName = basename($extension);
  52. $extensionSettings = ExtensionHelper::getExtensionSettings($extensionName);
  53. if ($extensionSettings->enabled == false) continue;
  54. $payment = new \stdClass();
  55. $payment->name = ExtensionHelper::getExtensionConfig($extensionName, 'name');
  56. $payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png');
  57. $paymentGateways[] = $payment;
  58. }
  59. }
  60. return view('store.checkout')->with([
  61. 'product' => $shopProduct,
  62. 'discountpercent' => $discount,
  63. 'discountvalue' => $discount * $shopProduct->price / 100,
  64. 'discountedprice' => $shopProduct->getPriceAfterDiscount(),
  65. 'taxvalue' => $shopProduct->getTaxValue(),
  66. 'taxpercent' => $shopProduct->getTaxPercent(),
  67. 'total' => $shopProduct->getTotalPrice(),
  68. 'paymentGateways' => $paymentGateways,
  69. 'productIsFree' => $price <= 0,
  70. 'credits_display_name' => $general_settings->credits_display_name
  71. ]);
  72. }
  73. /**
  74. * @param Request $request
  75. * @param ShopProduct $shopProduct
  76. * @return RedirectResponse
  77. */
  78. public function handleFreeProduct(ShopProduct $shopProduct)
  79. {
  80. /** @var User $user */
  81. $user = Auth::user();
  82. //create a 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. event(new PaymentEvent($user, $payment, $shopProduct));
  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. // on free products, we don't need to use a payment gateway
  108. $realPrice = $product->price - ($product->price * PartnerDiscount::getDiscount() / 100);
  109. if ($realPrice <= 0) {
  110. return $this->handleFreeProduct($product);
  111. }
  112. return redirect()->route('payment.' . $paymentGateway . 'Pay', ['shopProduct' => $product->id]);
  113. }
  114. /**
  115. * @param Request $request
  116. */
  117. public function Cancel(Request $request)
  118. {
  119. return redirect()->route('store.index')->with('info', 'Payment was Canceled');
  120. }
  121. /**
  122. * @return JsonResponse|mixed
  123. *
  124. * @throws Exception
  125. */
  126. public function dataTable()
  127. {
  128. $query = Payment::with('user');
  129. return datatables($query)
  130. ->addColumn('user', function (Payment $payment) {
  131. return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
  132. })
  133. ->editColumn('price', function (Payment $payment) {
  134. return $payment->formatToCurrency($payment->price);
  135. })
  136. ->editColumn('tax_value', function (Payment $payment) {
  137. return $payment->formatToCurrency($payment->tax_value);
  138. })
  139. ->editColumn('tax_percent', function (Payment $payment) {
  140. return $payment->tax_percent . ' %';
  141. })
  142. ->editColumn('total_price', function (Payment $payment) {
  143. return $payment->formatToCurrency($payment->total_price);
  144. })
  145. ->editColumn('created_at', function (Payment $payment) {
  146. return [
  147. 'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
  148. 'raw' => $payment->created_at ? strtotime($payment->created_at) : ''
  149. ];
  150. })
  151. ->addColumn('actions', function (Payment $payment) {
  152. 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>';
  153. })
  154. ->rawColumns(['actions', 'user'])
  155. ->make(true);
  156. }
  157. }