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.

106 line
3.3KB

  1. <?php
  2. namespace App\Http\Controllers\Web\RobotPayment\CreditCard;
  3. use App\Http\Controllers\Web\WebController;
  4. use App\Kintone\Models\CreditcardAutoPaymentInfo;
  5. use App\Kintone\Models\CreditcardAutoPaymentResult;
  6. use App\Util\DateUtil;
  7. use Exception;
  8. use Illuminate\Database\Eloquent\ModelNotFoundException;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. use Illuminate\Support\Facades\Log;
  12. use LogicException;
  13. class PaymentResultController extends WebController
  14. {
  15. public function name(): string
  16. {
  17. return "クレジットカード 決済結果";
  18. }
  19. public function description(): string
  20. {
  21. return "クレジットカード 決済結果";
  22. }
  23. public function __construct(protected PaymentResultParam $param)
  24. {
  25. parent::__construct();
  26. }
  27. protected function run(Request $request): Response
  28. {
  29. $param = $this->param;
  30. logger("リクエスト受信 creditcard result", ["param" => $request->all()]);
  31. try {
  32. $info = CreditcardAutoPaymentInfo::getAccess()->first(
  33. CreditcardAutoPaymentInfo::getQuery()
  34. ->where(CreditcardAutoPaymentInfo::FIELD_AUTO_PAYMENT_NO, $param->acid)
  35. ->where(CreditcardAutoPaymentInfo::FIELD_SHOP_ORDER_NO, $param->cod)
  36. );
  37. } catch (ModelNotFoundException $e) {
  38. Log::error(sprintf(
  39. "自動課金情報が存在しないため、クレジットカードの決済結果保存不可 自動課金番号<%s> 店舗側オーダー番号<%s>",
  40. $param->acid,
  41. $param->cod
  42. ));
  43. throw $e;
  44. }
  45. $this->クレジット支払結果登録();
  46. return response()->view('robot-payment.creditcard.ok');
  47. }
  48. private function クレジット支払結果登録()
  49. {
  50. $param = $this->param;
  51. $result = new CreditcardAutoPaymentResult();
  52. $result->creditcardAutoPaymentNo = $param->acid;
  53. $result->paymentNo = $param->gid;
  54. $result->paymentDate = DateUtil::now();
  55. $result->paymentResult = $this->get決済結果($param->rst);
  56. $result->errorCode = $param->ec;
  57. $result->paymentAmount = $param->ta ?? 0;
  58. // 機密な情報をリクエストデータから除外して登録する
  59. $requestArr = request()->toArray();
  60. unset($requestArr["id"]);
  61. unset($requestArr["pa"]);
  62. $result->dataResult = json_encode(
  63. $requestArr,
  64. JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT
  65. );
  66. $result->save();
  67. }
  68. private function get決済結果(string $paymentResultCode): string
  69. {
  70. if ($paymentResultCode === "1") {
  71. return "決済成功";
  72. }
  73. if ($paymentResultCode === "2") {
  74. return "決済失敗";
  75. }
  76. if ($paymentResultCode === "3") {
  77. return "リトライ課金失敗";
  78. }
  79. if ($paymentResultCode === "4") {
  80. return "ユーザー様による課金停止";
  81. }
  82. if ($paymentResultCode === "5") {
  83. return "お試し期間中にリトライ課金課金2回失敗";
  84. }
  85. throw new Exception("不正な決済結果コード " . $paymentResultCode);
  86. }
  87. }