Optionally send email when refunding a payment #1570

This commit is contained in:
Hillel Coren 2017-07-23 23:35:21 +03:00
parent c83ba8133a
commit 2b92f41f26
7 changed files with 77 additions and 21 deletions

View File

@ -236,7 +236,6 @@ class PaymentController extends BaseController
public function bulk()
{
$action = Input::get('action');
$amount = Input::get('refund_amount');
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
if ($action === 'email') {
@ -244,7 +243,10 @@ class PaymentController extends BaseController
$this->contactMailer->sendPaymentConfirmation($payment);
Session::flash('message', trans('texts.emailed_payment'));
} else {
$count = $this->paymentService->bulk($ids, $action, ['refund_amount' => $amount]);
$count = $this->paymentService->bulk($ids, $action, [
'refund_amount' => Input::get('refund_amount'),
'refund_email' => Input::get('refund_email'),
]);
if ($count > 0) {
$message = Utils::pluralize($action == 'refund' ? 'refunded_payment' : $action.'d_payment', $count);
Session::flash('message', $message);

View File

@ -231,17 +231,25 @@ class ContactMailer extends Mailer
/**
* @param Payment $payment
*/
public function sendPaymentConfirmation(Payment $payment)
public function sendPaymentConfirmation(Payment $payment, $refunded = 0)
{
$account = $payment->account;
$client = $payment->client;
$account->loadLocalizationSettings($client);
$invoice = $payment->invoice;
$accountName = $account->getDisplayName();
$emailTemplate = $account->getEmailTemplate(ENTITY_PAYMENT);
$emailSubject = $invoice->account->getEmailSubject(ENTITY_PAYMENT);
if ($refunded > 0) {
$emailSubject = trans('texts.refund_subject');
$emailTemplate = trans('texts.refund_body', [
'amount' => $account->formatMoney($refunded, $client),
'invoice_number' => $invoice->invoice_number,
]);
} else {
$emailSubject = $invoice->account->getEmailSubject(ENTITY_PAYMENT);
$emailTemplate = $account->getEmailTemplate(ENTITY_PAYMENT);
}
if ($payment->invitation) {
$user = $payment->invitation->user;
@ -270,9 +278,10 @@ class ContactMailer extends Mailer
'entityType' => ENTITY_INVOICE,
'bccEmail' => $account->getBccEmail(),
'fromEmail' => $account->getFromEmail(),
'isRefund' => $refunded > 0,
];
if ($account->attachPDF()) {
if (! $refunded && $account->attachPDF()) {
$data['pdfString'] = $invoice->getPDFString();
$data['pdfFileName'] = $invoice->getFileName();
}

View File

@ -180,17 +180,28 @@ class PaymentService extends BaseService
foreach ($payments as $payment) {
if (Auth::user()->can('edit', $payment)) {
$amount = ! empty($params['refund_amount']) ? floatval($params['refund_amount']) : null;
$sendEmail = ! empty($params['refund_email']) ? boolval($params['refund_email']) : false;
$paymentDriver = false;
$refunded = false;
if ($accountGateway = $payment->account_gateway) {
$paymentDriver = $accountGateway->paymentDriver();
}
if ($paymentDriver && $paymentDriver->canRefundPayments) {
if ($paymentDriver->refundPayment($payment, $amount)) {
$successful++;
$refunded = true;
}
} else {
$payment->recordRefund($amount);
$successful++;
$refunded = true;
}
if ($refunded && $sendEmail) {
$mailer = app('App\Ninja\Mailers\ContactMailer');
$mailer->sendPaymentConfirmation($payment, $amount);
}
}
}

View File

@ -2317,6 +2317,9 @@ $LANG = array(
'downloaded_quotes' => 'An email will be sent with the quote PDFs',
'clone_expense' => 'Clone Expense',
'default_documents' => 'Default Documents',
'send_email_to_client' => 'Send email to the client',
'refund_subject' => 'Refund Processed',
'refund_body' => 'You have been processed a refund of :amount for invoice :invoice_number.',
);

View File

@ -40,12 +40,14 @@
</td>
<td style="border-collapse: collapse; vertical-align: middle;" valign="middle">
<p class="right" style="line-height: 14px; margin: 0; padding: 0;">
<span style="font-size: 15px; color: #231F20;">
{{ trans('texts.' . $invoice->present()->balanceDueLabel) }}:
</span><br />
<span class="total" style="font-size: 26px; display: block;margin-top: 5px;">
{{ $account->formatMoney($invoice->getRequestedAmount(), $client) }}
</span>
@if (! isset($isRefund) || ! $isRefund)
<span style="font-size: 15px; color: #231F20;">
{{ trans('texts.' . $invoice->present()->balanceDueLabel) }}:
</span><br />
<span class="total" style="font-size: 26px; display: block;margin-top: 5px;">
{{ $account->formatMoney($invoice->getRequestedAmount(), $client) }}
</span>
@endif
</p>
</td>
</tr>

View File

@ -40,12 +40,14 @@
</td>
<td style="border-collapse: collapse; vertical-align: middle; line-height: 16px;" valign="middle">
<p style="margin: 0; padding: 0;">
<span style="font-size: 12px; color: #8f8d8e;">
{{ strtoupper(trans('texts.' . $invoice->present()->balanceDueLabel)) }}:
</span><br />
<span class="total" style="font-size: 27px; color: #FFFFFF; margin-top: 5px;display: block;">
{{ $account->formatMoney($invoice->getRequestedAmount(), $client) }}
</span>
@if (! isset($isRefund) || ! $isRefund)
<span style="font-size: 12px; color: #8f8d8e;">
{{ strtoupper(trans('texts.' . $invoice->present()->balanceDueLabel)) }}:
</span><br />
<span class="total" style="font-size: 27px; color: #FFFFFF; margin-top: 5px;display: block;">
{{ $account->formatMoney($invoice->getRequestedAmount(), $client) }}
</span>
@endif
</p>
</td>
</tr>

View File

@ -20,6 +20,17 @@
<div class="help-block">{{ trans('texts.refund_max') }} <span id="refundMax"></span></div>
</div>
</div>
<div class="form-group">
<label class="col-sm-offset-2 col-sm-2 control-label"></label>
<div class="col-sm-6">
<div class="input-group">
{!! Former::checkbox('refund_email')
->text('send_email_to_client')
->raw() !!}
</div>
</div>
</div>
</div>
</div>
</div>
@ -45,12 +56,28 @@
$('#paymentRefundModal').modal('show');
}
function handleRefundClicked(){
function onRefundClicked(){
$('#completeRefundButton').prop('disabled', true);
submitForm_payment('refund', paymentId);
}
function onRefundEmailChange() {
if (! isStorageSupported()) {
return;
}
var checked = $('#refund_email').is(':checked');
localStorage.setItem('last:send_refund_email', checked ? true : '');
}
$(function() {
$('#completeRefundButton').click(handleRefundClicked);
$('#completeRefundButton').click(onRefundClicked);
$('#refund_email').click(onRefundEmailChange);
if (isStorageSupported()) {
if (localStorage.getItem('last:send_refund_email')) {
$('#refund_email').prop('checked', true);
}
}
})
</script>