diff --git a/app/Http/Controllers/Admin/OverViewController.php b/app/Http/Controllers/Admin/OverViewController.php index f46f803d..aff82e14 100644 --- a/app/Http/Controllers/Admin/OverViewController.php +++ b/app/Http/Controllers/Admin/OverViewController.php @@ -45,26 +45,18 @@ class OverViewController extends Controller $counters->put('payments', collect()); //Get and save payments from last 2 months for later filtering and looping $payments = Payment::query()->where('created_at', '>=', Carbon::today()->startOfMonth()->subMonth())->where('status', 'paid')->get(); - //Prepare collections and set a few variables + //Prepare collections $counters['payments']->put('thisMonth', collect()); $counters['payments']->put('lastMonth', collect()); - $counters['payments']['thisMonth']->timeStart = Carbon::today()->startOfMonth()->toDateString(); - $counters['payments']['thisMonth']->timeEnd = Carbon::today()->toDateString(); - $counters['payments']['lastMonth']->timeStart = Carbon::today()->startOfMonth()->subMonth()->toDateString(); - $counters['payments']['lastMonth']->timeEnd = Carbon::today()->endOfMonth()->subMonth()->toDateString(); - //Prepare subCollection 'taxPayments' $counters->put('taxPayments', collect()); //Get and save taxPayments from last 2 years for later filtering and looping $taxPayments = Payment::query()->where('created_at', '>=', Carbon::today()->startOfYear()->subYear())->where('status', 'paid')->get(); - //Prepare collections and set a few variables + //Prepare collections $counters['taxPayments']->put('thisYear', collect()); $counters['taxPayments']->put('lastYear', collect()); - $counters['taxPayments']['thisYear']->timeStart = Carbon::today()->startOfYear()->toDateString(); - $counters['taxPayments']['thisYear']->timeEnd = Carbon::today()->toDateString(); - $counters['taxPayments']['lastYear']->timeStart = Carbon::today()->startOfYear()->subYear()->toDateString(); - $counters['taxPayments']['lastYear']->timeEnd = Carbon::today()->endOfYear()->subYear()->toDateString(); + //Fill out variables for each currency separately foreach($payments->where('created_at', '>=', Carbon::today()->startOfMonth()) as $payment){ @@ -87,10 +79,18 @@ class OverViewController extends Controller $counters['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price; $counters['payments']['lastMonth'][$paymentCurrency]->count ++; } + + //sort currencies alphabetically and set some additional variables + $counters['payments']['thisMonth'] = $counters['payments']['thisMonth']->sortKeys(); + $counters['payments']['thisMonth']->timeStart = Carbon::today()->startOfMonth()->toDateString(); + $counters['payments']['thisMonth']->timeEnd = Carbon::today()->toDateString(); + $counters['payments']['lastMonth'] = $counters['payments']['lastMonth']->sortKeys(); + $counters['payments']['lastMonth']->timeStart = Carbon::today()->startOfMonth()->subMonth()->toDateString(); + $counters['payments']['lastMonth']->timeEnd = Carbon::today()->endOfMonth()->subMonth()->toDateString(); $counters['payments']->total = Payment::query()->count(); - foreach($taxPayments->where('created_at', '>=', Carbon::today()->startOfYear()->subYear()) as $taxPayment){ - $paymentCurrency = $payment->currency_code; + foreach($taxPayments->where('created_at', '>=', Carbon::today()->startOfYear()) as $taxPayment){ + $paymentCurrency = $taxPayment->currency_code; if(!isset($counters['taxPayments']['thisYear'][$paymentCurrency])){ $counters['taxPayments']['thisYear']->put($paymentCurrency, collect()); $counters['taxPayments']['thisYear'][$paymentCurrency]->total = 0; @@ -103,8 +103,8 @@ class OverViewController extends Controller $counters['taxPayments']['thisYear'][$paymentCurrency]->price += $taxPayment->price; $counters['taxPayments']['thisYear'][$paymentCurrency]->taxes += $taxPayment->tax_value; } - foreach($taxPayments->where('created_at', '<', Carbon::today()->startOfYear()) as $taxPayment){ - $paymentCurrency = $payment->currency_code; + foreach($taxPayments->where('created_at', '>=', Carbon::today()->startOfYear()->subYear())->where('created_at', '<', Carbon::today()->startOfYear()) as $taxPayment){ + $paymentCurrency = $taxPayment->currency_code; if(!isset($counters['taxPayments']['lastYear'][$paymentCurrency])){ $counters['taxPayments']['lastYear']->put($paymentCurrency, collect()); $counters['taxPayments']['lastYear'][$paymentCurrency]->total = 0; @@ -118,6 +118,14 @@ class OverViewController extends Controller $counters['taxPayments']['lastYear'][$paymentCurrency]->taxes += $taxPayment->tax_value; } + //sort currencies alphabetically and set some additional variables + $counters['taxPayments']['thisYear'] = $counters['taxPayments']['thisYear']->sortKeys(); + $counters['taxPayments']['thisYear']->timeStart = Carbon::today()->startOfYear()->toDateString(); + $counters['taxPayments']['thisYear']->timeEnd = Carbon::today()->toDateString(); + $counters['taxPayments']['lastYear'] = $counters['taxPayments']['lastYear']->sortKeys(); + $counters['taxPayments']['lastYear']->timeStart = Carbon::today()->startOfYear()->subYear()->toDateString(); + $counters['taxPayments']['lastYear']->timeEnd = Carbon::today()->endOfYear()->subYear()->toDateString(); + $lastEgg = Egg::query()->latest('updated_at')->first(); $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown'); diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index 30f03085..e21e121d 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/app/Http/Controllers/Admin/PaymentController.php @@ -68,6 +68,58 @@ class PaymentController extends Controller ]); } + /** + * @param Request $request + * @param ShopProduct $shopProduct + * @return RedirectResponse + */ + public function FreePay(Request $request, ShopProduct $shopProduct) + { + //dd($shopProduct); + //check if the product is really free or the discount is 100% + if($shopProduct->getTotalPrice()>0) return redirect()->route('home')->with('error', __('An error ocured. Please try again.')); + + //give product + /** @var User $user */ + $user = Auth::user(); + + //not updating server limit + + //update User with bought item + if ($shopProduct->type=="Credits") { + $user->increment('credits', $shopProduct->quantity); + }elseif ($shopProduct->type=="Server slots"){ + $user->increment('server_limit', $shopProduct->quantity); + } + + //skipped the referral commission, because the user did not pay anything. + + //not giving client role + + //store payment + $payment = Payment::create([ + 'user_id' => $user->id, + 'payment_id' => uniqid(), + 'payment_method' => 'free', + 'type' => $shopProduct->type, + 'status' => 'paid', + 'amount' => $shopProduct->quantity, + 'price' => $shopProduct->price - ($shopProduct->price*PartnerDiscount::getDiscount()/100), + 'tax_value' => $shopProduct->getTaxValue(), + 'tax_percent' => $shopProduct->getTaxPercent(), + 'total_price' => $shopProduct->getTotalPrice(), + 'currency_code' => $shopProduct->currency_code, + 'shop_item_product_id' => $shopProduct->id, + ]); + + event(new UserUpdateCreditsEvent($user)); + + //not sending an invoice + + //redirect back to home + return redirect()->route('home')->with('success', __('Your credit balance has been increased!')); + } + /** * @param Request $request * @param ShopProduct $shopProduct @@ -75,6 +127,7 @@ class PaymentController extends Controller */ public function PaypalPay(Request $request, ShopProduct $shopProduct) { + if(!$this->checkAmount($shopProduct->getTotalPrice(), strtoupper($shopProduct->currency_code), "paypal")) return redirect()->route('home')->with('error', __('The product you chose can´t be purchased with this payment method. The total amount is too small. Please buy a bigger amount or try a different payment method.')); $request = new OrdersCreateRequest(); $request->prefer('return=representation'); $request->body = [ @@ -284,6 +337,7 @@ class PaymentController extends Controller */ public function StripePay(Request $request, ShopProduct $shopProduct) { + if(!$this->checkAmount($shopProduct->getTotalPrice(), strtoupper($shopProduct->currency_code), "stripe")) return redirect()->route('home')->with('error', __('The product you chose can´t be purchased with this payment method. The total amount is too small. Please buy a bigger amount or try a different payment method.')); $stripeClient = $this->getStripeClient(); @@ -664,6 +718,114 @@ class PaymentController extends Controller $user->notify(new InvoiceNotification($invoice, $user, $payment)); } + public function checkAmount($amount, $currencyCode, $payment_method) + { + $minimums = [ + "USD" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "AED" => [ + "paypal" => 0, + "stripe" => 2 + ], + "AUD" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "BGN" => [ + "paypal" => 0, + "stripe" => 1 + ], + "BRL" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "CAD" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "CHF" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "CZK" => [ + "paypal" => 0, + "stripe" => 15 + ], + "DKK" => [ + "paypal" => 0, + "stripe" => 2.5 + ], + "EUR" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "GBP" => [ + "paypal" => 0, + "stripe" => 0.3 + ], + "HKD" => [ + "paypal" => 0, + "stripe" => 4 + ], + "HRK" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "HUF" => [ + "paypal" => 0, + "stripe" => 175 + ], + "INR" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "JPY" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "MXN" => [ + "paypal" => 0, + "stripe" => 10 + ], + "MYR" => [ + "paypal" => 0, + "stripe" => 2 + ], + "NOK" => [ + "paypal" => 0, + "stripe" => 3 + ], + "NZD" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "PLN" => [ + "paypal" => 0, + "stripe" => 2 + ], + "RON" => [ + "paypal" => 0, + "stripe" => 2 + ], + "SEK" => [ + "paypal" => 0, + "stripe" => 3 + ], + "SGD" => [ + "paypal" => 0, + "stripe" => 0.5 + ], + "THB" => [ + "paypal" => 0, + "stripe" => 10 + ] + ]; + return $amount >= $minimums[$currencyCode][$payment_method]; + } + + /** * @return JsonResponse|mixed * @throws Exception diff --git a/resources/views/store/checkout.blade.php b/resources/views/store/checkout.blade.php index dbbe6cb3..2e0556ca 100644 --- a/resources/views/store/checkout.blade.php +++ b/resources/views/store/checkout.blade.php @@ -75,30 +75,33 @@
-

{{ __('Payment Methods') }}:

+ @if($total!=0) +

{{ __('Payment Methods') }}:

-
- @if (config('SETTINGS::PAYMENTS:PAYPAL:SECRET') || config('SETTINGS::PAYMENTS:PAYPAL:SANDBOX_SECRET')) - - @endif - @if (config('SETTINGS::PAYMENTS:STRIPE:TEST_SECRET') || config('SETTINGS::PAYMENTS:STRIPE:SECRET')) - - @endif -
+
+ @if (config('SETTINGS::PAYMENTS:PAYPAL:SECRET') || config('SETTINGS::PAYMENTS:PAYPAL:SANDBOX_SECRET')) + + @endif + @if (config('SETTINGS::PAYMENTS:STRIPE:TEST_SECRET') || config('SETTINGS::PAYMENTS:STRIPE:SECRET')) + + @endif +
+ @else +

{{ __('This product is free for you') }}.

+ @endif
@@ -155,7 +158,7 @@ return { //loading paymentMethod: '', - paymentRoute: '', + paymentRoute: ({{ $total }} == 0)?('{{ route('payment.FreePay', $product->id) }}'):'', setPaymentRoute(provider) { switch (provider) { case 'paypal': diff --git a/routes/web.php b/routes/web.php index db124a34..49e1a630 100644 --- a/routes/web.php +++ b/routes/web.php @@ -89,6 +89,7 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () { Route::get('payment/PaypalSuccess', [PaymentController::class, 'PaypalSuccess'])->name('payment.PaypalSuccess'); Route::get('payment/StripePay/{shopProduct}', [PaymentController::class, 'StripePay'])->name('payment.StripePay'); Route::get('payment/StripeSuccess', [PaymentController::class, 'StripeSuccess'])->name('payment.StripeSuccess'); + Route::get('payment/FreePay/{shopProduct}', [PaymentController::class, 'FreePay'])->name('payment.FreePay'); Route::get('payment/Cancel', [PaymentController::class, 'Cancel'])->name('payment.Cancel'); Route::get('users/logbackin', [UserController::class, 'logBackIn'])->name('users.logbackin');