2020-06-09 13:17:26 +02:00
|
|
|
<?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;
|
|
|
|
|
2020-08-12 04:02:21 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasPaid;
|
2020-08-17 00:58:52 +02:00
|
|
|
use App\Factory\PaymentFactory;
|
2020-08-30 00:00:57 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
2020-06-09 13:17:26 +02:00
|
|
|
use App\Models\Client;
|
2020-07-08 04:20:44 +02:00
|
|
|
use App\Models\ClientGatewayToken;
|
2020-06-09 13:17:26 +02:00
|
|
|
use App\Models\CompanyGateway;
|
2020-06-16 15:31:08 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
2020-06-09 13:17:26 +02:00
|
|
|
use App\PaymentDrivers\AbstractPaymentDriver;
|
2020-08-12 04:02:21 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-06-09 13:17:26 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\Utils\Traits\SystemLogTrait;
|
2020-08-17 00:58:52 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2020-06-09 13:17:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BaseDriver
|
|
|
|
* @package App\PaymentDrivers
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
/* 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-06-12 02:19:26 +02:00
|
|
|
/* The client */
|
2020-06-15 13:42:46 +02:00
|
|
|
public $client;
|
|
|
|
|
2020-07-08 04:20:44 +02:00
|
|
|
/* The initiated gateway driver class*/
|
2020-06-15 13:42:46 +02:00
|
|
|
public $payment_method;
|
2020-06-09 13:17:26 +02:00
|
|
|
|
2020-06-16 05:49:45 +02:00
|
|
|
public static $methods = [];
|
2020-06-16 02:21:40 +02:00
|
|
|
|
2020-06-10 14:42:10 +02:00
|
|
|
public function __construct(CompanyGateway $company_gateway, Client $client = null, $invitation = false)
|
2020-06-09 13:17:26 +02:00
|
|
|
{
|
|
|
|
$this->company_gateway = $company_gateway;
|
|
|
|
|
|
|
|
$this->invitation = $invitation;
|
|
|
|
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:19:26 +02:00
|
|
|
/**
|
|
|
|
* Authorize a payment method.
|
|
|
|
*
|
|
|
|
* Returns a reusable token for storage for future payments
|
|
|
|
* @param const $payment_method the GatewayType::constant
|
|
|
|
* @return view Return a view for collecting payment method information
|
|
|
|
*/
|
2020-06-10 10:05:30 +02:00
|
|
|
public function authorize($payment_method) {}
|
2020-06-09 13:17:26 +02:00
|
|
|
|
2020-06-12 02:19:26 +02:00
|
|
|
/**
|
|
|
|
* Executes purchase attempt for a given amount
|
|
|
|
*
|
|
|
|
* @param float $amount The amount to be collected
|
|
|
|
* @param boolean $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment)
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function purchase($amount, $return_client_response = false) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param boolean $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment)
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-06-24 03:15:51 +02:00
|
|
|
public function refund(Payment $payment, $amount, $return_client_response = false) {}
|
2020-06-09 13:17:26 +02:00
|
|
|
|
2020-06-15 13:42:46 +02:00
|
|
|
/**
|
|
|
|
* Set the inbound request payment method type for access.
|
|
|
|
*
|
|
|
|
* @param int $payment_method_id The Payment Method ID
|
|
|
|
*/
|
2020-06-16 14:41:56 +02:00
|
|
|
public function setPaymentMethod($payment_method_id){}
|
2020-06-16 02:21:40 +02:00
|
|
|
|
2020-06-16 15:31:08 +02:00
|
|
|
/**
|
|
|
|
* Helper method to attach invoices to a payment
|
|
|
|
*
|
|
|
|
* @param Payment $payment The payment
|
|
|
|
* @param array $hashed_ids The array of invoice hashed_ids
|
|
|
|
* @return Payment The payment object
|
|
|
|
*/
|
|
|
|
public function attachInvoices(Payment $payment, $hashed_ids): Payment
|
|
|
|
{
|
|
|
|
$transformed = $this->transformKeys($hashed_ids);
|
|
|
|
$array = is_array($transformed) ? $transformed : [$transformed];
|
2020-06-15 13:42:46 +02:00
|
|
|
|
2020-06-16 15:31:08 +02:00
|
|
|
$invoices = Invoice::whereIn('id', $array)
|
|
|
|
->whereClientId($this->client->id)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$payment->invoices()->sync($invoices);
|
|
|
|
$payment->save();
|
|
|
|
|
2020-08-12 00:17:32 +02:00
|
|
|
$payment->service()->applyNumber()->save();
|
|
|
|
|
2020-08-12 04:02:21 +02:00
|
|
|
$invoices->each(function ($invoice) use($payment){
|
|
|
|
event(new InvoiceWasPaid($invoice, $payment->company, Ninja::eventVars()));
|
|
|
|
});
|
|
|
|
|
2020-06-16 15:31:08 +02: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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
{
|
|
|
|
$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');
|
|
|
|
$payment->date = Carbon::now();
|
|
|
|
|
|
|
|
return $payment->service()->applyNumber()->save();
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:20:44 +02:00
|
|
|
/**
|
|
|
|
* Process an unattended payment
|
|
|
|
*
|
2020-07-08 08:54:16 +02:00
|
|
|
* @param ClientGatewayToken $cgt The client gateway token object
|
|
|
|
* @param float $amount The amount to bill
|
|
|
|
* @param Invoice $invoice Optional Invoice object being paid
|
|
|
|
* @return Response The payment response
|
2020-07-08 04:20:44 +02:00
|
|
|
*/
|
2020-07-08 08:54:16 +02:00
|
|
|
public function tokenBilling(ClientGatewayToken $cgt, float $amount, ?Invoice $invoice = null) {}
|
2020-08-30 00:00:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When a successful payment is made, we need to append the gateway fee
|
|
|
|
* to an invoice
|
|
|
|
*
|
|
|
|
* @param PaymentResponseRequest $request The incoming payment request
|
|
|
|
* @return void Success/Failure
|
|
|
|
*/
|
|
|
|
public function appendGatewayFeeToInvoice(PaymentResponseRequest $request) :void
|
|
|
|
{
|
|
|
|
/*Payment meta data*/
|
|
|
|
$payment_hash = $request->getPaymentHash();
|
2020-08-30 14:00:19 +02:00
|
|
|
|
2020-08-30 00:00:57 +02:00
|
|
|
/*Payment invoices*/
|
|
|
|
$payment_invoices = $payment_hash->invoices();
|
|
|
|
|
|
|
|
/*Fee charged at gateway*/
|
|
|
|
$fee_total = $payment_hash->fee_total;
|
|
|
|
|
|
|
|
/*Sum of invoice amounts*/
|
2020-08-30 14:00:19 +02:00
|
|
|
$invoice_totals = array_sum(array_column($payment_invoices,'amount'));
|
2020-08-30 00:00:57 +02:00
|
|
|
|
|
|
|
/*Hydrate invoices*/
|
2020-08-30 14:00:19 +02:00
|
|
|
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id')))->get();
|
2020-08-30 00:00:57 +02:00
|
|
|
|
|
|
|
/*Append gateway fee to invoice line item of first invoice*/
|
|
|
|
if($fee_total != 0){
|
|
|
|
$invoices->first()->service()->addGatewayFee($this->company_gateway, $invoice_totals)->save();
|
|
|
|
|
|
|
|
//We need to update invoice balance / client balance at this point so
|
|
|
|
//that payment record sanity is preserved //todo
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-06-09 13:17:26 +02:00
|
|
|
}
|