1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#include "shipping.h"
#include "handler.h"
#define SERVICES_PATH "serviceRates"
#define SERVICES_POST \
"{\
\"origin\":\"%s\",\
\"destination\":\"%s\",\
\"weight\":%d\
}"
#define SERVICES_WEIGHT 5
#define ORDER_PATH "order"
#define ORDER_POST \
"{\
\"booking_id\":\"%s-%s\",\
\"service_code\":\"%s\",\
\"parcel_total_weight\":%d,\
\"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_WEIGHT 5
#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[])
{
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");
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)
{
*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) + 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);
}
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 *[]){
"content",
"services",
NULL
}, (const char *[]){
"product_code",
"product_name",
"etd",
"rates"
}, 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;
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]), (int)weight);
total_weight += weight;
if (json)
json = realloc(json, strlen(json) + length + 1);
else {
json = malloc(length + 1);
memset(json, '\0', strlen(json));
}
strcat(json, item);
if (i + 1 < nitems)
strcat(json, ",");
else
json[strlen(json)] = '\0';
}
*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);
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);
}
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);
}
|