rhailib/examples/payment_usage_example.rhai

108 lines
3.3 KiB
Plaintext

// Example Rhai script demonstrating non-blocking payment functions
// This script shows how to use the new async payment functions
print("🚀 Non-Blocking Payment Example");
print("================================");
// Create a product asynchronously
print("📦 Creating product...");
let product = new_product()
.name("Premium Subscription")
.description("Monthly premium subscription service")
.metadata("category", "subscription")
.metadata("tier", "premium");
let product_result = product.create_async(
"payment-worker-1",
"product-context-123",
"sk_test_your_stripe_secret_key"
);
print(`Product creation dispatched: ${product_result}`);
// Create a price asynchronously
print("💰 Creating price...");
let price = new_price()
.amount(2999) // $29.99 in cents
.currency("usd")
.product("prod_premium_subscription") // Would be the actual product ID
.recurring("month")
.metadata("billing_cycle", "monthly");
let price_result = price.create_async(
"payment-worker-1",
"price-context-456",
"sk_test_your_stripe_secret_key"
);
print(`Price creation dispatched: ${price_result}`);
// Create a payment intent asynchronously
print("💳 Creating payment intent...");
let payment_intent = new_payment_intent()
.amount(2999)
.currency("usd")
.customer("cus_customer123")
.description("Premium subscription payment")
.add_payment_method_type("card")
.metadata("subscription_type", "premium")
.metadata("billing_period", "monthly");
let payment_result = payment_intent.create_async(
"payment-worker-1",
"payment-context-789",
"sk_test_your_stripe_secret_key"
);
print(`Payment intent creation dispatched: ${payment_result}`);
// Create a subscription asynchronously
print("📅 Creating subscription...");
let subscription = new_subscription()
.customer("cus_customer123")
.add_price("price_premium_monthly") // Would be the actual price ID
.trial_days(7)
.metadata("plan", "premium")
.metadata("source", "website");
let subscription_result = subscription.create_async(
"payment-worker-1",
"subscription-context-101",
"sk_test_your_stripe_secret_key"
);
print(`Subscription creation dispatched: ${subscription_result}`);
// Create a coupon asynchronously
print("🎫 Creating coupon...");
let coupon = new_coupon()
.duration("once")
.percent_off(20)
.metadata("campaign", "new_user_discount")
.metadata("valid_until", "2024-12-31");
let coupon_result = coupon.create_async(
"payment-worker-1",
"coupon-context-202",
"sk_test_your_stripe_secret_key"
);
print(`Coupon creation dispatched: ${coupon_result}`);
print("\n✅ All payment operations dispatched!");
print("🔄 HTTP requests are happening in the background");
print("📨 Response/error scripts will be triggered when complete");
print("\n📋 Summary:");
print(` Product: ${product_result}`);
print(` Price: ${price_result}`);
print(` Payment Intent: ${payment_result}`);
print(` Subscription: ${subscription_result}`);
print(` Coupon: ${coupon_result}`);
print("\n🎯 Key Benefits:");
print(" ✓ Immediate returns - no blocking");
print(" ✓ Concurrent processing capability");
print(" ✓ Event-driven response handling");
print(" ✓ No global state dependencies");
print(" ✓ Configurable per request");