PaymentController.php 7.2 KB

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