Last active
November 6, 2019 03:17
-
-
Save normandmickey/3d745828666f8e944cb50d351f5ef5c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ezTime.h> | |
#include <M5ez.h> | |
#include <M5Stack.h> | |
#include <ArduinoJson.h> | |
#include <HTTPClient.h> | |
String ONApiKey = "Replace with your ON ApiKey"; | |
String currency = "USD"; | |
void setup() { | |
ez.begin(); | |
} | |
void loop() { | |
ezMenu myMenu; | |
myMenu.addItem("Bitcoin Price", mainmenu_one); | |
myMenu.addItem("Lightning Invoice", mainmenu_two); | |
myMenu.addItem("Settings", mainmenu_three); | |
myMenu.run(); | |
} | |
void mainmenu_one() { | |
ONRates(); | |
} | |
void mainmenu_two() { | |
ONInvoice(); | |
} | |
void mainmenu_three() { | |
ez.settings.menu(); | |
} | |
void ONRates() { | |
HTTPClient client; | |
client.begin("https://api.opennode.co/v1/rates"); | |
int httpCode = client.GET(); | |
if (httpCode >0) { | |
const size_t capacity = 162*JSON_OBJECT_SIZE(1) + 8*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(169) + 2110; | |
DynamicJsonDocument doc(capacity); | |
deserializeJson(doc, client.getString()); | |
JsonObject data = doc["data"]; | |
String pair = "BTC" + currency; | |
ez.msgBox("", data[pair][currency]); | |
} | |
client.end(); | |
} | |
void ONInvoice() { | |
String amount = ez.textInput(); | |
HTTPClient client; | |
client.begin("https://api.opennode.co/v1/charges"); | |
client.addHeader("Content-Type", "application/json"); | |
client.addHeader("Authorization", ONApiKey); | |
String jsonPost = String("{") + "\"amount\":" + "\"" + amount + "\"" + "," + "\"currency\":" + "\"" + currency + "\"" + "}"; | |
int httpCode = client.POST(jsonPost); | |
if (httpCode >0) { | |
const size_t capacity = 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(19) + 950; | |
DynamicJsonDocument doc(capacity); | |
deserializeJson(doc, client.getString()); | |
JsonObject data = doc["data"]; | |
String lnInvoice = data["lightning_invoice"]["payreq"]; | |
String invoiceId = data["id"]; | |
ez.screen.clear(); | |
M5.begin(); | |
M5.Lcd.qrcode(lnInvoice); | |
delay(20000); | |
checkONInvoiceStatus(invoiceId); | |
} | |
client.end(); | |
} | |
void checkONInvoiceStatus(String invoiceId) { | |
delay (3000); | |
for (int x = 0; x < 720; x++) { | |
HTTPClient client; | |
String invoiceURL = "https://api.opennode.co/v1/charge/" + invoiceId; | |
client.begin(invoiceURL); | |
client.addHeader("Content-Type", "application/json"); | |
client.addHeader("Authorization", ONApiKey); | |
int httpCode = client.GET(); | |
if (httpCode >0) { | |
const size_t capacity = JSON_ARRAY_SIZE(0) + JSON_OBJECT_SIZE(0) + 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(8) + JSON_OBJECT_SIZE(19) + 1060; | |
DynamicJsonDocument doc(capacity); | |
deserializeJson(doc, client.getString()); | |
JsonObject data = doc["data"]; | |
client.end(); | |
//ez.msgBox("", data["status"]); | |
if (data["status"] == "paid") { ez.msgBox("", "Payment Complete"); | |
break; } | |
} | |
delay(5000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment