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

Add payment driver

This commit is contained in:
Lars Kusch 2021-10-10 11:43:55 +02:00
parent 3f9a801818
commit 3601dc5c2b
2 changed files with 153 additions and 1 deletions

View File

@ -0,0 +1,141 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Stripe;
use App\Exceptions\PaymentFailed;
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\PaymentDrivers\StripePaymentDriver;
class Bancontact
{
/** @var StripePaymentDriver */
public StripePaymentDriver $stripe;
public function __construct(StripePaymentDriver $stripe)
{
$this->stripe = $stripe;
}
public function authorizeView($data)
{
return render('gateways.stripe.bancontact.authorize', $data);
}
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;
$intent = \Stripe\PaymentIntent::create([
'amount' => $data['stripe_amount'],
'currency' => 'eur',
'payment_method_types' => ['bancontact'],
'customer' => $this->stripe->findOrCreateCustomer(),
'description' => $this->stripe->decodeUnicodeString(ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number')),
]);
$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.bancontact.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::BANCONTACT,
]);
}
public function paymentResponse($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)
{
/* @todo: https://github.com/invoiceninja/invoiceninja/pull/3789/files#r436175798 */
$this->stripe->init();
$data = [
'payment_method' => $payment_intent,
'payment_type' => PaymentType::BANCONTACT,
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'transaction_reference' => $payment_intent,
'gateway_type_id' => GatewayType::BANCONTACT,
];
$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);
}
}

View File

@ -37,6 +37,7 @@ use App\PaymentDrivers\Stripe\PRZELEWY24;
use App\PaymentDrivers\Stripe\GIROPAY; use App\PaymentDrivers\Stripe\GIROPAY;
use App\PaymentDrivers\Stripe\iDeal; use App\PaymentDrivers\Stripe\iDeal;
use App\PaymentDrivers\Stripe\EPS; use App\PaymentDrivers\Stripe\EPS;
use App\PaymentDrivers\Stripe\Bancontact;
use App\PaymentDrivers\Stripe\UpdatePaymentMethods; use App\PaymentDrivers\Stripe\UpdatePaymentMethods;
use App\PaymentDrivers\Stripe\Utilities; use App\PaymentDrivers\Stripe\Utilities;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
@ -85,6 +86,7 @@ class StripePaymentDriver extends BaseDriver
GatewayType::GIROPAY => GIROPAY::class, GatewayType::GIROPAY => GIROPAY::class,
GatewayType::IDEAL => iDeal::class, GatewayType::IDEAL => iDeal::class,
GatewayType::EPS => EPS::class, GatewayType::EPS => EPS::class,
GatewayType::BANCONTACT => Bancontact::class,
]; ];
const SYSTEM_LOG_TYPE = SystemLog::TYPE_STRIPE; const SYSTEM_LOG_TYPE = SystemLog::TYPE_STRIPE;
@ -176,7 +178,7 @@ class StripePaymentDriver extends BaseDriver
&& in_array($this->client->country->iso_3166_3, ["DEU"])){ && in_array($this->client->country->iso_3166_3, ["DEU"])){
$types[] = GatewayType::GIROPAY; $types[] = GatewayType::GIROPAY;
} }
if ($this->client if ($this->client
&& $this->client->currency() && $this->client->currency()
&& ($this->client->currency()->code == 'EUR') && ($this->client->currency()->code == 'EUR')
@ -191,6 +193,13 @@ class StripePaymentDriver extends BaseDriver
&& in_array($this->client->country->iso_3166_3, ["AUT"])) && in_array($this->client->country->iso_3166_3, ["AUT"]))
$types[] = GatewayType::EPS; $types[] = GatewayType::EPS;
if ($this->client
&& $this->client->currency()
&& ($this->client->currency()->code == 'EUR')
&& isset($this->client->country)
&& in_array($this->client->country->iso_3166_3, ["BEL", "DEU"]))
$types[] = GatewayType::BANCONTACT;
return $types; return $types;
} }
@ -224,6 +233,8 @@ class StripePaymentDriver extends BaseDriver
return 'gateways.stripe.ideal'; return 'gateways.stripe.ideal';
case GatewayType::EPS: case GatewayType::EPS:
return 'gateways.stripe.eps'; return 'gateways.stripe.eps';
case GatewayType::BANCONTACT:
return 'gateways.stripe.bancontact';
default: default:
break; break;
} }