1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/PaymentDrivers/Stripe/SOFORT.php
Benjamin Beganović ae88d5e08e php-cs-fixer format
2020-11-25 15:19:52 +01:00

108 lines
3.4 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers\Stripe;
use App\Events\Payment\PaymentWasCreated;
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;
use App\Utils\Ninja;
class SOFORT
{
/** @var StripePaymentDriver */
public $stripe;
public function __construct(StripePaymentDriver $stripe)
{
$this->stripe = $stripe;
}
public function paymentView(array $data)
{
$data['gateway'] = $this->stripe;
$data['return_url'] = $this->buildReturnUrl($data);
$data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['amount_with_fee'], $this->stripe->client->currency()->precision);
$data['client'] = $this->stripe->client;
$data['country'] = $this->stripe->client->country->iso_3166_2;
return render('gateways.stripe.sofort.pay', $data);
}
private function buildReturnUrl($data): string
{
return route('client.payments.response', [
'company_gateway_id' => $this->stripe->company_gateway->id,
'gateway_type_id' => GatewayType::SOFORT,
'hashed_ids' => implode(',', $data['hashed_ids']),
'amount' => $data['amount'],
'fee' => $data['fee'],
]);
}
public function paymentResponse($request)
{
$state = array_merge($request->all(), []);
$amount = $state['amount'] + $state['fee'];
$state['amount'] = $this->stripe->convertToStripeAmount($amount, $this->stripe->client->currency()->precision);
if ($request->redirect_status == 'succeeded') {
return $this->processSuccessfulPayment($state);
}
return $this->processUnsuccessfulPayment($state);
}
public function processSuccessfulPayment($state)
{
$state['charge_id'] = $state['source'];
$this->stripe->init();
$state['payment_type'] = PaymentType::SOFORT;
$data = [
'payment_method' => $state['charge_id'],
'payment_type' => $state['payment_type'],
'amount' => $state['amount'],
'gateway_type_id' => GatewayType::SOFORT,
];
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);
/* @todo: https://github.com/invoiceninja/invoiceninja/pull/3789/files#r436175798 */
if (isset($state['hashed_ids'])) {
$this->stripe->attachInvoices($payment, $state['hashed_ids']);
}
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
$logger_message = [
'server_response' => $state,
'data' => $data,
];
SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->stripe->client);
return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
}
public function processUnsuccessfulPayment($state)
{
return redirect()->route('client.invoices.index')->with('warning', ctrans('texts.status_cancelled'));
}
}