-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
403 lines (340 loc) · 15.2 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#![allow(unused_imports)]
#[macro_use]
extern crate log;
use actix_web::{App, post, HttpServer, middleware::Logger, web, HttpResponse, Responder};
use anyhow::Result;
use deadpool_postgres::{Pool as PGPool};
use dotenv::dotenv;
use lightning_rs_webhook::btcpay::{btcpay_middleware, get_invoice_data, WebhookPayload};
use lightning_rs_webhook::db::pg_pool_from_url;
use lightning_rs_webhook::error::ServiceError;
use lightning_rs_webhook::routes;
use serde_json::Value;
#[allow(unused_variables)]
async fn webhook_handler(pg_pool: &PGPool, payload: WebhookPayload) -> Result<()> {
match payload {
// Triggers when an invoice is considered settled and the merchant can proceed with the order's
// delivery. The invoice now has enough confirmations on the blockchain (if paid on-chain) according
// to your store's configuration.
WebhookPayload::InvoiceSettled(event) => {
debug!("InvoiceSettled Event: {event:?}");
// Note: The example below is expecting that the invoice was created with posData
// that includes a Nostr pubkey and content_id. Your needs may vary.
//
// There are two alternatives for invoice processing and database updating
// as examples. You only need to pick one of each, or write your own.
// Event Processing Approach 1. - using webhook data
// let pos_data = event
// .metadata.ok_or(ServiceError::InternalError)?
// .pos_data.ok_or(ServiceError::InternalError)?;
// let pubkey = pos_data
// .get("pubkey").ok_or(ServiceError::BadClientData)?
// .as_str().ok_or(ServiceError::BadClientData)?
// .to_string();
// let content_id = pos_data
// .get("content_id").ok_or(ServiceError::BadClientData)?
// .as_str().ok_or(ServiceError::BadClientData)?
// .to_string();
// Event Processing Approach 2. - using REST API to fetch full invoice data record
//
// Ensure store_id and invoice_id are populated
// let store_id = event.store_id.ok_or(ServiceError::BadClientData)?;
// let invoice_id = event.invoice_id.ok_or(ServiceError::BadClientData)?;
// // Fetch the invoice via the API
// let invoice_data = get_invoice_data(&store_id, &invoice_id)
// .await
// .map_err(|_| ServiceError::InternalError)?;
// debug!("{invoice_data:?}");
// // Validate Invoice response
// let invoice_id = invoice_data.id.ok_or(ServiceError::InternalError)?;
// let pos_data_json = invoice_data
// .metadata
// .ok_or(ServiceError::InternalError)?
// .pos_data
// .ok_or(ServiceError::InternalError)?;
// let pos_data: Value = serde_json::from_str(&pos_data_json)
// .map_err(|_| ServiceError::InternalError)?;
// // Extract what we need to update the database
// // Note: Since we are populating the posData values in BTCPay server, we can skip validation
// // here - unless you are risk adverse.
// let pubkey = pos_data.get("pubkey").ok_or(ServiceError::InternalError)?.to_string();
// let content_id = pos_data.get("content_id").ok_or(ServiceError::InternalError)?.to_string();
// Database Approach 1. - single query
// This query will insert the pubkey into the identities table if not found, before
// inserting the payment record. Noting, if the content_id is not found, it's an NOOP returning 200
// let pg_conn = pg_pool.get().await?;
// let result = pg_conn.execute("
// WITH selected_identity AS (
// SELECT id
// FROM identities
// WHERE pubkey = $1
// LIMIT 1
// ), inserted_identity AS (
// INSERT INTO identities (pubkey)
// SELECT $1
// WHERE NOT EXISTS (SELECT 1 FROM selected_identity)
// RETURNING id
// )
// INSERT INTO payments (identity_id, content_id)
// SELECT COALESCE(selected_identity.id, inserted_identity.id), content.id
// FROM selected_identity
// FULL JOIN inserted_identity ON true
// JOIN content ON content.content_id = $2
// ON CONFLICT (identity_id, content_id) DO NOTHING;
// ", &[&pubkey, &content_id]).await?;
// Database Approach 2. - using transactions
// let mut pg_conn = pg_pool.get().await?;
// let pg_trans = pg_conn.transaction().await?;
// // Ensure the identity pubkey record exists
// let db_identity = pg_trans.query_one("
// WITH new_i AS(
// INSERT INTO identities (pubkey)
// VALUES ($1)
// ON CONFLICT DO NOTHING
// RETURNING id
// )
// SELECT id FROM new_i
// UNION
// SELECT id FROM identities WHERE pubkey=$1",
// &[&pubkey]).await?;
// let identity_id: i32 = db_identity.get(0);
// // Insert or ignore if existing payment row exists
// pg_trans.execute("
// INSERT INTO payments (
// identity_id,
// content_id
// )
// VALUES (
// $1,
// (select id from content where content_id = $2)
// )
// ON CONFLICT (identity_id, content_id) DO NOTHING;
// ", &[&identity_id, &content_id]).await?;
// pg_trans.commit().await?;
Ok(())
},
// An payment relating to an invoice has settled
WebhookPayload::InvoicePaymentSettled(event) => {
debug!("InvoicePaymentSettled Event: {event:?}");
Ok(())
},
// An invoice received a payment
WebhookPayload::InvoiceReceivedPayment(event) => {
debug!("InvoiceReceivedPayment Event: {event:?}");
Ok(())
},
// An invoice expired
WebhookPayload::InvoiceExpired(event) => {
debug!("InvoiceExpired Event: {event:?}");
Ok(())
},
// An invoice became invalid
WebhookPayload::InvoiceInvalid(event) => {
debug!("InvoiceInvalid Event: {event:?}");
Ok(())
},
// Triggers when an invoice is fully paid, but doesn't have the required amount of confirmations
// on the blockchain yet according to your store's settings.
WebhookPayload::InvoiceProcessing(event) => {
debug!("InvoiceProcessing Event: {event:?}");
Ok(())
},
// A new invoice has been created
WebhookPayload::InvoiceCreated(event) => {
debug!("InvoiceCreated Event: {event:?}");
Ok(())
},
// Any unhandled webhook events - return ok, as we don't have any logic for them yet
WebhookPayload::Unsupported => {
debug!("Unsupported Event");
Ok(())
},
}
}
// Note: This is scoped to inject the middleware - /btcpay/webhook is the full path
#[post("/webhook")]
pub async fn btcpay_webhook_handler(payload: web::Json<WebhookPayload>, app_data: web::Data<AppData>) -> impl Responder {
match webhook_handler(&app_data.pg_pool, payload.into_inner()).await {
Ok(_) => {
// Webhook caller is expecting a 200 response
HttpResponse::Ok().finish()
},
Err(err) => {
error!("Error: {err:?}");
HttpResponse::InternalServerError().json("Internal Server Error")
}
}
}
pub struct AppData {
pub pg_pool: PGPool,
}
#[actix_web::main]
async fn main() -> Result<()> {
env_logger::init();
dotenv().ok();
let host: String = std::env::var("HOST").expect("HOST must be set");
let port: String = std::env::var("PORT").expect("PORT must be set");
let pg_address: String = std::env::var("POSTGRES_ADDRESS").expect("POSTGRES_ADDRESS must be set.");
let pg_pool = pg_pool_from_url(&pg_address)?;
println!("Running BTCPay Webhook Server on {host}:{port}");
HttpServer::new(move || {
let logger = Logger::default();
let app_data = AppData {
pg_pool: pg_pool.clone()
};
App::new()
.wrap(logger)
.app_data(web::Data::new(app_data))
.service(routes::health_handler)
.service(
web::scope("/btcpay")
.wrap(btcpay_middleware::BTCPayHeaderVerify)
.service(btcpay_webhook_handler)
)
})
.bind(format!("{host}:{port}"))?
.run()
.await?;
Ok(())
}
#[cfg(test)]
mod tests {
use actix_web::{http::header::ContentType, test, App};
use std::env;
use super::*;
#[actix_web::test]
async fn test_health_get() {
let app = test::init_service(App::new().service(super::routes::health_handler)).await;
let req = test::TestRequest::default()
.uri("/health")
.insert_header(ContentType::html())
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
}
#[actix_web::test]
async fn test_btcpay_webhook_post_valid() {
// Note: Dummy data
env::set_var("BTCPAY_WEBHOOK_SECRET", "Y6Tio3rXRT4dGqpk43GvBPK9fHQ");
env::set_var("POSTGRES_ADDRESS", "postgresql://postgres:postgres@localhost:5433/postgres");
let pg_address: String = std::env::var("POSTGRES_ADDRESS").expect("POSTGRES_ADDRESS must be set.");
let pg_pool = pg_pool_from_url(&pg_address).unwrap();
let app_data = AppData {
pg_pool: pg_pool.clone()
};
let app = test::init_service(App::new()
.app_data(web::Data::new(app_data))
.service(
web::scope("/btcpay")
.wrap(btcpay_middleware::BTCPayHeaderVerify)
.service(btcpay_webhook_handler)
)).await;
let req = test::TestRequest::default()
.uri("/btcpay/webhook")
.method(actix_http::Method::POST)
.insert_header((super::btcpay_middleware::BTCPAY_SIG_HEADER, "sha256=237906b0175aa4de911eba91ec0791e7482333a5de9f81a179442fc602b0d1be"))
// Note: We cannot use .set_json as it will mangle the payload and cause a bad signature check
.insert_header(ContentType::json())
.set_payload(r#"{
"manuallyMarked": false,
"deliveryId": "WZbyGsmWGZvYjRsYCH7Vmt",
"webhookId": "AT7ogqNzXkjf12sLVWPDNS",
"originalDeliveryId": "WZbyGsmWGZvYjRsYCH7Vmt",
"isRedelivery": false,
"type": "InvoiceSettled",
"timestamp": 1683049755,
"storeId": "BJKmPvug3KHVWyu1ECEiAstAQXFjJD1fX87EcgEhHVLT",
"invoiceId": "6wmoR7p5UFVzCYuwyiViKX",
"metadata": {
"orderId": "23",
"physical": false
}
}"#)
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
}
#[actix_web::test]
async fn test_btcpay_webhook_post_missing_sig_header() {
// Note: Dummy data
env::set_var("BTCPAY_WEBHOOK_SECRET", "Y6Tio3rXRT4dGqpk43GvBPK9fHQ");
env::set_var("POSTGRES_ADDRESS", "postgresql://postgres:postgres@localhost:5433/postgres");
let pg_address: String = std::env::var("POSTGRES_ADDRESS").expect("POSTGRES_ADDRESS must be set.");
let pg_pool = pg_pool_from_url(&pg_address).unwrap();
let app_data = AppData {
pg_pool: pg_pool.clone()
};
let app = test::init_service(App::new()
.app_data(web::Data::new(app_data))
.service(
web::scope("/btcpay")
.wrap(btcpay_middleware::BTCPayHeaderVerify)
.service(btcpay_webhook_handler)
)).await;
let req = test::TestRequest::default()
.uri("/btcpay/webhook")
.method(actix_http::Method::POST)
// Note: We cannot use .set_json as it will mangle the payload and cause a bad signature check
.insert_header(ContentType::json())
.set_payload(r#"{
"manuallyMarked": false,
"deliveryId": "WZbyGsmWGZvYjRsYCH7Vmt",
"webhookId": "AT7ogqNzXkjf12sLVWPDNS",
"originalDeliveryId": "WZbyGsmWGZvYjRsYCH7Vmt",
"isRedelivery": false,
"type": "InvoiceSettled",
"timestamp": 1683049755,
"storeId": "BJKmPvug3KHVWyu1ECEiAstAQXFjJD1fX87EcgEhHVLT",
"invoiceId": "6wmoR7p5UFVzCYuwyiViKX",
"metadata": {
"orderId": "23",
"physical": false
}
}"#)
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_client_error());
}
#[actix_web::test]
async fn test_btcpay_webhook_post_invalid_sig_header() {
// Note: Dummy data
env::set_var("BTCPAY_WEBHOOK_SECRET", "Y6Tio3rXRT4dGqpk43GvBPK9fHQ");
env::set_var("POSTGRES_ADDRESS", "postgresql://postgres:postgres@localhost:5433/postgres");
let pg_address: String = std::env::var("POSTGRES_ADDRESS").expect("POSTGRES_ADDRESS must be set.");
let pg_pool = pg_pool_from_url(&pg_address).unwrap();
let app_data = AppData {
pg_pool: pg_pool.clone()
};
let app = test::init_service(App::new()
.app_data(web::Data::new(app_data))
.service(
web::scope("/btcpay")
.wrap(btcpay_middleware::BTCPayHeaderVerify)
.service(btcpay_webhook_handler)
)).await;
let req = test::TestRequest::default()
.uri("/btcpay/webhook")
.method(actix_http::Method::POST)
.insert_header((super::btcpay_middleware::BTCPAY_SIG_HEADER, "sha256=BADSIG"))
// Note: We cannot use .set_json as it will mangle the payload and cause a bad signature check
.insert_header(ContentType::json())
.set_payload(r#"{
"manuallyMarked": false,
"deliveryId": "WZbyGsmWGZvYjRsYCH7Vmt",
"webhookId": "AT7ogqNzXkjf12sLVWPDNS",
"originalDeliveryId": "WZbyGsmWGZvYjRsYCH7Vmt",
"isRedelivery": false,
"type": "InvoiceSettled",
"timestamp": 1683049755,
"storeId": "BJKmPvug3KHVWyu1ECEiAstAQXFjJD1fX87EcgEhHVLT",
"invoiceId": "6wmoR7p5UFVzCYuwyiViKX",
"metadata": {
"orderId": "23",
"physical": false
}
}"#)
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_client_error());
}
}