PaymentController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. class PaymentController extends Controller
  20. {
  21. /**
  22. * @return Application|Factory|View
  23. */
  24. public function index()
  25. {
  26. return view('admin.payments.index')->with([
  27. 'payments' => Payment::paginate(15),
  28. ]);
  29. }
  30. /**
  31. * @param Request $request
  32. * @param ShopProduct $shopProduct
  33. * @return Application|Factory|View
  34. */
  35. public function checkOut(ShopProduct $shopProduct)
  36. {
  37. $discount = PartnerDiscount::getDiscount();
  38. $price = $shopProduct->price - ($shopProduct->price * $discount / 100);
  39. $paymentGateways = [];
  40. if ($price > 0) {
  41. $extensions = ExtensionHelper::getAllExtensionsByNamespace('PaymentGateways');
  42. // 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
  43. foreach ($extensions as $extension) {
  44. $extensionName = basename($extension);
  45. if (!ExtensionHelper::getExtensionConfig($extensionName, 'enabled')) continue; // skip if not enabled
  46. $payment = new \stdClass();
  47. $payment->name = ExtensionHelper::getExtensionConfig($extensionName, 'name');
  48. $payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png');
  49. $paymentGateways[] = $payment;
  50. }
  51. }
  52. return view('store.checkout')->with([
  53. 'product' => $shopProduct,
  54. 'discountpercent' => $discount,
  55. 'discountvalue' => $discount * $shopProduct->price / 100,
  56. 'discountedprice' => $shopProduct->getPriceAfterDiscount(),
  57. 'taxvalue' => $shopProduct->getTaxValue(),
  58. 'taxpercent' => $shopProduct->getTaxPercent(),
  59. 'total' => $shopProduct->getTotalPrice(),
  60. 'paymentGateways' => $paymentGateways,
  61. 'productIsFree' => $price <= 0,
  62. ]);
  63. }
  64. /**
  65. * @param Request $request
  66. * @param ShopProduct $shopProduct
  67. * @return RedirectResponse
  68. */
  69. public function handleFreeProduct(ShopProduct $shopProduct)
  70. {
  71. /** @var User $user */
  72. $user = Auth::user();
  73. //create a payment
  74. $payment = Payment::create([
  75. 'user_id' => $user->id,
  76. 'payment_id' => uniqid(),
  77. 'payment_method' => 'free',
  78. 'type' => $shopProduct->type,
  79. 'status' => 'paid',
  80. 'amount' => $shopProduct->quantity,
  81. 'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
  82. 'tax_value' => $shopProduct->getTaxValue(),
  83. 'tax_percent' => $shopProduct->getTaxPercent(),
  84. 'total_price' => $shopProduct->getTotalPrice(),
  85. 'currency_code' => $shopProduct->currency_code,
  86. 'shop_item_product_id' => $shopProduct->id,
  87. ]);
  88. event(new UserUpdateCreditsEvent($user));
  89. event(new PaymentEvent($user, $payment, $shopProduct));
  90. //not sending an invoice
  91. //redirect back to home
  92. return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
  93. }
  94. public function pay(Request $request)
  95. {
  96. $product = ShopProduct::find($request->product_id);
  97. $paymentGateway = $request->payment_method;
  98. // on free products, we don't need to use a payment gateway
  99. $realPrice = $product->price - ($product->price * PartnerDiscount::getDiscount() / 100);
  100. if ($realPrice <= 0) {
  101. return $this->handleFreeProduct($product);
  102. }
  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. }