2019-09-05 09:00:12 +02:00
|
|
|
<?php
|
2020-05-09 00:20:37 +02:00
|
|
|
|
2019-09-05 09:00:12 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-09-05 09:00:12 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-09-05 09:00:12 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-09-05 09:00:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers;
|
|
|
|
|
2019-09-30 08:54:24 +02:00
|
|
|
use App\Factory\PaymentFactory;
|
2020-08-30 14:00:19 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
2019-09-16 06:59:59 +02:00
|
|
|
use App\Models\Client;
|
2019-09-25 07:55:52 +02:00
|
|
|
use App\Models\ClientContact;
|
2019-09-05 14:42:26 +02:00
|
|
|
use App\Models\CompanyGateway;
|
2019-09-08 14:13:55 +02:00
|
|
|
use App\Models\GatewayType;
|
2019-10-01 03:56:48 +02:00
|
|
|
use App\Models\Invoice;
|
2019-09-30 08:54:24 +02:00
|
|
|
use App\Models\Payment;
|
2020-09-01 01:28:37 +02:00
|
|
|
use App\Models\PaymentHash;
|
2020-05-09 00:20:37 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-10-02 05:00:51 +02:00
|
|
|
use App\Utils\Traits\SystemLogTrait;
|
2019-10-01 03:56:48 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2019-09-05 14:42:26 +02:00
|
|
|
use Omnipay\Omnipay;
|
|
|
|
|
2019-09-05 09:00:12 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class BasePaymentDriver.
|
2019-09-26 07:14:07 +02:00
|
|
|
'amount' => $invoice->getRequestedAmount(),
|
|
|
|
'currency' => $invoice->getCurrencyCode(),
|
|
|
|
'returnUrl' => $completeUrl,
|
|
|
|
'cancelUrl' => $this->invitation->getLink(),
|
2019-11-27 11:27:24 +01:00
|
|
|
'description' => trans('texts.' . $invoice->getEntityType()) . " {$invoice->number}",
|
|
|
|
'transactionId' => $invoice->number,
|
2019-09-26 07:14:07 +02:00
|
|
|
'transactionType' => 'Purchase',
|
|
|
|
'clientIp' => Request::getClientIp(),
|
|
|
|
];
|
2019-09-05 09:00:12 +02:00
|
|
|
*/
|
2019-09-05 14:42:26 +02:00
|
|
|
class BasePaymentDriver
|
2019-09-05 09:00:12 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
use SystemLogTrait;
|
2020-05-09 00:20:37 +02:00
|
|
|
use MakesHash;
|
2020-03-25 14:08:37 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/* The company gateway instance*/
|
2020-06-01 14:14:41 +02:00
|
|
|
public $company_gateway;
|
2019-09-05 14:42:26 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/* The Omnipay payment driver instance*/
|
|
|
|
protected $gateway;
|
2019-09-05 14:42:26 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/* The Invitation */
|
|
|
|
protected $invitation;
|
2019-09-08 14:13:55 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/* Gateway capabilities */
|
|
|
|
protected $refundable = false;
|
2020-03-25 14:08:37 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/* Token billing */
|
|
|
|
protected $token_billing = false;
|
2019-09-06 01:00:23 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/* Authorise payment methods */
|
2019-09-15 13:40:46 +02:00
|
|
|
protected $can_authorise_credit_card = false;
|
|
|
|
|
2019-09-16 06:59:59 +02:00
|
|
|
public function __construct(CompanyGateway $company_gateway, Client $client, $invitation = false)
|
2019-09-05 14:42:26 +02:00
|
|
|
{
|
|
|
|
$this->company_gateway = $company_gateway;
|
2019-10-02 00:44:13 +02:00
|
|
|
|
2019-09-08 14:13:55 +02:00
|
|
|
$this->invitation = $invitation;
|
2019-10-02 00:44:13 +02:00
|
|
|
|
2019-09-16 06:59:59 +02:00
|
|
|
$this->client = $client;
|
2019-09-05 14:42:26 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns the Omnipay driver.
|
2020-11-01 06:09:09 +01:00
|
|
|
* @return stdClass Omnipay initialized object
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
|
|
|
protected function gateway()
|
2019-09-05 14:42:26 +02:00
|
|
|
{
|
|
|
|
$this->gateway = Omnipay::create($this->company_gateway->gateway->provider);
|
|
|
|
$this->gateway->initialize((array) $this->company_gateway->getConfig());
|
|
|
|
|
2019-09-06 01:00:23 +02:00
|
|
|
return $this;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the configuration fields for the
|
2020-09-06 11:38:10 +02:00
|
|
|
* Gatway.
|
2019-12-30 22:59:12 +01:00
|
|
|
* @return array The configuration fields
|
|
|
|
*/
|
|
|
|
public function getFields()
|
|
|
|
{
|
|
|
|
return $this->gateway->getDefaultParameters();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns the default gateway type.
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
|
|
|
public function gatewayTypes()
|
2019-09-08 14:13:55 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
GatewayType::CREDIT_CARD,
|
|
|
|
];
|
|
|
|
}
|
2019-09-06 01:00:23 +02:00
|
|
|
|
2020-05-09 00:20:37 +02:00
|
|
|
public function getCompanyGatewayId(): int
|
2019-09-25 06:03:28 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->company_gateway->id;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns whether refunds are possible with the gateway.
|
|
|
|
* @return bool TRUE|FALSE
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
2020-05-09 00:20:37 +02:00
|
|
|
public function getRefundable(): bool
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
|
|
|
return $this->refundable;
|
|
|
|
}
|
2019-09-06 07:22:05 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns whether token billing is possible with the gateway.
|
|
|
|
* @return bool TRUE|FALSE
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
2020-05-09 00:20:37 +02:00
|
|
|
public function getTokenBilling(): bool
|
2019-09-26 07:14:07 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->token_billing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether gateway can
|
|
|
|
* authorise and credit card.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return bool [type] [description]
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
2020-05-09 00:20:37 +02:00
|
|
|
public function canAuthoriseCreditCard(): bool
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
|
|
|
return $this->can_authorise_credit_card;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Refunds a given payment.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param $payment
|
|
|
|
* @param int $amount
|
|
|
|
* @return false
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
2020-02-19 21:44:12 +01:00
|
|
|
public function refundPayment($payment, $amount = 0)
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2020-05-04 23:22:31 +02:00
|
|
|
if ($amount) {
|
|
|
|
$amount = min($amount, $payment->getCompletedAmount());
|
|
|
|
} else {
|
|
|
|
$amount = $payment->getCompletedAmount();
|
|
|
|
}
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($payment->is_deleted || ! $amount) {
|
2020-02-19 21:44:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($payment->type_id == Payment::TYPE_CREDIT_CARD) {
|
|
|
|
return $payment->recordRefund($amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
$details = $this->refundDetails($payment, $amount);
|
|
|
|
$response = $this->gateway()->refund($details)->send();
|
|
|
|
|
|
|
|
if ($response->isSuccessful()) {
|
|
|
|
return $payment->recordRefund($amount);
|
|
|
|
} elseif ($this->attemptVoidPayment($response, $payment, $amount)) {
|
|
|
|
$details = ['transactionReference' => $payment->transaction_reference];
|
|
|
|
$response = $this->gateway->void($details)->send();
|
|
|
|
if ($response->isSuccessful()) {
|
|
|
|
return $payment->markVoided();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function attemptVoidPayment($response, $payment, $amount)
|
|
|
|
{
|
|
|
|
// Partial refund not allowed for unsettled transactions
|
|
|
|
return $amount == $payment->amount;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeCreditCardView(array $data)
|
|
|
|
{
|
|
|
|
}
|
2019-09-26 07:14:07 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function authorizeCreditCardResponse($request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processPaymentView(array $data)
|
|
|
|
{
|
|
|
|
}
|
2020-03-25 14:08:37 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function processPaymentResponse($request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Return the contact if possible.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* @return ClientContact The ClientContact object
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************* Omnipay ******************************************
|
2020-10-28 11:10:49 +01:00
|
|
|
* authorize($options) - authorize an amount on the customer's card
|
|
|
|
* completeAuthorize($options) - handle return from off-site gateways after authorization
|
|
|
|
* capture($options) - capture an amount you have previously authorized
|
|
|
|
* purchase($options) - authorize and immediately capture an amount on the customer's card
|
|
|
|
* completePurchase($options) - handle return from off-site gateways after purchase
|
|
|
|
* refund($options) - refund an already processed transaction
|
|
|
|
* void($options) - generally can only be called up to 24 hours after submitting a transaction
|
|
|
|
* acceptNotification() - convert an incoming request from an off-site gateway to a generic notification object for further processing
|
|
|
|
* @param $input
|
|
|
|
* @return array
|
2020-05-09 00:20:37 +02:00
|
|
|
*/
|
2019-12-30 22:59:12 +01:00
|
|
|
|
2020-05-09 00:20:37 +02:00
|
|
|
protected function paymentDetails($input): array
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2019-09-26 07:14:07 +02:00
|
|
|
$data = [
|
|
|
|
'currency' => $this->client->getCurrencyCode(),
|
|
|
|
'transactionType' => 'Purchase',
|
|
|
|
'clientIp' => request()->getClientIp(),
|
|
|
|
];
|
2019-10-02 00:44:13 +02:00
|
|
|
|
2019-09-26 07:14:07 +02:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function purchase($data, $items)
|
|
|
|
{
|
|
|
|
$this->gateway();
|
2019-09-30 01:26:37 +02:00
|
|
|
|
2020-09-01 01:28:37 +02:00
|
|
|
$response = $this->gateway
|
|
|
|
->purchase($data)
|
|
|
|
->setItems($items)
|
|
|
|
->send();
|
2019-09-06 07:22:05 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $response;
|
|
|
|
}
|
2020-03-25 14:08:37 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function completePurchase($data)
|
|
|
|
{
|
|
|
|
$this->gateway();
|
2019-09-30 08:54:24 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->gateway
|
2020-05-09 00:20:37 +02:00
|
|
|
->completePurchase($data)
|
|
|
|
->send();
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-09-30 08:54:24 +02:00
|
|
|
|
2020-06-09 13:07:18 +02:00
|
|
|
public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment
|
2019-12-30 22:59:12 +01: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;
|
2020-06-09 13:07:18 +02:00
|
|
|
$payment->status_id = $status;
|
2020-03-08 06:59:06 +01:00
|
|
|
$payment->currency_id = $this->client->getSetting('currency_id');
|
2019-12-30 22:59:12 +01:00
|
|
|
$payment->date = Carbon::now();
|
2020-03-25 14:08:37 +01:00
|
|
|
|
2020-08-19 03:06:19 +02:00
|
|
|
return $payment->service()->applyNumber()->save();
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-10-01 03:56:48 +02:00
|
|
|
|
2020-09-01 01:28:37 +02:00
|
|
|
public function attachInvoices(Payment $payment, PaymentHash $payment_hash): Payment
|
2019-10-01 03:56:48 +02:00
|
|
|
{
|
2020-09-01 01:28:37 +02:00
|
|
|
$paid_invoices = $payment_hash->invoices();
|
2021-07-20 01:30:12 +02:00
|
|
|
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->withTrashed()->get();
|
2019-12-30 22:59:12 +01:00
|
|
|
$payment->invoices()->sync($invoices);
|
2019-10-01 03:56:48 +02:00
|
|
|
$payment->save();
|
|
|
|
|
|
|
|
return $payment;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-08-30 14:00:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When a successful payment is made, we need to append the gateway fee
|
2020-09-06 11:38:10 +02:00
|
|
|
* to an invoice.
|
|
|
|
*
|
2020-08-30 14:00:19 +02:00
|
|
|
* @param PaymentResponseRequest $request The incoming payment request
|
|
|
|
* @return void Success/Failure
|
|
|
|
*/
|
2020-08-31 04:00:43 +02:00
|
|
|
public function confirmGatewayFee(PaymentResponseRequest $request) :void
|
2020-08-30 14:00:19 +02:00
|
|
|
{
|
|
|
|
/*Payment meta data*/
|
|
|
|
$payment_hash = $request->getPaymentHash();
|
|
|
|
|
|
|
|
/*Payment invoices*/
|
|
|
|
$payment_invoices = $payment_hash->invoices();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
// /*Fee charged at gateway*/
|
2020-08-30 14:00:19 +02:00
|
|
|
$fee_total = $payment_hash->fee_total;
|
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
// Sum of invoice amounts
|
|
|
|
// $invoice_totals = array_sum(array_column($payment_invoices,'amount'));
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-30 14:00:19 +02:00
|
|
|
/*Hydrate invoices*/
|
2021-07-20 01:30:12 +02:00
|
|
|
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id')))->withTrashed()->get();
|
2020-08-30 14:00:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoices->each(function ($invoice) use ($fee_total) {
|
|
|
|
if (collect($invoice->line_items)->contains('type_id', '3')) {
|
2020-08-31 04:00:43 +02:00
|
|
|
$invoice->service()->toggleFeesPaid()->save();
|
|
|
|
$invoice->client->service()->updateBalance($fee_total)->save();
|
2021-01-21 05:42:30 +01:00
|
|
|
$invoice->ledger()->updateInvoiceBalance($fee_total, "Gateway fee adjustment for Invoice {$invoice->number}");
|
2020-08-31 04:00:43 +02:00
|
|
|
}
|
|
|
|
});
|
2020-08-30 14:00:19 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|