From 448ecc4c2c36004bc444e808a8e77d0b804fdb67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Thu, 6 Oct 2022 20:48:13 +0800 Subject: Custom fields --- midtrans.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'midtrans.c') diff --git a/midtrans.c b/midtrans.c index 11954c6..27325c6 100644 --- a/midtrans.c +++ b/midtrans.c @@ -131,10 +131,28 @@ void midtrans_charge(enum midtrans_payment type, void *object, sprintf(payment, payment_tmpl, banktransfer->bank); break; } + size_t i = 0; + size_t fields_len = 0; + char *fields = NULL; + static const char *field_tmpl = "\"%s\": \"%s\","; + const size_t field_static_len = strlen(field_tmpl) - strlen("%s") * 2; + while (i < 5 && custom_fields[i] && custom_fields[i + 1]) { + size_t field_len = field_static_len + strlen(custom_fields[i]) + + strlen(custom_fields[i + 1]); + char field[field_len + 1]; + sprintf(field, field_tmpl, custom_fields[i], + custom_fields[i + 1]); + fields_len += field_len; + fields = realloc(fields, fields_len + field_len + 1); + strcpy(&fields[fields_len], field); + fields_len += field_len; + i += 2; + } static const char *post_tmpl = "{\n" "\t\"payment_type\": \"%s" "\t}," + "%s" "\t\"transaction_details\": {" "\t\t\"order_id\": \"%s\"," "\t\t\"gross_amount\": %ld" @@ -144,10 +162,10 @@ void midtrans_charge(enum midtrans_payment type, void *object, size_t gross_amount_len = 1; while ((gross_amount /= 10)) gross_amount_len++; - char post[strlen(post_tmpl) - strlen("%s") * 2 - strlen("%ld") - + payment_len + strlen(transaction->order_id) + char post[strlen(post_tmpl) - strlen("%s") * 3 - strlen("%ld") + + payment_len + fields_len + strlen(transaction->order_id) + gross_amount_len + 1]; - sprintf(post, post_tmpl, payment, transaction->order_id, + sprintf(post, post_tmpl, payment, fields, transaction->order_id, transaction->gross_amount); free(payment); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); -- cgit v1.2.3