diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-16 11:02:02 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-16 11:02:02 +0800 |
commit | 6f78942176d5909349305db37b5424a1c6ceccef (patch) | |
tree | 215a441aaca7856ef2000d6f40a838d0c02f89ee /anteraja.c | |
parent | d1b9c27a403fb2ac9e69e82ebea32722c6795daf (diff) |
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.
Diffstat (limited to 'anteraja.c')
-rw-r--r-- | anteraja.c | 94 |
1 files changed, 94 insertions, 0 deletions
@@ -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); +} |