Skip to content

Commit

Permalink
Merge pull request #3365 from craftcms/feature/fix-subscription-payme…
Browse files Browse the repository at this point in the history
…nt-method

FIx subscribing with non stripe gateways
  • Loading branch information
lukeholder authored Jan 11, 2024
2 parents c73072a + 8522882 commit edc882c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit edc882c

Please sign in to comment.