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

Payment to manual invoice causes webhook error #317

Closed
mikejpeters opened this issue Jan 15, 2025 · 5 comments
Closed

Payment to manual invoice causes webhook error #317

mikejpeters opened this issue Jan 15, 2025 · 5 comments

Comments

@mikejpeters
Copy link

mikejpeters commented Jan 15, 2025

Description

If an invoice is created manually using Stripe dashboard and a customer pays that invoice, the invoice.payment_succeeded event is sent with a null subscription, e.g.:

{
  "object": {
    "id": "in_123abc",
    "object": "invoice",
    "billing_reason": "manual",
    "paid": true,
    "subscription": null
  }
}

In this case, this plugin attempts to find a subscription using a null reference, which errors with Stripe\Exception\InvalidArgumentException The resource ID cannot be null or whitespace.

$subscriptionReference = $stripeInvoice['subscription'];
$counter = 0;
$limit = 5;
do {
// Handle cases when Stripe sends us a webhook so soon that we haven't processed the subscription that triggered the webhook
sleep(1);
$subscription = Subscription::find()->status(null)->reference($subscriptionReference)->one();

I think it should ignore the event when the subscription property is null, because it's not applicable to Craft CMS.

(I think the existing behavior doesn't cause any serious problems in Craft, but having errors thrown for improperly handled situations makes it more difficult to determine when there are important errors that require more urgent attention.)

Steps to reproduce

  1. Manually invoice (using Stripe Dashboard) and pay

Additional info

  • Craft CMS version: 4.13.9
  • Stripe for Craft Commerce version: 4.1.5.1
  • PHP version: 8.0.30
  • Database driver & version:
  • Plugins & versions:
Copy link

linear bot commented Jan 15, 2025

@lukeholder
Copy link
Member

Thanks for reporting this was fixed in version 5.0.4.2

https://github.com/craftcms/commerce-stripe/blob/5.x/CHANGELOG.md#5042---2024-08-12

@mikejpeters
Copy link
Author

Thanks for the response, unfortunately this particular issue is still not resolved. I am on version 5.0.4.3 now and I tested by setting up webhooks, and then manually creating and paying an invoice in Stripe, and this error was logged:

[Stripe\Exception\InvalidArgumentException] Stripe\Exception\InvalidArgumentException: The resource ID cannot be null or whitespace. in /var/www/html/vendor/stripe/stripe-php/lib/Service/AbstractService.php:105
Stack trace:
#0 /var/www/html/vendor/stripe/stripe-php/lib/Service/SubscriptionService.php(140): Stripe\Service\AbstractService->buildPath('/v1/subscriptio...', NULL)
#1 /var/www/html/vendor/craftcms/commerce-stripe/src/base/SubscriptionGateway.php(932): Stripe\Service\SubscriptionService->retrieve(NULL)
#2 /var/www/html/vendor/craftcms/commerce-stripe/src/base/SubscriptionGateway.php(494): craft\commerce\stripe\base\SubscriptionGateway->handleInvoiceSucceededEvent(Array)
#3 /var/www/html/vendor/craftcms/commerce-stripe/src/base/Gateway.php(332): craft\commerce\stripe\base\SubscriptionGateway->handleWebhook(Array)
#4 /var/www/html/vendor/craftcms/commerce/src/services/Webhooks.php(97): craft\commerce\stripe\base\Gateway->processWebHook()
#5 /var/www/html/vendor/craftcms/commerce/src/controllers/WebhooksController.php(55): craft\commerce\services\Webhooks->processWebhook(Object(craft\commerce\stripe\gateways\PaymentIntents))
#6 [internal function]: craft\commerce\controllers\WebhooksController->actionProcessWebhook('3')
#7 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#8 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#9 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('process-webhook', Array)
#10 /var/www/html/vendor/craftcms/cms/src/web/Application.php(361): yii\base\Module->runAction('commerce/webhoo...', Array)
#11 /var/www/html/vendor/craftcms/cms/src/web/Application.php(660): craft\web\Application->runAction('commerce/webhoo...', Array)
#12 /var/www/html/vendor/craftcms/cms/src/web/Application.php(323): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#13 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#14 /var/www/html/web/index.php(12): yii\base\Application->run()

I believe the issue is in assuming that the invoice subscription property will always be non-null, which isn't the case for manual invoices:

$subscriptionReference = $stripeInvoice['subscription'];

@lukeholder
Copy link
Member

lukeholder commented Feb 3, 2025

Thanks for the explainer. I have fixed for the next release.

To get the fix early, change your craftcms/commerce requirement in composer.json to:

"require": {
  "craftcms/commerce-stripe": "5.x-dev#bbfde14dc1b74010e5d6917ecd6d223f3400161a as 5.0.4.4",
  "...": "..."
}

Then run composer update.

We will update this ticket once the release is out.

@lukeholder lukeholder self-assigned this Feb 3, 2025
@nfourtythree
Copy link
Contributor

Stripe for Commerce version 5.0.5 has now been released with this fix included.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants