您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

158 行
5.1KB

  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. logger([
  44. "shop_id" => $this->sessionUser->shopId(),
  45. "駐車場管理コード" => $data->駐車場管理コード
  46. ]);
  47. throw new GeneralErrorMessageException("認証できない駐車場");
  48. }
  49. // 認証可能なサービス券一覧取得
  50. $setting = CertificationAvailableSetting::whereShopId($this->sessionUser->shopId())
  51. ->whereParkingManagementCode($data->駐車場管理コード)
  52. ->get();
  53. if ($setting->isEmpty()) {
  54. logger([
  55. "shop_id" => $this->sessionUser->shopId(),
  56. "駐車場管理コード" => $data->駐車場管理コード
  57. ]);
  58. throw new GeneralErrorMessageException("認証できるサービス券なし");
  59. }
  60. // 駐車場情報の取得
  61. $parking = Parking::whereParkCode($data->駐車場管理コード)
  62. ->first();
  63. if ($parking instanceof Parking === false) {
  64. logger([
  65. "shop_id" => $this->sessionUser->shopId(),
  66. "駐車場管理コード" => $data->駐車場管理コード
  67. ]);
  68. throw new GeneralErrorMessageException("存在しない駐車場");
  69. }
  70. // 認証済みチェック
  71. if (CertificationTicket::whereParkingManagementCode($data->駐車場管理コード)
  72. ->wherePublishingTerminalCode($data->発行端末)
  73. ->wherePublishingDate($data->発行日)
  74. ->wherePublishingNo($data->発行連番)
  75. ->exists()
  76. ) {
  77. throw new GeneralErrorMessageException("認証ずみのサービス券");
  78. }
  79. // サービス券一覧の取得
  80. $discountTicketCodes = $setting->pluck(CertificationAvailableSetting::COL_NAME_DISCOUNT_TICKET_CODE)->toArray();
  81. $discountTickets = DiscountTicket::getBuilder()
  82. ->where(DiscountTicket::COL_NAME_PARK_ID, $parking->id)
  83. ->whereIn(DiscountTicket::COL_NAME_DISCOUNT_TICKET_CODE, $discountTicketCodes)
  84. ->select([
  85. sprintf("%s as %s", DiscountTicket::COL_NAME_DISCOUNT_TICKET_CODE, ColumnName::DISCOUNT_TICKET_CODE),
  86. sprintf("%s as %s", DiscountTicket::COL_NAME_TICKET_NAME, "ticket_name"),
  87. ])->get();
  88. $res = [
  89. "parking" => [
  90. "parking_name" => $parking->park_name,
  91. "parking_management_code" => $parking->park_code,
  92. "publishing_terminal_code" => $data->発行端末,
  93. "publishing_date" => $data->発行日,
  94. "publishing_no" => $data->発行連番,
  95. ],
  96. "discount_tickets" => $discountTickets,
  97. ];
  98. return $this->successResponse($res);
  99. }
  100. }
  101. /**
  102. * 精算機で暗号化された文字列をパースする
  103. */
  104. class DataConverter
  105. {
  106. public string $発行端末;
  107. public Carbon $発行日;
  108. public int $発行連番;
  109. public string $顧客コード;
  110. public string $駐車場管理コード;
  111. public static function read(string $data): static
  112. {
  113. $ret = new static();
  114. $target = substr($data, 5);
  115. $dec = QRCryptoLogic::decrypt($target);
  116. if (strlen($dec) !== 25) {
  117. logger($target);
  118. logger($dec);
  119. throw new GeneralErrorMessageException("URL不正");
  120. }
  121. $ret->発行端末 = substr($dec, 0, 2);
  122. $ret->発行日 = Carbon::createFromFormat("Ymd", substr($dec, 2, 8));
  123. $ret->発行連番 = intval(substr($dec, 10, 6));
  124. $ret->顧客コード = substr($dec, 16, 4);
  125. $ret->駐車場管理コード = substr($dec, 20, 5);
  126. return $ret;
  127. }
  128. }