From 2daa2e1b993f461daa66d6c43f29714ec63a5818 Mon Sep 17 00:00:00 2001 From: JapSeyz Date: Tue, 14 Jun 2016 14:09:55 +0200 Subject: [PATCH] Allow Refunds of individual tickets Fixed use statement Fixed a spelling mistake --- .../Controllers/EventAttendeesController.php | 57 +++++++++++++++++++ ...37_add_is_refunded_column_to_attendees.php | 33 +++++++++++ .../Emails/notifyRefundedAttendee.blade.php | 18 ++++++ .../Modals/CancelAttendee.blade.php | 8 +++ .../ManageEvent/Modals/ManageOrder.blade.php | 5 ++ 5 files changed, 121 insertions(+) create mode 100644 database/migrations/2016_06_14_115337_add_is_refunded_column_to_attendees.php create mode 100644 resources/views/Emails/notifyRefundedAttendee.blade.php diff --git a/app/Http/Controllers/EventAttendeesController.php b/app/Http/Controllers/EventAttendeesController.php index 00a8c91a..b19e8fc7 100644 --- a/app/Http/Controllers/EventAttendeesController.php +++ b/app/Http/Controllers/EventAttendeesController.php @@ -17,6 +17,7 @@ use Auth; use DB; use Excel; use Mail; +use Omnipay\Omnipay; use Response; use Validator; use Config; @@ -716,6 +717,62 @@ class EventAttendeesController extends MyBaseController }); } + if($request->get('refund_attendee') == '1') { + + try { + // This does not account for an increased/decreased ticket price + // after the original purchase. + $refund_amount = $attendee->ticket->price; + $data['refund_amount'] = $refund_amount; + + $gateway = Omnipay::create($attendee->order->payment_gateway->name); + + // Only works for stripe + $gateway->initialize($attendee->order->account->getGateway($attendee->order->payment_gateway->id)->config); + + $request = $gateway->refund([ + 'transactionReference' => $attendee->order->transaction_id, + 'amount' => $refund_amount, + 'refundApplicationFee' => false, + ]); + + $response = $request->send(); + + if ($response->isSuccessful()) { + + // Update the attendee and their order + $attendee->is_refunded = 1; + $attendee->order->is_partially_refunded = 1; + $attendee->order->amount_refunded += $refund_amount; + + $attendee->order->save(); + $attendee->save(); + + // Let the user know that they have received a refund. + Mail::send('Emails.notifyRefundedAttendee', $data, function ($message) use ($attendee) { + $message->to($attendee->email, $attendee->full_name) + ->from(config('attendize.outgoing_email_noreply'), $attendee->event->organiser->name) + ->replyTo($attendee->event->organiser->email, $attendee->event->organiser->name) + ->subject('You have received a refund from ' . $attendee->event->organiser->name); + }); + } else { + $error_message = $response->getMessage(); + } + + } catch (\Exception $e) { + \Log::error($e); + $error_message = 'There has been a problem processing your refund. Please check your information and try again.'; + + } + } + + if ($error_message) { + return response()->json([ + 'status' => 'error', + 'message' => $error_message, + ]); + } + session()->flash('message', 'Successfully Cancelled Attenddee'); return response()->json([ diff --git a/database/migrations/2016_06_14_115337_add_is_refunded_column_to_attendees.php b/database/migrations/2016_06_14_115337_add_is_refunded_column_to_attendees.php new file mode 100644 index 00000000..209874b4 --- /dev/null +++ b/database/migrations/2016_06_14_115337_add_is_refunded_column_to_attendees.php @@ -0,0 +1,33 @@ +boolean('is_refunded')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('attendees', function($t) + { + $t->dropColumn('is_refunded'); + }); + } +} diff --git a/resources/views/Emails/notifyRefundedAttendee.blade.php b/resources/views/Emails/notifyRefundedAttendee.blade.php new file mode 100644 index 00000000..a7f13a5d --- /dev/null +++ b/resources/views/Emails/notifyRefundedAttendee.blade.php @@ -0,0 +1,18 @@ +@extends('Emails.Layouts.Master') + +@section('message_content') + +

Hi there,

+

+ You have received a refund on behalf of your cancelled ticket for {{{$attendee->event->title}}}. + {{{ $refund_amount }}} has been refunded to the original payee, you should see the payment in a few days. +

+ +

+ You can contact {{{ $attendee->event->organiser->name }}} directly at {{{$attendee->event->organiser->email}}} or by replying to this email should you require any more information. +

+@stop + +@section('footer') + +@stop \ No newline at end of file diff --git a/resources/views/ManageEvent/Modals/CancelAttendee.blade.php b/resources/views/ManageEvent/Modals/CancelAttendee.blade.php index a214ed6d..d240aaf9 100644 --- a/resources/views/ManageEvent/Modals/CancelAttendee.blade.php +++ b/resources/views/ManageEvent/Modals/CancelAttendee.blade.php @@ -24,6 +24,14 @@ + @if(config('attendize.default_payment_gateway') == config('attendize.payment_gateway_stripe')) +
+
+ + +
+
+ @endif