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.

122 line
3.7KB

  1. <?php
  2. namespace App\Http\Controllers\Web\RobotPayment\CreditCard;
  3. use App\Exceptions\AppCommonException;
  4. use App\Http\Controllers\Web\WebController;
  5. use App\Kintone\Models\ChangePaymentMethodCreditcardOrderApplication;
  6. use App\Kintone\Models\Customer;
  7. use App\Kintone\Models\SeasonTicketContract;
  8. use App\Models\User;
  9. use App\Util\DateUtil;
  10. use Illuminate\Http\JsonResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Str;
  14. class RegisterTokenCheckController extends WebController
  15. {
  16. public function name(): string
  17. {
  18. return "クレジットカード 新規登録用トークンチェック";
  19. }
  20. public function description(): string
  21. {
  22. return "クレジットカード 新規登録用トークンチェック";
  23. }
  24. private User $user;
  25. private Customer $customer;
  26. public function __construct(protected RegisterTokenCheckParam $param)
  27. {
  28. parent::__construct();
  29. $this->middleware('auth:sanctum');
  30. }
  31. protected function run(Request $request): JsonResponse
  32. {
  33. $param = $this->param;
  34. $application = ChangePaymentMethodCreditcardOrderApplication::getAccess()->first(
  35. ChangePaymentMethodCreditcardOrderApplication::getQuery()
  36. ->where(ChangePaymentMethodCreditcardOrderApplication::FIELD_TOKEN, $param->token)
  37. );
  38. // トークン有効期限チェック
  39. $now = DateUtil::now();
  40. if ($application->tokenExpiresAt !== null && $application->tokenExpiresAt->lt($now)) {
  41. return $this->failedResponse();
  42. }
  43. $this->user = Auth::user();
  44. $this->customer = Customer::findByCustomerCode($this->user->kintone_customer_code);
  45. // 申請可能チェック
  46. if ($this->customer->canApplyToChangePaymentMethodCreditcard() === false) {
  47. return $this->failedResponse();
  48. }
  49. // 申請パラメーターの返却
  50. $ret = [
  51. "url" => "https://credit.j-payment.co.jp/link/creditcard",
  52. "shop_code" => "128522",
  53. 'customer_code' => $this->user->kintone_customer_code,
  54. 'token' => config('custom.creditcard.token'),
  55. 'payment_type' => "CREDITCARD",
  56. 'job_type' => "CAPTURE",
  57. 'tax' => "0",
  58. 'send_fee' => "0",
  59. 'amount' => "0",
  60. "interval" => "4", // 毎月
  61. "payment_day" => "27",
  62. "auto_amount" => $this->getAmount(),
  63. "target_date" => $this->getTargetDate(),
  64. "email" => $this->customer->email,
  65. 'phone_number' => $this->customer->phoneNumber,
  66. "order_no" => Str::uuid()->toString(),
  67. ];
  68. return $this->successResponse($ret);
  69. }
  70. private function getAmount(): string
  71. {
  72. // 全契約を集計する
  73. $targets = SeasonTicketContract::getAccess()->all(
  74. SeasonTicketContract::getQuery()
  75. ->where(SeasonTicketContract::FIELD_CUSTOMER_CODE, $this->user->kintone_customer_code)
  76. );
  77. $amount = 0;
  78. foreach ($targets as $target) {
  79. if ($target->isTerminated()) {
  80. continue;
  81. }
  82. $plan = $target->getPlan();
  83. if (!$plan->canPayByCreditcard()) {
  84. throw new AppCommonException("クレジット支払できない契約が存在");
  85. }
  86. $amount += intval($plan->amount ?? 0);
  87. }
  88. return strval($amount);
  89. }
  90. private function getTargetDate(): string
  91. {
  92. /**
  93. * 翌月の27日を指定する
  94. */
  95. $target = DateUtil::now()
  96. ->addMonth()
  97. ->setDay(27);
  98. return $target->format("Y/m/d");
  99. }
  100. }