1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/PaymentDrivers/CustomPaymentDriver.php

165 lines
4.8 KiB
PHP
Raw Normal View History

2020-08-20 00:11:46 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-08-20 00:11:46 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2020-08-20 00:11:46 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-08-20 00:11:46 +02:00
*/
namespace App\PaymentDrivers;
2023-03-18 08:24:56 +01:00
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Invoice;
2020-08-20 00:11:46 +02:00
use App\Models\Payment;
2023-03-18 08:13:49 +01:00
use App\Models\PaymentHash;
use App\Models\PaymentType;
2023-03-18 08:24:56 +01:00
use App\Models\SystemLog;
use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesHash;
2020-08-20 00:11:46 +02:00
/**
* Class CustomPaymentDriver.
2020-08-20 00:11:46 +02:00
*/
class CustomPaymentDriver extends BaseDriver
{
use MakesHash;
2020-08-20 00:11:46 +02:00
public $token_billing = false;
public $can_authorise_credit_card = false;
2020-08-20 00:11:46 +02:00
/**
* Returns the gateway types.
2020-08-20 00:11:46 +02:00
*/
2020-12-09 15:17:48 +01:00
public function gatewayTypes(): array
2020-08-20 00:11:46 +02:00
{
$types = [
GatewayType::CUSTOM,
2020-08-20 00:11:46 +02:00
];
return $types;
}
2023-02-05 23:03:49 +01:00
public function init()
{
return $this;
}
2020-08-20 00:11:46 +02:00
public function setPaymentMethod($payment_method_id)
{
$this->payment_method = $payment_method_id;
return $this;
}
/**
* View for displaying custom content of the driver.
2020-12-10 12:38:46 +01:00
*
* @param array $data
* @return mixed
*/
2020-08-20 00:11:46 +02:00
public function processPaymentView($data)
{
2021-02-15 11:45:14 +01:00
$variables = [];
2021-02-15 11:45:14 +01:00
if (count($this->payment_hash->invoices()) > 0) {
$invoice_id = $this->decodePrimaryKey($this->payment_hash->invoices()[0]->invoice_id);
$invoice = Invoice::withTrashed()->find($invoice_id);
2021-02-15 11:45:14 +01:00
$variables = (new HtmlEngine($invoice->invitations->first()))->generateLabelsAndValues();
}
$variables['values']['$invoices'] = collect($this->payment_hash->invoices())->pluck('invoice_number')->implode(',');
$variables['labels']['$invoices_label'] = ctrans('texts.invoice_number_short');
2020-12-09 15:17:48 +01:00
$data['title'] = $this->company_gateway->getConfigField('name');
$data['instructions'] = strtr($this->company_gateway->getConfigField('text'), $variables['values']);
2020-12-09 15:17:48 +01:00
$this->payment_hash->data = array_merge((array) $this->payment_hash->data, $data);
$this->payment_hash->save();
2020-12-09 15:17:48 +01:00
$data['gateway'] = $this;
2023-03-18 08:13:49 +01:00
$data['payment_hash'] = $this->payment_hash->hash;
2020-12-09 15:17:48 +01:00
return render('gateways.custom.payment', $data);
2020-08-20 00:11:46 +02:00
}
/**
* Processing method for payment. Should never be reached with this driver.
2020-12-10 12:38:46 +01:00
*
* @return mixed
*/
2020-08-20 00:11:46 +02:00
public function processPaymentResponse($request)
{
2023-03-18 08:13:49 +01:00
if ($request->has('gateway_response')) {
$this->client = auth()->guard('contact')->user()->client;
$state = [
'server_response' => json_decode($request->gateway_response),
'payment_hash' => $request->payment_hash,
];
$payment_hash = PaymentHash::where('hash', $request->payment_hash)->first();
2023-03-18 08:24:56 +01:00
if ($payment_hash) {
2023-03-18 08:13:49 +01:00
$this->payment_hash = $payment_hash;
$payment_hash->data = array_merge((array) $payment_hash->data, $state);
$payment_hash->save();
}
$gateway_response = json_decode($request->gateway_response);
if ($gateway_response->status == 'COMPLETED') {
$this->logSuccessfulGatewayResponse(['response' => json_decode($request->gateway_response), 'data' => $payment_hash], SystemLog::TYPE_CUSTOM);
2023-03-18 08:24:56 +01:00
$data = [
'payment_method' => '',
'payment_type' => PaymentType::CREDIT_CARD_OTHER,
'amount' => $payment_hash->amount_with_fee(),
'transaction_reference' => $gateway_response?->purchase_units[0]?->payments?->captures[0]?->id,
'gateway_type_id' => GatewayType::PAYPAL,
];
$payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
SystemLogger::dispatch(
['response' => $payment_hash->data->server_response, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_STRIPE,
$this->client,
$this->client->company,
);
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
2023-03-18 08:13:49 +01:00
}
}
return redirect()->route('client.invoices');
2020-08-20 00:11:46 +02:00
}
/**
* Detach payment method from custom payment driver.
2020-10-28 11:10:49 +01:00
*
* @param ClientGatewayToken $token
* @return void
*/
public function detach(ClientGatewayToken $token)
{
// Driver doesn't support this feature.
}
public function getClientRequiredFields(): array
{
return [];
}
2020-08-20 00:11:46 +02:00
}