領収証発行サービス
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.

98 lines
2.9KB

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