diff --git a/src/Eccube/Form/Type/Shopping/OrderType.php b/src/Eccube/Form/Type/Shopping/OrderType.php index 7409d9bd2b9..51944be4451 100644 --- a/src/Eccube/Form/Type/Shopping/OrderType.php +++ b/src/Eccube/Form/Type/Shopping/OrderType.php @@ -165,6 +165,19 @@ public function buildForm(FormBuilderInterface $builder, array $options) $charge = $Order->getPayment() ? $Order->getPayment()->getCharge() : 0; $Payments = $this->filterPayments($Payments, $Order->getPaymentTotal() - $charge); + // フォームから送信された支払方法が選択肢に存在するかどうか + // 選択肢に存在しなければ、選択肢の中で最初の支払方法を選択状態にする + $Payment = null; + if ($data['Payment']) { + $Payment = $this->paymentRepository->find($data['Payment']); + } + $Payment = !is_null($Payment) && in_array($Payment, $Payments, true) ? + $Payment : (current($Payments) ?: null); + if ( !is_null($Payment)) { + $data['Payment'] = (string)$Payment->getId(); + $event->setData($data); + } + $form = $event->getForm(); $this->addPaymentForm($form, $Payments); }); diff --git a/src/Eccube/Resource/locale/messages.en.yaml b/src/Eccube/Resource/locale/messages.en.yaml index 01fa5bca2ce..07cf6118480 100644 --- a/src/Eccube/Resource/locale/messages.en.yaml +++ b/src/Eccube/Resource/locale/messages.en.yaml @@ -1861,6 +1861,7 @@ purchase_flow.tax_rate_update: Tax Rate has been updated. Please confirm the tot purchase_flow.over_customer_point: You are not able to use points more than your current points. purchase_flow.over_payment_total: The points are more than the total amount. purchase_flow.over_stock: "%name% does not have enough stock." +purchase_flow.payment_method_changed: "Payment method has been changed to %name% due to change in delivery method." #------------------------------------------------------------------------------------ # Command diff --git a/src/Eccube/Resource/locale/messages.ja.yaml b/src/Eccube/Resource/locale/messages.ja.yaml index 9cf4b99c635..3c28e98838f 100644 --- a/src/Eccube/Resource/locale/messages.ja.yaml +++ b/src/Eccube/Resource/locale/messages.ja.yaml @@ -1862,6 +1862,7 @@ purchase_flow.tax_rate_update: 税率が更新されました。金額をご確 purchase_flow.over_customer_point: 利用ポイントが所有ポイントを上回っています。 purchase_flow.over_payment_total: 利用ポイントがお支払い金額を上回っています。 purchase_flow.over_stock: "「%name%」の在庫が足りません。" +purchase_flow.payment_method_changed: "配送方法の変更により支払方法が「%name%」に変更されました。" #------------------------------------------------------------------------------------ # Command diff --git a/src/Eccube/Service/PurchaseFlow/Processor/PaymentChangePostValidator.php b/src/Eccube/Service/PurchaseFlow/Processor/PaymentChangePostValidator.php new file mode 100644 index 00000000000..d97a197ef85 --- /dev/null +++ b/src/Eccube/Service/PurchaseFlow/Processor/PaymentChangePostValidator.php @@ -0,0 +1,63 @@ +requestStack = $requestStack; + } + + /** + * @param ItemHolderInterface $itemHolder + * @param PurchaseContext $context + */ + public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) + { + /* @var Order $Order */ + $Order = $itemHolder; + $request = $this->requestStack->getCurrentRequest(); + $requestData = $request->request->all(); + + // 配送方法の変更によって選択していた支払方法が使用できなくなった場合、OrderTypeで支払方法が変更されている + if (isset($requestData['_shopping_order']['Payment'])) { + + if (!is_null($Order->getPayment()) && $Order->getPayment()->getId() != $requestData['_shopping_order']['Payment']) { + if ($Order->getPayment()) { + $this->throwInvalidItemException(trans('purchase_flow.payment_method_changed', ['%name%' => $Order->getPayment()->getMethod()]), null, true); + } + } + } + } +} diff --git a/tests/Eccube/Tests/Web/ShoppingControllerTest.php b/tests/Eccube/Tests/Web/ShoppingControllerTest.php index c229becbfd3..82ca4b378e4 100644 --- a/tests/Eccube/Tests/Web/ShoppingControllerTest.php +++ b/tests/Eccube/Tests/Web/ShoppingControllerTest.php @@ -396,8 +396,10 @@ public function testPaymentWithError() ]); $this->assertTrue($this->client->getResponse()->isSuccessful()); - $this->expected = 'お支払い方法を選択してください。'; - $this->actual = $crawler->filter('p.ec-errorMessage')->text(); + + // 先頭の支払方法(郵便振替)が選択されていることを確認 + $this->expected = '1'; + $this->actual = $crawler->filter('input[name="_shopping_order[Payment]"]:checked')->attr('value'); $this->verify(); }