PaymentController.php 7.3 KB

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