diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index a7001427..4c52a310 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/app/Http/Controllers/Admin/PaymentController.php @@ -27,6 +27,7 @@ use PayPalHttp\HttpException; class PaymentController extends Controller { + /** * @return Application|Factory|View */ @@ -45,7 +46,10 @@ class PaymentController extends Controller public function checkOut(Request $request, PaypalProduct $paypalProduct) { return view('store.checkout')->with([ - 'product' => $paypalProduct + 'product' => $paypalProduct, + 'taxvalue' => $paypalProduct->getTaxValue(), + 'taxpercent' => $paypalProduct->getTaxPercent(), + 'total' => $paypalProduct->getTotalPrice() ]); } @@ -65,8 +69,20 @@ class PaymentController extends Controller "reference_id" => uniqid(), "description" => $paypalProduct->description, "amount" => [ - "value" => $paypalProduct->price, - "currency_code" => strtoupper($paypalProduct->currency_code) + "value" => $paypalProduct->getTotalPrice(), + 'currency_code' => strtoupper($paypalProduct->currency_code), + 'breakdown' =>[ + 'item_total' => + [ + 'currency_code' => strtoupper($paypalProduct->currency_code), + 'value' => $paypalProduct->price, + ], + 'tax_total' => + [ + 'currency_code' => strtoupper($paypalProduct->currency_code), + 'value' => $paypalProduct->getTaxValue(), + ] + ] ] ] ], @@ -76,6 +92,8 @@ class PaymentController extends Controller 'brand_name' => config('app.name', 'Laravel'), 'shipping_preference' => 'NO_SHIPPING' ] + + ]; @@ -161,6 +179,9 @@ class PaymentController extends Controller 'status' => $response->result->status, 'amount' => $paypalProduct->quantity, 'price' => $paypalProduct->price, + 'tax_value' => $paypalProduct->getTaxValue(), + 'tax_percent' => $paypalProduct->getTaxPercent(), + 'total_price' => $paypalProduct->getTotalPrice(), 'currency_code' => $paypalProduct->currency_code, 'payer' => json_encode($response->result->payer), ]); @@ -199,7 +220,7 @@ class PaymentController extends Controller */ public function cancel(Request $request) { - return redirect()->route('store.index')->with('success', 'Payment was Cannceled'); + return redirect()->route('store.index')->with('success', 'Payment was Canceled'); } @@ -216,7 +237,13 @@ class PaymentController extends Controller return $payment->user->name; }) ->editColumn('price', function (Payment $payment) { - return $payment->formatCurrency(); + return $payment->formatToCurrency($payment->price); + }) + ->editColumn('tax_value', function (Payment $payment) { + return $payment->formatToCurrency($payment->tax_value); + }) + ->editColumn('total_price', function (Payment $payment) { + return $payment->formatToCurrency($payment->total_price); }) ->editColumn('created_at', function (Payment $payment) { return $payment->created_at ? $payment->created_at->diffForHumans() : ''; diff --git a/app/Http/Controllers/Admin/PaypalProductController.php b/app/Http/Controllers/Admin/PaypalProductController.php index 2fe1998e..ee4764e4 100644 --- a/app/Http/Controllers/Admin/PaypalProductController.php +++ b/app/Http/Controllers/Admin/PaypalProductController.php @@ -62,7 +62,7 @@ class PaypalProductController extends Controller $disabled = !is_null($request->input('disabled')); PaypalProduct::create(array_merge($request->all(), ['disabled' => $disabled])); - return redirect()->route('admin.store.index')->with('success', 'store item has been created!'); + return redirect()->route('admin.store.index')->with('success', 'Store item has been created!'); } /** @@ -112,7 +112,7 @@ class PaypalProductController extends Controller $disabled = !is_null($request->input('disabled')); $paypalProduct->update(array_merge($request->all(), ['disabled' => $disabled])); - return redirect()->route('admin.store.index')->with('success', 'store item has been updated!'); + return redirect()->route('admin.store.index')->with('success', 'Store item has been updated!'); } /** @@ -124,7 +124,7 @@ class PaypalProductController extends Controller { $paypalProduct->update(['disabled' => !$paypalProduct->disabled]); - return redirect()->route('admin.store.index')->with('success', 'product has been updated!'); + return redirect()->route('admin.store.index')->with('success', 'Product has been updated!'); } /** @@ -136,7 +136,7 @@ class PaypalProductController extends Controller public function destroy(PaypalProduct $paypalProduct) { $paypalProduct->delete(); - return redirect()->back()->with('success', 'store item has been removed!'); + return redirect()->back()->with('success', 'Store item has been removed!'); } @@ -173,7 +173,7 @@ class PaypalProductController extends Controller return $paypalProduct->created_at ? $paypalProduct->created_at->diffForHumans() : ''; }) ->editColumn('price', function (PaypalProduct $paypalProduct) { - return $paypalProduct->formatCurrency(); + return $paypalProduct->formatToCurrency($paypalProduct->price); }) ->rawColumns(['actions', 'disabled']) ->make(); diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index f394058b..a71f81da 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -84,7 +84,7 @@ class ProductController extends Controller $product->eggs()->attach($request->input('eggs')); $product->nodes()->attach($request->input('nodes')); - return redirect()->route('admin.products.index')->with('success', 'product has been created!'); + return redirect()->route('admin.products.index')->with('success', 'Product has been created!'); } /** @@ -152,7 +152,7 @@ class ProductController extends Controller $product->eggs()->attach($request->input('eggs')); $product->nodes()->attach($request->input('nodes')); - return redirect()->route('admin.products.index')->with('success', 'product has been updated!'); + return redirect()->route('admin.products.index')->with('success', 'Product has been updated!'); } /** @@ -164,7 +164,7 @@ class ProductController extends Controller { $product->update(['disabled' => !$product->disabled]); - return redirect()->route('admin.products.index')->with('success', 'product has been updated!'); + return redirect()->route('admin.products.index')->with('success', 'Product has been updated!'); } /** @@ -181,7 +181,7 @@ class ProductController extends Controller } $product->delete(); - return redirect()->back()->with('success', 'product has been removed!'); + return redirect()->back()->with('success', 'Product has been removed!'); } diff --git a/app/Http/Controllers/Admin/ServerController.php b/app/Http/Controllers/Admin/ServerController.php index 6d333feb..493993d8 100644 --- a/app/Http/Controllers/Admin/ServerController.php +++ b/app/Http/Controllers/Admin/ServerController.php @@ -92,7 +92,7 @@ class ServerController extends Controller { try { $server->delete(); - return redirect()->route('admin.servers.index')->with('success', 'server removed'); + return redirect()->route('admin.servers.index')->with('success', 'Server removed'); } catch (Exception $e) { return redirect()->route('admin.servers.index')->with('error', 'An exception has occurred while trying to remove a resource "' . $e->getMessage() . '"'); } @@ -109,7 +109,7 @@ class ServerController extends Controller return redirect()->back()->with('error', $exception->getMessage()); } - return redirect()->back()->with('success', 'server has been updated!'); + return redirect()->back()->with('success', 'Server has been updated!'); } /** diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 75e811da..33738d52 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -21,7 +21,7 @@ class ProductController extends Controller */ public function getNodesBasedOnEgg(Request $request, Egg $egg) { - if (is_null($egg->id)) return response()->json('egg id is required', '400'); + if (is_null($egg->id)) return response()->json('Egg ID is required', '400'); #get products that include this egg $products = Product::query()->with('nodes')->whereHas('eggs', function (Builder $builder) use ($egg) { diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index bdeb024c..00ec4b1c 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -86,6 +86,6 @@ class ProfileController extends Controller 'email' => $request->input('email'), ]); - return redirect()->route('profile.index')->with('success' , 'profile updated'); + return redirect()->route('profile.index')->with('success' , 'Profile updated'); } } diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index 313634fb..3ec0e29f 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -138,7 +138,7 @@ class ServerController extends Controller } } - return redirect()->route('servers.index')->with('success', 'server created'); + return redirect()->route('servers.index')->with('success', 'Server created'); } /** @@ -151,7 +151,7 @@ class ServerController extends Controller $server->delete(); Auth::user()->notify(new ServerCreationError($server)); - return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment were found.'); + return redirect()->route('servers.index')->with('error', 'No allocations satisfying the requirements for automatic deployment on this node were found.'); } /** @@ -172,7 +172,7 @@ class ServerController extends Controller { try { $server->delete(); - return redirect()->route('servers.index')->with('success', 'server removed'); + return redirect()->route('servers.index')->with('success', 'Server removed'); } catch (Exception $e) { return redirect()->route('servers.index')->with('error', 'An exception has occurred while trying to remove a resource "' . $e->getMessage() . '"'); } diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 85bb4b0d..2fb1db0e 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -29,6 +29,9 @@ class Payment extends Model 'type', 'amount', 'price', + 'tax_value', + 'total_price', + 'tax_percent', 'currency_code', ]; @@ -51,9 +54,15 @@ class Payment extends Model return $this->belongsTo(User::class); } - public function formatCurrency($locale = 'en_US') + /** + * @param mixed $value + * @param string $locale + * + * @return float + */ + public function formatToCurrency($value,$locale = 'en_US') { $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); - return $formatter->formatCurrency($this->price, $this->currency_code); + return $formatter->formatCurrency($value, $this->currency_code); } } diff --git a/app/Models/PaypalProduct.php b/app/Models/PaypalProduct.php index c39c0f8e..2463bf00 100644 --- a/app/Models/PaypalProduct.php +++ b/app/Models/PaypalProduct.php @@ -6,6 +6,7 @@ use Hidehalo\Nanoid\Client; use Illuminate\Database\Eloquent\Model; use NumberFormatter; use Spatie\Activitylog\Traits\LogsActivity; +use App\Models\Configuration; class PaypalProduct extends Model { @@ -40,12 +41,45 @@ class PaypalProduct extends Model } /** + * @param mixed $value * @param string $locale - * @return string + * + * @return float */ - public function formatCurrency($locale = 'en_US') + public function formatToCurrency($value,$locale = 'en_US') { $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); - return $formatter->formatCurrency($this->price, $this->currency_code); + return $formatter->formatCurrency($value, $this->currency_code); + } + + /** + * @description Returns the tax in % taken from the Configuration + * + * @return int + */ + public function getTaxPercent() + { + $tax = Configuration::getValueByKey("SALES_TAX"); + return $tax < 0 ? 0 : $tax; + } + + /** + * @description Returns the tax as Number + * + * @return float + */ + public function getTaxValue() + { + return $this->price*$this->getTaxPercent()/100; + } + + /** + * @description Returns the full price of a Product including tax + * + * @return float + */ + public function getTotalPrice() + { + return $this->price+($this->getTaxValue()); } } diff --git a/database/migrations/2021_11_05_071456_add_tax_to_paymentlogs.php b/database/migrations/2021_11_05_071456_add_tax_to_paymentlogs.php new file mode 100644 index 00000000..3796ee9e --- /dev/null +++ b/database/migrations/2021_11_05_071456_add_tax_to_paymentlogs.php @@ -0,0 +1,36 @@ +decimal('tax_value',8,2)->after('price')->nullable(); + $table->integer('tax_percent')->after('tax_value')->nullable(); + $table->decimal('total_price',8,2)->after('tax_percent')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('payments', function (Blueprint $table) { + $table->dropColumn('tax_value'); + $table->dropColumn('tax_percent'); + $table->dropColumn('total_price'); + }); + } +} diff --git a/database/seeders/Seeds/ConfigurationSeeder.php b/database/seeders/Seeds/ConfigurationSeeder.php index cce8c991..98d3a57a 100644 --- a/database/seeders/Seeds/ConfigurationSeeder.php +++ b/database/seeders/Seeds/ConfigurationSeeder.php @@ -136,6 +136,14 @@ class ConfigurationSeeder extends Seeder 'type' => 'boolean', 'description' => 'Charges the first hour worth of credits upon creating a server.' ]); + //sales tax + Configuration::firstOrCreate([ + 'key' => 'SALES_TAX', + ], [ + 'value' => '0', + 'type' => 'integer', + 'description' => 'The %-value of tax that will be added to the product price on checkout' + ]); } } diff --git a/resources/views/admin/payments/index.blade.php b/resources/views/admin/payments/index.blade.php index d22b2788..b6765901 100644 --- a/resources/views/admin/payments/index.blade.php +++ b/resources/views/admin/payments/index.blade.php @@ -37,7 +37,10 @@
Subtotal: | -{{$product->formatCurrency()}} | +{{$product->formatToCurrency($product->price)}} | |
---|---|---|---|
Tax (0%) | -0.00 | +Tax ({{$taxpercent}}%) | +{{$product->formatToCurrency($taxvalue)}} |
Quantity: | @@ -123,7 +123,7 @@|||
Total: | -{{$product->formatCurrency()}} | +{{$product->formatToCurrency($total)}} |