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

200 lines
6.6 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
2019-09-25 04:07:33 +02:00
use App\Models\Invoice;
2019-08-16 07:20:28 +02:00
use App\Models\Payment;
2023-02-27 05:32:37 +01:00
use Illuminate\View\View;
use App\Models\GatewayType;
2020-08-26 02:47:50 +02:00
use App\Models\PaymentHash;
2023-02-27 05:32:37 +01:00
use App\Models\PaymentType;
use Illuminate\Http\Request;
use App\Models\CompanyGateway;
use App\Factory\PaymentFactory;
2019-08-16 07:20:28 +02:00
use App\Utils\Traits\MakesHash;
2023-02-27 05:32:37 +01:00
use App\Utils\Traits\MakesDates;
use App\Http\Controllers\Controller;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\RedirectResponse;
2023-02-27 05:32:37 +01:00
use Illuminate\Contracts\View\Factory;
use App\PaymentDrivers\Stripe\BankTransfer;
use App\Services\ClientPortal\InstantPayment;
use App\Services\Subscription\SubscriptionService;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
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');
2023-02-27 05:32:37 +01:00
$bank_details = false;
$payment_intent = false;
$data = false;
$gateway = false;
if($payment->gateway_type_id == GatewayType::DIRECT_DEBIT && $payment->type_id == PaymentType::DIRECT_DEBIT){
if (method_exists($payment->company_gateway->driver($payment->client), 'getPaymentIntent')) {
$stripe = $payment->company_gateway->driver($payment->client);
$payment_intent = $stripe->getPaymentIntent($payment->transaction_reference);
$bt = new BankTransfer($stripe);
match($payment->currency->code){
'MXN' => $data = $bt->formatDataforMx($payment_intent),
'EUR' => $data = $bt->formatDataforEur($payment_intent),
'JPY' => $data = $bt->formatDataforJp($payment_intent),
'GBP' => $data = $bt->formatDataforUk($payment_intent),
};
$gateway = $stripe;
}
}
2019-08-16 07:20:28 +02:00
2023-02-27 05:32:37 +01:00
2020-03-23 18:10:42 +01:00
return $this->render('payments.show', [
'payment' => $payment,
2023-02-27 05:32:37 +01:00
'bank_details' => $payment_intent ? $data : false,
2023-03-15 14:13:30 +01:00
'currency' => $payment->currency ? strtolower($payment->currency->code) : strtolower($payment->client->currency()->code),
2020-03-23 18:10:42 +01:00
]);
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)
{
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.
2023-02-16 02:36:09 +01:00
if ($invoice && $invoice->status_id == Invoice::STATUS_PAID) {
2022-07-16 06:43:10 +02:00
$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);
}
2023-02-16 02:36:09 +01:00
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
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')));
2023-02-04 07:09:04 +01:00
2023-02-16 02:36:09 +01:00
$invoices->each(function ($i) {
2023-02-04 07:09:04 +01:00
$i->is_proforma = false;
$i->saveQuietly();
});
2021-10-15 09:47:41 +02:00
event('eloquent.created: App\Models\Payment', $payment);
2023-02-16 02:36:09 +01:00
if ($invoices->sum('balance') > 0) {
$invoice = $invoices->first();
2023-02-04 07:09:04 +01:00
$invoice->service()->touchPdf(true);
return redirect()->route('client.invoice.show', ['invoice' => $invoice->hashed_id, 'hash' => $request->input('hash')]);
}
if (property_exists($payment_hash->data, 'billing_context')) {
$billing_subscription = \App\Models\Subscription::find($this->decodePrimaryKey($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
}