Problem/Motivation

$payment_method_type can be null in StripePaymentElement::canCapturePayment

Proposed resolution

Turn

public function canCapturePayment(PaymentInterface $payment): bool {
  /** @var \Drupal\commerce_stripe\Plugin\Commerce\PaymentMethodType\StripePaymentMethodTypeInterface $payment_method_type */
  $payment_method_type = $payment->getPaymentMethod()?->getType();
  return $payment_method_type->canCapturePayment($payment);
}

into

public function canCapturePayment(PaymentInterface $payment): bool {
  $payment_method_type = $payment->getPaymentMethod()?->getType();
  if (!$payment_method_type instanceof StripePaymentMethodTypeInterface) {
    return FALSE;
  }

  return $payment_method_type->canCapturePayment($payment);
}
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

milanbombschliip created an issue. See original summary.

milanbombschliip’s picture

Assigned: milanbombschliip » Unassigned
Status: Active » Needs review
alanhdev’s picture

Looks like a duplicate of https://www.drupal.org/project/commerce_stripe/issues/3539260. Happy with either of these MRs as a fix.

milanbombschliip’s picture

anybody made their first commit to this issue’s fork.

anybody’s picture

Version: 2.1.0 » 2.x-dev

Thanks @alanhdev yes looks like a duplicate. I'd say the approach taken here is a bit cleaner, but maintainers should decide which way they prefer. Both fix the issue, but the other one more or less hides it...

rhovland’s picture

This one is the correct approach as it improves type safety of the code. The @var doc comment was probably suppressing a PHPStan error about this too.

anybody’s picture

Thanks @rhovland would you mind setting this RTBC then, if you tried?

mradcliffe’s picture

Status: Needs review » Reviewed & tested by the community

@unqunq mentioned that the MR resolved the issue.

I reviewed the code change, and this makes sense based on the return type so I will change the status to RTBC.