1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/PaymentDrivers/GoCardless/InstantBankPay.php

188 lines
6.2 KiB
PHP
Raw Normal View History

2021-11-11 15:52:32 +01:00
<?php
namespace App\PaymentDrivers\GoCardless;
2021-11-11 17:51:04 +01:00
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;
2021-11-11 17:52:24 +01:00
use App\PaymentDrivers\Common\MethodInterface;
2021-11-11 15:57:41 +01:00
use App\PaymentDrivers\GoCardlessPaymentDriver;
2021-11-11 17:52:05 +01:00
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\RedirectResponse;
2021-11-11 17:52:24 +01:00
use Illuminate\Http\Request;
2021-11-11 15:52:32 +01:00
class InstantBankPay implements MethodInterface
{
2021-11-11 15:57:41 +01:00
protected GoCardlessPaymentDriver $go_cardless;
public function __construct(GoCardlessPaymentDriver $go_cardless)
{
$this->go_cardless = $go_cardless;
$this->go_cardless->init();
}
2021-11-11 17:52:05 +01:00
/**
* Authorization page for Instant Bank Pay.
2021-11-11 17:52:24 +01:00
*
* @param array $data
* @return RedirectResponse
* @throws BindingResolutionException
2021-11-11 17:52:05 +01:00
*/
public function authorizeView(array $data): RedirectResponse
2021-11-11 17:51:04 +01:00
{
2021-11-11 17:52:05 +01:00
return redirect()->back();
2021-11-11 17:51:04 +01:00
}
2021-11-11 17:52:05 +01:00
/**
* Handle authorization for Instant Bank Pay.
2021-11-11 17:52:24 +01:00
*
* @param array $data
* @return RedirectResponse
* @throws BindingResolutionException
2021-11-11 17:52:05 +01:00
*/
public function authorizeResponse(Request $request): RedirectResponse
2021-11-11 17:51:04 +01:00
{
2021-11-11 17:52:05 +01:00
return redirect()->back();
2021-11-11 17:51:04 +01:00
}
public function paymentView(array $data)
{
try {
$billing_request = $this->go_cardless->gateway->billingRequests()->create([
'params' => [
'payment_request' => [
'description' => ctrans('texts.invoices').': '.collect($data['invoices'])->pluck('invoice_number'),
2021-11-11 17:51:04 +01:00
'amount' => (string) $data['amount_with_fee'] * 100,
'currency' => $this->go_cardless->client->getCurrencyCode(),
2022-11-07 02:52:17 +01:00
],
'metadata' => [
'payment_hash' => $this->go_cardless->payment_hash->hash,
],
],
2021-11-11 17:51:04 +01:00
]);
$billing_request_flow = $this->go_cardless->gateway->billingRequestFlows()->create([
'params' => [
'redirect_uri' => route('gocardless.ibp_redirect', [
'company_key' => $this->go_cardless->company_gateway->company->company_key,
'company_gateway_id' => $this->go_cardless->company_gateway->hashed_id,
'hash' => $this->go_cardless->payment_hash->hash,
2022-11-07 02:50:09 +01:00
]),
'links' => [
'billing_request' => $billing_request->id,
],
2021-11-11 17:51:04 +01:00
],
]);
$this->go_cardless->payment_hash
->withData('client_id', $this->go_cardless->client->id)
->withData('billing_request', $billing_request->id)
->withData('billing_request_flow', $billing_request_flow->id);
return redirect(
$billing_request_flow->authorisation_url
);
} catch (\Exception $exception) {
throw $exception;
}
}
public function paymentResponse($request)
{
$this->go_cardless->setPaymentHash(
$request->getPaymentHash()
);
2022-11-17 02:51:20 +01:00
$this->go_cardless->init();
2021-11-11 17:51:04 +01:00
try {
$billing_request = $this->go_cardless->gateway->billingRequests()->get(
$this->go_cardless->payment_hash->data->billing_request
);
2021-11-11 15:52:32 +01:00
2021-11-11 17:51:04 +01:00
$payment = $this->go_cardless->gateway->payments()->get(
$billing_request->payment_request->links->payment
);
2021-11-11 15:52:32 +01:00
2021-11-11 17:51:04 +01:00
if ($billing_request->status === 'fulfilled') {
return $this->processSuccessfulPayment($payment);
}
2021-11-11 15:52:32 +01:00
2021-11-11 17:51:04 +01:00
return $this->processUnsuccessfulPayment($payment);
} catch (\Exception $exception) {
throw new PaymentFailed(
2021-11-11 17:52:05 +01:00
$exception->getMessage(),
2021-11-11 17:51:04 +01:00
$exception->getCode()
);
}
}
/**
* Handle pending payments for Instant Bank Transfer.
*
* @param ResourcesPayment $payment
* @param array $data
* @return RedirectResponse
*/
public function processSuccessfulPayment(\GoCardlessPro\Resources\Payment $payment, array $data = [])
{
$data = [
'payment_method' => $payment->links->mandate,
'payment_type' => PaymentType::INSTANT_BANK_PAY,
'amount' => $this->go_cardless->payment_hash->data->amount_with_fee,
'transaction_reference' => $payment->id,
'gateway_type_id' => GatewayType::INSTANT_BANK_PAY,
];
$payment = $this->go_cardless->createPayment($data, Payment::STATUS_COMPLETED);
SystemLogger::dispatch(
['response' => $payment, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_GOCARDLESS,
$this->go_cardless->client,
$this->go_cardless->client->company,
);
return redirect()->route('client.payments.show', ['payment' => $this->go_cardless->encodePrimaryKey($payment->id)]);
}
2021-11-11 17:52:05 +01:00
/**
2021-11-11 17:51:04 +01:00
* Process unsuccessful payments for Direct Debit.
*
* @param ResourcesPayment $payment
* @return never
*/
public function processUnsuccessfulPayment(\GoCardlessPro\Resources\Payment $payment)
{
PaymentFailureMailer::dispatch($this->go_cardless->client, $payment->status, $this->go_cardless->client->company, $this->go_cardless->payment_hash->data->amount_with_fee);
PaymentFailureMailer::dispatch(
$this->go_cardless->client,
$payment,
$this->go_cardless->client->company,
$payment->amount
);
$message = [
'server_response' => $payment,
'data' => $this->go_cardless->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_GOCARDLESS,
$this->go_cardless->client,
$this->go_cardless->client->company,
);
}
2021-11-11 15:52:32 +01:00
}