領収証発行サービス
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

87 行
2.7KB

  1. <?php
  2. namespace App\Util\Custom\HelloTechno;
  3. use App\Exceptions\AppCommonException;
  4. use Illuminate\Http\Client\Response;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\Log;
  7. class API
  8. {
  9. private const RESULT_CODE = 'result_code';
  10. private const RESULT_CODE_SUCCESS = 'SUCCESS';
  11. private const FIELD_DATA = 'data';
  12. private const URL_CUSTOMERS = 'customers';
  13. private const URL_PARKINGS = 'parkings';
  14. private const URL_ADJUST_DATA = 'adjust-data';
  15. private const CONFIG_KEY_API_HOST = "logic.custom.hellotechno.host";
  16. public static function getCustomers(?string $customerCde = null)
  17. {
  18. $res = Http::get(static::getCustomersUrl($customerCde));
  19. return static::handleResponse($res);
  20. }
  21. public static function getParkings(string $customerCode, ?string $parkingManagementCode = null)
  22. {
  23. $res = Http::get(static::getParkingsUrl($customerCode, $parkingManagementCode));
  24. return static::handleResponse($res);
  25. }
  26. public static function getAdjustData(string $customerCode, string $parkingManagementCode, int $adjustSeqNo)
  27. {
  28. $res = Http::get(static::getAdjustDataUrl($customerCode, $parkingManagementCode, $adjustSeqNo));
  29. return static::handleResponse($res);
  30. }
  31. private static function getHost(): string
  32. {
  33. return config(static::CONFIG_KEY_API_HOST, "http://localhost");
  34. }
  35. private static function getCustomersUrl(?string $customerCode)
  36. {
  37. $condition = [static::getHost(), static::URL_CUSTOMERS];
  38. if ($customerCode) {
  39. $condition[] = $customerCode;
  40. }
  41. return implode('/', $condition);
  42. }
  43. private static function getParkingsUrl(string $customerCode, ?string $parkingManagementCode)
  44. {
  45. $condition = [static::getHost(), static::URL_PARKINGS, $customerCode];
  46. if ($parkingManagementCode) {
  47. $condition[] = $parkingManagementCode;
  48. }
  49. return implode('/', $condition);
  50. }
  51. private static function getAdjustDataUrl(string $customerCode, string $parkingManagementCode, int $adjustSeqNo)
  52. {
  53. $condition = [static::getHost(), static::URL_ADJUST_DATA, $customerCode, $parkingManagementCode, $adjustSeqNo];
  54. return implode('/', $condition);
  55. }
  56. private static function handleResponse(Response $res): array
  57. {
  58. if ($res->failed()) {
  59. throw $res->throw();
  60. }
  61. $data = $res->json();
  62. if (data_get($data, static::RESULT_CODE) !== static::RESULT_CODE_SUCCESS) {
  63. Log::error("HT RESPONSE ERROR");
  64. Log::error($data);
  65. throw new AppCommonException('HT API 失敗');
  66. }
  67. return data_get($data, static::FIELD_DATA, []);
  68. }
  69. }