From 24a3cc36fc320f610856b852f33653f354859ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 24 Jun 2020 17:18:03 +0200 Subject: [PATCH] Record activity and update payment value after refunding --- app/Services/Payment/RefundPayment.php | 82 ++++++++++++++------------ database/seeds/RandomDataSeeder.php | 24 ++++---- 2 files changed, 55 insertions(+), 51 deletions(-) diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index 822eaadbfb5c..dc271e38b05b 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -24,6 +24,8 @@ class RefundPayment private $gateway_refund_status; + private $activity_repository; + public function __construct($payment, $refund_data) { $this->payment = $payment; @@ -33,6 +35,8 @@ class RefundPayment $this->total_refund = 0; $this->gateway_refund_status = false; + + $this->activity_repository = new ActivityRepository(); } /** @@ -41,40 +45,50 @@ class RefundPayment { return $this->calculateTotalRefund() //sets amount for the refund (needed if we are refunding multiple invoices in one payment) - ->setStatus() //sets status of payment - ->buildCreditNote() //generate the credit note - ->buildCreditLineItems() //generate the credit note items - ->updateCreditables() //return the credits first - ->updatePaymentables() //update the paymentable items - ->adjustInvoices() - ->createActivity() // create the refund activity - ->processGatewayRefund() //process the gateway refund if needed - ->save(); + ->setStatus() //sets status of payment + ->buildCreditNote() //generate the credit note + ->buildCreditLineItems() //generate the credit note items + ->updateCreditables() //return the credits first + ->updatePaymentables() //update the paymentable items + ->adjustInvoices() + ->createActivity() // create the refund activity + ->processGatewayRefund() //process the gateway refund if needed + ->save(); } private function processGatewayRefund() { + // if ($this->refund_data['gateway_refund'] !== false && $this->total_refund > 0) { + if (true) { - if ($this->refund_data['gateway_refund'] !== false && $this->total_refund > 0) { - - $gateway = CompanyGateway::find($this->company_gateway_id); + $gateway = CompanyGateway::first(); if ($gateway) { $response = $gateway->driver($this->payment->client)->refund($this->payment, $this->total_refund); - if (!$response) { + if ($response['success']) { throw new PaymentRefundFailed(); } - //todo - //need to check the gateway response has successfully be transacted. + $this->payment->refunded = $this->total_refund; - //if a credit has been generated I think this is the correct section to fix the balance of the credit + $activity = [ + 'payment_id' => $this->payment->id, + 'user_id' => $this->payment->user->id, + 'company_id' => $this->payment->company->id, + 'activity_type_id' => Activity::REFUNDED_PAYMENT, + 'credit_id' => 1, // ??? + 'notes' => $response, + ]; + + /** Persist activiy to database. */ + // $this->activity_repository->save($activity, ??); + + /** Substract credit amount from the refunded value. */ } - } - else + } else { $this->payment->refunded += $this->total_refund; - + } return $this; } @@ -105,7 +119,7 @@ class RefundPayment private function calculateTotalRefund() { - if(isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) + if (isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) $this->total_refund = collect($this->refund_data['invoices'])->sum('amount'); else $this->total_refund = $this->refund_data['amount']; @@ -145,10 +159,8 @@ class RefundPayment { $ledger_string = ''; - if(isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) - { - foreach ($this->refund_data['invoices'] as $invoice) - { + if (isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) { + foreach ($this->refund_data['invoices'] as $invoice) { $inv = Invoice::find($invoice['invoice_id']); @@ -164,9 +176,7 @@ class RefundPayment $line_items[] = $credit_line_item; } - } - else - { + } else { $credit_line_item = InvoiceItemFactory::create(); $credit_line_item->quantity = 1; @@ -177,7 +187,7 @@ class RefundPayment $credit_line_item->date = $this->refund_data['date']; $line_items = []; - $line_items[] = $credit_line_item; + $line_items[] = $credit_line_item; } $this->credit_note->line_items = $line_items; @@ -188,23 +198,19 @@ class RefundPayment private function updatePaymentables() { - if(isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) - { + if (isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) { $this->payment->invoices->each(function ($paymentable_invoice) { - collect($this->refund_data['invoices'])->each(function ($refunded_invoice) use($paymentable_invoice){ + collect($this->refund_data['invoices'])->each(function ($refunded_invoice) use ($paymentable_invoice) { - if($refunded_invoice['invoice_id'] == $paymentable_invoice->id) - { + if ($refunded_invoice['invoice_id'] == $paymentable_invoice->id) { $paymentable_invoice->pivot->refunded += $refunded_invoice['amount']; $paymentable_invoice->pivot->save(); } - }); - }); } - + return $this; } @@ -248,8 +254,7 @@ class RefundPayment { $adjustment_amount = 0; - if(isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) - { + if (isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) { foreach ($this->refund_data['invoices'] as $refunded_invoice) { $invoice = Invoice::find($refunded_invoice['invoice_id']); @@ -277,7 +282,6 @@ class RefundPayment $this->payment->client->paid_to_date -= $this->refund_data['amount']; $this->payment->client->save(); - } return $this; diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index c4eed11060dc..e9cbebcb7d96 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -289,18 +289,18 @@ class RandomDataSeeder extends Seeder // $cg->save(); // } - // if (config('ninja.testvars.paypal')) { - // $cg = new CompanyGateway; - // $cg->company_id = $company->id; - // $cg->user_id = $user->id; - // $cg->gateway_key = '38f2c48af60c7dd69e04248cbb24c36e'; - // $cg->require_cvv = true; - // $cg->show_billing_address = true; - // $cg->show_shipping_address = true; - // $cg->update_details = true; - // $cg->config = encrypt(config('ninja.testvars.paypal')); - // $cg->save(); - // } + if (config('ninja.testvars.paypal')) { + $cg = new CompanyGateway; + $cg->company_id = $company->id; + $cg->user_id = $user->id; + $cg->gateway_key = '38f2c48af60c7dd69e04248cbb24c36e'; + $cg->require_cvv = true; + $cg->show_billing_address = true; + $cg->show_shipping_address = true; + $cg->update_details = true; + $cg->config = encrypt(config('ninja.testvars.paypal')); + $cg->save(); + } // if(config('ninja.testvars.checkout')) { // $cg = new CompanyGateway;