PaymentController.php 6.7 KB

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