PaymentController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. if (!ExtensionHelper::getExtensionConfig($extensionName, 'enabled')) continue; // skip if not enabled
  49. $payment = new \stdClass();
  50. $payment->name = ExtensionHelper::getExtensionConfig($extensionName, 'name');
  51. $payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png');
  52. $paymentGateways[] = $payment;
  53. }
  54. }
  55. return view('store.checkout')->with([
  56. 'product' => $shopProduct,
  57. 'discountpercent' => $discount,
  58. 'discountvalue' => $discount * $shopProduct->price / 100,
  59. 'discountedprice' => $shopProduct->getPriceAfterDiscount(),
  60. 'taxvalue' => $shopProduct->getTaxValue(),
  61. 'taxpercent' => $shopProduct->getTaxPercent(),
  62. 'total' => $shopProduct->getTotalPrice(),
  63. 'paymentGateways' => $paymentGateways,
  64. 'productIsFree' => $price <= 0,
  65. 'credits_display_name' => $general_settings->credits_display_name
  66. ]);
  67. }
  68. /**
  69. * @param Request $request
  70. * @param ShopProduct $shopProduct
  71. * @return RedirectResponse
  72. */
  73. public function handleFreeProduct(ShopProduct $shopProduct)
  74. {
  75. /** @var User $user */
  76. $user = Auth::user();
  77. //create a payment
  78. $payment = Payment::create([
  79. 'user_id' => $user->id,
  80. 'payment_id' => uniqid(),
  81. 'payment_method' => 'free',
  82. 'type' => $shopProduct->type,
  83. 'status' => 'paid',
  84. 'amount' => $shopProduct->quantity,
  85. 'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
  86. 'tax_value' => $shopProduct->getTaxValue(),
  87. 'tax_percent' => $shopProduct->getTaxPercent(),
  88. 'total_price' => $shopProduct->getTotalPrice(),
  89. 'currency_code' => $shopProduct->currency_code,
  90. 'shop_item_product_id' => $shopProduct->id,
  91. ]);
  92. event(new UserUpdateCreditsEvent($user));
  93. event(new PaymentEvent($user, $payment, $shopProduct));
  94. //not sending an invoice
  95. //redirect back to home
  96. return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
  97. }
  98. public function pay(Request $request)
  99. {
  100. $product = ShopProduct::find($request->product_id);
  101. $paymentGateway = $request->payment_method;
  102. // on free products, we don't need to use a payment gateway
  103. $realPrice = $product->price - ($product->price * PartnerDiscount::getDiscount() / 100);
  104. if ($realPrice <= 0) {
  105. return $this->handleFreeProduct($product);
  106. }
  107. return redirect()->route('payment.' . $paymentGateway . 'Pay', ['shopProduct' => $product->id]);
  108. }
  109. /**
  110. * @param Request $request
  111. */
  112. public function Cancel(Request $request)
  113. {
  114. return redirect()->route('store.index')->with('info', 'Payment was Canceled');
  115. }
  116. /**
  117. * @return JsonResponse|mixed
  118. *
  119. * @throws Exception
  120. */
  121. public function dataTable()
  122. {
  123. $query = Payment::with('user');
  124. return datatables($query)
  125. ->addColumn('user', function (Payment $payment) {
  126. return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
  127. })
  128. ->editColumn('price', function (Payment $payment) {
  129. return $payment->formatToCurrency($payment->price);
  130. })
  131. ->editColumn('tax_value', function (Payment $payment) {
  132. return $payment->formatToCurrency($payment->tax_value);
  133. })
  134. ->editColumn('tax_percent', function (Payment $payment) {
  135. return $payment->tax_percent . ' %';
  136. })
  137. ->editColumn('total_price', function (Payment $payment) {
  138. return $payment->formatToCurrency($payment->total_price);
  139. })
  140. ->editColumn('created_at', function (Payment $payment) {
  141. return [
  142. 'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
  143. 'raw' => $payment->created_at ? strtotime($payment->created_at) : ''
  144. ];
  145. })
  146. ->addColumn('actions', function (Payment $payment) {
  147. 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>';
  148. })
  149. ->rawColumns(['actions', 'user'])
  150. ->make(true);
  151. }
  152. }