1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/PaymentDrivers/Stripe/SEPA.php

180 lines
6.5 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2021-08-16 06:05:45 +02:00
*
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;
use App\Exceptions\PaymentFailed;
2023-10-26 04:57:44 +02:00
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
2023-10-26 04:57:44 +02:00
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
2021-10-13 15:45:33 +02:00
use App\PaymentDrivers\StripePaymentDriver;
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-12 18:27:17 +02:00
$this->stripe->init();
2021-10-05 16:31:48 +02:00
}
2021-10-05 16:34:19 +02:00
public function authorizeView($data)
{
2022-03-29 07:07:40 +02:00
$data['gateway'] = $this->stripe;
$data['payment_method_id'] = GatewayType::SEPA;
$data['client'] = $this->stripe->client;
$data['country'] = $this->stripe->client->country->iso_3166_2;
$data['currency'] = $this->stripe->client->currency();
2022-10-01 13:36:56 +02:00
$data['payment_hash'] = 'x';
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-13 15:45:33 +02:00
public function paymentView(array $data)
{
2021-10-05 16:54:34 +02:00
$data['gateway'] = $this->stripe;
2021-10-09 01:35:45 +02:00
$data['payment_method_id'] = GatewayType::SEPA;
2021-10-05 16:54:34 +02:00
$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-10-09 01:35:45 +02:00
$data['payment_hash'] = $this->stripe->payment_hash->hash;
2021-08-16 06:05:45 +02:00
2022-02-23 01:01:33 +01:00
$intent_data = [
2021-10-05 16:54:34 +02:00
'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(),
2023-02-23 03:38:58 +01:00
'description' => $this->stripe->getDescription(false),
'metadata' => [
'payment_hash' => $this->stripe->payment_hash->hash,
'gateway_type_id' => GatewayType::SEPA,
],
2022-02-23 01:01:33 +01:00
];
2021-10-05 16:54:34 +02:00
2023-02-16 02:36:09 +01:00
$intent = \Stripe\PaymentIntent::create($intent_data, array_merge($this->stripe->stripe_connect_auth, ['idempotency_key' => uniqid("st", true)]));
2021-10-05 16:54:34 +02:00
2022-02-23 01:01:33 +01:00
$data['pi_client_secret'] = $intent->client_secret;
2021-10-13 15:12:19 +02:00
2021-10-05 16:54:34 +02:00
$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);
}
public function paymentResponse(PaymentResponseRequest $request)
{
2021-10-09 01:35:45 +02:00
$gateway_response = json_decode($request->gateway_response);
2021-10-05 16:54:34 +02:00
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $request->all());
$this->stripe->payment_hash->save();
2022-11-27 00:47:59 +01:00
if (property_exists($gateway_response, 'status') && ($gateway_response->status == 'processing' || $gateway_response->status == 'succeeded')) {
2021-10-13 13:56:28 +02:00
if ($request->store_card) {
$this->storePaymentMethod($gateway_response);
}
2021-10-09 02:38:57 +02:00
2021-10-09 01:35:45 +02:00
return $this->processSuccessfulPayment($gateway_response->id);
2021-10-05 16:54:34 +02:00
}
return $this->processUnsuccessfulPayment();
}
public function processSuccessfulPayment(string $payment_intent)
{
$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
];
2021-10-13 13:56:28 +02:00
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);
2021-10-05 16:54:34 +02:00
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,
);
2021-10-13 13:56:28 +02:00
return redirect()->route('client.payments.show', $payment->hashed_id);
2021-10-05 16:54:34 +02:00
}
public function processUnsuccessfulPayment()
{
$server_response = $this->stripe->payment_hash->data;
2021-10-17 12:40:40 +02:00
$this->stripe->sendFailureMail($server_response);
2021-10-05 16:54:34 +02: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);
}
2021-10-09 02:38:57 +02:00
private function storePaymentMethod($intent)
{
try {
$method = $this->stripe->getStripePaymentMethod($intent->payment_method);
2024-01-14 05:05:00 +01:00
$payment_meta = new \stdClass();
2021-10-09 02:38:57 +02:00
$payment_meta->brand = (string) \sprintf('%s (%s)', $method->sepa_debit->bank_code, ctrans('texts.sepa'));
$payment_meta->last4 = (string) $method->sepa_debit->last4;
$payment_meta->state = 'authorized';
$payment_meta->type = GatewayType::SEPA;
$data = [
'payment_meta' => $payment_meta,
'token' => $intent->payment_method,
'payment_method_id' => GatewayType::SEPA,
];
$token = ClientGatewayToken::where([
'gateway_customer_reference' => $method->customer,
'token' => $method->id,
'client_id' => $this->stripe->client->id,
'company_id' => $this->stripe->client->company_id,
])->first();
if($token) {
return $token;
}
2021-10-09 02:38:57 +02:00
$this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $method->customer]);
} catch (\Exception $e) {
return $this->stripe->processInternallyFailedPayment($this->stripe, $e);
}
}
2021-08-16 06:05:45 +02:00
}