Manual.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 array $methodDetails = [
  13. 'key' => 'manual',
  14. ];
  15. public function purchase(Request $request): JsonResponse
  16. {
  17. $payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
  18. $url = route('manual.checkout', ['payment' => $payment->trade_no]);
  19. $payment->update(['url' => $url]);
  20. return response()->json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
  21. }
  22. public function redirectPage(string $trade_no): View
  23. {
  24. $payment = Payment::uid()->with(['order', 'order.goods'])->whereTradeNo($trade_no)->firstOrFail();
  25. $goods = $payment->order->goods;
  26. return view('user.components.payment.manual', [
  27. 'payment' => $payment,
  28. 'name' => $goods->name ?? trans('user.recharge_credit'),
  29. 'days' => $goods->days ?? 0,
  30. 'pay_type' => $payment->order->pay_type_label ?: 0,
  31. 'pay_type_icon' => $payment->order->pay_type_icon,
  32. ]);
  33. }
  34. public function inform(string $trade_no): JsonResponse
  35. {
  36. $payment = Payment::uid()->with(['order'])->whereTradeNo($trade_no)->firstOrFail();
  37. $payment->order->update(['status' => 1]);
  38. return response()->json(['status' => 'success', 'message' => trans('user.payment.order_creation.info')]);
  39. }
  40. public function notify(Request $request): View
  41. {
  42. $code = $request->input('sign');
  43. $status = $request->input('status');
  44. if (isset($status, $code)) {
  45. $payment_info = (new Hashids(config('app.key'), 8))->decode($code);
  46. if ($payment_info) {
  47. $payment = Payment::findOrFail($payment_info[0]);
  48. if ($payment && $payment->order && $payment->order->status === 1) {
  49. if ($status) {
  50. PaymentHelper::paymentReceived($payment->trade_no);
  51. } else {
  52. $payment->order->close();
  53. }
  54. }
  55. return view('components.payment.detail', ['order' => $payment->order->refresh(), 'user' => $payment->user->refresh()]);
  56. }
  57. }
  58. return view('auth.error', ['message' => 'No enough information']);
  59. }
  60. }