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.

195 line
5.2KB

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\API\SMBC\Payment\PollResultRecord;
  4. use App\Http\API\SMBC\Payment\SMBC;
  5. use App\Http\API\SMBC\Payment\PaymentStatus;
  6. use App\Kintone\Models\Customer;
  7. use App\Kintone\Models\DropDown\SmbcPayment\SmbcPaymentStatus;
  8. use App\Kintone\Models\SmbcPayment;
  9. use App\Models\SmbcPollStatus;
  10. use App\Util\DateUtil;
  11. use App\Util\DBUtil;
  12. use Exception;
  13. use Illuminate\Database\Eloquent\ModelNotFoundException;
  14. use Illuminate\Support\Carbon;
  15. use Illuminate\Support\Collection;
  16. class SMBCPaymentPoll extends BaseCommand
  17. {
  18. const COMMAND = "smbc:payment-poll";
  19. /**
  20. * The name and signature of the console command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = self::COMMAND;
  25. /**
  26. * The console command description.
  27. *
  28. * @var string
  29. */
  30. protected $description = 'SMBCへ支払結果を取得する';
  31. static public function getCommand()
  32. {
  33. return self::COMMAND;
  34. }
  35. /**
  36. * @var Collection<int, SmbcPayment>
  37. */
  38. private Collection $results;
  39. /**
  40. * Create a new command instance.
  41. *
  42. * @return void
  43. */
  44. public function __construct()
  45. {
  46. parent::__construct();
  47. $this->applications = collect();
  48. $this->results = collect();
  49. }
  50. /**
  51. * Execute the console command.
  52. *
  53. * @return int
  54. */
  55. public function service(): int
  56. {
  57. try {
  58. $db = DBUtil::instance();
  59. $db->beginTransaction();
  60. // 検索範囲の取得
  61. [$from, $to] = $this->getFromTo();
  62. $this->outputInfo(sprintf("検索範囲 %s-%s", $from->format('Y/m/d H:i:s'), $to->format('Y/m/d H:i:s')));
  63. // 検索実行
  64. $result = SMBC::poll($from, $to);
  65. // 検索結果の確認
  66. if (!$result->ok()) {
  67. $this->outputError($result->getMessage());
  68. return self::RESULTCODE_FAILED;
  69. }
  70. $this->outputInfo(sprintf("取得対象 %d件", $result->getCount()));
  71. // データハンドリング
  72. foreach ($result->getRecord() as $data) {
  73. $this->handleData($data);
  74. }
  75. // 検索実績の登録
  76. $this->saveFromTo($from, $to);
  77. // キントーンへ登録
  78. foreach ($this->results as $result) {
  79. $result->save();
  80. }
  81. $this->outputInfo(sprintf("登録件数:%d件", $this->results->count()));
  82. $db->commit();
  83. } catch (Exception $e) {
  84. $db->rollBack();
  85. throw $e;
  86. }
  87. return self::RESULTCODE_SUCCESS;
  88. }
  89. /**
  90. * @return Carbon[]
  91. */
  92. private function getFromTo()
  93. {
  94. $status = SmbcPollStatus::whereType(self::class)->first();
  95. $now = DateUtil::now();
  96. if ($status === null) {
  97. $this->outputInfo("検索範囲初期化");
  98. return [
  99. $now->clone()->addDays(-5),
  100. $now->clone(),
  101. ];
  102. }
  103. return [
  104. $status->condition_datetime_to->clone()->addSecond(),
  105. $now->clone(),
  106. ];
  107. }
  108. private function handleData(PollResultRecord $data)
  109. {
  110. try {
  111. $payment = SmbcPayment::getAccess()->first(
  112. SmbcPayment::getQuery()
  113. ->where(SmbcPayment::FIELD_ORDER_NO, $data->orderNo)
  114. );
  115. } catch (ModelNotFoundException $e) {
  116. $this->outputWarn(sprintf("存在しない支払予定 [%s]", $data->orderNo));
  117. throw $e;
  118. }
  119. // 完了ケース
  120. if ($data->paymentStatus === PaymentStatus::C0200_決済完了) {
  121. $payment->status = SmbcPaymentStatus::S003_決済結果反映済み;
  122. $payment->paymentAmount = $data->paymentAmount;
  123. $payment->paymentDate = $data->paymentDate;
  124. }
  125. // 未完了ケース
  126. else if (in_array(
  127. $data->paymentStatus,
  128. [
  129. PaymentStatus::C0011_請求取消,
  130. PaymentStatus::C0111_依頼取消,
  131. PaymentStatus::C0700_不明入金,
  132. PaymentStatus::C0800_不明通知,
  133. PaymentStatus::C0900_決済申込NG,
  134. ],
  135. true
  136. )) {
  137. $payment->status = SmbcPaymentStatus::S003_決済結果反映済み;
  138. }
  139. // 処理中ケース
  140. else {
  141. $payment->status = SmbcPaymentStatus::S002_決済結果待ち;
  142. }
  143. // 共通ケース
  144. $payment->acceptNo = $data->acceptNo;
  145. $payment->paymentStatus = $data->paymentStatusName;
  146. $payment->paymentStatusUpdateDatetime = $data->paymentStatusUpdateDatetime;
  147. $payment->paymentWarningStatus = $data->paymentWarningStatusName;
  148. $payment->paymentWarningStatusUpdateDatetime = $data->paymentWarningStatusUpdateDatetime;
  149. $payment->allResponse = $data->all;
  150. $this->results->push($payment);
  151. }
  152. private function saveFromTo(Carbon $from, Carbon $to)
  153. {
  154. $status = SmbcPollStatus::whereType(self::class)->firstOrNew();
  155. $status->type = self::class;
  156. $status->condition_datetime_to = $to;
  157. $status->save();
  158. }
  159. }