From 7e15b32baaf737d28e0086b516b9ed307ab3f0c5 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 10:44:12 +0800 Subject: No need for shipping to be passed as pointer when it needs to be global anyway. --- anteraja.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 2fc265c..cd2932f 100644 --- a/anteraja.c +++ b/anteraja.c @@ -13,21 +13,19 @@ extern CURL *curl; static const char *status_trail[] = { "status", NULL }; -void anteraja_init(char *provisions[], struct shipping *shipping) +void anteraja_init(char *provisions[]) { - enum { BASE_PATH, ACCESS_KEY }; - shipping->base = malloc(strlen(provisions[BASE_PATH]) + 1); - strcpy(shipping->base, provisions[BASE_PATH]); - headers((const char *[]){ "access-key-id", "secret-access-key", NULL }, - &provisions[ACCESS_KEY], shipping); - shipping->headers = curl_slist_append(shipping->headers, "Content-Type:application/json"); + enum { BASE_PATH, ACCESS_KEY_ID, SECRET_ACCESS_KEY, PREFIX }; + shipping.base = malloc(strlen(provisions[BASE_PATH]) + 1); + strcpy(shipping.base, provisions[BASE_PATH]); + headers((const char *[]){ "access-key-id", "secret-access-key", NULL }, &provisions[ACCESS_KEY_ID]); + shipping.headers = curl_slist_append(shipping.headers, "Content-Type:application/json"); } -void anteraja_services(const char *origin, const char *destination, double weight, - struct shipping *shipping, char **url, char **post) +void anteraja_services(const char *origin, const char *destination, double weight, char **url, char **post) { - *url = malloc(strlen(shipping->base) + strlen(SERVICES_PATH) + 1); - sprintf(*url, "%s%s", shipping->base, SERVICES_PATH); + *url = malloc(strlen(shipping.base) + strlen(SERVICES_PATH) + 1); + sprintf(*url, "%s%s", shipping.base, SERVICES_PATH); *post = malloc(strlen(SERVICES_POST) + strlen(origin) + strlen(destination) + strlen("50000") - 2 * strlen("%s") - strlen("%d") + 1); sprintf(*post, SERVICES_POST, origin, destination, weight < 1.0 ? 1000 : (int)weight * 1000); -- cgit v1.2.3 From 4298408d8e810ecfd22927987dc8482b0129466d 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 10:46:39 +0800 Subject: Services weight max length macro --- anteraja.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index cd2932f..49d2802 100644 --- a/anteraja.c +++ b/anteraja.c @@ -8,6 +8,7 @@ \"destination\":\"%s\",\ \"weight\":%d\ }" +#define SERVICES_WEIGHT 5 extern CURL *curl; @@ -26,7 +27,7 @@ void anteraja_services(const char *origin, const char *destination, double weigh { *url = malloc(strlen(shipping.base) + strlen(SERVICES_PATH) + 1); sprintf(*url, "%s%s", shipping.base, SERVICES_PATH); - *post = malloc(strlen(SERVICES_POST) + strlen(origin) + strlen(destination) + strlen("50000") + *post = malloc(strlen(SERVICES_POST) + strlen(origin) + strlen(destination) + SERVICES_WEIGHT - 2 * strlen("%s") - strlen("%d") + 1); sprintf(*post, SERVICES_POST, origin, destination, weight < 1.0 ? 1000 : (int)weight * 1000); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); -- cgit v1.2.3 From 920878096a1721fd6e0c1b7008abac51803c5aa4 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 10:51:24 +0800 Subject: Generalise the response handler so the common part gets to be reused. --- anteraja.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 49d2802..06a9b24 100644 --- a/anteraja.c +++ b/anteraja.c @@ -37,7 +37,7 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, struct pikul_services **services) { size_t realsize = size * nmemb; - handle_services(contents, realsize, status_trail, (const char *[]){ + handle(SERVICES, contents, realsize, status_trail, (const char *[]){ "content", "services", NULL -- cgit v1.2.3 From 6f78942176d5909349305db37b5424a1c6ceccef 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 11:02:02 +0800 Subject: Order functionality draft Not tested yet. Now the item object is still flattened as an array, initially to hurry the interfacing with the Perl module. But we were stuck with having to typemap char *** anyway, so we switch to SWIG because of the potential of ease. Still, we need to typemap char ***, but now that we're at SWIG, we might as well typemap a custom item struct. --- anteraja.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 06a9b24..aad6d04 100644 --- a/anteraja.c +++ b/anteraja.c @@ -10,9 +10,45 @@ }" #define SERVICES_WEIGHT 5 +#define ORDER_PATH "order" +#define ORDER_POST \ +"{\ +\"booking_id\":\"%s-%s\",\ +\"service_code\":\"%s\",\ +\"shipper\":{\ +\"name\":\"%s\",\ +\"phone\":\"%s\",\ +\"district\":\"%s\",\ +\"address\":\"%s\"\ +},\ +\"receiver\":{\ +\"name\":\"%s\",\ +\"phone\":\"%s\",\ +\"district\":\"%s\",\ +\"address\":\"%s\"\ +},\ +\"items\":[\ +%s\ +],\ +\"declared_value\":%d\ +}" +#define ORDER_SUBTOTAL 9 + +#define ORDER_ITEM \ +"{\ +\"item_name\":\"%s\",\ +\"item_quantity\":%d,\ +\"declared_value\":%d,\ +\"weight\":%d\ +}" +#define ORDER_ITEM_QUANTITY 2 +#define ORDER_ITEM_PRICE 9 +#define ORDER_ITEM_WEIGHT 5 + extern CURL *curl; static const char *status_trail[] = { "status", NULL }; +static char *prefix = NULL; void anteraja_init(char *provisions[]) { @@ -21,6 +57,10 @@ void anteraja_init(char *provisions[]) strcpy(shipping.base, provisions[BASE_PATH]); headers((const char *[]){ "access-key-id", "secret-access-key", NULL }, &provisions[ACCESS_KEY_ID]); shipping.headers = curl_slist_append(shipping.headers, "Content-Type:application/json"); + if (provisions[PREFIX]) { + prefix = malloc(strlen(provisions[PREFIX]) + 1); + strcpy(prefix, provisions[PREFIX]); + } } void anteraja_services(const char *origin, const char *destination, double weight, char **url, char **post) @@ -49,3 +89,57 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, }, services); return realsize; } + +void anteraja_order(const char *trx_id, const char *service, const char *sender_name, + const char *sender_phone, const char *origin, const char *sender_address, + const char *receiver_name, const char *receiver_phone, const char *destination, + const char *receiver_address, int nitems, char **items[], double subtotal, + char **url, char **post) +{ + *url = malloc(strlen(shipping.base) + strlen(ORDER_PATH) + 1); + sprintf(*url, "%s%s", shipping.base, ORDER_PATH); + enum { SKU, QUANTITY, DESCRIPTION, PRICE, WEIGHT }; + char *json = NULL; + 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") + 1; + char item[length]; + sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), + atoi(items[i][PRICE]), atoi(items[i][WEIGHT]) * 1000); + if (json) + json = realloc(json, strlen(json) + length); + else + json = malloc(length); + strcat(json, item); + if (i + 1 < nitems) + json[length] = ','; + else + json[length] = '\0'; + } + *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) + + 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); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); +} + +size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **waybill) +{ + size_t realsize = size * nmemb; + handle(ORDER, contents, realsize, status_trail, (const char *[]){ + "content", + "waybill_no", + NULL + }, NULL, waybill); + return realsize; +} + +void anteraja_cleanup() +{ + if (prefix) + free(prefix); +} -- cgit v1.2.3 From b018ae643dbba21c0060ccb0748a7b239b4ecb5a 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 21:11:36 +0800 Subject: Initialise items JSON before concatenating --- anteraja.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index aad6d04..ad194f5 100644 --- a/anteraja.c +++ b/anteraja.c @@ -108,8 +108,10 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ atoi(items[i][PRICE]), atoi(items[i][WEIGHT]) * 1000); if (json) json = realloc(json, strlen(json) + length); - else + else { json = malloc(length); + memset(json, '\0', strlen(json)); + } strcat(json, item); if (i + 1 < nitems) json[length] = ','; -- cgit v1.2.3 From 3500f0a5cd000ff0d650ab716f92a35dcf1c1581 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:29:59 +0800 Subject: Fix the truncated items JSON By making sure it's always concatenated with a null terminated string, AND fix the index that gets null terminated in the end. --- anteraja.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index ad194f5..c64ac68 100644 --- a/anteraja.c +++ b/anteraja.c @@ -102,21 +102,22 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ char *json = NULL; 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") + 1; - char item[length]; + + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") + + strlen(","); + char item[length + 1]; sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), atoi(items[i][PRICE]), atoi(items[i][WEIGHT]) * 1000); if (json) - json = realloc(json, strlen(json) + length); + json = realloc(json, strlen(json) + length + 1); else { - json = malloc(length); + json = malloc(length + 1); memset(json, '\0', strlen(json)); } strcat(json, item); if (i + 1 < nitems) - json[length] = ','; + strcat(json, ","); else - json[length] = '\0'; + json[strlen(json)] = '\0'; } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) + strlen(sender_name) + strlen(sender_phone) + strlen(origin) -- cgit v1.2.3 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(-) (limited to 'anteraja.c') 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 From 4482f46ee073110c7c52d73bc8a8006437eba6f2 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 23:02:24 +0800 Subject: use_insurance turns out to be required This one follows the example given for the boolean JSON value, which is written as true or false. The response is error 500, internal server error. Despite the example, it says max length is 1 in the middle part of the doc, so maybe it should be written as 1 or 0. Will be tried next. --- anteraja.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 8b78815..65f7206 100644 --- a/anteraja.c +++ b/anteraja.c @@ -31,9 +31,11 @@ \"items\":[\ %s\ ],\ +\"use_insurance\":%s,\ \"declared_value\":%d\ }" #define ORDER_WEIGHT 5 +#define ORDER_INSURANCE strlen("false") #define ORDER_SUBTOTAL 9 #define ORDER_ITEM \ @@ -95,7 +97,7 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, void anteraja_order(const char *trx_id, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], double subtotal, + const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal, char **url, char **post) { *url = malloc(strlen(shipping.base) + strlen(ORDER_PATH) + 1); @@ -129,11 +131,11 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) + 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") - 2 * strlen("%d") + 1); + + strlen(destination) + strlen(receiver_address) + strlen(json) + ORDER_INSURANCE + + ORDER_SUBTOTAL - 13 * 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); + receiver_address, json, insurance ? "true" : "false", (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -- cgit v1.2.3 From dfa57092e26c242c2ab64dc1d029e5dff44eee67 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: Sat, 17 Jul 2021 11:05:19 +0800 Subject: Rename trx_id to order_number --- anteraja.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 65f7206..b49c07c 100644 --- a/anteraja.c +++ b/anteraja.c @@ -94,7 +94,7 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, return realsize; } -void anteraja_order(const char *trx_id, const char *service, const char *sender_name, +void anteraja_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, const char *receiver_name, const char *receiver_phone, const char *destination, const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal, @@ -128,13 +128,13 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ else json[strlen(json)] = '\0'; } - *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) + *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(order_number) + strlen(service) + 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_INSURANCE + ORDER_SUBTOTAL - 13 * 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, + sprintf(*post, ORDER_POST, prefix, order_number, service, (int)total_weight, sender_name, + sender_phone, origin, sender_address, receiver_name, receiver_phone, destination, receiver_address, json, insurance ? "true" : "false", (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -- cgit v1.2.3 From b999dd2f128bcf209c62d3419a559e70222e7b04 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: Sat, 17 Jul 2021 15:29:20 +0800 Subject: Postal codes for both sender & receiver --- anteraja.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index b49c07c..89239ab 100644 --- a/anteraja.c +++ b/anteraja.c @@ -20,13 +20,15 @@ \"name\":\"%s\",\ \"phone\":\"%s\",\ \"district\":\"%s\",\ -\"address\":\"%s\"\ +\"address\":\"%s\",\ +\"postcode\":\"%s\"\ },\ \"receiver\":{\ \"name\":\"%s\",\ \"phone\":\"%s\",\ \"district\":\"%s\",\ -\"address\":\"%s\"\ +\"address\":\"%s\",\ +\"postcode\":\"%s\"\ },\ \"items\":[\ %s\ @@ -96,9 +98,9 @@ size_t anteraja_services_handle(const char *contents, size_t size, size_t nmemb, void anteraja_order(const char *order_number, const char *service, const char *sender_name, const char *sender_phone, const char *origin, const char *sender_address, - const char *receiver_name, const char *receiver_phone, const char *destination, - const char *receiver_address, int nitems, char **items[], _Bool insurance, double subtotal, - char **url, char **post) + const char *sender_postal, const char *receiver_name, const char *receiver_phone, + const char *destination, const char *receiver_address, const char *receiver_postal, + int nitems, char **items[], _Bool insurance, double subtotal, char **url, char **post) { *url = malloc(strlen(shipping.base) + strlen(ORDER_PATH) + 1); sprintf(*url, "%s%s", shipping.base, ORDER_PATH); @@ -130,12 +132,14 @@ void anteraja_order(const char *order_number, const char *service, const char *s } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(order_number) + strlen(service) + 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_INSURANCE - + ORDER_SUBTOTAL - 13 * strlen("%s") - 2 * strlen("%d") + 1); + + strlen(sender_address) + strlen(sender_postal) + strlen(receiver_name) + + strlen(receiver_phone) + strlen(destination) + strlen(receiver_address) + + strlen(receiver_postal) + strlen(json) + ORDER_INSURANCE + ORDER_SUBTOTAL + - 15 * strlen("%s") - 2 * strlen("%d") + 1); sprintf(*post, ORDER_POST, prefix, order_number, service, (int)total_weight, sender_name, - sender_phone, origin, sender_address, receiver_name, receiver_phone, destination, - receiver_address, json, insurance ? "true" : "false", (int)subtotal); + sender_phone, origin, sender_address, sender_postal, receiver_name, receiver_phone, + destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", + (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -- cgit v1.2.3 From 738aa65629d7b552762cc825829ab5871cd99c4d 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: Sat, 17 Jul 2021 16:01:30 +0800 Subject: Tidy up a bit --- anteraja.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 89239ab..ffd739a 100644 --- a/anteraja.c +++ b/anteraja.c @@ -106,7 +106,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s sprintf(*url, "%s%s", shipping.base, ORDER_PATH); enum { SKU, QUANTITY, DESCRIPTION, PRICE, WEIGHT }; char *json = NULL; - double total_weight = 0; + 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") -- cgit v1.2.3 From f09a04b548164a6571234b9800e5e29e185595db 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: Sat, 17 Jul 2021 16:02:04 +0800 Subject: Minimum subtotal is 1000 for Anteraja --- anteraja.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index ffd739a..d743a54 100644 --- a/anteraja.c +++ b/anteraja.c @@ -139,7 +139,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s sprintf(*post, ORDER_POST, prefix, order_number, service, (int)total_weight, sender_name, sender_phone, origin, sender_address, sender_postal, receiver_name, receiver_phone, destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", - (int)subtotal); + subtotal < 1000.0 ? 1000 :(int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -- cgit v1.2.3 From 7b90ae5879f80ddb9f0a11aaa0fb63d16844fa59 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: Sat, 17 Jul 2021 16:06:00 +0800 Subject: Minimum unit price must be 1000 too for Anteraja --- anteraja.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index d743a54..14e32a0 100644 --- a/anteraja.c +++ b/anteraja.c @@ -115,8 +115,11 @@ void anteraja_order(const char *order_number, const char *service, const char *s 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]), (int)weight); + double price = atof(items[i][PRICE]); + if (price < 1000.0) + price = 1000.0; + sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), (int)price, + (int)weight); total_weight += weight; if (json) json = realloc(json, strlen(json) + length + 1); -- cgit v1.2.3 From 2419d61c5171479586ba606834dcbfaa138c9bb3 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: Sat, 17 Jul 2021 20:47:59 +0800 Subject: Rename waybill to tracking_number --- anteraja.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 14e32a0..0e5f2e7 100644 --- a/anteraja.c +++ b/anteraja.c @@ -146,14 +146,14 @@ void anteraja_order(const char *order_number, const char *service, const char *s curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **waybill) +size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **tracking_number) { size_t realsize = size * nmemb; handle(ORDER, contents, realsize, status_trail, (const char *[]){ "content", "waybill_no", NULL - }, NULL, waybill); + }, NULL, tracking_number); return realsize; } -- cgit v1.2.3 From e9e2ea78fa012e909aff1b0f0838c321e496fb67 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: Sat, 17 Jul 2021 22:42:03 +0800 Subject: nitems include item quantities that are more than 1 --- anteraja.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 0e5f2e7..a3bc1c3 100644 --- a/anteraja.c +++ b/anteraja.c @@ -112,15 +112,15 @@ void anteraja_order(const char *order_number, const char *service, const char *s + 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; + int quantity = atoi(items[i][QUANTITY]); double price = atof(items[i][PRICE]); if (price < 1000.0) price = 1000.0; - sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), (int)price, - (int)weight); + double weight = atof(items[i][WEIGHT]) * 1000.0; + if (weight < 100.0) + weight = 100.0; total_weight += weight; + sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], quantity, (int)price, (int)weight); if (json) json = realloc(json, strlen(json) + length + 1); else { @@ -132,6 +132,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s strcat(json, ","); else json[strlen(json)] = '\0'; + i += quantity - 1; } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(order_number) + strlen(service) + ORDER_WEIGHT + strlen(sender_name) + strlen(sender_phone) + strlen(origin) -- cgit v1.2.3 From ba54930b22390f34ca9a445753c5ed8acde17549 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: Sat, 17 Jul 2021 22:47:56 +0800 Subject: Correct previous commit --- anteraja.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index a3bc1c3..422b2d0 100644 --- a/anteraja.c +++ b/anteraja.c @@ -113,6 +113,8 @@ void anteraja_order(const char *order_number, const char *service, const char *s + strlen(","); char item[length + 1]; int quantity = atoi(items[i][QUANTITY]); + if (quantity > 1) + nitems -= quantity - 1; double price = atof(items[i][PRICE]); if (price < 1000.0) price = 1000.0; @@ -132,7 +134,6 @@ void anteraja_order(const char *order_number, const char *service, const char *s strcat(json, ","); else json[strlen(json)] = '\0'; - i += quantity - 1; } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(order_number) + strlen(service) + ORDER_WEIGHT + strlen(sender_name) + strlen(sender_phone) + strlen(origin) @@ -145,6 +146,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", subtotal < 1000.0 ? 1000 :(int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); + printf("POST: %s\n", *post); } size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **tracking_number) -- cgit v1.2.3 From b82ced0aaf6019f1893190d5fadfc1537f996dd9 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: Sat, 17 Jul 2021 22:54:16 +0800 Subject: Shouldn't have been added --- anteraja.c | 1 - 1 file changed, 1 deletion(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 422b2d0..9874d34 100644 --- a/anteraja.c +++ b/anteraja.c @@ -146,7 +146,6 @@ void anteraja_order(const char *order_number, const char *service, const char *s destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", subtotal < 1000.0 ? 1000 :(int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); - printf("POST: %s\n", *post); } size_t anteraja_order_handle(const char *contents, size_t size, size_t nmemb, char **tracking_number) -- cgit v1.2.3 From b167efeabf9b6292c6c7a15f7a83b48c2eb86a1b 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: Sun, 18 Jul 2021 10:53:32 +0800 Subject: Weight times quantity to make up total --- anteraja.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 9874d34..8c151ff 100644 --- a/anteraja.c +++ b/anteraja.c @@ -121,7 +121,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s double weight = atof(items[i][WEIGHT]) * 1000.0; if (weight < 100.0) weight = 100.0; - total_weight += weight; + total_weight += weight * quantity; sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], quantity, (int)price, (int)weight); if (json) json = realloc(json, strlen(json) + length + 1); -- cgit v1.2.3 From b53957a8fd25bcce8eb98f71b6e9e711b48ce605 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: Sun, 18 Jul 2021 11:59:55 +0800 Subject: Insurance is not determined by user's decision It depends on some weight threshhold. --- anteraja.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'anteraja.c') diff --git a/anteraja.c b/anteraja.c index 8c151ff..81b709a 100644 --- a/anteraja.c +++ b/anteraja.c @@ -100,7 +100,7 @@ void anteraja_order(const char *order_number, const char *service, const char *s const char *sender_phone, const char *origin, const char *sender_address, const char *sender_postal, const char *receiver_name, const char *receiver_phone, const char *destination, const char *receiver_address, const char *receiver_postal, - int nitems, char **items[], _Bool insurance, double subtotal, char **url, char **post) + int nitems, char **items[], double subtotal, char **url, char **post) { *url = malloc(strlen(shipping.base) + strlen(ORDER_PATH) + 1); sprintf(*url, "%s%s", shipping.base, ORDER_PATH); @@ -142,9 +142,9 @@ void anteraja_order(const char *order_number, const char *service, const char *s + strlen(receiver_postal) + strlen(json) + ORDER_INSURANCE + ORDER_SUBTOTAL - 15 * strlen("%s") - 2 * strlen("%d") + 1); sprintf(*post, ORDER_POST, prefix, order_number, service, (int)total_weight, sender_name, - sender_phone, origin, sender_address, sender_postal, receiver_name, receiver_phone, - destination, receiver_address, receiver_postal, json, insurance ? "true" : "false", - subtotal < 1000.0 ? 1000 :(int)subtotal); + sender_phone, origin, sender_address, sender_postal, receiver_name, receiver_phone, + destination, receiver_address, receiver_postal, json, + total_weight < 1000.0 ? "true" : "false", subtotal < 1000.0 ? 1000 : (int)subtotal); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, *post); } -- cgit v1.2.3