简体   繁体   English

后端的条带付款整数金额-想要转换为.hbs前端的$ USD.XX

[英]Stripe Payment Integer Amount in Backend - Want to translate to pretty $USD.XX in .hbs frontend

I looked around but can't get my goal result. 我环顾四周,但无法获得目标结果。 All my Stripe Payment money values are stored as default integers and saved in the db this way. 我所有的Stripe Payment货币值都存储为默认整数,并以此方式保存在db中。 When I pull the values from the db and load them into a variable to pass to a handlebars rendered page, I can see the value without a decimal point as expected, when I keep that same form value and pass it to my pay button from Stripe, passing the integer amount to this button gets the value as expected in payment, but what can I do to format that integer on the front end to give it a pretty $USD.XX value, yet keeping the db working with integers? 当我从数据库中提取值并将其加载到变量中以传递到呈现给车把的页面时,当我保持相同的表单值并将其从Stripe传递到我的pay按钮时,可以看到没有预期的小数点的值,将整数金额传递给此按钮将获得付款中预期的值,但是我该怎么做才能在前端格式化该整数以使其具有相当的$ USD.XX值,而又保持数据库使用整数? When I divide the amount by 100 in the route that returns these values, this converted value finds its way back into the Pay Button, and sends a lower amount when payment dialog pops up. 当我在返回这些值的路径中将金额除以100时,此转换后的值将返回到“付款”按钮,并在弹出“付款”对话框时发送较低的金额。 I want to avoid this problem by intercepting the integer amount, performing format translation only for the front end to displaying the prettys. 我想通过截取整数来避免此问题,仅对前端执行格式转换以显示漂亮的图片。 I also got a form that edits this, but I don't want my users to have to worry about needing to enter only an integer value, so hopefully a filter that responds like an atm, when you start to enter values in there, pennies first... I just can't find the easy button to this problem. 我还得到了一个可以对此进行编辑的表格,但是我不想让我的用户不必担心只需要输入一个整数值,因此希望当您开始在其中输入值时,一个像atm一样响应的过滤器首先...我只是找不到解决此问题的简单按钮。

I got to my desired effect, 我达到了我想要的效果,

The work around I did was to create the 'placeholder' variables that will hold as a string the values of the formatted amounts ie. 我所做的工作是创建“占位符”变量,该变量将格式值的值即字符串保存为字符串。 bal for Model.balance, quo for Model.quote, cre for Model.credit. bal代表Model.balance,quo代表Model.quote,cre代表Model.credit。

In the route will do the conversion: setting the temporary placeholder variable with the formatting code and passing it out to render... 在路由中将进行转换:使用格式代码设置临时占位符变量并将其传递给呈现...

           job.bal = parseFloat(Math.round(job.balance / 100)).toFixed(2).toString();
           job.cre = parseFloat(Math.round(job.credit / 100)).toFixed(2).toString();
           job.quo = parseFloat(Math.round(job.quote / 100)).toFixed(2).toString();

The returned values have these temp placeholder values with the format I'm looking for. 返回的值具有这些临时占位符值,并且具有我正在寻找的格式。

Note: I had to add the placeholder variables into the Model.Schema, and also into the instance that creates this schema before I was able to even populate it with the parseFloat(...) 注意:我必须先将占位符变量添加到Model.Schema中,还要添加到创建此模式的实例中,然后才能使用parseFloat(...)填充它。

After that I was able to view in the .hbs render the decimal'd amount values as a string, out to the view displaying these values. 在那之后我能够以查看.hbs呈现decimal'd量的值作为一个字符串,拿出来展示这些价值的看法。

When I click the Pay Now button, it its passed the original {{balance}} into the 当我点击立即付款按钮时,它会将原始的{{balance}}传递到

 data-amount: {{balance}}

I use {{balance}} etc. for the backend integer amount format that Stripe likes, then use {{bal}} etc. for the front end view for the users to see their monetary values in a toFixed(2) decimal'd view. 我将{{balance}}等用于Stripe喜欢的后端整数金额格式,然后将{{bal}}等用于前端视图,以便用户查看以toFixed(2)十进制表示的货币值视图。

Whether of not this is best practice, probably not, but it's a solution I found, implemented and wanted to share. 这是否不是最佳实践,也许不是,但这是我找到,实现并希望分享的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM