Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed new subscription creation with a non Stripe gateway #3365

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Craft Commerce now requires Craft CMS 4.6.0 or later.
- Fixed a bug where calling `Carts::forgetCart()` wouldn’t completely clear the cart. ([#3353](https://github.com/craftcms/commerce/issues/3353))
- Fixed a bug where the Edit Order page could become locked when editing an adjustment. ([#3351](https://github.com/craftcms/commerce/issues/3351))
- Fixed a bug that prevented the creation of a non Stripe subscription. ([#3365](https://github.com/craftcms/commerce/pull/3365))

## 4.3.3 - 2023-12-14

Expand Down
18 changes: 15 additions & 3 deletions src/controllers/SubscriptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use craft\commerce\helpers\PaymentForm;
use craft\commerce\Plugin;
use craft\commerce\Plugin as Commerce;
use craft\commerce\stripe\gateways\PaymentIntents;
use craft\commerce\web\assets\commercecp\CommerceCpAsset;
use craft\helpers\App;
use craft\helpers\StringHelper;
Expand Down Expand Up @@ -195,15 +196,26 @@ public function actionSubscribe(): ?Response
try {
// We provide backward compatibility for stripe plugin 3.0
$paymentFormData = $this->request->getBodyParam(PaymentForm::getPaymentFormParamName($gateway->handle)) ?? [];
if (isset($paymentFormData['paymentMethodId'])) {
// throw deprecation error
Craft::$app->getDeprecator()->log('SubscriptionController::create-newPaymentMethod', 'The subscription subscription create action now requires that a default payment source is set up before subscribing.');

$createPaymentSource = function($gateway, $paymentFormData) use ($plugin) {
$paymentForm = $gateway->getPaymentFormModel();
$paymentForm->setAttributes($paymentFormData, false);

if ($paymentForm->validate()) {
$plugin->getPaymentSources()->createPaymentSource(Craft::$app->getUser()->getId(), $gateway, $paymentForm);
}
};

$exists = class_exists(PaymentIntents::class);
/** @phpstan-ignore-next-line */
if ($exists && $plan->getGateway() instanceof PaymentIntents) {
Craft::$app->getDeprecator()->log('SubscriptionController::create-newPaymentMethod', 'The subscription create action now requires that a customer’s default payment source is set up before subscribing with Stripe.');
// Only be backward compatible with Stripe if they supply the payment method ID.
if (isset($paymentFormData['paymentMethodId'])) {
$createPaymentSource($gateway, $paymentFormData);
}
} else {
$createPaymentSource($gateway, $paymentFormData);
}

$fieldsLocation = $this->request->getParam('fieldsLocation', 'fields');
Expand Down
Loading