PaymentController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Events\UserUpdateCreditsEvent;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\PartnerDiscount;
  6. use App\Models\Payment;
  7. use App\Models\User;
  8. use App\Models\ShopProduct;
  9. use App\Notifications\ConfirmPaymentNotification;
  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 Illuminate\Support\Facades\DB;
  19. use PayPalCheckoutSdk\Core\PayPalHttpClient;
  20. use PayPalCheckoutSdk\Core\ProductionEnvironment;
  21. use PayPalCheckoutSdk\Core\SandboxEnvironment;
  22. use PayPalHttp\HttpException;
  23. use Stripe\Stripe;
  24. use App\Helpers\ExtensionHelper;
  25. class PaymentController extends Controller
  26. {
  27. /**
  28. * @return Application|Factory|View
  29. */
  30. public function index()
  31. {
  32. return view('admin.payments.index')->with([
  33. 'payments' => Payment::paginate(15),
  34. ]);
  35. }
  36. /**
  37. * @param Request $request
  38. * @param ShopProduct $shopProduct
  39. * @return Application|Factory|View
  40. */
  41. public function checkOut(ShopProduct $shopProduct)
  42. {
  43. // get all payment gateway extensions
  44. $extensions = glob(app_path() . '/Extensions/PaymentGateways/*', GLOB_ONLYDIR);
  45. // 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
  46. $paymentGateways = [];
  47. foreach ($extensions as $extension) {
  48. $extensionName = basename($extension);
  49. $config = ExtensionHelper::getExtensionConfig($extensionName, 'PaymentGateways');
  50. if ($config) {
  51. $payment = new \stdClass();
  52. $payment->name = $config['name'];
  53. $payment->image = asset('images/Extensions/PaymentGateways/' . strtolower($extensionName) . '_logo.png');
  54. $paymentGateways[] = $payment;
  55. }
  56. }
  57. return view('store.checkout')->with([
  58. 'product' => $shopProduct,
  59. 'discountpercent' => PartnerDiscount::getDiscount(),
  60. 'discountvalue' => PartnerDiscount::getDiscount() * $shopProduct->price / 100,
  61. 'discountedprice' => $shopProduct->getPriceAfterDiscount(),
  62. 'taxvalue' => $shopProduct->getTaxValue(),
  63. 'taxpercent' => $shopProduct->getTaxPercent(),
  64. 'total' => $shopProduct->getTotalPrice(),
  65. 'paymentGateways' => $paymentGateways,
  66. ]);
  67. }
  68. /**
  69. * @param Request $request
  70. * @param ShopProduct $shopProduct
  71. * @return RedirectResponse
  72. */
  73. public function FreePay(ShopProduct $shopProduct)
  74. {
  75. //dd($shopProduct);
  76. //check if the product is really free or the discount is 100%
  77. if ($shopProduct->getTotalPrice() > 0) return redirect()->route('home')->with('error', __('An error ocured. Please try again.'));
  78. //give product
  79. /** @var User $user */
  80. $user = Auth::user();
  81. //not updating server limit
  82. //update User with bought item
  83. if ($shopProduct->type == "Credits") {
  84. $user->increment('credits', $shopProduct->quantity);
  85. } elseif ($shopProduct->type == "Server slots") {
  86. $user->increment('server_limit', $shopProduct->quantity);
  87. }
  88. //skipped the referral commission, because the user did not pay anything.
  89. //not giving client role
  90. //store payment
  91. $payment = Payment::create([
  92. 'user_id' => $user->id,
  93. 'payment_id' => uniqid(),
  94. 'payment_method' => 'free',
  95. 'type' => $shopProduct->type,
  96. 'status' => 'paid',
  97. 'amount' => $shopProduct->quantity,
  98. 'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
  99. 'tax_value' => $shopProduct->getTaxValue(),
  100. 'tax_percent' => $shopProduct->getTaxPercent(),
  101. 'total_price' => $shopProduct->getTotalPrice(),
  102. 'currency_code' => $shopProduct->currency_code,
  103. 'shop_item_product_id' => $shopProduct->id,
  104. ]);
  105. event(new UserUpdateCreditsEvent($user));
  106. //not sending an invoice
  107. //redirect back to home
  108. return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
  109. }
  110. public function pay(Request $request)
  111. {
  112. $product = ShopProduct::find($request->product_id);
  113. $paymentGateway = $request->payment_method;
  114. return redirect()->route('payment.' . $paymentGateway . 'Pay', ['shopProduct' => $product->id]);
  115. }
  116. /**
  117. * @param Request $request
  118. */
  119. public function Cancel(Request $request)
  120. {
  121. return redirect()->route('store.index')->with('info', 'Payment was Canceled');
  122. }
  123. /**
  124. * @return JsonResponse|mixed
  125. *
  126. * @throws Exception
  127. */
  128. public function dataTable()
  129. {
  130. $query = Payment::with('user');
  131. return datatables($query)
  132. ->addColumn('user', function (Payment $payment) {
  133. return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
  134. })
  135. ->editColumn('price', function (Payment $payment) {
  136. return $payment->formatToCurrency($payment->price);
  137. })
  138. ->editColumn('tax_value', function (Payment $payment) {
  139. return $payment->formatToCurrency($payment->tax_value);
  140. })
  141. ->editColumn('tax_percent', function (Payment $payment) {
  142. return $payment->tax_percent . ' %';
  143. })
  144. ->editColumn('total_price', function (Payment $payment) {
  145. return $payment->formatToCurrency($payment->total_price);
  146. })
  147. ->editColumn('created_at', function (Payment $payment) {
  148. return [
  149. 'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
  150. 'raw' => $payment->created_at ? strtotime($payment->created_at) : ''
  151. ];
  152. })
  153. ->addColumn('actions', function (Payment $payment) {
  154. 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>';
  155. })
  156. ->rawColumns(['actions', 'user'])
  157. ->make(true);
  158. }
  159. }