rhailib/src/dsl/examples/payment/main.rs
2025-07-21 20:56:29 +02:00

47 lines
1.7 KiB
Rust

use rhailib_dsl::payment::register_payment_rhai_module;
use rhai::{Engine, EvalAltResult, Scope};
use std::fs;
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<EvalAltResult>> {
// Load environment variables from .env file
dotenv::from_filename("examples/payment/.env").ok();
// Get Stripe API key from environment
let stripe_secret_key = env::var("STRIPE_SECRET_KEY")
.unwrap_or_else(|_| {
println!("⚠️ STRIPE_SECRET_KEY not found in .env file, using demo key");
println!(" Create examples/payment/.env with: STRIPE_SECRET_KEY=sk_test_your_key_here");
"sk_test_demo_key_will_fail_gracefully".to_string()
});
// Create a new Rhai engine
let mut engine = Engine::new();
// Register the payment module
register_payment_rhai_module(&mut engine);
// Create a scope and set the Stripe API key variable
let mut scope = Scope::new();
scope.push("STRIPE_API_KEY", stripe_secret_key.clone());
println!("=== Rhai Payment Module Example ===");
println!("🔑 Using Stripe API key: {}***", &stripe_secret_key[..15.min(stripe_secret_key.len())]);
println!("Reading and executing payment.rhai script...\n");
// Read the Rhai script
let script = fs::read_to_string("examples/payment/payment.rhai")
.expect("Failed to read payment.rhai file");
// Execute the script with the scope
match engine.eval_with_scope::<()>(&mut scope, &script) {
Ok(_) => println!("\n✅ Payment script executed successfully!"),
Err(e) => {
eprintln!("❌ Error executing script: {}", e);
return Err(e);
}
}
Ok(())
}