summaryrefslogtreecommitdiff
path: root/midtrans.c
diff options
context:
space:
mode:
Diffstat (limited to 'midtrans.c')
-rw-r--r--midtrans.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/midtrans.c b/midtrans.c
index 5005581..c51ce5b 100644
--- a/midtrans.c
+++ b/midtrans.c
@@ -11,6 +11,7 @@
static _Bool sandbox;
static char *base_url;
static CURL *curl;
+static struct curl_slist *slist;
void midtrans_init(_Bool production, const char *api_key, const char *cainfo)
{
@@ -20,14 +21,17 @@ void midtrans_init(_Bool production, const char *api_key, const char *cainfo)
base_url = malloc(strlen(url_tmpl) - strlen ("%s")
+ sandbox * strlen(infix) + 1);
sprintf(base_url, url_tmpl, production ? "" : infix);
+
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
+
static const char *basic_tmpl = "%s:";
const size_t basic_len = strlen(basic_tmpl) - strlen("%s")
+ strlen(api_key);
char basic[basic_len + 1];
sprintf(basic, basic_tmpl, api_key);
+
BIO *b64 = BIO_new(BIO_f_base64());
BIO *mem = BIO_new(BIO_s_mem());
BIO_push(b64, mem);
@@ -36,14 +40,16 @@ void midtrans_init(_Bool production, const char *api_key, const char *cainfo)
long base64_len = BIO_get_mem_data(b64, &pp);
char base64[base64_len + 1];
strlcpy(base64, pp, base64_len + 1);
+ BIO_free_all(b64);
+
static const char *hdr_tmpl = "Authorization: Basic %s";
char auth[strlen(hdr_tmpl) - strlen("%s") + base64_len + 1];
sprintf(auth, hdr_tmpl, base64);
- BIO_free_all(b64);
- struct curl_slist *slist = curl_slist_append(NULL, auth);
+ slist = curl_slist_append(NULL, auth);
slist = curl_slist_append(slist, "Accept: application/json");
slist = curl_slist_append(slist, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
+
if (cainfo)
curl_easy_setopt(curl, CURLOPT_CAINFO, cainfo);
}
@@ -61,6 +67,7 @@ void midtrans_status(const char *order_id)
void midtrans_cleanup()
{
free(base_url);
+ curl_slist_free_all(slist);
curl_easy_cleanup(curl);
curl_global_cleanup();
}