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

800 lines
27 KiB
PHP
Raw Normal View History

2020-06-09 13:17:26 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-06-09 13:17:26 +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-06-09 13:17:26 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-06-09 13:17:26 +02:00
*/
namespace App\PaymentDrivers;
2023-03-18 08:24:56 +01:00
use App\Events\Invoice\InvoiceWasPaid;
use App\Events\Payment\PaymentWasCreated;
use App\Exceptions\PaymentFailed;
use App\Factory\PaymentFactory;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Mail\NinjaMailer;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Mail\PaymentFailedMailer;
use App\Jobs\Util\SystemLogger;
use App\Mail\Admin\ClientPaymentFailureObject;
2023-02-17 22:36:51 +01:00
use App\Models\Client;
2023-03-18 08:24:56 +01:00
use App\Models\ClientContact;
use App\Models\ClientGatewayToken;
use App\Models\CompanyGateway;
use App\Models\GatewayType;
2023-02-17 22:36:51 +01:00
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentHash;
2023-03-18 08:24:56 +01:00
use App\Models\SystemLog;
use App\Services\Subscription\SubscriptionService;
use App\Utils\Helpers;
use App\Utils\Ninja;
use App\Utils\Number;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SystemLogTrait;
2023-02-17 22:36:51 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
2023-03-01 00:54:56 +01:00
use Illuminate\Support\Facades\App;
2023-03-18 08:24:56 +01:00
use Illuminate\Support\Str;
2020-06-09 13:17:26 +02:00
/**
* Class BaseDriver.
2020-06-09 13:17:26 +02:00
*/
2020-06-09 14:54:22 +02:00
class BaseDriver extends AbstractPaymentDriver
2020-06-09 13:17:26 +02:00
{
use SystemLogTrait;
use MakesHash;
/* The company gateway instance*/
public $company_gateway;
/* The Invitation */
2020-06-15 13:42:46 +02:00
public $invitation;
2020-06-09 13:17:26 +02:00
2023-08-08 23:44:34 +02:00
/**
* The Client
*
* @var \App\Models\Client|null $client
*/
2023-02-17 10:33:47 +01:00
public $client;
2020-06-09 13:17:26 +02:00
/* Gateway capabilities */
2020-06-15 13:42:46 +02:00
public $refundable = false;
2020-06-09 13:17:26 +02:00
/* Token billing */
2020-06-15 13:42:46 +02:00
public $token_billing = false;
2020-06-09 13:17:26 +02:00
/* Authorise payment methods */
2020-06-15 13:42:46 +02:00
public $can_authorise_credit_card = false;
2020-06-09 13:17:26 +02:00
2020-11-25 22:07:09 +01:00
/* The initialized gateway driver class*/
2020-06-15 13:42:46 +02:00
public $payment_method;
2020-06-09 13:17:26 +02:00
2023-02-17 10:33:47 +01:00
/**
* @var PaymentHash
*/
2021-11-07 10:31:03 +01:00
public $payment_hash;
2023-02-17 10:33:47 +01:00
/**
* @var Helpers`
*/
public $helpers;
2020-11-25 22:07:09 +01:00
/* Array of payment methods */
2020-06-16 05:49:45 +02:00
public static $methods = [];
/** @var array */
public $required_fields = [];
2020-06-16 02:21:40 +02:00
2023-02-17 11:05:01 +01:00
public function __construct(CompanyGateway $company_gateway, ?Client $client = null, $invitation = null)
2020-06-09 13:17:26 +02:00
{
$this->company_gateway = $company_gateway;
$this->invitation = $invitation;
$this->client = $client;
$this->helpers = new Helpers();
2020-06-09 13:17:26 +02:00
}
2023-02-14 21:41:21 +01:00
public function init()
{
return $this;
}
public function updateCustomer()
{
return $this;
}
2023-02-17 10:33:47 +01:00
/**
* Required fields for client to fill, to proceed with gateway actions.
*
* @return array[]
*/
public function getClientRequiredFields(): array
{
$fields = [];
if ($this->company_gateway->require_client_name) {
$fields[] = ['name' => 'client_name', 'label' => ctrans('texts.client_name'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_contact_name) {
$fields[] = ['name' => 'contact_first_name', 'label' => ctrans('texts.first_name'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'contact_last_name', 'label' => ctrans('texts.last_name'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_contact_email) {
$fields[] = ['name' => 'contact_email', 'label' => ctrans('texts.email'), 'type' => 'text', 'validation' => 'required,email:rfc'];
}
if ($this->company_gateway->require_client_phone) {
$fields[] = ['name' => 'client_phone', 'label' => ctrans('texts.client_phone'), 'type' => 'tel', 'validation' => 'required'];
}
if ($this->company_gateway->require_billing_address) {
$fields[] = ['name' => 'client_address_line_1', 'label' => ctrans('texts.address1'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_city', 'label' => ctrans('texts.city'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_state', 'label' => ctrans('texts.state'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_country_id', 'label' => ctrans('texts.country'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_postal_code) {
$fields[] = ['name' => 'client_postal_code', 'label' => ctrans('texts.postal_code'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_shipping_address) {
$fields[] = ['name' => 'client_shipping_address_line_1', 'label' => ctrans('texts.shipping_address1'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_city', 'label' => ctrans('texts.shipping_city'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_state', 'label' => ctrans('texts.shipping_state'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_postal_code', 'label' => ctrans('texts.shipping_postal_code'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_country_id', 'label' => ctrans('texts.shipping_country'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_custom_value1) {
$fields[] = ['name' => 'client_custom_value1', 'label' => $this->helpers->makeCustomField($this->client->company->custom_fields, 'client1'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_custom_value2) {
$fields[] = ['name' => 'client_custom_value2', 'label' => $this->helpers->makeCustomField($this->client->company->custom_fields, 'client2'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_custom_value3) {
$fields[] = ['name' => 'client_custom_value3', 'label' => $this->helpers->makeCustomField($this->client->company->custom_fields, 'client3'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_custom_value4) {
$fields[] = ['name' => 'client_custom_value4', 'label' => $this->helpers->makeCustomField($this->client->company->custom_fields, 'client4'), 'type' => 'text', 'validation' => 'required'];
}
return $fields;
}
2020-06-12 02:19:26 +02:00
/**
* Authorize a payment method.
*
* Returns a reusable token for storage for future payments
2020-11-27 12:08:42 +01:00
*
2020-11-25 22:07:09 +01:00
* @param array $data
* @return mixed Return a view for collecting payment method information
2020-06-12 02:19:26 +02:00
*/
2020-11-27 12:08:42 +01:00
public function authorizeView(array $data)
{
}
2020-06-12 02:19:26 +02:00
/**
2020-11-25 22:07:09 +01:00
* The payment authorization response
2020-11-27 12:08:42 +01:00
*
* @param Request $request
2020-11-25 22:07:09 +01:00
* @return mixed Return a response for collecting payment method information
2020-06-12 02:19:26 +02:00
*/
2020-11-27 12:08:42 +01:00
public function authorizeResponse(Request $request)
{
}
2020-11-25 22:07:09 +01:00
/**
* Process a payment
2020-11-27 12:08:42 +01:00
*
* @param array $data
2020-11-25 22:07:09 +01:00
* @return mixed Return a view for the payment
*/
2020-11-27 12:08:42 +01:00
public function processPaymentView(array $data)
{
}
2020-11-25 22:07:09 +01:00
/**
* Process payment response
2020-11-27 12:08:42 +01:00
*
2020-11-25 22:07:09 +01:00
* @param Request $request
* @return mixed Return a response for the payment
*/
2020-11-27 12:08:42 +01:00
public function processPaymentResponse(Request $request)
{
}
2020-06-12 02:19:26 +02:00
/**
* Executes a refund attempt for a given amount with a transaction_reference.
*
2020-06-24 03:15:51 +02:00
* @param Payment $payment The Payment Object
2020-06-12 02:19:26 +02:00
* @param float $amount The amount to be refunded
2020-10-12 06:10:34 +02:00
* @param bool $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment)
* @return mixed
2020-06-12 02:19:26 +02:00
*/
2020-11-27 12:08:42 +01:00
public function refund(Payment $payment, $amount, $return_client_response = false)
{
}
2020-11-25 22:07:09 +01:00
/**
* Process an unattended payment.
*
* @param ClientGatewayToken $cgt The client gateway token object
* @param PaymentHash $payment_hash The Payment hash containing the payment meta data
* @return void The payment response
*/
2020-11-27 12:08:42 +01:00
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
}
2020-06-09 13:17:26 +02:00
2021-06-20 12:24:11 +02:00
/**
* Detaches a payment method from the gateway
*
2021-06-20 12:24:11 +02:00
* @param ClientGatewayToken $token The gateway token
* @return bool boolean response
*/
public function detach(ClientGatewayToken $token)
{
return true;
}
2020-06-15 13:42:46 +02:00
/**
* Set the inbound request payment method type for access.
*
2020-06-15 13:42:46 +02:00
* @param int $payment_method_id The Payment Method ID
*/
2020-11-27 12:08:42 +01:00
public function setPaymentMethod($payment_method_id)
{
}
2020-11-25 22:07:09 +01:00
public function setClient(Client $client)
{
$this->client = $client;
2023-08-19 04:23:37 +02:00
return $this;
}
2020-11-25 22:07:09 +01:00
/************************** Helper methods *************************************/
2020-06-16 02:21:40 +02:00
public function setPaymentHash(PaymentHash $payment_hash)
{
$this->payment_hash = $payment_hash;
return $this;
}
/**
* Helper method to attach invoices to a payment.
*
2020-10-28 11:10:49 +01:00
* @param Payment $payment The payment
* @param PaymentHash $payment_hash
* @return Payment The payment object
*/
2020-09-01 01:28:37 +02:00
public function attachInvoices(Payment $payment, PaymentHash $payment_hash): Payment
{
2020-09-01 01:28:37 +02:00
$paid_invoices = $payment_hash->invoices();
2023-08-06 09:03:12 +02:00
$invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->withTrashed()->get();
$payment->invoices()->sync($invoices);
2021-12-17 12:11:36 +01:00
$payment->service()->applyNumber()->save();
$invoices->each(function ($invoice) use ($payment) {
2021-01-24 21:55:04 +01:00
event(new InvoiceWasPaid($invoice, $payment, $payment->company, Ninja::eventVars()));
2020-08-12 04:02:21 +02:00
});
2021-12-17 12:11:36 +01:00
return $payment;
}
2020-07-08 04:20:44 +02:00
2020-08-17 00:58:52 +02:00
/**
* Create a payment from an online payment.
*
2020-08-17 00:58:52 +02:00
* @param array $data Payment data
* @param int $status The payment status_id
* @return Payment The payment object
*/
public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment
{
if (in_array($status, [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING])) {
$this->confirmGatewayFee();
}
2021-03-17 16:12:25 +01:00
2021-10-24 06:14:36 +02:00
/*Never create a payment with a duplicate transaction reference*/
if (array_key_exists('transaction_reference', $data)) {
2021-10-24 06:14:36 +02:00
$_payment = Payment::where('transaction_reference', $data['transaction_reference'])
->where('client_id', $this->client->id)
2021-11-29 11:59:48 +01:00
->first();
2021-10-24 06:14:36 +02:00
if ($_payment) {
return $_payment;
}
2021-10-24 06:14:36 +02:00
}
2020-08-17 00:58:52 +02:00
$payment = PaymentFactory::create($this->client->company->id, $this->client->user->id);
$payment->client_id = $this->client->id;
$payment->company_gateway_id = $this->company_gateway->id;
$payment->status_id = $status;
$payment->currency_id = $this->client->getSetting('currency_id');
2023-06-15 08:23:08 +02:00
$payment->date = Carbon::now()->addSeconds($this->client->company->timezone()->utc_offset)->format('Y-m-d');
$payment->gateway_type_id = $data['gateway_type_id'];
2021-01-27 11:38:28 +01:00
$client_contact = $this->getContact();
$client_contact_id = $client_contact ? $client_contact->id : null;
$payment->amount = $data['amount'];
$payment->type_id = $data['payment_type'];
$payment->transaction_reference = $data['transaction_reference'];
$payment->client_contact_id = $client_contact_id;
2021-10-15 09:47:41 +02:00
$payment->saveQuietly();
/* Return early if the payment is not completed or pending*/
if (! in_array($status, [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING])) {
return $payment;
}
$this->payment_hash->payment_id = $payment->id;
$this->payment_hash->save();
2022-01-06 11:06:46 +01:00
$this->attachInvoices($payment, $this->payment_hash);
if ($this->payment_hash->credits_total() > 0) {
2021-01-06 06:54:04 +01:00
$payment = $payment->service()->applyCredits($this->payment_hash)->save();
}
2021-01-06 06:54:04 +01:00
$payment->service()->updateInvoicePayment($this->payment_hash);
2021-10-15 09:47:41 +02:00
event('eloquent.created: App\Models\Payment', $payment);
if ($this->client->getSetting('client_online_payment_notification') && in_array($status, [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING])) {
$payment->service()->sendEmail();
}
//todo
//catch any payment failures here also and fire a subsequent failure email if necessary? note only need for delayed payment forms
//perhaps this type of functionality should be handled higher up to provide better context?
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
2021-12-04 12:50:12 +01:00
if (property_exists($this->payment_hash->data, 'billing_context') && $status == Payment::STATUS_COMPLETED) {
2023-02-17 22:36:51 +01:00
if (is_int($this->payment_hash->data->billing_context->subscription_id)) {
2023-02-05 22:20:32 +01:00
$billing_subscription = \App\Models\Subscription::find($this->payment_hash->data->billing_context->subscription_id);
2023-02-17 22:36:51 +01:00
} else {
2023-02-05 22:20:32 +01:00
$billing_subscription = \App\Models\Subscription::find($this->decodePrimaryKey($this->payment_hash->data->billing_context->subscription_id));
2023-02-17 22:36:51 +01:00
}
// To access campaign hash => $this->payment_hash->data->billing_context->campaign;
// To access campaign data => Cache::get(CAMPAIGN_HASH)
// To access utm data => session()->get('utm-' . CAMPAIGN_HASH);
(new SubscriptionService($billing_subscription))->completePurchase($this->payment_hash);
}
2021-03-17 16:12:25 +01:00
2020-08-17 00:58:52 +02:00
return $payment->service()->applyNumber()->save();
}
2020-08-30 00:00:57 +02:00
/**
* When a successful payment is made, we need to append the gateway fee
* to an invoice.
*
2020-08-30 00:00:57 +02:00
* @return void Success/Failure
*/
public function confirmGatewayFee() :void
2020-08-30 00:00:57 +02:00
{
/*Payment invoices*/
$payment_invoices = $this->payment_hash->invoices();
2020-11-25 22:07:09 +01:00
/*Fee charged at gateway*/
$fee_total = $this->payment_hash->fee_total;
2020-08-30 00:00:57 +02:00
/*Hydrate invoices*/
2023-08-06 09:03:12 +02:00
$invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id')))->withTrashed()->get();
2020-08-30 00:00:57 +02:00
2023-04-29 03:10:52 +02:00
$invoices->each(function ($invoice) {
if (collect($invoice->line_items)->contains('type_id', '3')) {
2021-09-26 12:17:09 +02:00
$invoice->service()->toggleFeesPaid()->save();
}
});
2020-08-30 00:00:57 +02:00
}
2020-09-09 12:05:10 +02:00
/**
2020-10-28 11:10:49 +01:00
* In case of a payment failure we should always
* return the invoice to its original state
2020-10-28 11:10:49 +01:00
*
* @param PaymentHash $payment_hash The payment hash containing the list of invoices
2020-10-28 11:10:49 +01:00
* @return void
*/
public function unWindGatewayFees(PaymentHash $payment_hash)
{
2023-08-06 09:03:12 +02:00
$invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->get();
$invoices->each(function ($invoice) {
2022-04-02 07:13:31 +02:00
$invoice->service()->removeUnpaidGatewayFees();
});
}
2020-09-09 12:05:10 +02:00
/**
* Return the contact if possible.
*
*/
public function getContact()
{
if ($this->invitation) {
return ClientContact::find($this->invitation->client_contact_id);
} elseif (auth()->guard('contact')->user()) {
return auth()->user();
} else {
return false;
}
}
2020-10-26 14:40:50 +01:00
/**
* Store payment method as company gateway token.
2020-10-28 11:10:49 +01:00
*
* @param array $data
* @return null|ClientGatewayToken
2020-10-26 14:40:50 +01:00
*/
public function storeGatewayToken(array $data, array $additional = []): ?ClientGatewayToken
2020-10-26 14:40:50 +01:00
{
$company_gateway_token = new ClientGatewayToken();
$company_gateway_token->company_id = $this->client->company->id;
$company_gateway_token->client_id = $this->client->id;
$company_gateway_token->token = $data['token'];
$company_gateway_token->company_gateway_id = $this->company_gateway->id;
$company_gateway_token->gateway_type_id = $data['payment_method_id'];
$company_gateway_token->meta = $data['payment_meta'];
foreach ($additional as $key => $value) {
$company_gateway_token->{$key} = $value;
}
2020-10-26 14:40:50 +01:00
$company_gateway_token->save();
if ($this->client->gateway_tokens->count() == 1) {
$this->client->gateway_tokens()->update(['is_default' => 0]);
$company_gateway_token->is_default = 1;
$company_gateway_token->save();
}
return $company_gateway_token;
}
public function processInternallyFailedPayment($gateway, $e)
{
if (! is_null($this->payment_hash)) {
$this->unWindGatewayFees($this->payment_hash);
}
2021-02-01 22:33:04 +01:00
2022-06-16 03:21:10 +02:00
$error = $e->getMessage();
if (! $this->payment_hash) {
2021-11-06 06:05:56 +01:00
throw new PaymentFailed($error, $e->getCode());
}
2021-11-06 06:05:56 +01:00
$amount = array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total;
2021-10-17 12:40:40 +02:00
$this->sendFailureMail($error);
2021-10-13 11:24:15 +02:00
SystemLogger::dispatch(
$gateway->payment_hash,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
$gateway::SYSTEM_LOG_TYPE,
$gateway->client,
$gateway->client->company,
);
throw new PaymentFailed($error, $e->getCode());
}
public function sendFailureMail($error)
2021-10-17 12:40:40 +02:00
{
2023-02-17 22:36:51 +01:00
if (is_object($error)) {
$error = 'Payment Aborted';
}
if (! is_null($this->payment_hash)) {
$this->unWindGatewayFees($this->payment_hash);
}
if (! $error) {
$error = '';
}
2021-10-17 12:40:40 +02:00
PaymentFailedMailer::dispatch(
$this->payment_hash,
$this->client->company,
$this->client,
$error
);
}
2021-10-13 11:24:15 +02:00
public function clientPaymentFailureMailer($error)
{
2021-10-13 09:56:10 +02:00
if ($this->payment_hash && is_array($this->payment_hash->invoices())) {
$nmo = new NinjaMailerObject;
2021-10-13 11:24:15 +02:00
$nmo->mailable = new NinjaMailer((new ClientPaymentFailureObject($this->client, $error, $this->client->company, $this->payment_hash))->build());
$nmo->company = $this->client->company;
$nmo->settings = $this->client->company->settings;
2021-04-23 07:26:58 +02:00
2023-08-06 09:03:12 +02:00
$invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->withTrashed()->get();
2021-04-23 07:26:58 +02:00
2023-09-05 03:54:05 +02:00
// $invoices->each(function ($invoice) {
// $invoice->service()->deletePdf();
// });
2021-02-16 11:14:53 +01:00
$invoices->first()->invitations->each(function ($invitation) use ($nmo) {
if ((bool) $invitation->contact->send_email !== false && $invitation->contact->email) {
$nmo->to_user = $invitation->contact;
NinjaMailerJob::dispatch($nmo);
}
});
}
}
/**
* Wrapper method for checking if resource is good.
2020-11-27 12:08:42 +01:00
*
* @param mixed $resource
* @return bool
*/
public function checkRequiredResource($resource): bool
{
if (is_null($resource) || empty($resource)) {
return true;
}
return false;
}
/*Generic Global unsuccessful transaction method when the client is present*/
public function processUnsuccessfulTransaction($response, $client_present = true)
2021-07-21 09:04:44 +02:00
{
2022-04-28 04:40:07 +02:00
$error = array_key_exists('error', $response) ? $response['error'] : 'Undefined Error';
$error_code = array_key_exists('error_code', $response) ? $response['error_code'] : 'Undefined Error Code';
2021-07-21 09:04:44 +02:00
$this->unWindGatewayFees($this->payment_hash);
2021-10-17 12:40:40 +02:00
$this->sendFailureMail($error);
2021-07-21 09:04:44 +02:00
$nmo = new NinjaMailerObject;
$nmo->mailable = new NinjaMailer((new ClientPaymentFailureObject($this->client, $error, $this->client->company, $this->payment_hash))->build());
2021-07-21 09:04:44 +02:00
$nmo->company = $this->client->company;
$nmo->settings = $this->client->company->settings;
2023-08-06 09:03:12 +02:00
$invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->withTrashed()->get();
2021-07-21 09:04:44 +02:00
2023-09-05 03:54:05 +02:00
// $invoices->each(function ($invoice) {
// $invoice->service()->deletePdf();
// });
2021-07-21 09:04:44 +02:00
$invoices->first()->invitations->each(function ($invitation) use ($nmo) {
if (! $invitation->contact->trashed()) {
2021-07-21 09:04:44 +02:00
$nmo->to_user = $invitation->contact;
NinjaMailerJob::dispatch($nmo);
}
});
$message = [
'server_response' => $response,
'data' => $this->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
2021-09-06 07:48:31 +02:00
SystemLog::TYPE_PAYTRACE,
2021-07-21 09:04:44 +02:00
$this->client,
$this->client->company,
);
if ($client_present) {
throw new PaymentFailed($error, 500);
}
2021-07-21 09:04:44 +02:00
}
public function checkRequirements()
{
if ($this->company_gateway->require_billing_address) {
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->address1)) {
$this->required_fields[] = 'billing_address1';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->address2)) {
$this->required_fields[] = 'billing_address2';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->city)) {
$this->required_fields[] = 'billing_city';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->state)) {
$this->required_fields[] = 'billing_state';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->postal_code)) {
$this->required_fields[] = 'billing_postal_code';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->country_id)) {
$this->required_fields[] = 'billing_country';
}
}
if ($this->company_gateway->require_shipping_address) {
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->shipping_address1)) {
$this->required_fields[] = 'shipping_address1';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->shipping_address2)) {
$this->required_fields[] = 'shipping_address2';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->shipping_city)) {
$this->required_fields[] = 'shipping_city';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->shipping_state)) {
$this->required_fields[] = 'shipping_state';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->shipping_postal_code)) {
$this->required_fields[] = 'shipping_postal_code';
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->shipping_country_id)) {
$this->required_fields[] = 'shipping_country';
}
}
if ($this->company_gateway->require_client_name) {
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->name)) {
$this->required_fields[] = 'name';
}
}
if ($this->company_gateway->require_client_phone) {
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->phone)) {
$this->required_fields[] = 'phone';
}
}
if ($this->company_gateway->require_postal_code) {
// In case "require_postal_code" is true, we don't need billing address.
foreach ($this->required_fields as $position => $field) {
if (Str::startsWith($field, 'billing')) {
unset($this->required_fields[$position]);
}
2020-11-27 12:08:42 +01:00
}
2021-10-06 05:47:17 +02:00
if ($this->checkRequiredResource($this->client->postal_code)) {
$this->required_fields[] = 'postal_code';
}
}
return $this;
}
2020-12-09 15:17:48 +01:00
public function getCompanyGatewayId(): int
{
return $this->company_gateway->id;
}
2021-01-11 23:06:56 +01:00
public function logSuccessfulGatewayResponse($response, $gateway_const)
{
SystemLogger::dispatch(
$response,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
$gateway_const,
$this->client,
$this->client->company,
2021-01-11 23:06:56 +01:00
);
}
2021-07-05 00:59:52 +02:00
public function genericWebhookUrl()
{
return route('payment_notification_webhook', [
'company_key' => $this->client->company->company_key,
'company_gateway_id' => $this->encodePrimaryKey($this->company_gateway->id),
2021-07-05 02:30:47 +02:00
'client' => $this->encodePrimaryKey($this->client->id),
2021-07-05 00:59:52 +02:00
]);
2021-07-05 02:23:42 +02:00
}
2021-07-05 02:30:47 +02:00
2021-06-30 13:21:46 +02:00
/* Performs an extra iterate on the gatewayTypes() array and passes back only the enabled gateways*/
2021-06-30 14:03:13 +02:00
public function gatewayTypeEnabled($type)
2021-06-30 13:21:46 +02:00
{
$types = [];
2021-06-30 14:14:16 +02:00
$types[] = GatewayType::CREDIT_CARD;
$types[] = GatewayType::BANK_TRANSFER;
2021-06-30 13:21:46 +02:00
return $types;
2021-07-05 00:59:52 +02:00
}
2021-08-17 06:01:11 +02:00
2023-05-14 01:25:08 +02:00
public function getStatementDescriptor(): string
{
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->client->getMergedSettings()));
App::setLocale($this->client->company->locale());
if (! $this->payment_hash || !$this->client)
2023-05-31 06:36:35 +02:00
return 'Descriptor';
2023-05-14 01:25:08 +02:00
$invoices_string = \implode(', ', collect($this->payment_hash->invoices())->pluck('invoice_number')->toArray()) ?: null;
2023-07-04 01:07:31 +02:00
if (!$invoices_string)
return str_replace(["*","<",">","'",'"'], "", $this->client->company->present()->name());
2023-05-14 01:25:08 +02:00
$invoices_string = str_replace(["*","<",">","'",'"'], "-", $invoices_string);
2023-05-31 23:54:08 +02:00
$invoices_string = "I-".$invoices_string;
2023-05-31 06:36:35 +02:00
2023-05-14 01:25:08 +02:00
$invoices_string = substr($invoices_string,0,22);
$invoices_string = str_pad($invoices_string, 5, ctrans('texts.invoice'), STR_PAD_LEFT);
2023-05-14 01:25:08 +02:00
return $invoices_string;
}
2021-10-13 05:04:14 +02:00
/**
* Generic description handler
*/
public function getDescription(bool $abbreviated = true)
{
2023-03-01 00:54:56 +01:00
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->client->getMergedSettings()));
App::setLocale($this->client->company->locale());
if (! $this->payment_hash || !$this->client) {
return 'No description';
}
2021-10-13 05:04:14 +02:00
$invoices_string = \implode(', ', collect($this->payment_hash->invoices())->pluck('invoice_number')->toArray()) ?: null;
2023-05-14 01:25:08 +02:00
$amount = Number::formatMoney($this->payment_hash?->amount_with_fee() ?? 0, $this->client);
2023-03-21 22:00:20 +01:00
if($abbreviated && $invoices_string){
return $invoices_string;
} elseif ($abbreviated || ! $invoices_string) {
return ctrans('texts.gateway_payment_text_no_invoice', [
'amount' => $amount,
'client' => $this->client->present()->name(),
]);
}
2021-10-13 05:04:14 +02:00
return ctrans('texts.gateway_payment_text', [
'invoices' => $invoices_string,
'amount' => $amount,
2023-03-18 08:24:56 +01:00
'client' => $this->client->present()->name(),
]);
2021-10-13 05:04:14 +02:00
return sprintf('%s: %s', ctrans('texts.invoices'), \implode(', ', collect($this->payment_hash->invoices())->pluck('invoice_number')->toArray()));
}
/**
* Stub for disconnecting from the gateway.
*
* @return void
*/
2021-08-17 06:01:11 +02:00
public function disconnect()
{
return true;
}
2020-06-09 13:17:26 +02:00
}