PaymentController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Events\CouponUsedEvent;
  4. use App\Events\PaymentEvent;
  5. use App\Events\UserUpdateCreditsEvent;
  6. use App\Http\Controllers\Controller;
  7. use App\Models\PartnerDiscount;
  8. use App\Models\Payment;
  9. use App\Models\User;
  10. use App\Models\ShopProduct;
  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\CouponSettings;
  22. use App\Settings\GeneralSettings;
  23. use App\Settings\LocaleSettings;
  24. use Illuminate\Support\Facades\Log;
  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, CouponSettings $coupon_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. 'isCouponsEnabled' => $coupon_settings->enabled,
  77. ]);
  78. }
  79. /**
  80. * @param Request $request
  81. * @param ShopProduct $shopProduct
  82. * @return RedirectResponse
  83. */
  84. public function handleFreeProduct(ShopProduct $shopProduct)
  85. {
  86. /** @var User $user */
  87. $user = Auth::user();
  88. //create a payment
  89. $payment = Payment::create([
  90. 'user_id' => $user->id,
  91. 'payment_id' => uniqid(),
  92. 'payment_method' => 'free',
  93. 'type' => $shopProduct->type,
  94. 'status' => 'paid',
  95. 'amount' => $shopProduct->quantity,
  96. 'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
  97. 'tax_value' => $shopProduct->getTaxValue(),
  98. 'tax_percent' => $shopProduct->getTaxPercent(),
  99. 'total_price' => $shopProduct->getTotalPrice(),
  100. 'currency_code' => $shopProduct->currency_code,
  101. 'shop_item_product_id' => $shopProduct->id,
  102. ]);
  103. event(new UserUpdateCreditsEvent($user));
  104. event(new PaymentEvent($user, $payment, $shopProduct));
  105. //not sending an invoice
  106. //redirect back to home
  107. return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
  108. }
  109. public function pay(Request $request)
  110. {
  111. try {
  112. $user = Auth::user();
  113. $user = User::findOrFail($user->id);
  114. $productId = $request->product_id;
  115. $shopProduct = ShopProduct::findOrFail($productId);
  116. $discount = PartnerDiscount::getDiscount();
  117. $paymentGateway = $request->payment_method;
  118. $couponCode = $request->coupon_code;
  119. $subtotal = $shopProduct->price;
  120. // Apply Coupon
  121. if ($couponCode) {
  122. if ($this->isCouponValid($couponCode, $user, $shopProduct->id)) {
  123. $subtotal = $this->applyCoupon($couponCode, $subtotal);
  124. }
  125. }
  126. // Apply Partner Discount
  127. $subtotal = $subtotal - ($subtotal * $discount / 100);
  128. if ($subtotal <= 0) {
  129. if ($couponCode) {
  130. event(new CouponUsedEvent($couponCode));
  131. }
  132. return $this->handleFreeProduct($shopProduct);
  133. }
  134. // Format the total price to a readable string
  135. $totalPriceString = number_format($subtotal, 2, '.', '');
  136. // create a new payment
  137. $payment = Payment::create([
  138. 'user_id' => $user->id,
  139. 'payment_id' => null,
  140. 'payment_method' => $paymentGateway,
  141. 'type' => $shopProduct->type,
  142. 'status' => 'open',
  143. 'amount' => $shopProduct->quantity,
  144. 'price' => $totalPriceString,
  145. 'tax_value' => $shopProduct->getTaxValue(),
  146. 'tax_percent' => $shopProduct->getTaxPercent(),
  147. 'total_price' => $shopProduct->getTotalPrice(),
  148. 'currency_code' => $shopProduct->currency_code,
  149. 'shop_item_product_id' => $shopProduct->id,
  150. ]);
  151. $paymentGatewayExtension = ExtensionHelper::getExtensionClass($paymentGateway);
  152. $redirectUrl = $paymentGatewayExtension::getRedirectUrl($payment, $shopProduct, $totalPriceString);
  153. if ($couponCode) {
  154. event(new CouponUsedEvent($couponCode));
  155. }
  156. } catch (Exception $e) {
  157. Log::error($e->getMessage());
  158. return redirect()->route('store.index')->with('error', __('Oops, something went wrong! Please try again later.'));
  159. }
  160. return redirect()->away($redirectUrl);
  161. }
  162. /**
  163. * @param Request $request
  164. */
  165. public function Cancel(Request $request)
  166. {
  167. return redirect()->route('store.index')->with('info', 'Payment was Canceled');
  168. }
  169. /**
  170. * @return JsonResponse|mixed
  171. *
  172. * @throws Exception
  173. */
  174. public function dataTable()
  175. {
  176. $query = Payment::with('user');
  177. return datatables($query)
  178. ->addColumn('user', function (Payment $payment) {
  179. return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
  180. })
  181. ->editColumn('price', function (Payment $payment) {
  182. return $payment->formatToCurrency($payment->price);
  183. })
  184. ->editColumn('tax_value', function (Payment $payment) {
  185. return $payment->formatToCurrency($payment->tax_value);
  186. })
  187. ->editColumn('tax_percent', function (Payment $payment) {
  188. return $payment->tax_percent . ' %';
  189. })
  190. ->editColumn('total_price', function (Payment $payment) {
  191. return $payment->formatToCurrency($payment->total_price);
  192. })
  193. ->editColumn('created_at', function (Payment $payment) {
  194. return [
  195. 'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
  196. 'raw' => $payment->created_at ? strtotime($payment->created_at) : ''
  197. ];
  198. })
  199. ->addColumn('actions', function (Payment $payment) {
  200. 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>';
  201. })
  202. ->rawColumns(['actions', 'user'])
  203. ->make(true);
  204. }
  205. }