question

georgi avatar image
georgi asked georgi commented

Wrong calculations for total when refunding two separate items

Here is the case: Let's say I am a retailer selling apples. One apple - one dollar. I sell two apples today - 2$. The customer comes back tomorrow, returns one of the apples. I do a refund. So I still have earned 1$. Customer comes the day after tomorrow and returns the other apple. I do a refund. 0$ profit.

This is what I have done with an old purchase. But when I make a request to get the order details I receive total for order 100? Why is it 100?

Here is the json:

 {
  "href": "https://www.clover.com/v3/merchants/NJSN3NEHPQDD0/orders/RTZPKNNX0Y3JP",
  "id": "RTZPKNNX0Y3JP",
  "currency": "USD",
  "employee": {
    "id": "BC88DMKKDNJG8"
  },
  "total": 100,
  "taxRemoved": false,
  "isVat": false,
  "state": "locked",
  "manualTransaction": false,
  "groupLineItems": true,
  "testMode": false,
  "payType": "FULL",
  "createdTime": 1459241189000,
  "clientCreatedTime": 1459241189000,
  "modifiedTime": 1466673034000,
  "lineItems": {
    "elements": [
      {
        "id": "N10E22KJKFN5W",
        "orderRef": {
          "id": "RTZPKNNX0Y3JP"
        },
        "item": {
          "id": "74N2V63P4T3MG"
        },
        "name": "Apple",
        "alternateName": "",
        "price": 100,
        "printed": false,
        "createdTime": 1459241189000,
        "orderClientCreatedTime": 1459241189000,
        "exchanged": false,
        "refunded": true,
        "isRevenue": true
      },
      {
        "id": "RWJ5NE2K3GE8J",
        "orderRef": {
          "id": "RTZPKNNX0Y3JP"
        },
        "item": {
          "id": "74N2V63P4T3MG"
        },
        "name": "Apple",
        "alternateName": "",
        "price": 100,
        "printed": false,
        "createdTime": 1459241190000,
        "orderClientCreatedTime": 1459241189000,
        "exchanged": false,
        "refunded": true,
        "isRevenue": true
      }
    ]
  },
  "payments": {
    "elements": [
      {
        "id": "RAXJFM04WAJ6P",
        "order": {
          "id": "RTZPKNNX0Y3JP"
        },
        "device": {
          "id": "d2ef4c13-31be-44ac-99d4-480a492264fb"
        },
        "tender": {
          "href": "https://www.clover.com/v3/merchants/NJSN3NEHPQDD0/tenders/HZFN8P7QEXAMJ",
          "id": "HZFN8P7QEXAMJ"
        },
        "amount": 200,
        "taxAmount": 0,
        "cashTendered": 200,
        "employee": {
          "id": "BC88DMKKDNJG8"
        },
        "createdTime": 1459241343000,
        "clientCreatedTime": 1459241342000,
        "modifiedTime": 1459241343000,
        "result": "SUCCESS"
      }
    ]
  },
  "refunds": {
    "elements": [
      {
        "id": "Q6657EPXW8KH0",
        "orderRef": {
          "id": "RTZPKNNX0Y3JP"
        },
        "device": {
          "id": "d2ef4c13-31be-44ac-99d4-480a492264fb"
        },
        "amount": 100,
        "taxAmount": 0,
        "createdTime": 1466583149000,
        "clientCreatedTime": 1466583149000,
        "payment": {
          "id": "RAXJFM04WAJ6P"
        },
        "employee": {
          "id": "BC88DMKKDNJG8"
        }
      },
      {
        "id": "JDNCBVKVSTF42",
        "orderRef": {
          "id": "RTZPKNNX0Y3JP"
        },
        "device": {
          "id": "d2ef4c13-31be-44ac-99d4-480a492264fb"
        },
        "amount": 100,
        "taxAmount": 0,
        "createdTime": 1466673034000,
        "clientCreatedTime": 1466673034000,
        "payment": {
          "id": "RAXJFM04WAJ6P"
        },
        "employee": {
          "id": "BC88DMKKDNJG8"
        }
      }
    ]
  },
  "device": {
    "id": "d2ef4c13-31be-44ac-99d4-480a492264fb"
  }
}
OrdersRefund
2 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

The problem, if there is one, depends on the flow for doing the refund. The Clover server doesn't calc order totals. It relies on the client updating the total after changes are made. The reasons for this is that client must work offline with no connection to the server, so it must be the source of truth. Can you explain how the refunds occurred? Did you initiate the refund? Was it done through Clover apps?

0 Likes 0 ·
georgi avatar image georgi commented ·

The refunds were done after I opened the Orders app and clicked edit. After edit I clicked Exchange/Refund and first refund one of the products. Then I closed the orders, 10 minutes later I made another refund in the same way. Both times the emulator was connected to the Internet. As you can see in the JSON file both the refunds are visible to the server. There are 2 records in the refunds field, both the elements in the lineItems are with a field refunded: true. Only the total calculation is wrong.

0 Likes 0 ·

1 Answer

Jeffrey Blattman avatar image
Jeffrey Blattman answered georgi commented

I tested the same and it worked for me. Again, the total you are getting is not calculated. It's whatever the client told it was the total. Are you running any other apps that could be making order modifications? The server doesn't care. You can set any total you like.

EDIT: Because there is ongoing confusion about this in the comments, I'll try one more time to explain it here where I have more space. The "total" field that is returned from server IS NOT CALCULATED ON THE SERVER OR BY THE SERVER from order data (refunds, line items, etc). The "total" field is set by the client when changes are made to the order. That is the ONLY way the total of an order, as set in the "total" field, changes. In this case the problem is that the client didn't update the order total. I know this as I can see the network requests. I don't know why that happened.

The web APIs are dumb. They rely on the client doing the right thing. Yes, clients can modify the order total to whatever you like. Clover POS software calculates the total itself so you won't affect our apps. If however you wrote your own POS on top of Clover, this would be a problem for you.

Why are the APIs like this? Because Clover works in offline mode with no network connection. If you can't expect to get to the server to have it tell you things like order total, you have to be prepared to do it locally on the client. And if you are doing it locally on the client, why do it on the server at all? Moreover, if you did it in two places, you'd have to resolve conflicts (server things total is A, but client thinks it's B). The client is the source of truth when it comes to order total.

That means if you are making order modifications that effect the total in your app using the Clove REST APIs, you must update the order total yourself. If you are using the Clover Android SDK, our SDK handles it for you (e.g., if you add a line item, we'll update the total). If you are writing your own POS software on top of Clover and not using our SDK, you need to handle it yourself.

5 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

georgi avatar image georgi commented ·

Thanks, jeff. I will make some tests tomorrow but it is really strange that the app says that the total is 100. That means that the app does not calculate correctly. After all the total field should always be calculated and not set manually?

0 Likes 0 ·
Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

What do you mean "app says total is 100". What app? Yes, the client doesn't use the "total" field from the server, it always does local calculations. E.g., this: https://www.clover.com/r/RTZPKNNX0Y3JP shows a total of 0. Like I said, any app could have set the total to 100 on the server, not just Clover apps. That's why I asked what else might be mutating that order.

0 Likes 0 ·
georgi avatar image georgi commented ·

I am sorry, I did not mean app, I meant the response from the server. Can you see line number 8 at the response I receive? I have pasted it when I created this question. Here it is:

 "total": 100,

It says total is 100, but as you see in the online order you sent me a link to, it is 0. Where does that difference come from? I am not using any other apps except those by Clover and the one I am developing. And I need to have a correct "total" field, otherwise I will calculate it myself.

0 Likes 0 ·
Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

Yes, I agree it's wrong. Like I said a few times above, the total value from the server is not calculated. The server relies on the client telling it what the total is. The server is dumb. It just saves the value sent to it from the client. So in this case the mystery is why the client didn't update the total after the refund. I don't have an answer for that at this point. It would be very useful if you could figure out how to reproduce the problem.

0 Likes 0 ·
georgi avatar image georgi commented ·

Thanks, jeff. Appreciate the help, I will investigate tomorrow.

The thing is that the server has the correct data. The refunds and lineItems fields are correct so basically it cannot be a network issue and it comes down to a math problem within the app.

And the other thing that concerns me is that in the app you can come and buy 2 apples for 2 dollars total, but I can modify the total and charge you 100$ for both? I will recalculate the total based on the purchases and the refunds fields and will write back when I understand how I have made this error.

0 Likes 0 ·

Welcome to the
Clover Developer Community