Created
July 23, 2020 18:55
-
-
Save paperscissors/e9199af49f334c2112acef7ee7e007a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Complete the order, send notifications and invoice, if applicable | |
* @param Request $request | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public function complete(Request $request) | |
{ | |
$user = $request->user(); | |
$order = Order::find($request->order_id); | |
$total = $order->calculated_total; | |
// if we're not sending an invoice, we can go ahead and charge them | |
if ($request->sendInvoice === 'no') { | |
try { | |
$payment = $user->charge($total*100, $request->payment); | |
} catch (\Exception $e) { | |
return response()->json([ | |
'message' => 'There was an error while trying to process your payment.'], $e->getCode()); | |
} | |
} | |
// update the order status and metadata. 1 means paid, 2 means invoiced. | |
$order->update([ | |
'order_total'=>$total, | |
'completed' => $request->sendInvoice === 'no' ? 1 : 2, | |
'metadata' => json_encode([ | |
'referer' => request()->server('HTTP_REFERER'), | |
'browser' => request()->server('HTTP_USER_AGENT'), | |
]), | |
'ip_address' => request()->ip, | |
]); | |
// if the customer added a note to their order, create it here. | |
if ($request->notes) { | |
$order->notes()->create([ | |
'name' => 'Order note from customer', | |
'body' => $request->notes, | |
'is_customer' => true | |
]); | |
} | |
//TODO: if the user requested to be invoiced, this is probably a good place to do it. | |
// send the user and admin an email notification about their order. | |
$user->notify(new OrderCompleted($order)); // send to client | |
Notification::route('mail', config('mail.from.address')) | |
->notify(new OrderCompletedAdmin($order->id)); | |
return response()->json(['order completed']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment