You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.6KB

  1. <?php
  2. namespace App\Http\Controllers\Web\QRService\Certification;
  3. use App\Codes\Model\QRServiceUsage;
  4. use App\Exceptions\GeneralErrorMessageException;
  5. use App\Http\Controllers\Web\WebController;
  6. use App\Logics\QRService\QRCryptoLogic;
  7. use App\Models\ColumnName;
  8. use App\Models\HtpmsCustomer\Existing\DiscountTicket;
  9. use App\Models\HtpmsCustomer\Existing\Parking;
  10. use App\Models\HtpmsCustomer\Mst\ShopNoRelation;
  11. use App\Models\HtpmsCustomer\QRService\CertificationAvailableSetting;
  12. use App\Models\HtpmsCustomer\QRService\CertificationTicket;
  13. use Illuminate\Http\JsonResponse;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Carbon;
  16. class CheckDataFormatController extends WebController
  17. {
  18. public function name(): string
  19. {
  20. return "データチェック";
  21. }
  22. public function description(): string
  23. {
  24. return "暗号化文字列をチェック認証に必要な情報を取得する";
  25. }
  26. public function __construct(protected CheckDataFormatParam $param)
  27. {
  28. parent::__construct();
  29. }
  30. protected function run(Request $request): JsonResponse
  31. {
  32. $param = $this->param;
  33. // 暗号化文字列の解読
  34. $data = DataConverter::read($param->data);
  35. // 対象の駐車場か判定
  36. $relation = ShopNoRelation::byKey(
  37. $this->sessionUser->shopId(),
  38. $data->駐車場管理コード,
  39. QRServiceUsage::認証方式
  40. )
  41. ->first();
  42. if ($relation === null) {
  43. throw new GeneralErrorMessageException("認証できない駐車場");
  44. }
  45. // 認証可能なサービス券一覧取得
  46. $setting = CertificationAvailableSetting::whereShopId($this->sessionUser->shopId())
  47. ->whereParkingManagementCode($data->駐車場管理コード)
  48. ->get();
  49. if ($setting->isEmpty()) {
  50. throw new GeneralErrorMessageException("認証できるサービス券なし");
  51. }
  52. // 駐車場情報の取得
  53. $parking = Parking::whereParkCode($data->駐車場管理コード)
  54. ->first();
  55. if ($parking instanceof Parking === false) {
  56. throw new GeneralErrorMessageException("存在しない駐車場");
  57. }
  58. // 認証済みチェック
  59. if (CertificationTicket::whereParkingManagementCode($data->駐車場管理コード)
  60. ->wherePublishingTerminalCode($data->発行端末)
  61. ->wherePublishingDate($data->発行日)
  62. ->wherePublishingNo($data->発行連番)
  63. ->exists()
  64. ) {
  65. throw new GeneralErrorMessageException("認証ずみのサービス券");
  66. }
  67. // サービス券一覧の取得
  68. $discountTicketCodes = $setting->pluck(CertificationAvailableSetting::COL_NAME_DISCOUNT_TICKET_CODE)->toArray();
  69. $discountTickets = DiscountTicket::whereParkId($parking->id)
  70. ->whereIn(DiscountTicket::COL_NAME_DISCOUNT_TICKET_CODE, $discountTicketCodes)
  71. ->select([
  72. sprintf("%s as %s", DiscountTicket::COL_NAME_DISCOUNT_TICKET_CODE, ColumnName::DISCOUNT_TICKET_CODE),
  73. sprintf("%s as %s", DiscountTicket::COL_NAME_TICKET_NAME, "ticket_name"),
  74. ])->get();
  75. $res = [
  76. "parking" => [
  77. "parking_name" => $parking->park_name,
  78. "parking_management_code" => $parking->park_code,
  79. "publishing_terminal_code" => $data->発行端末,
  80. "publishing_date" => $data->発行日,
  81. "publishing_no" => $data->発行連番,
  82. ],
  83. "discount_tickets" => $discountTickets,
  84. ];
  85. return $this->successResponse($res);
  86. }
  87. }
  88. /**
  89. * 精算機で暗号化された文字列をパースする
  90. */
  91. class DataConverter
  92. {
  93. public string $発行端末;
  94. public Carbon $発行日;
  95. public int $発行連番;
  96. public string $顧客コード;
  97. public string $駐車場管理コード;
  98. public static function read(string $data): static
  99. {
  100. $ret = new static();
  101. $target = substr($data, 5);
  102. $dec = QRCryptoLogic::decrypt($target);
  103. if (strlen($dec) !== 25) {
  104. logger($target);
  105. logger($dec);
  106. throw new GeneralErrorMessageException("URL不正");
  107. }
  108. $ret->発行端末 = substr($dec, 0, 2);
  109. $ret->発行日 = Carbon::createFromFormat("Ymd", substr($dec, 2, 8));
  110. $ret->発行連番 = intval(substr($dec, 10, 6));
  111. $ret->顧客コード = substr($dec, 16, 4);
  112. $ret->駐車場管理コード = substr($dec, 20, 5);
  113. return $ret;
  114. }
  115. }