From d2dce6cbcb96108d621bade362add9851d2d828b Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 19 Dec 2023 11:20:08 +0000 Subject: [PATCH 1/2] Fixed JS files being served from an incorrect path --- CHANGELOG.md | 2 ++ src/gateways/PaymentIntents.php | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0970d75..6e83dcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ # Release Notes for Stripe for Craft Commerce ## Unreleased + - Payment method data is now stored in expanded form within transaction response data. ([#276](https://github.com/craftcms/commerce-stripe/pull/276)) +- Fixed a bug where JavaScript files were being served incorrectly. ([#270](https://github.com/craftcms/commerce-stripe/issues/270)) ## 4.0.1.1 - 2023-10-25 diff --git a/src/gateways/PaymentIntents.php b/src/gateways/PaymentIntents.php index 05eb047..ca9fd2c 100644 --- a/src/gateways/PaymentIntents.php +++ b/src/gateways/PaymentIntents.php @@ -165,17 +165,25 @@ public function getPaymentFormHtml(array $params): ?string $view = Craft::$app->getView(); $previousMode = $view->getTemplateMode(); - $view->setTemplateMode(View::TEMPLATE_MODE_CP); + if (Craft::$app->getRequest()->isCpRequest) { + $view->setTemplateMode(View::TEMPLATE_MODE_CP); + } $view->registerScript('', View::POS_END, ['src' => 'https://js.stripe.com/v3/']); // we need this to load at end of body - if ($params['paymentFormType'] == self::PAYMENT_FORM_TYPE_CHECKOUT) { - $html = $view->renderTemplate('commerce-stripe/paymentForms/checkoutForm', $params); - } else { + $templatePath = ($params['paymentFormType'] == self::PAYMENT_FORM_TYPE_CHECKOUT) + ? 'commerce-stripe/paymentForms/checkoutForm' + : 'commerce-stripe/paymentForms/elementsForm'; + + if ($params['paymentFormType'] == self::PAYMENT_FORM_TYPE_ELEMENTS) { $view->registerAssetBundle(ElementsFormAsset::class); - $html = $view->renderTemplate('commerce-stripe/paymentForms/elementsForm', $params); } + // Template mode needs to be CP for the payment form to work + $view->setTemplateMode(View::TEMPLATE_MODE_CP); + + $html = $view->renderTemplate($templatePath, $params); + $view->setTemplateMode($previousMode); return $html; From 3e16e8554963b400f115b804b401437316a4978a Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 19 Dec 2023 11:21:39 +0000 Subject: [PATCH 2/2] Reorder code to read a little more logically --- src/gateways/PaymentIntents.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/gateways/PaymentIntents.php b/src/gateways/PaymentIntents.php index ca9fd2c..4f113d8 100644 --- a/src/gateways/PaymentIntents.php +++ b/src/gateways/PaymentIntents.php @@ -171,10 +171,6 @@ public function getPaymentFormHtml(array $params): ?string $view->registerScript('', View::POS_END, ['src' => 'https://js.stripe.com/v3/']); // we need this to load at end of body - $templatePath = ($params['paymentFormType'] == self::PAYMENT_FORM_TYPE_CHECKOUT) - ? 'commerce-stripe/paymentForms/checkoutForm' - : 'commerce-stripe/paymentForms/elementsForm'; - if ($params['paymentFormType'] == self::PAYMENT_FORM_TYPE_ELEMENTS) { $view->registerAssetBundle(ElementsFormAsset::class); } @@ -182,6 +178,10 @@ public function getPaymentFormHtml(array $params): ?string // Template mode needs to be CP for the payment form to work $view->setTemplateMode(View::TEMPLATE_MODE_CP); + $templatePath = ($params['paymentFormType'] == self::PAYMENT_FORM_TYPE_CHECKOUT) + ? 'commerce-stripe/paymentForms/checkoutForm' + : 'commerce-stripe/paymentForms/elementsForm'; + $html = $view->renderTemplate($templatePath, $params); $view->setTemplateMode($previousMode); @@ -227,7 +227,7 @@ public function completePurchase(Transaction $transaction): RequestResponseInter $paymentIntentOptions = [ 'expand' => ['payment_method'], ]; - + if ($data['object'] == 'payment_intent') { $paymentIntent = $this->getStripeClient()->paymentIntents->retrieve($data['id'], $paymentIntentOptions); } else {