craftcms / commerce-stripe

Stripe payment gateway for Craft Commerce
https://plugins.craftcms.com/commerce-stripe
MIT License
30 stars 51 forks source link

`Attempt to read property "id" on null` error when subscribing to a plan #307

Open tommysvr opened 1 month ago

tommysvr commented 1 month ago

Description

Getting Attempt to read property "id" on null / No payment sources are saved to use for subscriptions as back-to-back errors when subscribing to a plan. I've tested with a fresh Craft/Commerce install using the example templates.

2024-07-29 18:00:46 [web.ERROR] [craft\commerce\stripe\errors\PaymentSourceException] craft\commerce\stripe\errors\PaymentSourceException: No payment sources are saved to use for subscriptions. in /var/www/html/vendor/craftcms/commerce-stripe/src/gateways/PaymentIntents.php:400
Stack trace:
#0 /var/www/html/vendor/craftcms/commerce/src/services/Subscriptions.php(515): craft\commerce\stripe\gateways\PaymentIntents->subscribe(Object(craft\elements\User), Object(craft\commerce\stripe\models\Plan), Object(craft\commerce\stripe\models\forms\Subscription))
#1 /var/www/html/vendor/craftcms/commerce/src/controllers/SubscriptionsController.php(229): craft\commerce\services\Subscriptions->createSubscription(Object(craft\elements\User), Object(craft\commerce\stripe\models\Plan), Object(craft\commerce\stripe\models\forms\Subscription), Array)
#2 [internal function]: craft\commerce\controllers\SubscriptionsController->actionSubscribe()
#3 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#4 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#5 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('subscribe', Array)
#6 /var/www/html/packages/cms/src/web/Application.php(349): yii\base\Module->runAction('commerce/subscr...', Array)
#7 /var/www/html/packages/cms/src/web/Application.php(650): craft\web\Application->runAction('commerce/subscr...', Array)
#8 /var/www/html/packages/cms/src/web/Application.php(311): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#9 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#10 /var/www/html/web/index.php(12): yii\base\Application->run()
#11 {main} {"memory":1611016,"exception":"[object] (craft\\commerce\\stripe\\errors\\PaymentSourceException(code: 0): No payment sources are saved to use for subscriptions. at /var/www/html/vendor/craftcms/commerce-stripe/src/gateways/PaymentIntents.php:400)"} 
2024-07-29 18:00:46 [web.ERROR] [yii\base\ErrorException:2] yii\base\ErrorException: Attempt to read property "id" on null in /var/www/html/vendor/craftcms/commerce/src/controllers/SubscriptionsController.php:241
Stack trace:
#0 /var/www/html/packages/cms/src/web/ErrorHandler.php(79): yii\base\ErrorHandler->handleError(2, 'Attempt to read...', '/var/www/html/v...', 241)
#1 /var/www/html/vendor/craftcms/commerce/src/controllers/SubscriptionsController.php(241): craft\web\ErrorHandler->handleError(2, 'Attempt to read...', '/var/www/html/v...', 241)
#2 [internal function]: craft\commerce\controllers\SubscriptionsController->actionSubscribe()
#3 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#4 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#5 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('subscribe', Array)
#6 /var/www/html/packages/cms/src/web/Application.php(349): yii\base\Module->runAction('commerce/subscr...', Array)
#7 /var/www/html/packages/cms/src/web/Application.php(650): craft\web\Application->runAction('commerce/subscr...', Array)
#8 /var/www/html/packages/cms/src/web/Application.php(311): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#9 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#10 /var/www/html/web/index.php(12): yii\base\Application->run()
#11 {main} {"memory":1432968,"exception":"[object] (yii\\base\\ErrorException(code: 2): Attempt to read property \"id\" on null at /var/www/html/vendor/craftcms/commerce/src/controllers/SubscriptionsController.php:241)"} 

Steps to reproduce

  1. Using the example templates, go to My Account -> My Stored Cards
  2. Under 'Add a Card', select Stripe and add payment details
  3. Go to Plans -> Subscribe to a plan
  4. Attempt to read property "id" on null

Note: it works as expected when adding a card via the 'Manage on Stripe' link.

https://github.com/user-attachments/assets/075ea759-3566-40a4-ad1a-6274315a023f

Additional info

linear[bot] commented 1 month ago

PT-2014 `Attempt to read property "id" on null` error when subscribing to a plan