|
- <?php
-
- namespace App\Http\Controllers\Web\RobotPayment\CreditCard;
-
- use App\Exceptions\AppCommonException;
- use App\Http\Controllers\Web\WebController;
- use App\Kintone\Models\ChangePaymentMethodCreditcardOrderApplication;
- use App\Kintone\Models\Customer;
- use App\Kintone\Models\SeasonTicketContract;
- use App\Models\User;
- use App\Util\DateUtil;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Str;
-
- class RegisterTokenCheckController extends WebController
- {
-
- public function name(): string
- {
- return "クレジットカード 新規登録用トークンチェック";
- }
-
- public function description(): string
- {
- return "クレジットカード 新規登録用トークンチェック";
- }
-
- private User $user;
- private Customer $customer;
-
- public function __construct(protected RegisterTokenCheckParam $param)
- {
- parent::__construct();
- $this->middleware('auth:sanctum');
- }
-
- protected function run(Request $request): JsonResponse
- {
- $param = $this->param;
-
- $application = ChangePaymentMethodCreditcardOrderApplication::getAccess()->first(
- ChangePaymentMethodCreditcardOrderApplication::getQuery()
- ->where(ChangePaymentMethodCreditcardOrderApplication::FIELD_TOKEN, $param->token)
- );
-
- // トークン有効期限チェック
- $now = DateUtil::now();
- if ($application->tokenExpiresAt !== null && $application->tokenExpiresAt->lt($now)) {
- return $this->failedResponse();
- }
-
- $this->user = Auth::user();
- $this->customer = Customer::findByCustomerCode($this->user->kintone_customer_code);
-
- // 申請可能チェック
- if ($this->customer->canApplyToChangePaymentMethodCreditcard() === false) {
- return $this->failedResponse();
- }
-
- // 申請パラメーターの返却
- $ret = [
- "url" => "https://credit.j-payment.co.jp/link/creditcard",
- "shop_code" => "128522",
- 'customer_code' => $this->user->kintone_customer_code,
- 'token' => config('custom.creditcard.token'),
- 'payment_type' => "CREDITCARD",
- 'job_type' => "CAPTURE",
- 'tax' => "0",
- 'send_fee' => "0",
- 'amount' => "0",
- "interval" => "4", // 毎月
- "payment_day" => "27",
- "auto_amount" => $this->getAmount(),
- "target_date" => $this->getTargetDate(),
- "email" => $this->customer->email,
- 'phone_number' => $this->customer->phoneNumber,
- "order_no" => Str::uuid()->toString(),
- ];
-
- return $this->successResponse($ret);
- }
-
- private function getAmount(): string
- {
- // 全契約を集計する
- $targets = SeasonTicketContract::getAccess()->all(
- SeasonTicketContract::getQuery()
- ->where(SeasonTicketContract::FIELD_CUSTOMER_CODE, $this->user->kintone_customer_code)
- );
-
- $amount = 0;
- foreach ($targets as $target) {
- if ($target->isTerminated()) {
- continue;
- }
- $plan = $target->getPlan();
- if (!$plan->canPayByCreditcard()) {
- throw new AppCommonException("クレジット支払できない契約が存在");
- }
-
- $amount += intval($plan->amount ?? 0);
- }
-
- return strval($amount);
- }
-
-
- private function getTargetDate(): string
- {
- /**
- * 翌月の27日を指定する
- */
- $target = DateUtil::now()
- ->addMonth()
- ->setDay(27);
-
- return $target->format("Y/m/d");
- }
- }
|