2019-08-16 07:20:28 +02:00
|
|
|
<?php
|
2020-05-09 00:20:37 +02:00
|
|
|
|
2019-08-16 07:20:28 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +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;
|
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
use App\Factory\PaymentFactory;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
|
|
|
use App\Models\CompanyGateway;
|
|
|
|
use App\Models\GatewayType;
|
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;
|
2023-02-27 05:32:37 +01:00
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\PaymentDrivers\Stripe\BankTransfer;
|
|
|
|
use App\Services\ClientPortal\InstantPayment;
|
|
|
|
use App\Services\Subscription\SubscriptionService;
|
2023-03-18 08:24:56 +01:00
|
|
|
use App\Utils\Traits\MakesDates;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Contracts\View\Factory;
|
2023-08-06 04:20:18 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2023-03-18 08:24:56 +01:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\View\View;
|
2020-04-15 02:30:52 +02:00
|
|
|
|
2019-08-16 07:20:28 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +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
|
|
|
|
|
|
|
/**
|
2020-04-23 00:49:23 +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
|
|
|
*/
|
2020-04-23 00:49:23 +02:00
|
|
|
public function index()
|
2019-08-16 07:20:28 +02:00
|
|
|
{
|
2020-04-23 00:49:23 +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
|
|
|
*/
|
2019-09-25 08:23:51 +02:00
|
|
|
public function show(Request $request, Payment $payment)
|
2019-08-16 07:20:28 +02:00
|
|
|
{
|
2019-09-25 08:23:51 +02:00
|
|
|
$payment->load('invoices');
|
2023-02-27 05:32:37 +01:00
|
|
|
$bank_details = false;
|
|
|
|
$payment_intent = false;
|
|
|
|
$data = false;
|
|
|
|
$gateway = false;
|
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
if ($payment->gateway_type_id == GatewayType::DIRECT_DEBIT && $payment->type_id == PaymentType::DIRECT_DEBIT) {
|
2023-02-27 05:32:37 +01:00
|
|
|
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);
|
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
match ($payment->currency->code) {
|
2023-02-27 05:32:37 +01:00
|
|
|
'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
|
|
|
}
|
|
|
|
|
2021-12-15 00:07:04 +01: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.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
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)
|
2020-03-26 04:23:57 +01:00
|
|
|
{
|
2021-10-06 05:47:17 +02:00
|
|
|
return (new InstantPayment($request))->run();
|
2019-09-12 08:10:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 14:12:39 +02:00
|
|
|
public function response(PaymentResponseRequest $request)
|
2019-09-25 06:03:28 +02:00
|
|
|
{
|
2023-08-04 08:40:44 +02:00
|
|
|
/** @var \App\Models\CompanyGateway $gateway **/
|
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();
|
2022-01-24 23:24:05 +01:00
|
|
|
$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);
|
|
|
|
}
|
2022-01-24 23:24:05 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
return $gateway
|
|
|
|
->driver($client)
|
|
|
|
->setPaymentMethod($request->input('payment_method_id'))
|
|
|
|
->setPaymentHash($payment_hash)
|
|
|
|
->checkRequirements()
|
|
|
|
->processPaymentResponse($request);
|
2019-09-25 06:03:28 +02:00
|
|
|
}
|
2020-10-13 12:32:15 +02:00
|
|
|
|
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
|
2023-07-26 04:59:36 +02:00
|
|
|
* @return \Response The response view
|
2020-10-14 01:53:20 +02:00
|
|
|
*/
|
2020-10-13 12:32:15 +02:00
|
|
|
public function credit_response(Request $request)
|
2022-06-21 11:57:17 +02:00
|
|
|
{
|
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
|
|
|
|
2023-08-06 04:20:18 +02:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Invoice> $invoices */
|
2023-08-06 09:03:12 +02:00
|
|
|
$invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')));
|
2023-08-06 04:20:18 +02:00
|
|
|
|
|
|
|
$invoices->each(function ($invoice) {
|
|
|
|
/** @var \App\Models\Invoice $invoice **/
|
|
|
|
$invoice->is_proforma = false;
|
|
|
|
$invoice->saveQuietly();
|
2023-02-04 07:09:04 +01:00
|
|
|
});
|
2022-12-22 05:58:18 +01:00
|
|
|
|
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) {
|
2023-08-06 04:20:18 +02:00
|
|
|
/** @var \App\Models\Invoice $invoice **/
|
2022-12-22 05:58:18 +01:00
|
|
|
$invoice = $invoices->first();
|
|
|
|
|
|
|
|
return redirect()->route('client.invoice.show', ['invoice' => $invoice->hashed_id, 'hash' => $request->input('hash')]);
|
|
|
|
}
|
|
|
|
|
2021-04-16 06:47:11 +02:00
|
|
|
if (property_exists($payment_hash->data, 'billing_context')) {
|
2023-01-18 06:52:32 +01:00
|
|
|
$billing_subscription = \App\Models\Subscription::find($this->decodePrimaryKey($payment_hash->data->billing_context->subscription_id));
|
2021-04-16 06:47:11 +02:00
|
|
|
|
|
|
|
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-13 12:32:15 +02:00
|
|
|
}
|
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
|
|
|
}
|