1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Http/Controllers/ClientPortal/PaymentController.php

139 lines
4.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
*
* @copyright Copyright (c) 2021. 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
}
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)
{
2020-12-30 12:02:04 +01:00
$gateway = CompanyGateway::findOrFail($request->input('company_gateway_id'));
2020-12-09 15:17:48 +01:00
$payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->payment_hash])->first();
return $gateway
->driver(auth()->user()->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)
2021-06-29 08:05:03 +02:00
{
2020-10-14 01:53:20 +02:00
$payment_hash = PaymentHash::whereRaw('BINARY `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;
$payment->save();
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
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
}