1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00

Merge pull request #3842 from beganovich/v2-2506-checkout-refunds

Checkout.com payments & refunds improvements
This commit is contained in:
David Bomba 2020-06-26 07:40:43 +10:00 committed by GitHub
commit 42f4d06d13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 22 deletions

View File

@ -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(),
];
}
}
}

View File

@ -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'];