123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Events\UserUpdateCreditsEvent;
- use App\Http\Controllers\Controller;
- use App\Models\InvoiceSettings;
- use App\Models\Payment;
- use App\Models\CreditProduct;
- use App\Models\Settings;
- use App\Models\User;
- use App\Notifications\InvoiceNotification;
- use App\Notifications\ConfirmPaymentNotification;
- use Exception;
- use Illuminate\Contracts\Foundation\Application;
- use Illuminate\Contracts\View\Factory;
- use Illuminate\Contracts\View\View;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Storage;
- use LaravelDaily\Invoices\Classes\Buyer;
- use LaravelDaily\Invoices\Classes\InvoiceItem;
- use LaravelDaily\Invoices\Classes\Party;
- use LaravelDaily\Invoices\Invoice;
- use PayPalCheckoutSdk\Core\PayPalHttpClient;
- use PayPalCheckoutSdk\Core\ProductionEnvironment;
- use PayPalCheckoutSdk\Core\SandboxEnvironment;
- use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
- use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
- use PayPalHttp\HttpException;
- use Stripe\Stripe;
- class PaymentController extends Controller
- {
- /**
- * @return Application|Factory|View
- */
- public function index()
- {
- return view('admin.payments.index')->with([
- 'payments' => Payment::paginate(15)
- ]);
- }
- /**
- * @param Request $request
- * @param CreditProduct $creditProduct
- * @return Application|Factory|View
- */
- public function checkOut(Request $request, CreditProduct $creditProduct)
- {
- return view('store.checkout')->with([
- 'product' => $creditProduct,
- 'taxvalue' => $creditProduct->getTaxValue(),
- 'taxpercent' => $creditProduct->getTaxPercent(),
- 'total' => $creditProduct->getTotalPrice()
- ]);
- }
- /**
- * @param Request $request
- * @param CreditProduct $creditProduct
- * @return RedirectResponse
- */
- public function PaypalPay(Request $request, CreditProduct $creditProduct)
- {
- $request = new OrdersCreateRequest();
- $request->prefer('return=representation');
- $request->body = [
- "intent" => "CAPTURE",
- "purchase_units" => [
- [
- "reference_id" => uniqid(),
- "description" => $creditProduct->description,
- "amount" => [
- "value" => $creditProduct->getTotalPrice(),
- 'currency_code' => strtoupper($creditProduct->currency_code),
- 'breakdown' => [
- 'item_total' =>
- [
- 'currency_code' => strtoupper($creditProduct->currency_code),
- 'value' => $creditProduct->price,
- ],
- 'tax_total' =>
- [
- 'currency_code' => strtoupper($creditProduct->currency_code),
- 'value' => $creditProduct->getTaxValue(),
- ]
- ]
- ]
- ]
- ],
- "application_context" => [
- "cancel_url" => route('payment.Cancel'),
- "return_url" => route('payment.PaypalSuccess', ['product' => $creditProduct->id]),
- 'brand_name' => config('app.name', 'Laravel'),
- 'shipping_preference' => 'NO_SHIPPING'
- ]
- ];
- try {
- // Call API with your client and get a response for your call
- $response = $this->getPayPalClient()->execute($request);
- return redirect()->away($response->result->links[1]->href);
- // If call returns body in response, you can get the deserialized version from the result attribute of the response
- } catch (HttpException $ex) {
- echo $ex->statusCode;
- dd(json_decode($ex->getMessage()));
- }
- }
- /**
- * @return PayPalHttpClient
- */
- protected function getPayPalClient()
- {
- $environment = env('APP_ENV') == 'local'
- ? new SandboxEnvironment($this->getPaypalClientId(), $this->getPaypalClientSecret())
- : new ProductionEnvironment($this->getPaypalClientId(), $this->getPaypalClientSecret());
- return new PayPalHttpClient($environment);
- }
- /**
- * @return string
- */
- protected function getPaypalClientId()
- {
- return env('APP_ENV') == 'local' ? config("SETTINGS::PAYMENTS:PAYPAL:SANDBOX_CLIENT_ID") : config("SETTINGS::PAYMENTS:PAYPAL:CLIENT_ID");
- }
- /**
- * @return string
- */
- protected function getPaypalClientSecret()
- {
- return env('APP_ENV') == 'local' ? config("SETTINGS::PAYMENTS:PAYPAL:SANDBOX_SECRET") : config("SETTINGS::PAYMENTS:PAYPAL:SECRET");
- }
- /**
- * @param Request $laravelRequest
- */
- public function PaypalSuccess(Request $laravelRequest)
- {
- /** @var CreditProduct $creditProduct */
- $creditProduct = CreditProduct::findOrFail($laravelRequest->input('product'));
- /** @var User $user */
- $user = Auth::user();
- $request = new OrdersCaptureRequest($laravelRequest->input('token'));
- $request->prefer('return=representation');
- try {
- // Call API with your client and get a response for your call
- $response = $this->getPayPalClient()->execute($request);
- if ($response->statusCode == 201 || $response->statusCode == 200) {
- //update credits
- $user->increment('credits', $creditProduct->quantity);
- //update server limit
- if (config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE') !== 0) {
- if ($user->server_limit < config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')) {
- $user->update(['server_limit' => config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')]);
- }
- }
- //update role
- if ($user->role == 'member') {
- $user->update(['role' => 'client']);
- }
- //store payment
- $payment = Payment::create([
- 'user_id' => $user->id,
- 'payment_id' => $response->result->id,
- 'payment_method' => 'paypal',
- 'type' => 'Credits',
- 'status' => 'paid',
- 'amount' => $creditProduct->quantity,
- 'price' => $creditProduct->price,
- 'tax_value' => $creditProduct->getTaxValue(),
- 'tax_percent' => $creditProduct->getTaxPercent(),
- 'total_price' => $creditProduct->getTotalPrice(),
- 'currency_code' => $creditProduct->currency_code,
- 'credit_product_id' => $creditProduct->id,
- ]);
- event(new UserUpdateCreditsEvent($user));
- //only create invoice if SETTINGS::INVOICE:ENABLED is true
- if (config('SETTINGS::INVOICE:ENABLED') == 'true') {
- $this->createInvoice($user, $payment, 'paid');
- }
- //redirect back to home
- return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
- }
- // If call returns body in response, you can get the deserialized version from the result attribute of the response
- if (env('APP_ENV') == 'local') {
- dd($response);
- } else {
- abort(500);
- }
- } catch (HttpException $ex) {
- if (env('APP_ENV') == 'local') {
- echo $ex->statusCode;
- dd($ex->getMessage());
- } else {
- abort(422);
- }
- }
- }
- /**
- * @param Request $request
- */
- public function Cancel(Request $request)
- {
- return redirect()->route('store.index')->with('success', 'Payment was Canceled');
- }
- /**
- * @param Request $request
- * @param CreditProduct $creditProduct
- * @return RedirectResponse
- */
- public function StripePay(Request $request, CreditProduct $creditProduct)
- {
- $stripeClient = $this->getStripeClient();
- $request = $stripeClient->checkout->sessions->create([
- 'line_items' => [
- [
- 'price_data' => [
- 'currency' => $creditProduct->currency_code,
- 'product_data' => [
- 'name' => $creditProduct->display,
- 'description' => $creditProduct->description,
- ],
- 'unit_amount_decimal' => round($creditProduct->price * 100, 2),
- ],
- 'quantity' => 1,
- ],
- [
- 'price_data' => [
- 'currency' => $creditProduct->currency_code,
- 'product_data' => [
- 'name' => 'Product Tax',
- 'description' => $creditProduct->getTaxPercent() . "%",
- ],
- 'unit_amount_decimal' => round($creditProduct->getTaxValue(), 2) * 100,
- ],
- 'quantity' => 1,
- ]
- ],
- 'mode' => 'payment',
- "payment_method_types" => str_getcsv(config("SETTINGS::PAYMENTS:STRIPE:METHODS")),
- 'success_url' => route('payment.StripeSuccess', ['product' => $creditProduct->id]) . '&session_id={CHECKOUT_SESSION_ID}',
- 'cancel_url' => route('payment.Cancel'),
- ]);
- return redirect($request->url, 303);
- }
- /**
- * @param Request $request
- */
- public function StripeSuccess(Request $request)
- {
- /** @var CreditProduct $creditProduct */
- $creditProduct = CreditProduct::findOrFail($request->input('product'));
- /** @var User $user */
- $user = Auth::user();
- $stripeClient = $this->getStripeClient();
- try {
- //get stripe data
- $paymentSession = $stripeClient->checkout->sessions->retrieve($request->input('session_id'));
- $paymentIntent = $stripeClient->paymentIntents->retrieve($paymentSession->payment_intent);
- //get DB entry of this payment ID if existing
- $paymentDbEntry = Payment::where('payment_id', $paymentSession->payment_intent)->count();
- // check if payment is 100% completed and payment does not exist in db already
- if ($paymentSession->status == "complete" && $paymentIntent->status == "succeeded" && $paymentDbEntry == 0) {
- //update credits
- $user->increment('credits', $creditProduct->quantity);
- //update server limit
- if (config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE') !== 0) {
- if ($user->server_limit < config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')) {
- $user->update(['server_limit' => config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')]);
- }
- }
- //update role
- if ($user->role == 'member') {
- $user->update(['role' => 'client']);
- }
- //store paid payment
- $payment = Payment::create([
- 'user_id' => $user->id,
- 'payment_id' => $paymentSession->payment_intent,
- 'payment_method' => 'stripe',
- 'type' => 'Credits',
- 'status' => 'paid',
- 'amount' => $creditProduct->quantity,
- 'price' => $creditProduct->price,
- 'tax_value' => $creditProduct->getTaxValue(),
- 'total_price' => $creditProduct->getTotalPrice(),
- 'tax_percent' => $creditProduct->getTaxPercent(),
- 'currency_code' => $creditProduct->currency_code,
- 'credit_product_id' => $creditProduct->id,
- ]);
- //payment notification
- $user->notify(new ConfirmPaymentNotification($payment));
- event(new UserUpdateCreditsEvent($user));
- //only create invoice if SETTINGS::INVOICE:ENABLED is true
- if (config('SETTINGS::INVOICE:ENABLED') == 'true') {
- $this->createInvoice($user, $payment, 'paid');
- }
- //redirect back to home
- return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
- } else {
- if ($paymentIntent->status == "processing") {
- //store processing payment
- $payment = Payment::create([
- 'user_id' => $user->id,
- 'payment_id' => $paymentSession->payment_intent,
- 'payment_method' => 'stripe',
- 'type' => 'Credits',
- 'status' => 'processing',
- 'amount' => $creditProduct->quantity,
- 'price' => $creditProduct->price,
- 'tax_value' => $creditProduct->getTaxValue(),
- 'total_price' => $creditProduct->getTotalPrice(),
- 'tax_percent' => $creditProduct->getTaxPercent(),
- 'currency_code' => $creditProduct->currency_code,
- 'credit_product_id' => $creditProduct->id,
- ]);
- //only create invoice if SETTINGS::INVOICE:ENABLED is true
- if (config('SETTINGS::INVOICE:ENABLED') == 'true') {
- $this->createInvoice($user, $payment, 'paid');
- }
- //redirect back to home
- return redirect()->route('home')->with('success', __('Your payment is being processed!'));
- }
- if ($paymentDbEntry == 0 && $paymentIntent->status != "processing") {
- $stripeClient->paymentIntents->cancel($paymentIntent->id);
- //redirect back to home
- return redirect()->route('home')->with('success', __('Your payment has been canceled!'));
- } else {
- abort(402);
- }
- }
- } catch (HttpException $ex) {
- if (env('APP_ENV') == 'local') {
- echo $ex->statusCode;
- dd($ex->getMessage());
- } else {
- abort(422);
- }
- }
- }
- /**
- * @param Request $request
- */
- protected function handleStripePaymentSuccessHook($paymentIntent)
- {
- try {
- // Get payment db entry
- $payment = Payment::where('payment_id', $paymentIntent->id)->first();
- $user = User::where('id', $payment->user_id)->first();
- if ($paymentIntent->status == 'succeeded' && $payment->status == 'processing') {
- // Increment User Credits
- $user->increment('credits', $payment->amount);
- //update server limit
- if (config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE') !== 0) {
- if ($user->server_limit < config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')) {
- $user->update(['server_limit' => config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')]);
- }
- }
- //update role
- if ($user->role == 'member') {
- $user->update(['role' => 'client']);
- }
- //update payment db entry status
- $payment->update(['status' => 'paid']);
- //payment notification
- $user->notify(new ConfirmPaymentNotification($payment));
- event(new UserUpdateCreditsEvent($user));
- //only create invoice if SETTINGS::INVOICE:ENABLED is true
- if (config('SETTINGS::INVOICE:ENABLED') == 'true') {
- $this->createInvoice($user, $payment, 'paid');
- }
- }
- } catch (HttpException $ex) {
- abort(422);
- }
- }
- /**
- * @param Request $request
- */
- public function StripeWebhooks(Request $request)
- {
- \Stripe\Stripe::setApiKey($this->getStripeSecret());
- try {
- $payload = @file_get_contents('php://input');
- $sig_header = $request->header('Stripe-Signature');
- $event = null;
- $event = \Stripe\Webhook::constructEvent(
- $payload,
- $sig_header,
- $this->getStripeEndpointSecret()
- );
- } catch (\UnexpectedValueException $e) {
- // Invalid payload
- abort(400);
- } catch (\Stripe\Exception\SignatureVerificationException $e) {
- // Invalid signature
- abort(400);
- }
- // Handle the event
- switch ($event->type) {
- case 'payment_intent.succeeded':
- $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
- $this->handleStripePaymentSuccessHook($paymentIntent);
- break;
- default:
- echo 'Received unknown event type ' . $event->type;
- }
- }
- /**
- * @return \Stripe\StripeClient
- */
- protected function getStripeClient()
- {
- return new \Stripe\StripeClient($this->getStripeSecret());
- }
- /**
- * @return string
- */
- protected function getStripeSecret()
- {
- return env('APP_ENV') == 'local'
- ? config("SETTINGS::PAYMENTS:STRIPE:TEST_SECRET")
- : config("SETTINGS::PAYMENTS:STRIPE:SECRET");
- }
- /**
- * @return string
- */
- protected function getStripeEndpointSecret()
- {
- return env('APP_ENV') == 'local'
- ? config("SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET")
- : config("SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET");
- }
- protected function createInvoice($user, $payment, $paymentStatus)
- {
- $creditProduct = CreditProduct::where('id', $payment->credit_product_id)->first();
- //create invoice
- $lastInvoiceID = \App\Models\Invoice::where("invoice_name", "like", "%" . now()->format('mY') . "%")->count("id");
- $newInvoiceID = $lastInvoiceID + 1;
- $logoPath = storage_path('app/public/logo.png');
- $seller = new Party([
- 'name' => config("SETTINGS::INVOICE:COMPANY_NAME"),
- 'phone' => config("SETTINGS::INVOICE:COMPANY_PHONE"),
- 'address' => config("SETTINGS::INVOICE:COMPANY_ADDRESS"),
- 'vat' => config("SETTINGS::INVOICE:COMPANY_VAT"),
- 'custom_fields' => [
- 'E-Mail' => config("SETTINGS::INVOICE:COMPANY_MAIL"),
- "Web" => config("SETTINGS::INVOICE:COMPANY_WEBSITE")
- ],
- ]);
- $customer = new Buyer([
- 'name' => $user->name,
- 'custom_fields' => [
- 'E-Mail' => $user->email,
- 'Client ID' => $user->id,
- ],
- ]);
- $item = (new InvoiceItem())
- ->title($creditProduct->description)
- ->pricePerUnit($creditProduct->price);
- $notes = [
- __("Payment method") . ": " . $payment->payment_method,
- ];
- $notes = implode("<br>", $notes);
- $invoice = Invoice::make()
- ->template('controlpanel')
- ->name(__("Invoice"))
- ->buyer($customer)
- ->seller($seller)
- ->discountByPercent(0)
- ->taxRate(floatval($creditProduct->getTaxPercent()))
- ->shipping(0)
- ->addItem($item)
- ->status(__($paymentStatus))
- ->series(now()->format('mY'))
- ->delimiter("-")
- ->sequence($newInvoiceID)
- ->serialNumberFormat(config("SETTINGS::INVOICE:PREFIX") . '{DELIMITER}{SERIES}{SEQUENCE}')
- ->notes($notes);
- if (file_exists($logoPath)) {
- $invoice->logo($logoPath);
- }
- //Save the invoice in "storage\app\invoice\USER_ID\YEAR"
- $invoice->filename = $invoice->getSerialNumber() . '.pdf';
- $invoice->render();
- Storage::disk("local")->put("invoice/" . $user->id . "/" . now()->format('Y') . "/" . $invoice->filename, $invoice->output);
- \App\Models\Invoice::create([
- 'invoice_user' => $user->id,
- 'invoice_name' => $invoice->getSerialNumber(),
- 'payment_id' => $payment->payment_id,
- ]);
- //Send Invoice per Mail
- $user->notify(new InvoiceNotification($invoice, $user, $payment));
- }
- /**
- * @return JsonResponse|mixed
- * @throws Exception
- */
- public function dataTable()
- {
- $query = Payment::with('user');
- return datatables($query)
- ->editColumn('user', function (Payment $payment) {
- return $payment->user->name;
- })
- ->editColumn('price', function (Payment $payment) {
- return $payment->formatToCurrency($payment->price);
- })
- ->editColumn('tax_value', function (Payment $payment) {
- return $payment->formatToCurrency($payment->tax_value);
- })
- ->editColumn('tax_percent', function (Payment $payment) {
- return $payment->tax_percent . ' %';
- })
- ->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() : '';
- })
- ->addColumn('actions', function (Payment $payment) {
- 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>
- ';
- })
- ->rawColumns(['actions'])
- ->make(true);
- }
- }
|