summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <prabowo@darapsa.org>2022-10-05 18:51:24 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <prabowo@darapsa.org>2022-10-05 18:51:24 +0800
commitbdc0d3aa58f7dcf5861f5d360a44d9dea78da6cf (patch)
treebf9fe06e1cb02a0eee3302c654a4db570867fd07
parent1c7e8d93fbb4f131250f737b11a8ad99df167ea8 (diff)
Sandbox/production-ness is implied by the key prefix
-rw-r--r--midtrans.c14
-rw-r--r--midtrans.h2
2 files changed, 9 insertions, 7 deletions
diff --git a/midtrans.c b/midtrans.c
index e3f3a77..c1d7f9e 100644
--- a/midtrans.c
+++ b/midtrans.c
@@ -8,18 +8,20 @@
#define ORDER_ID "SANDBOX-G710367688-806"
-static _Bool sandbox;
+static _Bool production = 0;
static char *base_url;
static CURL *curl;
static struct curl_slist *slist;
-void midtrans_init(_Bool production, const char *api_key, const char *cainfo)
+void midtrans_init(const char *api_key, const char *cainfo)
{
- sandbox = !production;
+ static const char *prefix = "SB-";
+ if (strncmp(api_key, prefix, strlen(prefix)))
+ production = 1;
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);
+ + !production * strlen(infix) + 1);
sprintf(base_url, url_tmpl, production ? "" : infix);
curl_global_init(CURL_GLOBAL_SSL);
@@ -71,8 +73,8 @@ 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);
+ + (production ? strlen(order_id) : strlen(ORDER_ID)) + 1];
+ sprintf(url, tmpl, base_url, production ? order_id : ORDER_ID);
curl_easy_setopt(curl, CURLOPT_URL, url);
char *res = NULL;
diff --git a/midtrans.h b/midtrans.h
index bc73ec0..9afce42 100644
--- a/midtrans.h
+++ b/midtrans.h
@@ -5,7 +5,7 @@
extern "C" {
#endif
-void midtrans_init(_Bool production, const char *api_key, const char *cainfo);
+void midtrans_init(const char *api_key, const char *cainfo);
void midtrans_status(const char *order_id);
void midtrans_cleanup();