From 6960f565208af11828920f262d201e9db7a9aba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Fri, 16 Jul 2021 22:32:55 +0800 Subject: parcel_total_weight turns out to be required We use Interchange as a reference, and the unit weight is not saved to database for the transaction, only shipping cost. So to make it convenient for the library users, the total weight calculation is done by the library, so no need to pass any total weight argument. --- anteraja.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/anteraja.c b/anteraja.c index c64ac68..8b78815 100644 --- a/anteraja.c +++ b/anteraja.c @@ -15,6 +15,7 @@ "{\ \"booking_id\":\"%s-%s\",\ \"service_code\":\"%s\",\ +\"parcel_total_weight\":%d,\ \"shipper\":{\ \"name\":\"%s\",\ \"phone\":\"%s\",\ @@ -32,6 +33,7 @@ ],\ \"declared_value\":%d\ }" +#define ORDER_WEIGHT 5 #define ORDER_SUBTOTAL 9 #define ORDER_ITEM \ @@ -100,13 +102,18 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ sprintf(*url, "%s%s", shipping.base, ORDER_PATH); enum { SKU, QUANTITY, DESCRIPTION, PRICE, WEIGHT }; char *json = NULL; + double total_weight = 0; for (int i = 0; i < nitems; i++) { size_t length = strlen(ORDER_ITEM) + strlen(items[i][DESCRIPTION]) + ORDER_ITEM_QUANTITY + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") + strlen(","); char item[length + 1]; + double weight = atof(items[i][WEIGHT]) * 1000.0; + if (weight < 100.0) + weight = 100.0; sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), - atoi(items[i][PRICE]), atoi(items[i][WEIGHT]) * 1000); + atoi(items[i][PRICE]), (int)weight); + total_weight += weight; if (json) json = realloc(json, strlen(json) + length + 1); else { @@ -120,13 +127,13 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ json[strlen(json)] = '\0'; } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) - + strlen(sender_name) + strlen(sender_phone) + strlen(origin) + + ORDER_WEIGHT + strlen(sender_name) + strlen(sender_phone) + strlen(origin) + strlen(sender_address) + strlen(receiver_name) + strlen(receiver_phone) + strlen(destination) + strlen(receiver_address) + strlen(json) + ORDER_SUBTOTAL - - 12 * strlen("%s") - strlen("%d") + 1); - sprintf(*post, ORDER_POST, prefix, trx_id, service, sender_name, sender_phone, origin, - sender_address, receiver_name, receiver_phone, destination, receiver_address, json, - (int)subtotal); + - 12 * strlen("%s") - 2 * strlen("%d") + 1); + sprintf(*post, ORDER_POST, prefix, trx_id, service, (int)total_weight, sender_name, sender_phone, + origin, sender_address, receiver_name, receiver_phone, destination, + receiver_address, json, (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -- cgit v1.2.3