commerce icon indicating copy to clipboard operation
commerce copied to clipboard

[5.x]: craft\commerce\elements\Order::setPaymentCurrency(): Argument #1 ($value) must be of type string, null given

Open vnali opened this issue 8 months ago • 6 comments

What happened?

Hello. This is a bug for example template.

Description

Making a payment for the outstanding balance of a partially paid order using the example template throws this error.

Steps to reproduce

  • Enable partial payment for a store.
  • Make a partial payment on an order.
  • Visit my order page and select the relevant order.
  • Click the 'Make Payment' button to pay the outstanding balance.
  • Select the Dummy gateway.
TypeError: craft\commerce\elements\Order::setPaymentCurrency(): Argument #1 ($value) must be of type string, null given, called in /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php on line 1861 and defined in /var/www/html/vendor/craftcms/commerce/src/elements/Order.php:3404
Stack trace:
#0 /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php(1861): craft\commerce\elements\Order->setPaymentCurrency()
#1 /var/www/html/vendor/craftcms/cms/src/helpers/Template.php(148): Twig\Extension\CoreExtension::getAttribute()
#2 /var/www/html/storage/runtime/compiled_templates/e7/e71bd81f211bf15f30bfb5e9fed00dd8.php(266): craft\helpers\Template::attribute()
#3 /var/www/html/vendor/twig/twig/src/Template.php(431): __TwigTemplate_583fae0a3ea388bfd988f31bf8c82e17->block_main()
#4 /var/www/html/storage/runtime/compiled_templates/39/391f2e59b209d8ac25f5d9417489ca5b.php(399): Twig\Template->yieldBlock()
#5 /var/www/html/vendor/twig/twig/src/Template.php(431): __TwigTemplate_3ca12679132cb4bd8493f1a541f6d179->block_body()
#6 /var/www/html/storage/runtime/compiled_templates/39/391f2e59b209d8ac25f5d9417489ca5b.php(303): Twig\Template->yieldBlock()
#7 /var/www/html/vendor/twig/twig/src/Template.php(387): __TwigTemplate_3ca12679132cb4bd8493f1a541f6d179->doDisplay()
#8 /var/www/html/storage/runtime/compiled_templates/e7/e71bd81f211bf15f30bfb5e9fed00dd8.php(81): Twig\Template->yield()
#9 /var/www/html/vendor/twig/twig/src/Template.php(387): __TwigTemplate_583fae0a3ea388bfd988f31bf8c82e17->doDisplay()
#10 /var/www/html/vendor/twig/twig/src/Template.php(343): Twig\Template->yield()
#11 /var/www/html/vendor/twig/twig/src/Template.php(358): Twig\Template->display()
#12 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(35): Twig\Template->render()
#13 /var/www/html/vendor/twig/twig/src/Environment.php(320): Twig\TemplateWrapper->render()
#14 /var/www/html/vendor/craftcms/cms/src/web/View.php(581): Twig\Environment->render()
#15 /var/www/html/vendor/craftcms/cms/src/web/View.php(634): craft\web\View->renderTemplate()
#16 /var/www/html/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(57): craft\web\View->renderPageTemplate()
#17 /var/www/html/vendor/yiisoft/yii2/web/Response.php(1109): craft\web\TemplateResponseFormatter->format()
#18 /var/www/html/vendor/craftcms/cms/src/web/Response.php(341): yii\web\Response->prepare()
#19 /var/www/html/vendor/yiisoft/yii2/web/Response.php(340): craft\web\Response->prepare()
#20 /var/www/html/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#21 /var/www/html/web/index.php(12): yii\base\Application->run()
#22 {main}

Craft CMS version

5.7.4

Craft Commerce version

5.3.10

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

vnali avatar Apr 29 '25 17:04 vnali

Taking a look at this today and will let you know what I find. Thanks.

lukeholder avatar May 05 '25 02:05 lukeholder

@vnali Can you resave your payment currencies in the control panel and try again?

lukeholder avatar May 09 '25 07:05 lukeholder

@lukeholder i have only one currency as USD (Primary) in admin/commerce/store-management/primary/payment-currencies which there is no way i can resave it.

More details: I tried to inspect which parameters are passed when paying the remaining balance via /shop/customer/order and after that selecting a gateway, and I noticed that paymentCurrency is not provided in the pay-static.twig template.

Here’s what the parameters look like:

array:3 [
  "number" => "d3f8..."
  "email" => "xyz.."
  "gatewayId" => "1"
]

However, on line 131 of pay-static.twig, there's this line:

{% do cart.setPaymentCurrency(paymentCurrency) %}

The problem is that paymentCurrency is required, but it’s not being passed.

vnali avatar May 09 '25 08:05 vnali

@vnali can you pass paymentCurrency in the request to pay, and not use the do cart.setPaymentCurrency setting it in the template will not save it on the order, so when the pay request is made, the paymentCurrency will not be set.

lukeholder avatar May 12 '25 05:05 lukeholder

sorry @lukeholder I think I explained myself poorly, so I uploaded a video to explain it better. I didn’t write the template myself; I used the example templates from commerce repo here

https://github.com/user-attachments/assets/51162ddb-cf0b-49a9-b20e-fac9ed7889ea

vnali avatar May 12 '25 15:05 vnali

Thanks for reporting. I fixed the example templates in this PR: https://github.com/craftcms/commerce/pull/4083

You are welcome to look at the changes and update your example templates directly, alternatively we will let you know here once the PR is included in a release.

Thanks.

lukeholder avatar Jul 25 '25 07:07 lukeholder

Commerce 5.4.4 is out with that change.

brandonkelly avatar Jul 31 '25 18:07 brandonkelly