1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Controllers/ClientPortal/PaymentController.php

167 lines
5.0 KiB
PHP
Raw Normal View History

2019-08-16 07:20:28 +02:00
<?php
2019-08-16 07:20:28 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-08-16 07:20:28 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2019-08-16 07:20:28 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-08-16 07:20:28 +02:00
*/
namespace App\Http\Controllers\ClientPortal;
use App\Exceptions\PaymentFailed;
2020-10-13 14:28:30 +02:00
use App\Factory\PaymentFactory;
2019-08-16 07:20:28 +02:00
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Invoice\InjectSignature;
use App\Jobs\Util\SystemLogger;
2019-09-13 00:33:48 +02:00
use App\Models\CompanyGateway;
2019-09-25 04:07:33 +02:00
use App\Models\Invoice;
2019-08-16 07:20:28 +02:00
use App\Models\Payment;
2020-08-26 02:47:50 +02:00
use App\Models\PaymentHash;
use App\Models\SystemLog;
2021-10-06 05:47:17 +02:00
use App\Services\ClientPortal\InstantPayment;
use App\Services\Subscription\SubscriptionService;
2019-09-25 04:07:33 +02:00
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
2019-08-16 07:20:28 +02:00
use App\Utils\Traits\MakesHash;
2020-10-28 11:10:49 +01:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
2019-08-16 07:20:28 +02:00
use Illuminate\Http\Request;
2021-03-17 16:12:25 +01:00
use Illuminate\Support\Facades\Cache;
2020-08-26 02:47:50 +02:00
use Illuminate\Support\Str;
2020-10-28 11:10:49 +01:00
use Illuminate\View\View;
2019-08-16 07:20:28 +02:00
/**
* Class PaymentController.
2019-08-16 07:20:28 +02:00
*/
class PaymentController extends Controller
{
use MakesHash;
2019-09-25 04:07:33 +02:00
use MakesDates;
2019-08-16 07:20:28 +02:00
/**
* Show the list of payments.
2019-08-16 07:20:28 +02:00
*
2020-10-28 11:10:49 +01:00
* @return Factory|View
2019-08-16 07:20:28 +02:00
*/
public function index()
2019-08-16 07:20:28 +02:00
{
return $this->render('payments.index');
2019-08-16 07:20:28 +02:00
}
/**
* Display the specified resource.
*
2020-03-23 18:10:42 +01:00
* @param Request $request
* @param Payment $payment
2020-10-28 11:10:49 +01:00
* @return Factory|View
2019-08-16 07:20:28 +02:00
*/
public function show(Request $request, Payment $payment)
2019-08-16 07:20:28 +02:00
{
$payment->load('invoices');
2019-08-16 07:20:28 +02:00
2020-03-23 18:10:42 +01:00
return $this->render('payments.show', [
'payment' => $payment,
]);
2019-08-16 07:20:28 +02:00
}
public function catch_process(Request $request)
{
return $this->render('payments.index');
}
2019-09-12 08:10:21 +02:00
/**
* Presents the payment screen for a given
* gateway and payment method.
* The request will also contain the amount
2019-09-12 13:46:09 +02:00
* and invoice ids for reference.
*
2020-10-28 11:10:49 +01:00
* @param Request $request
* @return RedirectResponse|mixed
2019-09-12 08:10:21 +02:00
*/
2020-10-15 02:37:16 +02:00
public function process(Request $request)
{
2021-10-06 05:47:17 +02:00
return (new InstantPayment($request))->run();
2019-09-12 08:10:21 +02:00
}
public function response(PaymentResponseRequest $request)
{
2022-07-09 04:15:35 +02:00
2020-12-30 12:02:04 +01:00
$gateway = CompanyGateway::findOrFail($request->input('company_gateway_id'));
2022-07-09 04:15:35 +02:00
$payment_hash = PaymentHash::where('hash', $request->payment_hash)->firstOrFail();
$invoice = Invoice::with('client')->find($payment_hash->fee_invoice_id);
2022-07-09 04:15:35 +02:00
$client = $invoice ? $invoice->client : auth()->guard('contact')->user()->client;
// 09-07-2022 catch duplicate responses for invoices that already paid here.
2022-07-16 06:43:10 +02:00
if($invoice && $invoice->status_id == Invoice::STATUS_PAID){
$data = [
'invoice' => $invoice,
2022-08-28 10:55:55 +02:00
'key' => false,
'invitation' => $invoice->invitations->first()
2022-07-16 06:43:10 +02:00
];
if ($request->query('mode') === 'fullscreen') {
return render('invoices.show-fullscreen', $data);
}
return $this->render('invoices.show', $data);
}
return $gateway
->driver($client)
->setPaymentMethod($request->input('payment_method_id'))
->setPaymentHash($payment_hash)
->checkRequirements()
->processPaymentResponse($request);
}
2020-10-14 01:53:20 +02:00
/**
* Pay for invoice/s using credits only.
2020-10-28 11:10:49 +01:00
*
2021-01-25 16:46:40 +01:00
* @param Request $request The request object
2020-10-14 01:53:20 +02:00
* @return Response The response view
*/
public function credit_response(Request $request)
{
2022-02-26 08:48:22 +01:00
$payment_hash = PaymentHash::where('hash', $request->input('payment_hash'))->first();
2020-10-13 14:28:30 +02:00
2020-10-14 01:53:20 +02:00
/* Hydrate the $payment */
2020-11-25 15:19:52 +01:00
if ($payment_hash->payment()->exists()) {
2020-10-13 14:28:30 +02:00
$payment = $payment_hash->payment;
2020-11-25 15:19:52 +01:00
} else {
2020-10-14 01:53:20 +02:00
$payment = PaymentFactory::create($payment_hash->fee_invoice->company_id, $payment_hash->fee_invoice->user_id);
$payment->client_id = $payment_hash->fee_invoice->client_id;
2022-01-19 23:26:52 +01:00
$payment->saveQuietly();
$payment->currency_id = $payment->client->getSetting('currency_id');
2021-10-15 09:47:41 +02:00
$payment->saveQuietly();
2020-10-14 01:53:20 +02:00
2020-10-13 14:28:30 +02:00
$payment_hash->payment_id = $payment->id;
$payment_hash->save();
}
2021-01-06 06:54:04 +01:00
$payment = $payment->service()->applyCredits($payment_hash)->save();
2020-10-13 14:28:30 +02:00
2021-10-15 09:47:41 +02:00
event('eloquent.created: App\Models\Payment', $payment);
if (property_exists($payment_hash->data, 'billing_context')) {
$billing_subscription = \App\Models\Subscription::find($payment_hash->data->billing_context->subscription_id);
return (new SubscriptionService($billing_subscription))->completePurchase($payment_hash);
}
2020-10-14 01:53:20 +02:00
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
}
2020-10-15 02:37:16 +02:00
2020-10-15 05:35:35 +02:00
public function processCreditPayment(Request $request, array $data)
2020-10-15 02:37:16 +02:00
{
2020-10-15 05:35:35 +02:00
return render('gateways.credit.index', $data);
2020-10-15 02:37:16 +02:00
}
2019-08-16 07:20:28 +02:00
}