summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-07-24 13:15:24 -0300
committerSebastian <sebasjm@gmail.com>2022-07-24 13:15:24 -0300
commit1c35f6a85eba4eb91c0918fe7bcaf286c7e96a98 (patch)
tree1b90c8cef5d86b92e4ac96404af560cfb6d4fd16
parent90627094f1437806967eff7fdf701b3c4247d5d9 (diff)
downloadmch2022-1c35f6a85eba4eb91c0918fe7bcaf286c7e96a98.tar.gz
mch2022-1c35f6a85eba4eb91c0918fe7bcaf286c7e96a98.tar.bz2
mch2022-1c35f6a85eba4eb91c0918fe7bcaf286c7e96a98.zip
testing order status
-rw-r--r--main/main.c231
1 files changed, 195 insertions, 36 deletions
diff --git a/main/main.c b/main/main.c
index 494dc7f..7a4e848 100644
--- a/main/main.c
+++ b/main/main.c
@@ -76,13 +76,13 @@ void show_message(const char *message)
// Determine how the text dimensions so we can display it centered on
// screen.
- pax_vec1_t dims = pax_text_size(font, font->default_size / 2, message);
+ pax_vec1_t dims = pax_text_size(font, font->default_size, message);
// Draw the centered text.
pax_draw_text(
- &buf, // Buffer to draw to.
- 0xff000000, // color
- font, font->default_size / 2, // Font and size to use.
+ &buf, // Buffer to draw to.
+ 0xff000000, // color
+ font, font->default_size, // Font and size to use.
// Position (top left corner) of the app.
(buf.width - dims.x) / 2.0,
(buf.height - dims.y) / 2.0,
@@ -93,6 +93,24 @@ void show_message(const char *message)
disp_flush();
}
+int read_key()
+{
+ rp2040_input_message_t buttonMessage = {0};
+ if (xQueueReceive(buttonQueue, &buttonMessage, 0) == pdTRUE)
+ {
+ if (buttonMessage.state)
+ {
+ if (buttonMessage.input == RP2040_INPUT_BUTTON_START)
+ {
+ exit_to_launcher();
+ }
+
+ return buttonMessage.input;
+ }
+ }
+ return 0;
+}
+
int wait_any_key_or_exit()
{
while (1)
@@ -119,7 +137,68 @@ int wait_any_key_or_exit()
#define TALER_PAYMENT_URI "taler://pay/" TALER_MERCHANT_HOST "/instances/" TALER_MERCHANT_NAME "/"
#define TALER_PAYMENT_TEMPLATE TALER_PAYMENT_URI "%s/"
-bool create_new_order(float amount, char *token, char **paymentURI)
+#define TALER_CREATE_ORDER_URL_TEMPLATE "/instances/" TALER_MERCHANT_NAME "/private/orders/%s"
+
+bool get_order_status(const char *token, const char *orderId, char **order_status)
+{
+ char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
+ int token_len = strlen(token);
+
+ int order_status_url_size = sizeof(TALER_CREATE_ORDER_URL_TEMPLATE) + strlen(orderId) + 1;
+ char *order_status_url = (char *)malloc(order_status_url_size);
+ snprintf(order_status_url, order_status_url_size, TALER_CREATE_ORDER_URL_TEMPLATE, orderId);
+
+ esp_http_client_config_t config = {
+ .port = 443,
+ .host = TALER_MERCHANT_HOST,
+ .path = order_status_url,
+ .keep_alive_enable = true,
+ .method = HTTP_METHOD_GET,
+ .transport_type = HTTP_TRANSPORT_OVER_SSL,
+ .cert_pem = inet_sec_root_cert_pem_start,
+ .user_data = local_response_buffer,
+ .event_handler = _http_event_handler,
+ };
+ esp_http_client_handle_t client = esp_http_client_init(&config);
+
+ {
+ char *auth_buffer = (char *)malloc(sizeof("Bearer secret-token:") + token_len + 1);
+ sprintf(auth_buffer, "Bearer secret-token:%s", token);
+ esp_http_client_set_header(client, "Authorization", auth_buffer);
+ free(auth_buffer);
+ }
+
+ ESP_LOGI(TAG, "querying order status...");
+ esp_err_t err = esp_http_client_perform(client);
+
+ if (err == ESP_OK)
+ {
+ ESP_LOGI(TAG, "query ok. buffer has %s bytes", local_response_buffer);
+ cJSON *response_json = cJSON_Parse(local_response_buffer);
+ cJSON *orderStatus = cJSON_GetObjectItemCaseSensitive(response_json, "order_status");
+ show_message(orderStatus->valuestring);
+
+ int order_len = strlen(orderStatus->valuestring);
+ *order_status = (char *)malloc(order_len + 1);
+ strncpy(*order_status, orderStatus->valuestring, order_len);
+ (*order_status)[order_len] = '\0';
+ ESP_LOGI(TAG, "order status %s", *order_status);
+ cJSON_Delete(response_json);
+ }
+ else
+ {
+ char buffer[200];
+ snprintf(buffer, sizeof(buffer), "failed = %d, %s",
+ esp_http_client_get_status_code(client),
+ esp_err_to_name(err));
+ show_message(buffer);
+ }
+
+ esp_http_client_cleanup(client);
+ return err == ESP_OK;
+}
+
+bool create_new_order(float amount, const char *token, char **orderId_out)
{
char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
int token_len = strlen(token);
@@ -149,19 +228,20 @@ bool create_new_order(float amount, char *token, char **paymentURI)
snprintf(post_data, sizeof(post_data), "{\"order\":{\"summary\":\"sold in mch2022\",\"amount\":\"ARS:%3.2f\"},\"create_token\":false}", amount);
esp_http_client_set_post_field(client, post_data, strlen(post_data));
- ESP_LOGI(TAG, "querying...");
+ ESP_LOGI(TAG, "creating order...");
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK)
{
- ESP_LOGI(TAG, "query ok. buffer has %s bytes", local_response_buffer);
+ ESP_LOGI(TAG, "query ok. buffer has %s", local_response_buffer);
cJSON *response_json = cJSON_Parse(local_response_buffer);
cJSON *orderId = cJSON_GetObjectItemCaseSensitive(response_json, "order_id");
show_message(orderId->valuestring);
int order_len = strlen(orderId->valuestring);
- *paymentURI = (char *)malloc(sizeof(TALER_PAYMENT_URI) + order_len + /*last slash*/ 1 + /*0*/ 1);
- sprintf(*paymentURI, TALER_PAYMENT_TEMPLATE, orderId->valuestring);
- ESP_LOGI(TAG, "payment uri %s", *paymentURI);
+ *orderId_out = (char *)malloc(order_len + 1);
+ strncpy(*orderId_out, orderId->valuestring, order_len);
+ (*orderId_out)[order_len] = '\0';
+ ESP_LOGI(TAG, "order id '%s' len %d", *orderId_out, order_len);
cJSON_Delete(response_json);
}
else
@@ -189,15 +269,15 @@ void esp_qrcode_print_screen(esp_qrcode_handle_t qrcode)
pax_background(&buf, 0xb0FFFFFF); // black background
const char *message = "Use the GNU Taler Wallet to pay\n or any key to cancel.";
- pax_vec1_t dims = pax_text_size(font, font->default_size / 2, message);
+ pax_vec1_t dims = pax_text_size(font, font->default_size, message);
// 240y * 320x
// Draw the centered text.
pax_draw_text(
- &buf, // Buffer to draw to.
- 0xff000000, // color
- font, font->default_size / 2, // Font and size to use.
+ &buf, // Buffer to draw to.
+ 0xff000000, // color
+ font, font->default_size, // Font and size to use.
// Position (top left corner) of the app.
(buf.width - dims.x) / 2.0,
5,
@@ -252,50 +332,62 @@ void show_menu()
int status_buffer_size = sizeof(MENU_TEMPLATE) + strlen(TALER_MERCHANT_NAME) + strlen(LAST_STATUS) + strlen(WIFI_STATUS);
status_buffer = (char *)malloc(status_buffer_size);
snprintf(status_buffer, status_buffer_size, MENU_TEMPLATE, TALER_MERCHANT_NAME, WIFI_STATUS, LAST_STATUS, NEXT_ORDER_PRICE);
- pax_draw_text(&buf, 0xFF000000, font, 18, 20, 60, status_buffer);
+ pax_draw_text(&buf, 0xFF000000, font, 18, 20, 80, status_buffer);
pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅰 create order 🅱 exit");
disp_flush();
}
+void setup_wifi()
+{
+ show_message("Connecting to MCH2022 wifi");
+ ESP_LOGI(TAG, "Initializing wifi...");
+
+ wifi_init();
+ bool connected = false;
+ while (!connected)
+ {
+ connected = wifi_connect_ent("MCH2022", "badge", "badge", "badge", ESP_EAP_TTLS_PHASE2_PAP, 3);
+ if (!connected)
+ {
+ ESP_LOGI(TAG, "could not connect, trying again...");
+ show_message("go near an MCH2022 access point");
+ }
+ }
+ ESP_LOGI(TAG, "Connected.");
+}
+
void app_main()
{
ESP_LOGI(TAG, "Starting...");
initialize_things();
- show_message("Connecting to MCH2022");
- ESP_LOGI(TAG, "Initializing wifi...");
- // wifi_init();
-
- // bool connected = false;
- // while (!connected)
- // {
- // connected = wifi_connect_ent("MCH2022", "badge", "badge", "badge", ESP_EAP_TTLS_PHASE2_PAP, 3);
- // if (!connected)
- // {
- // ESP_LOGI(TAG, "could not connect, trying again...");
- // show_message("go near an MCH2022 access point");
- // }
- // }
- ESP_LOGI(TAG, "Connected.");
- show_message("connected!");
+ setup_wifi();
while (1)
{
show_menu();
- ESP_LOGI(TAG, "order generated!");
const int key = wait_any_key_or_exit();
if (key == RP2040_INPUT_BUTTON_ACCEPT)
{
char *orderId = NULL;
- bool ok = create_new_order(NEXT_ORDER_PRICE, "305a16ce7cc2e2793060e39b9210a700", &orderId);
+ char creating_order_message[200];
+ snprintf(creating_order_message, 200, "Creating order for ARS:%3.2f ...", NEXT_ORDER_PRICE);
+ show_message(creating_order_message);
+
+ bool order_created = create_new_order(NEXT_ORDER_PRICE, "305a16ce7cc2e2793060e39b9210a700", &orderId);
- if (ok)
+ if (order_created)
{
+ char *paymentURI = NULL;
+ int order_len = strlen(orderId);
+ paymentURI = (char *)malloc(sizeof(TALER_PAYMENT_URI) + order_len + /*last slash*/ 1 + /*0*/ 1);
+ sprintf(paymentURI, TALER_PAYMENT_TEMPLATE, orderId);
+
LAST_STATUS = "generated, not payed";
esp_qrcode_config_t cfg = {
.max_qrcode_version = 10,
@@ -303,28 +395,95 @@ void app_main()
.qrcode_ecc_level = ESP_QRCODE_ECC_LOW,
};
- esp_qrcode_generate(&cfg, orderId);
+ esp_qrcode_generate(&cfg, paymentURI);
+ free(paymentURI);
}
else
{
LAST_STATUS = "error creating the order";
+ continue;
}
- // free(orderId);
+
+ ESP_LOGI(TAG, "user is seeing qr code now");
+
+ bool order_is_pay_or_cancel = false;
+ bool qr_scanned = false;
+
+ int tick = 0;
+ while (!order_is_pay_or_cancel)
+ {
+ char *order_status = NULL;
+
+ ESP_LOGI(TAG, "====== TESTING ORDER %s", orderId);
+
+ bool query_ok = get_order_status("305a16ce7cc2e2793060e39b9210a700", orderId, &order_status);
+
+ if (!query_ok)
+ {
+ ESP_LOGI(TAG, "query went wrong");
+ LAST_STATUS = "error querying order, try again";
+ break;
+ }
+ ESP_LOGI(TAG, "query status: %s", order_status);
+
+ char *tick_str[] = {
+ ".",
+ "..",
+ "...",
+ "....",
+ ".....",
+ };
+ char status_buf[200];
+ snprintf(status_buf, 200, "%s %s", order_status, tick_str[tick]);
+ tick++;
+ if (tick > 3)
+ {
+ tick = 0;
+ }
+ LAST_STATUS = status_buf;
+
+ ESP_LOGI(TAG, "showing menu");
+ show_menu();
+
+ if (!qr_scanned && strcmp(order_status, "claimed") == 0)
+ {
+ show_message("payment claimed! waiting to be paid...");
+ }
+
+ if (strcmp(order_status, "paid") == 0)
+ {
+ order_is_pay_or_cancel = true;
+ }
+ int last_key = read_key();
+ ESP_LOGI(TAG, "read key: %d", last_key);
+ if (last_key == RP2040_INPUT_BUTTON_BACK)
+ {
+ order_is_pay_or_cancel = true;
+ }
+
+ free(order_status);
+ }
+
+ free(orderId);
}
if (key == RP2040_INPUT_JOYSTICK_UP)
{
+ ESP_LOGI(TAG, "inc price 0.1");
NEXT_ORDER_PRICE += 0.1;
}
if (key == RP2040_INPUT_JOYSTICK_DOWN)
{
+ ESP_LOGI(TAG, "dec price 0.1");
NEXT_ORDER_PRICE -= 0.1;
}
if (key == RP2040_INPUT_JOYSTICK_LEFT)
{
+ ESP_LOGI(TAG, "inc price 1");
NEXT_ORDER_PRICE += 1;
}
if (key == RP2040_INPUT_JOYSTICK_RIGHT)
{
+ ESP_LOGI(TAG, "dec price 1");
NEXT_ORDER_PRICE -= 1;
}
}