PaymentController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_data = null;
  113. $coupon_code = $request->coupon_code;
  114. // on free products, we don't need to use a payment gateway
  115. $realPrice = $product->price - ($product->price * PartnerDiscount::getDiscount() / 100);
  116. if ($realPrice <= 0) {
  117. return $this->handleFreeProduct($product);
  118. }
  119. if ($coupon_code) {
  120. $isValidCoupon = $this->validateCoupon($request);
  121. if ($isValidCoupon->getStatusCode() == 200) {
  122. $coupon_data = $isValidCoupon;
  123. $discountPrice = $this->calcDiscount($product, $isValidCoupon->getData());
  124. }
  125. }
  126. if ($coupon_data) {
  127. return redirect()->route('payment.' . $paymentGateway . 'Pay', [
  128. 'shopProduct' => $product->id,
  129. 'discountPrice' => $discountPrice
  130. ]);
  131. }
  132. return redirect()->route('payment.' . $paymentGateway . 'Pay', ['shopProduct' => $product->id]);
  133. }
  134. /**
  135. * @param Request $request
  136. */
  137. public function Cancel(Request $request)
  138. {
  139. return redirect()->route('store.index')->with('info', 'Payment was Canceled');
  140. }
  141. protected function getCouponDiscount(float $productPrice, string $discount)
  142. {
  143. return $productPrice - ($productPrice * $discount / 100);
  144. }
  145. /**
  146. * @return JsonResponse|mixed
  147. *
  148. * @throws Exception
  149. */
  150. public function dataTable()
  151. {
  152. $query = Payment::with('user');
  153. return datatables($query)
  154. ->addColumn('user', function (Payment $payment) {
  155. return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
  156. })
  157. ->editColumn('price', function (Payment $payment) {
  158. return $payment->formatToCurrency($payment->price);
  159. })
  160. ->editColumn('tax_value', function (Payment $payment) {
  161. return $payment->formatToCurrency($payment->tax_value);
  162. })
  163. ->editColumn('tax_percent', function (Payment $payment) {
  164. return $payment->tax_percent . ' %';
  165. })
  166. ->editColumn('total_price', function (Payment $payment) {
  167. return $payment->formatToCurrency($payment->total_price);
  168. })
  169. ->editColumn('created_at', function (Payment $payment) {
  170. return [
  171. 'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
  172. 'raw' => $payment->created_at ? strtotime($payment->created_at) : ''
  173. ];
  174. })
  175. ->addColumn('actions', function (Payment $payment) {
  176. 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>';
  177. })
  178. ->rawColumns(['actions', 'user'])
  179. ->make(true);
  180. }
  181. }