Created on 12 May 2023, over 1 year ago
Updated 7 November 2023, 12 months ago

Hi,

It would be great to be able to have a programmatic value assigned for the price.

I want to be able to grab the price from a field on a node that will be available to the webform, so something like price = $node->get('field_price')->getString().

Currently it is difficult to have different prices for different products.

Thanks,

Glenn

✨ Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡³πŸ‡ΏNew Zealand glennnz

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

Comments & Activities

  • Issue created by @glennnz
  • πŸ‡ΈπŸ‡ΎSyria hodba Dubai

    If I got your point right and the node is available to the webform, then you can use the token of the field in the price.
    Can you check if this works for you?

  • πŸ‡³πŸ‡ΏNew Zealand glennnz

    @hodba

    No, this doesn't work. The field in the webform element settings asks for a float number; putting a token there results in an error:

    TypeError: Unsupported operand types: string * int in Drupal\stripe_webform_payment\Plugin\WebformElement\StripeWebformPayment->createPaymentIntent() (line 351 of modules/contrib/stripe_webform_payment/src/Plugin/WebformElement/StripeWebformPayment.php).

  • Status changed to Closed: works as designed over 1 year ago
  • πŸ‡³πŸ‡ΏNew Zealand glennnz

    Solved.

    I built a module to create a custom token with my calculated price; this works beautifully.

  • πŸ‡³πŸ‡ΏNew Zealand glennnz

    Perhaps edit the instructions for the 'Amount' field; it asks for a float and doesn't indicate that a token can be used.

  • Status changed to Active over 1 year ago
  • πŸ‡³πŸ‡ΏNew Zealand glennnz

    Ah, ran into problems.....

    In my webform definition.yml file, I have:

    stripe_payment:
          '#type': stripe_payment
          '#title': Payment
          '#stripe_source': custom
          '#stripe_amount' : ['custom:price']
          '#stripe_currency' : nzd
          '#stripe_product': prod_NsAdetyTMZWPCL
          '#stripe_address': true
          '#stripe_address_type': billing
          '#stripe_customer_attributes': ''
          '#stripe_appearance': ''
          '#stripe_payment_intent_config': |-
            automatic_payment_methods:
              enabled: true
    

    In my mymodule.module, I have:

    function mymodule_token_info() {
      $tokens['price'] = [
        'name' => t('Final Price'),
        'description' => t('The price of the product.'),
      ];
      return [
        'types' => ['custom' => $type],
        'tokens' => ['custom' => $tokens],
      ];
    }
    
    function mymodule_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
      $replacements = [];
    
      if ($type == 'custom') {
        foreach ($tokens as $name => $original) {
    
          switch ($name) {
            case 'price':
              $replacements[$original] = '123999123';
              break;
          }
        }
      }
      return $replacements;
    }
    

    The '123999123' is a placeholder for the moment; this will become a calculated value.

    My code is creating an error in the createPaymentIntent(), I get this error:
    TypeError: Unsupported operand types: array * int in Drupal\stripe_webform_payment\Plugin\WebformElement\StripeWebformPayment->createPaymentIntent() (line 351 of modules/contrib/stripe_webform_payment/src/Plugin/WebformElement/StripeWebformPayment.php).

    Help would be appreciated.

    Thanks.

  • @hodba opened merge request.
  • πŸ‡ΈπŸ‡ΎSyria hodba Dubai

    Let's fix them one by one.
    I updated the code to accept tokens by converting the token value (text, number, or decimal) to a float value that Stripe accepts. Can you check the latest dev version and let me know if it is working for your use case?

  • πŸ‡³πŸ‡ΏNew Zealand glennnz

    Works perfectly!

    Thanks.

  • Status changed to Fixed over 1 year ago
  • πŸ‡ΈπŸ‡ΎSyria hodba Dubai
  • Status changed to Fixed over 1 year ago
  • πŸ‡ΈπŸ‡ΎSyria hodba Dubai
  • πŸ‡ΊπŸ‡ΈUnited States Tankeroo

    Hi! I need help with this.

    I have a Custom Twig Element which the end result gives me a float. The Twig calculates based on a previous option element which depends on what the user selects.

    On the "Custom Price" field, I place the token: [webform:element:order_total:value]
    Am I doing this right? I get the error
    Drupal\stripe_webform_payment\Plugin\WebformElement\StripeWebformPayment::updateNestedValues(): Argument #3 ($paymentIntentId) must be of type string, null given, called in /home/test_site/modules/stripe_webform_payment/src/Plugin/WebformElement/StripeWebformPayment.php on line 233

    Please help, thanks!

  • πŸ‡ΈπŸ‡ΎSyria hodba Dubai

    Can you provide me more details please about your specific case in details? e.g. how the Weform is configured? Are you using multistep webform? did you please it at the end? does the total number and the stripe element are on the same page?
    did you try [webform:element:order_total] without the value?

  • πŸ‡ΊπŸ‡ΈUnited States Tankeroo

    Hi @hodba, Thanks for your response.

    Modules: webform 6.12, stripe 2.0.0-beta4, stripe webform payment 1.0.0-rc1+4-dev

    Use Case: I have a one page webform (all elements are same page) for registration to conference.
    Usual use of elements, nothing out of the ordinary.
    In the middle of the form, I have a select element with two options "single_300" and "double_150"
    Second to last element is a computed Twig element which says if user selects single_300 price is 300.00, if double_150 is selected then price is 150.00

    {# Price list variables #}
    
    {% if data.fangxing_room_type == 'double_150' %}
    {% set price = 150.00 %}
    {% elseif data.fangxing_room_type == 'single_300' %}
    {% set price = 300.00 %}
    {% elseif data.fangxing_room_type == 'roomate_300' %}
    {% set price = 300.00 %}
    {% endif %}
    
    {{ price }}

    Then the very last element I want to add is the "stripe payment" element.
    I choose Custom Payment and in the "Amount" box I have tried [webform_submission:values:order_total] and [webform:element:order_total] and [webform:element:order_total:value]
    Currency is "usd". I give it a title and key and press "save". I get this error:

    The price amount or currency is not defined, please correct this and try again.
    Unable to render elements, please view the below message(s) and the error log.
    
        Drupal\stripe_webform_payment\Plugin\WebformElement\StripeWebformPayment::updateNestedValues(): Argument #3 ($paymentIntentId) must be of type string, null given, called in /home/test_site/modules/stripe_webform_payment/src/Plugin/WebformElement/StripeWebformPayment.php on line 233

    Thank you for the help!

  • πŸ‡ΈπŸ‡ΎSyria hodba Dubai

    I see where the issue is.

    In order to pick up the price as a token, you need to separate the payment field in a new page where the calculation is done before proceeding to the payment. This is due to the fact that Stripe element needs to generate the payment intent before the payment form is loaded. When you put the price as a token in the Stripe element, and place all elements in one page, the paymentIntent is generated before the token can be used.

    Hope this helps you resolve the issue.

    Best,

  • πŸ‡ΊπŸ‡ΈUnited States Tankeroo

    That absolutely helped! Thank you for explaining that and pointed me to the right direction.
    I've moved the payment to a multi-page wizard on a second page and it works.

    The token [webform_submission:values:order_total] is what's being used.

    Just as a note for future users who may come here to look for solutions. When using multi-page with stripe, you'll see this warning.
    This was what prevented me from trying multi-page. But, I guess in this instance (at the time of posting) it can be ignored.
    While stripe elements should work with wizard-like webforms, because the actual charge happens on the client side, the process can be very prone to user errors when moving accross pages and using the browser navigation buttons. It is strongly recommend not to use this feature.

    Thank you, @hodba

Production build 0.71.5 2024