diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <prabowo@darapsa.org> | 2022-10-05 18:51:24 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <prabowo@darapsa.org> | 2022-10-05 18:51:24 +0800 |
commit | bdc0d3aa58f7dcf5861f5d360a44d9dea78da6cf (patch) | |
tree | bf9fe06e1cb02a0eee3302c654a4db570867fd07 | |
parent | 1c7e8d93fbb4f131250f737b11a8ad99df167ea8 (diff) |
Sandbox/production-ness is implied by the key prefix
-rw-r--r-- | midtrans.c | 14 | ||||
-rw-r--r-- | midtrans.h | 2 |
2 files changed, 9 insertions, 7 deletions
@@ -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; @@ -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(); |