2020-02-03 11:33:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* payment Ninja (https://paymentninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/paymentninja/paymentninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. payment Ninja LLC (https://paymentninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Payment;
|
|
|
|
|
|
|
|
use App\Factory\PaymentFactory;
|
2020-02-20 22:05:01 +01:00
|
|
|
use App\Models\Invoice;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Models\Payment;
|
2020-08-12 00:17:32 +02:00
|
|
|
use App\Services\Payment\ApplyNumber;
|
2020-06-28 00:24:08 +02:00
|
|
|
use App\Services\Payment\DeletePayment;
|
2020-05-29 00:21:47 +02:00
|
|
|
use App\Services\Payment\RefundPayment;
|
2020-02-20 22:05:01 +01:00
|
|
|
use App\Services\Payment\UpdateInvoicePayment;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
|
|
|
class PaymentService
|
|
|
|
{
|
|
|
|
private $payment;
|
|
|
|
|
|
|
|
public function __construct($payment)
|
|
|
|
{
|
|
|
|
$this->payment = $payment;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function manualPayment($invoice) :?Payment
|
|
|
|
{
|
|
|
|
/* Create Payment */
|
|
|
|
$payment = PaymentFactory::create($invoice->company_id, $invoice->user_id);
|
|
|
|
|
|
|
|
$payment->amount = $invoice->balance;
|
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
|
|
|
$payment->client_id = $invoice->client_id;
|
|
|
|
$payment->transaction_reference = ctrans('texts.manual_entry');
|
2020-03-08 06:59:06 +01:00
|
|
|
$payment->currency_id = $invoice->client->getSetting('currency_id');
|
2020-02-03 11:33:07 +01:00
|
|
|
/* Create a payment relationship to the invoice entity */
|
|
|
|
$payment->save();
|
|
|
|
|
|
|
|
$payment->invoices()->attach($invoice->id, [
|
|
|
|
'amount' => $payment->amount
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
public function sendEmail($contact = null)
|
|
|
|
{
|
2020-05-09 00:35:49 +02:00
|
|
|
return (new SendEmail($this->payment, $contact))->run();
|
2020-02-20 22:05:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function reversePayment()
|
|
|
|
{
|
|
|
|
$invoices = $this->payment->invoices()->get();
|
|
|
|
$client = $this->payment->client;
|
|
|
|
|
|
|
|
$invoices->each(function ($invoice) {
|
|
|
|
if ($invoice->pivot->amount > 0) {
|
|
|
|
$invoice->status_id = Invoice::STATUS_SENT;
|
|
|
|
$invoice->balance = $invoice->pivot->amount;
|
|
|
|
$invoice->save();
|
|
|
|
}
|
|
|
|
});
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
$this->payment->ledger()->updatePaymentBalance($this->payment->amount);
|
|
|
|
|
|
|
|
$client->service()
|
|
|
|
->updateBalance($this->payment->amount)
|
|
|
|
->updatePaidToDate($this->payment->amount*-1)
|
|
|
|
->save();
|
|
|
|
}
|
|
|
|
|
2020-05-29 00:21:47 +02:00
|
|
|
public function refundPayment(array $data) :?Payment
|
|
|
|
{
|
|
|
|
return ((new RefundPayment($this->payment, $data)))->run();
|
|
|
|
}
|
|
|
|
|
2020-06-28 00:24:08 +02:00
|
|
|
public function deletePayment() :?Payment
|
|
|
|
{
|
2020-06-28 05:05:58 +02:00
|
|
|
return (new DeletePayment($this->payment))->run();
|
2020-06-28 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
public function updateInvoicePayment($payment_hash = null) :?Payment
|
2020-02-20 22:05:01 +01:00
|
|
|
{
|
2020-08-31 04:00:43 +02:00
|
|
|
return ((new UpdateInvoicePayment($this->payment, $payment_hash)))->run();
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
2020-08-12 00:17:32 +02:00
|
|
|
|
|
|
|
public function applyNumber()
|
|
|
|
{
|
|
|
|
$this->payment = (new ApplyNumber($this->payment))->run();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-08-13 04:30:45 +02:00
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
$this->payment->save();
|
|
|
|
|
|
|
|
return $this->payment->fresh();
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|