PaymentController.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http\Controllers\Guest;
  3. use App\Models\Order;
  4. use App\Services\OrderService;
  5. use App\Services\PaymentService;
  6. use App\Services\TelegramService;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. class PaymentController extends Controller
  10. {
  11. public function notify($method, $uuid, Request $request)
  12. {
  13. try {
  14. $paymentService = new PaymentService($method, null, $uuid);
  15. $verify = $paymentService->notify($request->input());
  16. if (!$verify) abort(500, 'verify error');
  17. if (!$this->handle($verify['trade_no'], $verify['callback_no'])) {
  18. abort(500, 'handle error');
  19. }
  20. die(isset($paymentService->customResult) ? $paymentService->customResult : 'success');
  21. } catch (\Exception $e) {
  22. abort(500, 'fail');
  23. }
  24. }
  25. private function handle($tradeNo, $callbackNo)
  26. {
  27. $order = Order::where('trade_no', $tradeNo)->first();
  28. if (!$order) {
  29. abort(500, 'order is not found');
  30. }
  31. if ($order->status === 1) return true;
  32. $orderService = new OrderService($order);
  33. if (!$orderService->paid($callbackNo)) {
  34. return false;
  35. }
  36. $telegramService = new TelegramService();
  37. $message = sprintf(
  38. "💰成功收款%s元\n———————————————\n订单号:%s",
  39. $order->total_amount / 100,
  40. $order->trade_no
  41. );
  42. $telegramService->sendMessageWithAdmin($message);
  43. return true;
  44. }
  45. }