Manual.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Utils\Payments;
  3. use App\Models\Payment;
  4. use App\Utils\Library\PaymentHelper;
  5. use App\Utils\Library\Templates\Gateway;
  6. use Hashids\Hashids;
  7. use Illuminate\Contracts\View\View;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Http\Request;
  10. class Manual implements Gateway
  11. {
  12. public static function metadata(): array
  13. {
  14. return [
  15. 'key' => 'manual',
  16. ];
  17. }
  18. public function purchase(Request $request): JsonResponse
  19. {
  20. $payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
  21. $url = route('manual.checkout', ['payment' => $payment->trade_no]);
  22. $payment->update(['url' => $url]);
  23. return response()->json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
  24. }
  25. public function redirectPage(string $trade_no): View
  26. {
  27. $payment = Payment::uid()->with(['order', 'order.goods'])->whereTradeNo($trade_no)->firstOrFail();
  28. $goods = $payment->order->goods;
  29. return view('user.components.payment.manual', [
  30. 'payment' => $payment,
  31. 'name' => $goods->name ?? trans('user.recharge_credit'),
  32. 'days' => $goods->days ?? 0,
  33. 'pay_type' => $payment->order->pay_type_label ?: 0,
  34. 'pay_type_icon' => $payment->order->pay_type_icon,
  35. ]);
  36. }
  37. public function inform(string $trade_no): JsonResponse
  38. {
  39. $payment = Payment::uid()->with(['order'])->whereTradeNo($trade_no)->firstOrFail();
  40. $payment->order->update(['status' => 1]);
  41. return response()->json(['status' => 'success', 'message' => trans('user.payment.order_creation.info')]);
  42. }
  43. public function notify(Request $request): View
  44. {
  45. $code = $request->input('sign');
  46. $status = $request->input('status');
  47. if (isset($status, $code)) {
  48. $payment_info = (new Hashids(config('app.key'), 8))->decode($code);
  49. if ($payment_info) {
  50. $payment = Payment::findOrFail($payment_info[0]);
  51. if ($payment && $payment->order && $payment->order->status === 1) {
  52. if ($status) {
  53. PaymentHelper::paymentReceived($payment->trade_no);
  54. } else {
  55. $payment->order->close();
  56. }
  57. }
  58. return view('components.payment.detail', ['order' => $payment->order->refresh(), 'user' => $payment->user->refresh()]);
  59. }
  60. }
  61. return view('auth.error', ['message' => 'No enough information']);
  62. }
  63. }