1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/PaymentDrivers/Stripe/SEPA.php

141 lines
4.9 KiB
PHP
Raw Normal View History

2021-08-16 06:05:45 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-10-06 08:49:59 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-08-16 06:05:45 +02:00
*/
namespace App\PaymentDrivers\Stripe;
2021-10-05 16:19:38 +02:00
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
2021-10-05 16:31:48 +02:00
use App\PaymentDrivers\StripePaymentDriver;
2021-10-05 16:54:34 +02:00
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\Exceptions\PaymentFailed;
2021-08-16 06:05:45 +02:00
2021-10-05 16:56:18 +02:00
class SEPA
2021-08-16 06:05:45 +02:00
{
2021-10-05 16:31:48 +02:00
/** @var StripePaymentDriver */
public StripePaymentDriver $stripe;
public function __construct(StripePaymentDriver $stripe)
{
$this->stripe = $stripe;
}
2021-10-05 16:34:19 +02:00
public function authorizeView($data)
{
2021-10-05 16:39:01 +02:00
return render('gateways.stripe.sepa.authorize', $data);
2021-10-05 16:34:19 +02:00
}
2021-08-16 06:05:45 +02:00
2021-10-05 16:54:34 +02:00
public function paymentView(array $data) {
$data['gateway'] = $this->stripe;
$data['return_url'] = $this->buildReturnUrl();
$data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency());
$data['client'] = $this->stripe->client;
$data['customer'] = $this->stripe->findOrCreateCustomer()->id;
$data['country'] = $this->stripe->client->country->iso_3166_2;
2021-08-16 06:05:45 +02:00
2021-10-05 16:54:34 +02:00
$intent = \Stripe\PaymentIntent::create([
'amount' => $data['stripe_amount'],
'currency' => 'eur',
2021-10-06 08:53:36 +02:00
'payment_method_types' => ['sepa_debit'],
2021-10-06 14:35:16 +02:00
'setup_future_usage' => 'off_session',
2021-10-05 16:54:34 +02:00
'customer' => $this->stripe->findOrCreateCustomer(),
'description' => $this->stripe->decodeUnicodeString(ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number')),
2021-08-16 06:05:45 +02:00
2021-10-05 16:54:34 +02:00
]);
$data['pi_client_secret'] = $intent->client_secret;
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $data['stripe_amount']]);
$this->stripe->payment_hash->save();
return render('gateways.stripe.sepa.pay', $data);
}
private function buildReturnUrl(): string
{
return route('client.payments.response', [
'company_gateway_id' => $this->stripe->company_gateway->id,
'payment_hash' => $this->stripe->payment_hash->hash,
'payment_method_id' => GatewayType::SEPA,
]);
}
public function paymentResponse(PaymentResponseRequest $request)
{
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $request->all());
$this->stripe->payment_hash->save();
if ($request->redirect_status == 'succeeded') {
return $this->processSuccessfulPayment($request->payment_intent);
}
return $this->processUnsuccessfulPayment();
}
public function processSuccessfulPayment(string $payment_intent)
{
$this->stripe->init();
$data = [
'payment_method' => $payment_intent,
2021-10-06 08:53:36 +02:00
'payment_type' => PaymentType::SEPA,
2021-10-05 16:54:34 +02:00
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'transaction_reference' => $payment_intent,
2021-10-06 08:53:36 +02:00
'gateway_type_id' => GatewayType::SEPA,
2021-10-05 16:54:34 +02:00
];
$this->stripe->createPayment($data, Payment::STATUS_PENDING);
SystemLogger::dispatch(
['response' => $this->stripe->payment_hash->data, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_STRIPE,
$this->stripe->client,
$this->stripe->client->company,
);
return redirect()->route('client.payments.index');
}
public function processUnsuccessfulPayment()
{
$server_response = $this->stripe->payment_hash->data;
PaymentFailureMailer::dispatch(
$this->stripe->client,
$server_response,
$this->stripe->client->company,
$this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency())
);
$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);
}
2021-08-16 06:05:45 +02:00
}