summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-07-24 14:33:50 -0300
committerSebastian <sebasjm@gmail.com>2022-07-24 14:33:50 -0300
commit0344dbd3fea94f29009d599279d5797ed2add452 (patch)
tree44f14c65fc777c34c4618f8434f1911c43ba2f44
parent1c35f6a85eba4eb91c0918fe7bcaf286c7e96a98 (diff)
downloadmch2022-0344dbd3fea94f29009d599279d5797ed2add452.tar.gz
mch2022-0344dbd3fea94f29009d599279d5797ed2add452.tar.bz2
mch2022-0344dbd3fea94f29009d599279d5797ed2add452.zip
retry when there is an error
-rw-r--r--Makefile2
-rw-r--r--README.md8
-rw-r--r--main/main.c304
3 files changed, 196 insertions, 118 deletions
diff --git a/Makefile b/Makefile
index 99ea022..c7e84f0 100644
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,7 @@ build:
source "$(IDF_PATH)/export.sh" && idf.py build
install: build
- python3 tools/webusb_push.py "Template App" build/main.bin --run
+ python3 tools/webusb_push.py "GNU Taler Merchant" build/main.bin --run
monitor:
source "$(IDF_PATH)/export.sh" && idf.py monitor -p $(PORT)
diff --git a/README.md b/README.md
index 96e7d54..2cf9c0f 100644
--- a/README.md
+++ b/README.md
@@ -52,11 +52,17 @@ Actions
- button A: create payment
- button B: cancel payment
-###Possible next steps:
+### Possible next steps:
- refund the last order
- show merchant logo and email
- show QR code with and URL to the merchant catalog (?)
+- read config from file:
+- - number digit amount
+- - backend uri
+- - auth token
+- - merchant name
+- - order summary
---
diff --git a/main/main.c b/main/main.c
index 7a4e848..aa6773b 100644
--- a/main/main.c
+++ b/main/main.c
@@ -100,7 +100,7 @@ int read_key()
{
if (buttonMessage.state)
{
- if (buttonMessage.input == RP2040_INPUT_BUTTON_START)
+ if (buttonMessage.input == RP2040_INPUT_BUTTON_HOME)
{
exit_to_launcher();
}
@@ -120,7 +120,7 @@ int wait_any_key_or_exit()
{
if (buttonMessage.state)
{
- if (buttonMessage.input == RP2040_INPUT_BUTTON_START)
+ if (buttonMessage.input == RP2040_INPUT_BUTTON_HOME)
{
exit_to_launcher();
}
@@ -176,7 +176,6 @@ bool get_order_status(const char *token, const char *orderId, char **order_statu
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);
@@ -198,13 +197,9 @@ bool get_order_status(const char *token, const char *orderId, char **order_statu
return err == ESP_OK;
}
-bool create_new_order(float amount, const char *token, char **orderId_out)
+bool post_new_order(float amount, const char *token, char **orderId_out)
{
- char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
- int token_len = strlen(token);
-
- char post_data[128];
-
+ char newOrderBuffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
esp_http_client_config_t config = {
.port = 443,
.host = TALER_MERCHANT_HOST,
@@ -213,30 +208,34 @@ bool create_new_order(float amount, const char *token, char **orderId_out)
.method = HTTP_METHOD_POST,
.transport_type = HTTP_TRANSPORT_OVER_SSL,
.cert_pem = inet_sec_root_cert_pem_start,
- .user_data = local_response_buffer,
+ .user_data = newOrderBuffer,
.event_handler = _http_event_handler,
};
- esp_http_client_handle_t client = esp_http_client_init(&config);
- esp_http_client_set_header(client, "Content-Type", "application/json");
+ esp_http_client_handle_t newOrderClient = esp_http_client_init(&config);
+
+ int token_len = strlen(token);
+
+ char post_data[128];
+
+ esp_http_client_set_header(newOrderClient, "Content-Type", "application/json");
{
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);
+ esp_http_client_set_header(newOrderClient, "Authorization", auth_buffer);
free(auth_buffer);
}
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_http_client_set_post_field(newOrderClient, post_data, strlen(post_data));
ESP_LOGI(TAG, "creating order...");
- esp_err_t err = esp_http_client_perform(client);
+ esp_err_t err = esp_http_client_perform(newOrderClient);
if (err == ESP_OK)
{
- ESP_LOGI(TAG, "query ok. buffer has %s", local_response_buffer);
- cJSON *response_json = cJSON_Parse(local_response_buffer);
+ ESP_LOGI(TAG, "query ok. buffer has %s", newOrderBuffer);
+ cJSON *response_json = cJSON_Parse(newOrderBuffer);
cJSON *orderId = cJSON_GetObjectItemCaseSensitive(response_json, "order_id");
- show_message(orderId->valuestring);
int order_len = strlen(orderId->valuestring);
*orderId_out = (char *)malloc(order_len + 1);
strncpy(*orderId_out, orderId->valuestring, order_len);
@@ -248,12 +247,12 @@ bool create_new_order(float amount, const char *token, char **orderId_out)
{
char buffer[200];
snprintf(buffer, sizeof(buffer), "failed = %d, %s",
- esp_http_client_get_status_code(client),
+ esp_http_client_get_status_code(newOrderClient),
esp_err_to_name(err));
show_message(buffer);
}
- esp_http_client_cleanup(client);
+ esp_http_client_cleanup(newOrderClient);
return err == ESP_OK;
}
@@ -315,12 +314,18 @@ void esp_qrcode_print_screen(esp_qrcode_handle_t qrcode)
disp_flush();
}
-char *LAST_STATUS = "ok";
-char *WIFI_STATUS = "connected";
+char LAST_ORDER[200] = "";
+
+char LAST_STATUS[200] = "";
+char WIFI_STATUS[200] = "connected";
float NEXT_ORDER_PRICE = 3.2;
#define MENU_TEMPLATE "Merchant name: %s\nWifi status: %s\nLast order: %s\nNext order price: %3.2f\n"
-void show_menu()
+#define SECOND_OPTION_EXIT 0
+#define SECOND_OPTION_RETRY 1
+#define SECOND_OPTION_REFUND 2
+
+void show_menu(int second_option)
{
pax_background(&buf, 0xb0FFFFFF); // black background
// pax_draw_image(&buf, &icon_png_start, 10, 10);
@@ -334,7 +339,18 @@ void show_menu()
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, 80, status_buffer);
- pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅰 create order 🅱 exit");
+ if (second_option == SECOND_OPTION_EXIT)
+ {
+ pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅰 create order 🅱 exit");
+ }
+ if (second_option == SECOND_OPTION_RETRY)
+ {
+ pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅰 create order 🅱 retry");
+ }
+ if (second_option == SECOND_OPTION_REFUND)
+ {
+ pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅰 create order 🅱 refund");
+ }
disp_flush();
}
@@ -347,7 +363,7 @@ void setup_wifi()
bool connected = false;
while (!connected)
{
- connected = wifi_connect_ent("MCH2022", "badge", "badge", "badge", ESP_EAP_TTLS_PHASE2_PAP, 3);
+ connected = wifi_connect_ent("MCH2022", "badge", "badge", "badge", ESP_EAP_TTLS_PHASE2_PAP, 30);
if (!connected)
{
ESP_LOGI(TAG, "could not connect, trying again...");
@@ -357,6 +373,102 @@ void setup_wifi()
ESP_LOGI(TAG, "Connected.");
}
+bool create_new_order()
+{
+ char *orderId = NULL;
+
+ 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 = post_new_order(NEXT_ORDER_PRICE, "305a16ce7cc2e2793060e39b9210a700", &orderId);
+
+ 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);
+
+ strcpy(LAST_ORDER, orderId);
+ LAST_ORDER[order_len] = '\0';
+
+ esp_qrcode_config_t cfg = {
+ .max_qrcode_version = 10,
+ .display_func = esp_qrcode_print_screen,
+ .qrcode_ecc_level = ESP_QRCODE_ECC_LOW,
+ };
+
+ esp_qrcode_generate(&cfg, paymentURI);
+ free(paymentURI);
+ free(orderId);
+ return true;
+ }
+ else
+ {
+ strcpy(LAST_STATUS, "error creating the order");
+ return false;
+ }
+}
+bool query_order_until_paid()
+{
+ bool order_is_pay_or_cancel = false;
+ bool qr_scanned = false;
+ bool paid = false;
+
+ int tick = 0;
+ while (!order_is_pay_or_cancel)
+ {
+ char *order_status = NULL;
+
+ ESP_LOGI(TAG, "====== TESTING ORDER %s", LAST_ORDER);
+
+ bool query_ok = get_order_status("305a16ce7cc2e2793060e39b9210a700", LAST_ORDER, &order_status);
+
+ if (!query_ok)
+ {
+ ESP_LOGI(TAG, "query went wrong");
+ strcpy(LAST_STATUS, "error getting status");
+ return false;
+ }
+ ESP_LOGI(TAG, "query status: %s", order_status);
+
+ char *tick_str[] = {
+ ".",
+ "..",
+ "...",
+ "....",
+ };
+ snprintf(LAST_STATUS, 200, "%s %s", order_status, tick_str[tick]);
+ tick++;
+ if (tick > 3)
+ {
+ tick = 0;
+ }
+
+ if (!qr_scanned && strcmp(order_status, "claimed") == 0)
+ {
+ show_message("payment claimed!\nwaiting to be paid...");
+ qr_scanned = true;
+ }
+
+ if (strcmp(order_status, "paid") == 0)
+ {
+ order_is_pay_or_cancel = true;
+ paid = 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);
+ }
+ return paid;
+}
+
void app_main()
{
@@ -364,127 +476,87 @@ void app_main()
initialize_things();
setup_wifi();
-
+ int second_option = SECOND_OPTION_EXIT;
while (1)
{
- show_menu();
+ show_menu(second_option);
const int key = wait_any_key_or_exit();
+ ///////////////////////////
+ // start of main switch
+ ///////////////////////////
if (key == RP2040_INPUT_BUTTON_ACCEPT)
{
- char *orderId = NULL;
-
- 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 (order_created)
+ bool ok = create_new_order();
+ if (!ok)
{
- 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,
- .display_func = esp_qrcode_print_screen,
- .qrcode_ecc_level = ESP_QRCODE_ECC_LOW,
- };
-
- esp_qrcode_generate(&cfg, paymentURI);
- free(paymentURI);
+ second_option = SECOND_OPTION_EXIT;
+ continue;
+ }
+ ESP_LOGI(TAG, "user is seeing qr code now");
+ ok = query_order_until_paid();
+ if (!ok)
+ {
+ second_option = SECOND_OPTION_RETRY;
}
else
{
- LAST_STATUS = "error creating the order";
- continue;
+ second_option = SECOND_OPTION_REFUND;
}
-
- 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)
+ }
+ else if (key == RP2040_INPUT_BUTTON_BACK && second_option == SECOND_OPTION_EXIT)
+ {
+ exit_to_launcher();
+ }
+ else if (key == RP2040_INPUT_BUTTON_BACK && second_option == SECOND_OPTION_RETRY)
+ {
+ ESP_LOGI(TAG, "retrying order %s", LAST_ORDER);
+ bool ok = query_order_until_paid();
+ if (!ok)
{
- 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);
+ second_option = SECOND_OPTION_RETRY;
+ }
+ else
+ {
+ second_option = SECOND_OPTION_REFUND;
}
-
- free(orderId);
}
- if (key == RP2040_INPUT_JOYSTICK_UP)
+ else if (key == RP2040_INPUT_BUTTON_BACK && second_option == SECOND_OPTION_REFUND)
+ {
+ show_message("not yet implemented!");
+ wait_any_key_or_exit();
+ }
+ else if (key == RP2040_INPUT_JOYSTICK_UP)
{
ESP_LOGI(TAG, "inc price 0.1");
NEXT_ORDER_PRICE += 0.1;
}
- if (key == RP2040_INPUT_JOYSTICK_DOWN)
+ else if (key == RP2040_INPUT_JOYSTICK_DOWN)
{
ESP_LOGI(TAG, "dec price 0.1");
NEXT_ORDER_PRICE -= 0.1;
}
- if (key == RP2040_INPUT_JOYSTICK_LEFT)
+ else if (key == RP2040_INPUT_JOYSTICK_LEFT)
{
ESP_LOGI(TAG, "inc price 1");
NEXT_ORDER_PRICE += 1;
}
- if (key == RP2040_INPUT_JOYSTICK_RIGHT)
+ else if (key == RP2040_INPUT_JOYSTICK_RIGHT)
{
ESP_LOGI(TAG, "dec price 1");
NEXT_ORDER_PRICE -= 1;
}
+ ///////////////////////////
+ // end of main switch
+ ///////////////////////////
+ if (NEXT_ORDER_PRICE > 30)
+ {
+ NEXT_ORDER_PRICE = 30;
+ }
+ if (NEXT_ORDER_PRICE < 0.1)
+ {
+ NEXT_ORDER_PRICE = 0.1;
+ }
}
}