1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Better refund handling

This commit is contained in:
Joshua Dwire 2016-05-14 23:03:39 -04:00
parent 65feee34e3
commit 919aec2192
3 changed files with 65 additions and 30 deletions

View File

@ -307,8 +307,8 @@ class ActivityListener
$this->activityRepo->create(
$payment,
ACTIVITY_TYPE_DELETE_PAYMENT,
$payment->amount,
$payment->amount * -1
$payment->amount - $payment->refunded,
($payment->amount - $payment->refunded) * -1
);
}
@ -343,8 +343,8 @@ class ActivityListener
$this->activityRepo->create(
$payment,
ACTIVITY_TYPE_FAILED_PAYMENT,
$payment->amount,
$payment->amount * -1
($payment->amount - $payment->refunded),
($payment->amount - $payment->refunded) * -1
);
}
@ -367,8 +367,8 @@ class ActivityListener
$this->activityRepo->create(
$payment,
ACTIVITY_TYPE_RESTORE_PAYMENT,
$event->fromDeleted ? $payment->amount * -1 : 0,
$event->fromDeleted ? $payment->amount : 0
$event->fromDeleted ? ($payment->amount - $payment->refunded) * -1 : 0,
$event->fromDeleted ? ($payment->amount - $payment->refunded) : 0
);
}
}

View File

@ -1048,41 +1048,76 @@ class PaymentService extends BaseService
if ($payment->payment_type_id != PAYMENT_TYPE_CREDIT) {
$gateway = $this->createGateway($accountGateway);
$refund = $gateway->refund(array(
'transactionReference' => $payment->transaction_reference,
'amount' => $amount,
));
$response = $refund->send();
if ($response->isSuccessful()) {
$payment->recordRefund($amount);
if ($accountGateway->gateway_id != GATEWAY_WEPAY) {
$refund = $gateway->refund(array(
'transactionReference' => $payment->transaction_reference,
'amount' => $amount,
));
$response = $refund->send();
if ($response->isSuccessful()) {
$payment->recordRefund($amount);
} else {
$data = $response->getData();
if ($data instanceof \Braintree\Result\Error) {
$error = $data->errors->deepAll()[0];
if ($error && $error->code == 91506) {
if ($amount == $payment->amount) {
// This is an unsettled transaction; try to void it
$void = $gateway->void(array(
'transactionReference' => $payment->transaction_reference,
));
$response = $void->send();
if ($response->isSuccessful()) {
$payment->markVoided();
}
} else {
$this->error('Unknown', 'Partial refund not allowed for unsettled transactions.', $accountGateway);
return false;
}
}
}
if (!$response->isSuccessful()) {
$this->error('Unknown', $response->getMessage(), $accountGateway);
return false;
}
}
} else {
$data = $response->getData();
$wepay = \Utils::setupWePay($accountGateway);
if ($data instanceof \Braintree\Result\Error) {
$error = $data->errors->deepAll()[0];
if ($error && $error->code == 91506) {
try {
$wepay->request('checkout/refund', array(
'checkout_id' => intval($payment->transaction_reference),
'refund_reason' => 'Refund issued by merchant.',
'amount' => $amount,
));
$payment->recordRefund($amount);
} catch (\WePayException $ex) {
if ($ex->getCode() == 4004) {
if ($amount == $payment->amount) {
// This is an unsettled transaction; try to void it
$void = $gateway->void(array(
'transactionReference' => $payment->transaction_reference,
));
$response = $void->send();
if ($response->isSuccessful()) {
try {
// This is an uncaptured transaction; try to cancel it
$wepay->request('checkout/cancel', array(
'checkout_id' => intval($payment->transaction_reference),
'cancel_reason' => 'Refund issued by merchant.',
));
$payment->markVoided();
} catch (\WePayException $ex) {
$this->error('Unknown', $ex->getMessage(), $accountGateway);
}
} else {
$this->error('Unknown', 'Partial refund not allowed for unsettled transactions.', $accountGateway);
return false;
}
} else {
$this->error('Unknown', $ex->getMessage(), $accountGateway);
}
}
if (!$response->isSuccessful()) {
$this->error('Unknown', $response->getMessage(), $accountGateway);
return false;
}
}
} else {
$payment->recordRefund($amount);

View File

@ -26,7 +26,7 @@
&nbsp;<label for="trashed" style="font-weight:normal; margin-left: 10px;">
<input id="trashed" type="checkbox" onclick="setTrashVisible()"
{{ Session::get("show_trash:{$entityType}") ? 'checked' : ''}}/>&nbsp; {{ trans('texts.show_archived_deleted')}} {{ Utils::transFlowText($entityType.'s') }}
</label>-onli
</label>
<div id="top_right_buttons" class="pull-right">
<input id="tableFilter" type="text" style="width:140px;margin-right:17px;background-color: white !important"