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

165 lines
5.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-13 15:45:33 +02:00
use App\Exceptions\PaymentFailed;
2021-10-05 16:19:38 +02:00
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
2021-10-05 16:54:34 +02:00
use App\Jobs\Util\SystemLogger;
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)
{
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
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')),
]);
$data['pi_client_secret'] = $intent->client_secret;
2021-10-13 15:12:19 +02:00
if (count($data['tokens']) > 0) {
$setup_intent = $this->stripe->stripe->setupIntents->create([
'payment_method_types' => ['sepa_debit'],
'customer' => $this->stripe->findOrCreateCustomer()->id,
]);
$data['si_client_secret'] = $setup_intent->client_secret;
}
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();
2021-10-13 15:12:19 +02: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);
$payment_meta = new \stdClass;
$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,
];
$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
}