1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/PaymentDrivers/Stripe/BACS.php

192 lines
7.2 KiB
PHP
Raw Normal View History

2022-12-16 11:09:37 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Stripe;
use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Util\SystemLogger;
2023-01-20 09:20:30 +01:00
use App\Models\ClientGatewayToken;
2022-12-16 11:09:37 +01:00
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\StripePaymentDriver;
use App\PaymentDrivers\Stripe\Jobs\UpdateCustomer;
2022-12-16 14:48:33 +01:00
use Stripe\Checkout\Session;
2022-12-16 11:09:37 +01:00
use Stripe\PaymentIntent;
use Stripe\PaymentMethod;
use App\Utils\Number;
class BACS
{
public $stripe;
public function __construct(StripePaymentDriver $stripe)
{
$this->stripe = $stripe;
}
public function authorizeView(array $data)
{
2022-12-16 14:43:03 +01:00
$customer = $this->stripe->findOrCreateCustomer();
2022-12-16 14:55:13 +01:00
$data['session'] = Session::create([
2022-12-16 14:43:03 +01:00
'payment_method_types' => ['bacs_debit'],
'mode' => 'setup',
'customer' => $customer->id,
2022-12-17 14:22:03 +01:00
'success_url' => str_replace("%7B", "{", str_replace("%7D", "}", $this->buildAuthorizeUrl())),
2022-12-16 16:37:02 +01:00
'cancel_url' => route('client.payment_methods.index'),
2022-12-16 14:43:03 +01:00
]);
2022-12-16 14:55:13 +01:00
return render('gateways.stripe.bacs.authorize', $data);
2022-12-16 14:43:03 +01:00
}
2022-12-17 14:22:03 +01:00
private function buildAuthorizeUrl(): string
2022-12-16 14:43:03 +01:00
{
2022-12-16 16:55:42 +01:00
return route('client.payment_methods.confirm', [
2022-12-16 16:37:02 +01:00
'method' => GatewayType::BACS,
2022-12-16 15:19:00 +01:00
'session_id' => "{CHECKOUT_SESSION_ID}",
2022-12-16 14:43:03 +01:00
]);
2022-12-16 11:09:37 +01:00
}
public function authorizeResponse($request)
{
$this->stripe->init();
2022-12-17 06:58:45 +01:00
if ($request->session_id) {
2022-12-16 16:54:07 +01:00
$session = $this->stripe->stripe->checkout->sessions->retrieve($request->session_id, ['expand' => ['setup_intent']]);
2022-12-16 11:09:37 +01:00
2022-12-17 06:58:45 +01:00
$customer = $this->stripe->findOrCreateCustomer();
2022-12-17 07:16:20 +01:00
$this->stripe->attach($session->setup_intent->payment_method, $customer);
$payment_method = $this->stripe->getStripePaymentMethod($session->setup_intent->payment_method);
2022-12-17 07:11:02 +01:00
$this->storePaymentMethod($payment_method, $customer);
2022-12-17 06:58:45 +01:00
}
2022-12-16 11:09:37 +01:00
return redirect()->route('client.payment_methods.index');
}
public function paymentView(array $data)
{
2022-12-22 11:34:17 +01:00
$data['gateway'] = $this->stripe;
$data['amount'] = $data['total']['amount_with_fee'];
2022-12-22 11:38:32 +01:00
$data['payment_hash'] = $this->stripe->payment_hash->hash;
2022-12-16 11:09:37 +01:00
2022-12-22 11:34:17 +01:00
return render('gateways.stripe.bacs.pay', $data);
}
public function paymentResponse(PaymentResponseRequest $request)
2023-01-16 14:50:33 +01:00
2022-12-22 11:34:17 +01:00
{
$this->stripe->init();
$invoice_numbers = collect($this->stripe->payment_hash->invoices())->pluck('invoice_number')->implode(',');
$description = ctrans('texts.stripe_payment_text', ['invoicenumber' => $invoice_numbers, 'amount' => Number::formatMoney($request->amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]);
2022-12-16 11:09:37 +01:00
$payment_intent_data = [
2022-12-22 11:34:17 +01:00
'amount' => $this->stripe->convertToStripeAmount($request->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
2022-12-16 11:09:37 +01:00
'currency' => $this->stripe->client->getCurrencyCode(),
'customer' => $this->stripe->findOrCreateCustomer(),
'description' => $description,
2022-12-17 06:22:57 +01:00
'payment_method_types' => ['bacs_debit'],
2022-12-16 11:09:37 +01:00
'metadata' => [
'payment_hash' => $this->stripe->payment_hash->hash,
'gateway_type_id' => GatewayType::BACS,
],
2022-12-22 11:34:17 +01:00
'payment_method' => $request->token,
2022-12-22 11:06:00 +01:00
'confirm' => true,
2022-12-16 11:09:37 +01:00
];
$state = [
2023-01-16 15:40:28 +01:00
'payment_hash' => $this->stripe->payment_hash->hash,
2023-01-16 15:07:06 +01:00
'payment_intent' => $this->stripe->createPaymentIntent($payment_intent_data),
2022-12-16 11:09:37 +01:00
];
$state = array_merge($state, $request->all());
$state['customer'] = $state['payment_intent']->customer;
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $state);
$this->stripe->payment_hash->save();
2023-01-16 15:15:45 +01:00
if ($state['payment_intent']->status == 'processing') {
2023-01-16 15:11:47 +01:00
$this->stripe->logSuccessfulGatewayResponse(['response' => $state['payment_intent'], 'data' => $this->stripe->payment_hash], SystemLog::TYPE_STRIPE);
2022-12-16 11:09:37 +01:00
2023-01-16 15:22:18 +01:00
return $this->processSuccessfulPayment($state['payment_intent']);
2022-12-16 11:09:37 +01:00
}
2023-01-16 15:11:47 +01:00
return $this->processUnsuccessfulPayment("");
2022-12-16 11:09:37 +01:00
}
2023-01-16 15:22:18 +01:00
public function processSuccessfulPayment($payment_id)
2022-12-16 11:09:37 +01:00
{
UpdateCustomer::dispatch($this->stripe->company_gateway->company->company_key, $this->stripe->company_gateway->id, $this->stripe->client->id);
$data = [
2023-01-16 15:22:18 +01:00
'payment_method' => $payment_id['id'],
2023-01-16 14:58:38 +01:00
'payment_type' => PaymentType::BACS,
2023-01-16 15:24:47 +01:00
'amount' => $this->stripe->convertFromStripeAmount($payment_id->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
2023-01-16 15:22:18 +01:00
'transaction_reference' => $payment_id['id'],
2023-01-16 14:58:38 +01:00
'gateway_type_id' => GatewayType::BACS,
2022-12-16 11:09:37 +01:00
];
2023-01-16 14:58:38 +01:00
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);
2022-12-16 11:09:37 +01:00
SystemLogger::dispatch(
2023-01-16 15:22:18 +01:00
['response' => $payment_id, 'data' => $data],
2022-12-16 11:09:37 +01:00
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_STRIPE,
$this->stripe->client,
$this->stripe->client->company,
);
return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
}
public function processUnsuccessfulPayment($server_response)
{
2023-01-16 15:15:45 +01:00
$this->stripe->sendFailureMail($server_response);
2022-12-16 11:09:37 +01:00
$message = [
'server_response' => $server_response,
'data' => $this->stripe->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_STRIPE,
$this->stripe->client,
$this->stripe->client->company,
);
throw new PaymentFailed('Failed to process the payment.', 500);
}
2022-12-16 17:18:20 +01:00
private function storePaymentMethod($method, $customer)
2022-12-16 11:09:37 +01:00
{
try {
$payment_meta = new \stdClass;
2022-12-17 07:11:02 +01:00
$payment_meta->brand = (string) $method->bacs_debit->sort_code;
$payment_meta->last4 = (string) $method->bacs_debit->last4;
$payment_meta->state = 'unauthorized';
2022-12-16 11:09:37 +01:00
$payment_meta->type = GatewayType::BACS;
$data = [
'payment_meta' => $payment_meta,
2022-12-17 07:11:02 +01:00
'token' => $method->id,
2022-12-16 16:58:27 +01:00
'payment_method_id' => GatewayType::BACS,
2022-12-16 11:09:37 +01:00
];
2023-01-20 09:20:30 +01:00
$clientgateway = ClientGatewayToken::query()
->where('token', $method->id)
->first();
if (!$clientgateway){
$this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]);
}
2022-12-16 11:09:37 +01:00
} catch (\Exception $e) {
return $this->stripe->processInternallyFailedPayment($this->stripe, $e);
}
}
}