Allow customising payment intent attributes and metadata

Created on 31 December 2021, about 3 years ago
Updated 20 March 2023, almost 2 years ago

Problem/Motivation

Custom and contrib modules cannot customise the payment intent.
https://stripe.com/docs/api/payment_intents/create

Steps to reproduce

Proposed resolution

Add new Event when create PaymentIntent object.

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

🇵🇱Poland Krzysztof Domański Poland

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom jonathanshaw Stroud, UK
    +++ b/src/Plugin/Commerce/PaymentGateway/Stripe.php
    @@ -472,6 +473,16 @@ class Stripe extends OnsitePaymentGatewayBase implements StripeInterface {
    +    $intent_attributes = $event->getIntentAttributes();
    +    $intent_array = NestedArray::mergeDeep($intent_array, $intent_attributes);
    

    I think we can simply do $intent_array = $event->getIntentAttributes() here.

    And then in PaymentIntentEvent let's add an addIntentAttributes() method that uses NestedArray::mergeDeep() to merge the added attributes to the ones originally provided to the event. It's just a convenience so the event subscribers don't have to call getIntentAttributes(), merge deep with whatever additional attributes they want, and then call setIntentAttributes().

  • 🇯🇴Jordan mhawwari

    Needed to use this patch for adding an intent description.
    I applied the approach in #17 and added a new addIntentAttributes() method. I also just added a @deprecated tag for #16

    The only concern here is if we should dispatch the event in the same place the transaction data event is being dispatched in the createPayment() method and pass the payment object if a project needed to pass info about the drupal payment to stripe.

  • 🇯🇴Jordan mhawwari

    I guess we can pass an empty intent array to only update attributes added from the event subscriber and that can avoid updating all the intent attributes in createPayment() after payment save. So we can pass new PaymentIntentEvent($order, [], $payment) to replace new TransactionDataEvent($payment).

  • 🇳🇿New Zealand john pitcairn

    The only concern here is if we should dispatch the event in the same place the transaction data event is being dispatched in the createPayment() method

    No. See #5 ✨ Allow customising payment intent attributes and metadata Fixed .

  • 🇳🇿New Zealand john pitcairn

    PaymentIntentEvent is missing a use statement for NestedArray.

  • 🇳🇿New Zealand john pitcairn

    The comments in StripeEvents also need attention.

    PaymentIntentEvent is only dispatched when a payment intent is created. It does not allow modification of transation data if a payment intent is updated.

    TransactionDataEvent did allow that, but is now deprecated, and it is only passed the payment at __construct(), not the intent attributes.

    The only place we call PaymentIntent::update() is following the TransactionData event dispatch, to merge in any added metadata, and that is going to be removed.

    I think we can remove any mention of updating a payment intent.

  • Status changed to Needs review almost 2 years ago
  • 🇳🇿New Zealand john pitcairn

    Patch as per #21-22.

  • 🇬🇧United Kingdom jonathanshaw Stroud, UK
    +++ b/src/Event/StripeEvents.php
    @@ -7,12 +7,24 @@ namespace Drupal\commerce_stripe\Event;
    +  const PAYMENT_INTENT = 'commerce_stripe.payment_intent';
    

    I think we should call this PAYMENT_INTENT_CREATE in cae we also want to create a PAYMENT_INTENT_UPDATE in the future.

  • Status changed to Needs work almost 2 years ago
  • 🇬🇧United Kingdom jonathanshaw Stroud, UK

    This is not a replacement for every conceivable use case for TRANSACTION_DATA event because it is called earlier. But it should handle the 99% use cases rather better than the current approach, so I think its fine as is. We can always add further events in future if demmand is there.

  • Status changed to Needs review almost 2 years ago
  • 🇳🇿New Zealand john pitcairn

    OK and we'd just use the same event class when dispatching an update event.

    This patch changes the event constant. Anyone relying on it will need to update their subscriber getSubscribedEvents() method.

    It also adds "Will be removed in 2.x" to the deprecation notices, and the docblock clarifies that the event fires before the payment intent is created.

  • Status changed to RTBC almost 2 years ago
  • 🇬🇧United Kingdom jonathanshaw Stroud, UK
    +++ b/src/Event/StripeEvents.php
    @@ -7,12 +7,23 @@ namespace Drupal\commerce_stripe\Event;
    +   * @deprecated Will be removed in 2.x. Use StripeEvents::PAYMENT_INTENT to
    

    Constant needs changing here too.

    RTBC as trivial and can be fixed on commit.

  • Status changed to Fixed almost 2 years ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024