summaryrefslogtreecommitdiff
path: root/midtrans.c
diff options
context:
space:
mode:
Diffstat (limited to 'midtrans.c')
-rw-r--r--midtrans.c45
1 files changed, 37 insertions, 8 deletions
diff --git a/midtrans.c b/midtrans.c
index 932820d..7fce842 100644
--- a/midtrans.c
+++ b/midtrans.c
@@ -8,26 +8,55 @@
#include <json.h>
#include "midtrans.h"
+#define ORDER_ID "SANDBOX-G710367688-806"
+
+static _Bool sandbox;
+static char *base_url;
#ifndef __EMSCRIPTEN__
-static char *cainfo = NULL;
+static CURL *curl;
#endif
-void midtrans_init(const char *certificate)
+void midtrans_init(_Bool production, const char *api_key, const char *cainfo)
{
+ sandbox = !production;
+ static const char *url_tmpl = "https://api.%smidtrans.com/v2/";
+ static const char *infix = "sandbox.";
+ base_url = malloc(strlen(url_tmpl) - strlen ("%s")
+ + sandbox * strlen(infix) + 1);
+ sprintf(base_url, url_tmpl, production ? "" : infix);
#ifndef __EMSCRIPTEN__
curl_global_init(CURL_GLOBAL_SSL);
- if (certificate) {
- cainfo = malloc(strlen(certificate) + 1);
- strcpy(cainfo, certificate);
- }
+ curl = curl_easy_init();
+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
+ struct curl_slist *list
+ = curl_slist_append(NULL, "Accept: application/json");
+ list = curl_slist_append(list, "Content-Type: application/json");
+ static const char *hdr_tmpl = "Authorization: %s";
+ char auth[strlen(hdr_tmpl) - strlen("%s") + strlen(api_key) + 1];
+ sprintf(auth, hdr_tmpl, api_key);
+ list = curl_slist_append(list, auth);
+ if (cainfo)
+ curl_easy_setopt(curl, CURLOPT_CAINFO, cainfo);
+#endif
+}
+
+void midtrans_status(const char *order_id)
+{
+ static const char *tmpl = "%s%s/status";
+ char url[strlen(tmpl) - strlen("%s") * 2 + strlen(base_url)
+ + (sandbox ? strlen(ORDER_ID) : strlen(order_id)) + 1];
+ sprintf(url, tmpl, base_url, sandbox ? ORDER_ID : order_id);
+#ifndef __EMSCRIPTEN__
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ curl_easy_perform(curl);
#endif
}
void midtrans_cleanup()
{
+ free(base_url);
#ifndef __EMSCRIPTEN__
- if (cainfo)
- free(cainfo);
+ curl_easy_cleanup(curl);
curl_global_cleanup();
#endif
}