<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shramik Nakarmi on Medium]]></title>
        <description><![CDATA[Stories by Shramik Nakarmi on Medium]]></description>
        <link>https://medium.com/@shramiknakarmi?source=rss-a11b8ff4b351------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kF7MeBn7cPpTkEWpRQlXiw.jpeg</url>
            <title>Stories by Shramik Nakarmi on Medium</title>
            <link>https://medium.com/@shramiknakarmi?source=rss-a11b8ff4b351------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:49:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shramiknakarmi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dealing with numbers and money]]></title>
            <link>https://shramiknakarmi.medium.com/dealing-with-numbers-and-money-c3e59505f939?source=rss-a11b8ff4b351------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3e59505f939</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[value-objects]]></category>
            <category><![CDATA[money]]></category>
            <category><![CDATA[domain-driven-development]]></category>
            <category><![CDATA[laravel-framework]]></category>
            <dc:creator><![CDATA[Shramik Nakarmi]]></dc:creator>
            <pubDate>Wed, 31 Jan 2024 13:15:25 GMT</pubDate>
            <atom:updated>2024-01-31T13:54:24.917Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/72/0*v3NUoCahpE1yB42a.png" /></figure><p>Working with numbers and more specifically money can be hard. My experience is that there are lots of (mostly) legacy projects where dealing with money, numbers, and payments is implemented in a horrible way. Float numbers, strings, integers, calculation errors because of the decimals, inconsistency all over the place, $29.99998 and 30.000001% on the frontend. You know what I mean.</p><h3>Dealing with money</h3><p>In order to handle money values in a reliable way we need to store them as cent value. Instead of storing $19.99 as 19.99 in the database, we store them as 1999. This way we’ll have no problem with decimal points, rounding, multiplying, or dividing numbers.</p><p>The other thing we can do is use value objects instead of treating them as float numbers in PHP. So instead of this:</p><p>​<br>$revenue = $transaction-&gt;amount - $fee - $tax;<br>​</p><p>We’ll write code such as this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cXUY1MxkD76Uf3Za" /></figure><p>We’ll use Eloquent attribute casts and value objects to make everything as smooth as possible. If you’re not sure what a value object is, <a href="https://click.convertkit-mail2.com/qdu6xog5odc8u9xpzkblhnpk9wkk/vqh3hrhpo8d3qqbg/aHR0cHM6Ly9tYXJ0aW5qb28uZGV2L2RvbWFpbi1kcml2ZW4tZGVzaWduLXdpdGgtbGFyYXZlbC12YWx1ZS1vYmplY3Rz">check out this article</a>.</p><h3>Dealing with dates</h3><p>Another thing I find quite confusing in features such as dashboards, and reports is dealing with dates. Have you ever seen code like this?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*giae5LjcyIRBLxmy" /></figure><p>startDate and endDate are PHP Date objects.</p><p>The other class in the same project does this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4rUz-vEph6smxKhx" /></figure><p>from and to are strings.</p><p>The third method uses dedicated $fromDate and $endDate variables and they are UNIX timestamps. In the fourth class… You know what I’m talking about. It’s a complete mess.</p><p>We’re going to solve that problem by introducing domain-specific objects such as StartDate end EndDate and we’ll have functions such as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Qk1FI0QYqQhPMnyu" /></figure><p>$interval-&gt;startDate is an instance of StartDate which is <strong>always</strong> (no matter what) Y-m-d 00:00:00 and $internal-&gt;endDate is an instance of EndDate which is <strong>always</strong> (no matter what) Y-m-d 23:59:59.</p><h3>Money value object</h3><p>Before writing any business logic we need to take care of money values. There’s an excellent package called <a href="https://click.convertkit-mail2.com/qdu6xog5odc8u9xpzkblhnpk9wkk/l2hehmh3lwzvdpt6/aHR0cHM6Ly9naXRodWIuY29tL21vbmV5cGhwL21vbmV5">money</a> that gives us some value objects to deal with money and currencies.</p><p>It works like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fEekLSMXl7-EMo5Q" /></figure><p>It treats values as integers in cent values, and everything is an object so you add to numbers by calling the add method. We need to cast the Product model’s price attribute to this Money object. In order to do that we need a custom cast:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RjH76FZNnc2BdoPV" /></figure><p>There are only two methods:</p><ul><li>get handles when you have a model and you want to access the property. In this case, the $value attribute is the value of the price column from the database. So this method runs when you do this: echo $product-&gt;price</li><li>set handles when you want to set a property on the model. But instead of $product-&gt;price = 500 we want to do this: $product-&gt;price = Money::USD(500). This is why the set method returns with $value-&gt;getAmount(). $value is a Money object and the set method gets the amount so 500 will be inserted into the database.</li></ul><p>To sum it up, we want something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AunGFVaajY944RhT" /></figure><p>I’m not proud of this sentence, but here’s the truth: we made the whole thing a little bit more like Java. Which is a good thing if we’re dealing with money and transactions.</p><p>The last thing is to actually use the MoneyCast:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QAn256a5TNAoiDJX" /></figure><p>We need to specify that the price column uses our shiny MoneyCast.</p><p>Now, let’s see these classes in action! Let’s imagine we’re working on a product such as Paddle. Paddle is a finance application. More specifically it’s a merchant of record that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/0*fEjb6QG_sR7YGomt" /></figure><p>This is what’s happening:</p><ul><li>A seller (or content creator) uploads a product to Paddle</li><li>They integrate Paddle into their landing page</li><li>Buyers buy the product from the landing page using a Paddle checkout form</li><li>Paddle pays the seller every month</li></ul><h3>Buying a product (transaction)</h3><p>When someone buys something we need to create a new transaction so the API endpoint is POST /transactions. A transaction needs the following three parameters:</p><ul><li>product_id</li><li>quantity</li><li>customer_email</li></ul><p>The request looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bILwFxR6zR9eevZi" /></figure><p>As you can see, I like these pretty straightforward getter methods in request classes. I don’t write business logic here, only one-liner findOrFail type queries to get something from the database. It’s highly subjective, of course. In theory, you shouldn’t do something like that, but in practice, I never experienced any problem with this approach.</p><p>Now let’s think about what we need to do when someone buys a product:</p><ul><li>Calculate the total price based on the product and the quantity</li><li>Calculate the VAT amount based on the customer’s IP address</li><li>Charge the customer using Stripe</li><li>Store the actual transaction</li><li>Trigger webhooks</li></ul><p>In this chapter, we’re not going to deal with Stripe. We’ll handle that later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*49LXQnXj0EQsCmNH" /></figure><p>You can see how we can work with the Money object, for example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wiLK7kUtL2M_zsIz" /></figure><p>Because of the cast we discussed earlier, $product-&gt;price returns a Money object that has methods on it. Each of these operations, such as multiplication returns a new Money object and doesn’t change the original one. These objects are immutable. We have the total amount of the transaction and we also need to calculate the fee:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YeSWy2ggLHRvI0uU" /></figure><p>Once again, we use the multiply method. The fee rate is stored in a settings table that looks like this:</p><p>It’s a “singleton” table, meaning it has only one row. Because of that, we can bind a singleton instance to Laravel’s service container:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FiAEwGa9T-nRSA5O" /></figure><p>Whenever you do this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ypGWGHVsgyC-Pyjx" /></figure><p>You have the first and only record from the table.</p><p>When you have “settings” in your project you can go with this singleton approach, or you can create a key-value style table such as this:</p><p>Instead of having a single row, you have a row for every setting. I, personally, like the singleton approach better since it’s more “reliable.” You just look at the table and know exactly what settings your project has. The key-value store is a bit “random.” Any developer can insert any value without others knowing about it. Unless you maintain an enum or array with the possible keys. In the singleton model, if you need a new setting, you have to add a new column, with a type, and possibly with a default value as well. It’s visible and well-documented. Works better for me. Of course, you can end up with a table that has 50+ columns. Which can be annoying.</p><p>So we have a settings table. The fee_rate column is cast into a Percent value object:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0zfPn_XbGRxZXxut" /></figure><p>​</p><p>As I said earlier, working with numbers and money is quite challenging but using these value objects and techniques can help <strong>a lot.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3e59505f939" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use the laravel collection inside JS ??]]></title>
            <link>https://shramiknakarmi.medium.com/how-to-use-the-laravel-collection-inside-js-2c5c90ed9af2?source=rss-a11b8ff4b351------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c5c90ed9af2</guid>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shramik Nakarmi]]></dc:creator>
            <pubDate>Sun, 23 Dec 2018 10:55:16 GMT</pubDate>
            <atom:updated>2018-12-26T04:59:04.176Z</atom:updated>
            <content:encoded><![CDATA[<p>How to use the laravel collection inside JS ??</p><p>$items = Collection;</p><p>return $items to the required blade file;</p><p>and inside the blade’s script section : var items = ‘{!! $items!!}’;</p><p>and access the required object from the JSON Array</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c5c90ed9af2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solidity : Example 1]]></title>
            <link>https://shramiknakarmi.medium.com/solidity-example-1-f6db2312d973?source=rss-a11b8ff4b351------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6db2312d973</guid>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Shramik Nakarmi]]></dc:creator>
            <pubDate>Sun, 09 Dec 2018 18:26:26 GMT</pubDate>
            <atom:updated>2018-12-09T18:54:51.743Z</atom:updated>
            <content:encoded><![CDATA[<h3>Solidity : Smart Contract</h3><p>pragma solidity &gt;=0.4.22 &lt;0.6.0; //solidity version</p><p>contract Developer{<br> <br> struct Developer{<br> address developer_address;<br> uint developer_code;<br> string name;<br> string language;<br> uint claps_hold;</p><p>}<br> <br> mapping(address =&gt; Developer) developers;<br> <br> <br> address[] public developerAccounts; <br> event getTotalClaps(address,uint);<br> <br> constructor () public{<br> addDeveloper(address(0x4b0897b0513fdc7c541b6d9d7e929c4e5364d2db),1,”Prabin Siwakoti”,”C#”,500);<br> addDeveloper(address(0xdd870fa1b7c4700f2bd7f44238821c26f7392148),2,”Shramik Nakarmi”,”PHP”,300);<br> }<br> <br> <br> function addDeveloper(address _developer_address,uint _developer_code,string _name,string _language,uint _claps) public{<br> <br> var developer = developers[_developer_address]; // key value<br> <br> developer.developer_code = _developer_code;<br> developer.name = _name;<br> developer.language = _language;<br> developer.claps_hold = _claps;<br> developerAccounts.push(_developer_address);<br> }<br> <br> <br> function getDeveloper(address _developer_address) public returns(uint,string,string,uint){<br> var developer_address = _developer_address;<br> return (developers[developer_address].developer_code,developers[developer_address].name,developers[developer_address].language,developers[developer_address].claps_hold);<br> }<br> <br> <br> function getDeveloperAccounts() public returns(address[]) {<br> return developerAccounts; <br> }<br> <br> function countDevelopers() public returns(uint){<br> return developerAccounts.length;<br> }<br> <br> function clapForDeveloper(address _developer_address) public returns(uint){<br> var developer = developers[_developer_address];<br> developer.claps_hold = developer.claps_hold + 1;<br> emit getTotalClaps(_developer_address,developer.claps_hold);<br> }</p><p>}</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6db2312d973" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Things to remember while deploying production build files (react/vue/angular)]]></title>
            <link>https://shramiknakarmi.medium.com/things-to-remember-while-deploying-production-build-files-react-vue-angular-ae79927bc0fc?source=rss-a11b8ff4b351------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae79927bc0fc</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[production]]></category>
            <dc:creator><![CDATA[Shramik Nakarmi]]></dc:creator>
            <pubDate>Wed, 05 Dec 2018 17:56:16 GMT</pubDate>
            <atom:updated>2018-12-05T17:56:16.074Z</atom:updated>
            <content:encoded><![CDATA[<p>Things to remember while deploying production build files (react/vue/angular)</p><ol><li>to hide the source code/map, you can delete the files ending with “.js.map” ,now when you check the source map in the developer console you will no longer see the source code written in your application</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae79927bc0fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>