From b4d3498ac3ed08c5e68a8abbdc2800d7ef697e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 25 Jun 2020 17:08:15 +0200 Subject: [PATCH] Checkout.com payments & refunds improvements: - Fix refunding & exception handling - Fix capturing the payments with Checkout SDK - Update credit note balance after refunding --- .../CheckoutComPaymentDriver.php | 27 ++++++++++--- app/Services/Payment/RefundPayment.php | 38 ++++++++++--------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index 8cd1eb48cf..5273413963 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -107,7 +107,6 @@ class CheckoutComPaymentDriver extends BasePaymentDriver if ($request->has('token') && !is_null($request->token)) { $method = new IdSource($state['token']); $payment = new CheckoutPayment($method, $state['currency']); - $payment->capture = false; $payment->amount = $state['value']; } else { $method = new TokenSource($state['server_response']->cardToken); @@ -276,13 +275,31 @@ class CheckoutComPaymentDriver extends BasePaymentDriver public function refund(Payment $payment, $amount) { - $payment = new \Checkout\Models\Payments\Refund($payment->transaction_reference); - $payment->amount = $amount; + $this->init(); + + $checkout_payment = new \Checkout\Models\Payments\Refund($payment->transaction_reference); try { - $refund = $this->gateway->payments()->refund($payment); + $refund = $this->gateway->payments()->refund($checkout_payment); + $checkout_payment = $this->gateway->payments()->details($refund->id); + + $response = ['refund_response' => $refund, 'checkout_payment_fetch' => $checkout_payment]; + + return [ + 'transaction_reference' => $refund->action_id, + 'transaction_response' => json_encode($response), + 'success' => $checkout_payment->status == 'Refunded', + 'description' => $checkout_payment->status, + 'code' => $checkout_payment->http_code, + ]; } catch (CheckoutHttpException $e) { - // .. + return [ + 'transaction_reference' => null, + 'transaction_response' => json_encode($e->getMessage()), + 'success' => false, + 'description' => $e->getMessage(), + 'code' => $e->getCode(), + ]; } } } diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index fa4d4667c5..60dd10acb1 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -39,8 +39,6 @@ class RefundPayment $this->activity_repository = new ActivityRepository(); } - /** - */ public function run() { @@ -51,7 +49,6 @@ class RefundPayment ->updateCreditables() //return the credits first ->updatePaymentables() //update the paymentable items ->adjustInvoices() - ->createActivity() // create the refund activity ->processGatewayRefund() //process the gateway refund if needed ->save(); } @@ -70,19 +67,9 @@ class RefundPayment $this->payment->refunded = $this->total_refund; - $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. */ + $this + ->createActivity($gateway) + ->updateCreditNoteBalance(); } } else { $this->payment->refunded += $this->total_refund; @@ -91,7 +78,21 @@ class RefundPayment return $this; } - private function createActivity() + public function updateCreditNoteBalance() + { + $this->credit_note->balance -= $this->total_refund; + $this->credit_note->status_id = Credit::STATUS_APPLIED; + + $this->credit_note->balance === 0 + ? $this->credit_note->status_id = Credit::STATUS_APPLIED + : $this->credit_note->status_id = Credit::STATUS_PARTIAL; + + $this->credit_note->save(); + + return $this; + } + + private function createActivity($notes) { $fields = new \stdClass; $activity_repo = new ActivityRepository(); @@ -101,6 +102,7 @@ class RefundPayment $fields->company_id = $this->payment->company_id; $fields->activity_type_id = Activity::REFUNDED_PAYMENT; $fields->credit_id = $this->credit_note->id; + $fields->notes = json_encode($notes); if (isset($this->refund_data['invoices'])) { foreach ($this->refund_data['invoices'] as $invoice) { @@ -264,6 +266,8 @@ class RefundPayment $invoice->service()->setStatus(Invoice::STATUS_PARTIAL); } + $invoice->save(); + $client = $invoice->client; $adjustment_amount += $refunded_invoice['amount'];