diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2022-10-31 17:52:47 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2022-10-31 17:52:47 +0800 |
commit | d3d482be762c6baa8b8f90366749b38f8e1d5476 (patch) | |
tree | 9433b0d801a41912444955d213d38a5a46208285 /midtrans.c | |
parent | 3ab105554ff49b0e40c6a982f3a8509737efc40b (diff) |
Status & charge return strings
The most likely needed info like VA number.
Diffstat (limited to 'midtrans.c')
-rw-r--r-- | midtrans.c | 59 |
1 files changed, 42 insertions, 17 deletions
@@ -81,19 +81,7 @@ void midtrans_init(const char *api_key, char *pem) })); } -static void *request(void *arg) -{ - struct response res = { 0, NULL }; - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res); - curl_easy_perform(curl); -#ifdef DEBUG - printf("%s\n", res.data); -#endif - free(res.data); - return NULL; -} - -void midtrans_status(const char *order_id) +char *midtrans_status(const char *order_id) { static const char *tmpl = "%s%s/status"; char url[strlen(tmpl) - strlen("%s") * 2 + strlen(base_url) @@ -101,11 +89,29 @@ void midtrans_status(const char *order_id) sprintf(url, tmpl, base_url, order_id); curl_easy_setopt(curl, CURLOPT_URL, url); - request(NULL); + struct response res = { 0, NULL }; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res); + curl_easy_perform(curl); + + json_tokener *tokener = json_tokener_new(); + json_object *object = json_tokener_parse_ex(tokener, res.data, + res.size); + free(res.data); + + json_object *transaction_status = NULL; + json_object_object_get_ex(object, "transaction_status", + &transaction_status); + const char *string = json_object_get_string(transaction_status); + char *status = malloc(strlen(string) + 1); + strcpy(status, string); + json_tokener_free(tokener); + + return status; } -void midtrans_charge_banktransfer(struct midtrans_banktransfer *banktransfer, - struct midtrans_transaction *transaction, char *custom_fields[]) +char *midtrans_charge_banktransfer(struct midtrans_banktransfer + *banktransfer, struct midtrans_transaction *transaction, + char *custom_fields[]) { static const char *url_tmpl = "%scharge"; char url[strlen(url_tmpl) - strlen("%s") + strlen(base_url) + 1]; @@ -172,7 +178,26 @@ void midtrans_charge_banktransfer(struct midtrans_banktransfer *banktransfer, free(payment); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); - request(NULL); + struct response res = { 0, NULL }; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res); + curl_easy_perform(curl); + + json_tokener *tokener = json_tokener_new(); + json_object *status = json_tokener_parse_ex(tokener, res.data, + res.size); + free(res.data); + + json_object *va_numbers = NULL; + json_object_object_get_ex(status, "va_numbers", &va_numbers); + json_object *object = json_object_array_get_idx(va_numbers, 0); + json_object *number = NULL; + json_object_object_get_ex(object, "va_number", &number); + const char *string = json_object_get_string(number); + char *virtualaccount_number = malloc(strlen(string) + 1); + strcpy(virtualaccount_number, string); + json_tokener_free(tokener); + + return virtualaccount_number; } void midtrans_cleanup() |