"card_type" not being set is causing fatal error in CreditCard::buildLabel() in commerce_payment

Created on 22 June 2023, about 1 year ago

Problem/Motivation

Creating a payment with commerce_decoupled_checkout with the gateway set to a gateway of type Commerce Decoupled Stripe results in a fatal error:

Argument 1 passed to Drupal\commerce_payment\CreditCard::getType() must be of the type string, null given

Steps to reproduce

  1. Install commerce_decoupled_checkout and commerce_decoupled_stripe and configure them following the steps specified on the module pages.
  2. POST /commerce/payment/create/{order_id}with the following payload:

    {
        "gateway": "decoupled_stripe",  // The gateway machine name.
        "type": "credit_card",
        "capture": false
    }
  3. You'll get the error.

Changing the payload to

{
    "gateway": "decoupled_stripe",  // The gateway machine name.
    "type": "credit_card",
    "capture": false,
    "details": {
        "card_type": "visa"
    }
}

does not help as the $payment_details parameter of StripeGatewayBase::createPaymentMethod() is being ignored in the current implementation.

Error trace: TypeError: Drupal\commerce_payment\CreditCard::getType(): Argument #1 ($id) must be of type string, null given, called in /var/www/html/web/modules/contrib/commerce/modules/payment/src/Plugin/Commerce/PaymentMethodType/CreditCard.php on line 23 in Drupal\commerce_payment\CreditCard::getType() (line 28 of modules/contrib/commerce/modules/payment/src/CreditCard.php).
Drupal\commerce_payment\Plugin\Commerce\PaymentMethodType\CreditCard->buildLabel(Object) (Line: 81)
Drupal\commerce_payment\Entity\PaymentMethod->label() (Line: 75)
Drupal\commerce_log\EventSubscriber\PaymentEventSubscriber->onPaymentInsert(Object, 'commerce_payment.commerce_payment.insert', Object)
call_user_func(Array, Object, 'commerce_payment.commerce_payment.insert', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'commerce_payment.commerce_payment.insert') (Line: 60)
Drupal\commerce\CommerceContentEntityStorage->invokeHook('insert', Object) (Line: 564)
Drupal\Core\Entity\EntityStorageBase->doPostSave(Object, ) (Line: 781)
Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object, ) (Line: 489)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 804)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 339)
Drupal\Core\Entity\EntityBase->save() (Line: 100)
Drupal\commerce_decoupled_stripe\Plugin\Commerce\PaymentGateway\StripeGateway->createPayment(Object, ) (Line: 163)
Drupal\commerce_decoupled_checkout\Plugin\rest\resource\PaymentCreateResource->post('28', Array)
call_user_func_array(Array, Array) (Line: 219)
Drupal\rest\RequestHandler->delegateToRestResourcePlugin(Object, Object, Array, Object) (Line: 68)
Drupal\rest\RequestHandler->handle(Object, Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 580)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 163)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 74)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 686)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Proposed resolution

Check for card_type in $payment_details parameter of StripeGatewayBase::createPaymentMethod(). If not available default to 'visa'. This value is needed for Drupal\commerce_payment\CreditCard::getType() to work, but will be overridden when the payment is captured.

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024