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.

74 lines
1.9KB

  1. <?php
  2. namespace App\Http\Controllers\Web\QRService;
  3. use App\Http\Controllers\Web\WebController;
  4. use App\Logics\QRService\CreateLogic;
  5. use App\Logics\QRService\QRCryptoLogic;
  6. use App\Models\HtpmsCustomer\QRService\AcquisitionTicket;
  7. use App\Models\HtpmsCustomer\QRService\AcquisitionTicketToken;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Http\Request;
  10. class CreateTicketController extends WebController
  11. {
  12. public function name(): string
  13. {
  14. return "サービス券取得";
  15. }
  16. public function description(): string
  17. {
  18. return "サービス券を取得する";
  19. }
  20. public function __construct(protected CreateTicketParam $param)
  21. {
  22. parent::__construct();
  23. }
  24. protected function run(Request $request): JsonResponse
  25. {
  26. $param = $this->param;
  27. $token = AcquisitionTicketToken::whereToken($param->token)->first();
  28. if ($token === null) {
  29. return $this->failedResponse();
  30. }
  31. if ($param->ticketId) {
  32. $ticket = AcquisitionTicket::find($param->ticketId);
  33. if ($ticket === null || !$ticket->canUse()) {
  34. $ticket = CreateLogic::create($param->token);
  35. }
  36. } else {
  37. $ticket = CreateLogic::create($param->token);
  38. }
  39. $res = [
  40. 'ticket_id' => $ticket->id,
  41. 'data' => $this->convertToQrStr($ticket),
  42. ];
  43. return $this->successResponse($res);
  44. }
  45. private function convertToQrStr(AcquisitionTicket $ticket): string
  46. {
  47. $body = sprintf(
  48. "%02d%s%06d%02d%02d",
  49. "01",
  50. $ticket->publishing_date->format('Ymd'),
  51. $ticket->publishing_no,
  52. $ticket->shop_no,
  53. $ticket->discount_ticket_code,
  54. );
  55. return sprintf(
  56. "HT004%s",
  57. QRCryptoLogic::encrypt($body)
  58. );
  59. }
  60. }