|
- <?php
-
- namespace App\Console\Commands;
-
- use App\Http\API\SMBC\Payment\PollResultRecord;
- use App\Http\API\SMBC\Payment\SMBC;
- use App\Http\API\SMBC\Payment\PaymentStatus;
- use App\Kintone\Models\Customer;
- use App\Kintone\Models\DropDown\SmbcPayment\SmbcPaymentStatus;
- use App\Kintone\Models\SmbcPayment;
- use App\Models\SmbcPollStatus;
- use App\Util\DateUtil;
- use App\Util\DBUtil;
- use Exception;
- use Illuminate\Database\Eloquent\ModelNotFoundException;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Collection;
-
- class SMBCPaymentPoll extends BaseCommand
- {
-
- const COMMAND = "smbc:payment-poll";
-
-
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = self::COMMAND;
-
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'SMBCへ支払結果を取得する';
-
- static public function getCommand()
- {
- return self::COMMAND;
- }
-
-
- /**
- * @var Collection<int, SmbcPayment>
- */
- private Collection $results;
-
-
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- $this->applications = collect();
- $this->results = collect();
- }
-
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function service(): int
- {
- try {
- $db = DBUtil::instance();
- $db->beginTransaction();
-
- // 検索範囲の取得
- [$from, $to] = $this->getFromTo();
- $this->outputInfo(sprintf("検索範囲 %s-%s", $from->format('Y/m/d H:i:s'), $to->format('Y/m/d H:i:s')));
-
- // 検索実行
- $result = SMBC::poll($from, $to);
-
- // 検索結果の確認
- if (!$result->ok()) {
- $this->outputError($result->getMessage());
- return self::RESULTCODE_FAILED;
- }
- $this->outputInfo(sprintf("取得対象 %d件", $result->getCount()));
-
- // データハンドリング
- foreach ($result->getRecord() as $data) {
- $this->handleData($data);
- }
-
- // 検索実績の登録
- $this->saveFromTo($from, $to);
-
- // キントーンへ登録
- foreach ($this->results as $result) {
- $result->save();
- }
- $this->outputInfo(sprintf("登録件数:%d件", $this->results->count()));
-
- $db->commit();
- } catch (Exception $e) {
- $db->rollBack();
- throw $e;
- }
-
- return self::RESULTCODE_SUCCESS;
- }
-
- /**
- * @return Carbon[]
- */
- private function getFromTo()
- {
-
- $status = SmbcPollStatus::whereType(self::class)->first();
- $now = DateUtil::now();
- if ($status === null) {
- $this->outputInfo("検索範囲初期化");
- return [
- $now->clone()->addDays(-5),
- $now->clone(),
- ];
- }
-
- return [
- $status->condition_datetime_to->clone()->addSecond(),
- $now->clone(),
- ];
- }
-
- private function handleData(PollResultRecord $data)
- {
- try {
- $payment = SmbcPayment::getAccess()->first(
- SmbcPayment::getQuery()
- ->where(SmbcPayment::FIELD_ORDER_NO, $data->orderNo)
- );
- } catch (ModelNotFoundException $e) {
- $this->outputWarn(sprintf("存在しない支払予定 [%s]", $data->orderNo));
- throw $e;
- }
-
-
- // 完了ケース
- if ($data->paymentStatus === PaymentStatus::C0200_決済完了) {
- $payment->status = SmbcPaymentStatus::S003_決済結果反映済み;
-
- $payment->paymentAmount = $data->paymentAmount;
- $payment->paymentDate = $data->paymentDate;
- }
-
- // 未完了ケース
- else if (in_array(
- $data->paymentStatus,
- [
- PaymentStatus::C0011_請求取消,
- PaymentStatus::C0111_依頼取消,
- PaymentStatus::C0700_不明入金,
- PaymentStatus::C0800_不明通知,
- PaymentStatus::C0900_決済申込NG,
- ],
- true
- )) {
- $payment->status = SmbcPaymentStatus::S003_決済結果反映済み;
- }
-
- // 処理中ケース
- else {
- $payment->status = SmbcPaymentStatus::S002_決済結果待ち;
- }
-
- // 共通ケース
-
- $payment->acceptNo = $data->acceptNo;
- $payment->paymentStatus = $data->paymentStatusName;
- $payment->paymentStatusUpdateDatetime = $data->paymentStatusUpdateDatetime;
- $payment->paymentWarningStatus = $data->paymentWarningStatusName;
- $payment->paymentWarningStatusUpdateDatetime = $data->paymentWarningStatusUpdateDatetime;
- $payment->allResponse = $data->all;
-
-
- $this->results->push($payment);
- }
-
- private function saveFromTo(Carbon $from, Carbon $to)
- {
- $status = SmbcPollStatus::whereType(self::class)->firstOrNew();
- $status->type = self::class;
- $status->condition_datetime_to = $to;
- $status->save();
- }
- }
|