implement non blocking flow based architecture for payments
This commit is contained in:
@@ -17,15 +17,18 @@ let product = new_product()
|
||||
|
||||
print(`Product created: ${product.name}`);
|
||||
|
||||
// Create the product in Stripe
|
||||
print("🔄 Attempting to create product in Stripe...");
|
||||
// Create the product in Stripe (non-blocking)
|
||||
print("🔄 Dispatching product creation to Stripe...");
|
||||
try {
|
||||
let product_id = product.create();
|
||||
print(`✅ Product ID: ${product_id}`);
|
||||
let product_result = product.create_async(STRIPE_API_KEY, "payment-example", "new_create_product_response", "new_create_product_error");
|
||||
print(`✅ Product creation dispatched: ${product_result}`);
|
||||
// In non-blocking mode, we use a demo product ID for the rest of the example
|
||||
let product_id = "prod_demo_example_id";
|
||||
print("💡 Using demo product ID for remaining operations in non-blocking mode");
|
||||
} catch(error) {
|
||||
print(`❌ Failed to create product: ${error}`);
|
||||
print(`❌ Failed to dispatch product creation: ${error}`);
|
||||
print("This is expected with a demo API key. In production, use a valid Stripe secret key.");
|
||||
return; // Exit early since we can't continue without a valid product
|
||||
let product_id = "prod_demo_example_id"; // Continue with demo ID
|
||||
}
|
||||
|
||||
print("\n💰 Creating Prices...");
|
||||
@@ -37,8 +40,9 @@ let upfront_price = new_price()
|
||||
.product(product_id)
|
||||
.metadata("type", "upfront");
|
||||
|
||||
let upfront_price_id = upfront_price.create();
|
||||
print(`✅ Upfront Price ID: ${upfront_price_id}`);
|
||||
let upfront_result = upfront_price.create_async(STRIPE_API_KEY, "payment-example", "new_create_price_response", "new_create_price_error");
|
||||
print(`✅ Upfront Price creation dispatched: ${upfront_result}`);
|
||||
let upfront_price_id = "price_demo_upfront_id";
|
||||
|
||||
// Create monthly subscription price
|
||||
let monthly_price = new_price()
|
||||
@@ -48,8 +52,9 @@ let monthly_price = new_price()
|
||||
.recurring("month")
|
||||
.metadata("type", "monthly_subscription");
|
||||
|
||||
let monthly_price_id = monthly_price.create();
|
||||
print(`✅ Monthly Price ID: ${monthly_price_id}`);
|
||||
let monthly_result = monthly_price.create_async(STRIPE_API_KEY, "payment-example", "new_create_price_response", "new_create_price_error");
|
||||
print(`✅ Monthly Price creation dispatched: ${monthly_result}`);
|
||||
let monthly_price_id = "price_demo_monthly_id";
|
||||
|
||||
// Create annual subscription price with discount
|
||||
let annual_price = new_price()
|
||||
@@ -60,8 +65,9 @@ let annual_price = new_price()
|
||||
.metadata("type", "annual_subscription")
|
||||
.metadata("discount", "2_months_free");
|
||||
|
||||
let annual_price_id = annual_price.create();
|
||||
print(`✅ Annual Price ID: ${annual_price_id}`);
|
||||
let annual_result = annual_price.create_async(STRIPE_API_KEY, "payment-example", "new_create_price_response", "new_create_price_error");
|
||||
print(`✅ Annual Price creation dispatched: ${annual_result}`);
|
||||
let annual_price_id = "price_demo_annual_id";
|
||||
|
||||
print("\n🎟️ Creating Discount Coupons...");
|
||||
|
||||
@@ -72,8 +78,9 @@ let percent_coupon = new_coupon()
|
||||
.metadata("campaign", "new_customer_discount")
|
||||
.metadata("code", "WELCOME25");
|
||||
|
||||
let percent_coupon_id = percent_coupon.create();
|
||||
print(`✅ 25% Off Coupon ID: ${percent_coupon_id}`);
|
||||
let percent_result = percent_coupon.create_async(STRIPE_API_KEY, "payment-example", "new_create_coupon_response", "new_create_coupon_error");
|
||||
print(`✅ 25% Off Coupon creation dispatched: ${percent_result}`);
|
||||
let percent_coupon_id = "coupon_demo_25percent_id";
|
||||
|
||||
// Create a fixed amount coupon
|
||||
let amount_coupon = new_coupon()
|
||||
@@ -83,8 +90,9 @@ let amount_coupon = new_coupon()
|
||||
.metadata("campaign", "loyalty_program")
|
||||
.metadata("code", "LOYAL5");
|
||||
|
||||
let amount_coupon_id = amount_coupon.create();
|
||||
print(`✅ $5 Off Coupon ID: ${amount_coupon_id}`);
|
||||
let amount_result = amount_coupon.create_async(STRIPE_API_KEY, "payment-example", "new_create_coupon_response", "new_create_coupon_error");
|
||||
print(`✅ $5 Off Coupon creation dispatched: ${amount_result}`);
|
||||
let amount_coupon_id = "coupon_demo_5dollar_id";
|
||||
|
||||
print("\n💳 Creating Payment Intent for Upfront Payment...");
|
||||
|
||||
@@ -100,8 +108,9 @@ let payment_intent = new_payment_intent()
|
||||
.metadata("price_id", upfront_price_id)
|
||||
.metadata("payment_type", "upfront");
|
||||
|
||||
let payment_intent_id = payment_intent.create();
|
||||
print(`✅ Payment Intent ID: ${payment_intent_id}`);
|
||||
let payment_result = payment_intent.create_async(STRIPE_API_KEY, "payment-example", "new_create_payment_intent_response", "new_create_payment_intent_error");
|
||||
print(`✅ Payment Intent creation dispatched: ${payment_result}`);
|
||||
let payment_intent_id = "pi_demo_payment_intent_id";
|
||||
|
||||
print("\n🔄 Creating Subscription...");
|
||||
|
||||
@@ -115,8 +124,9 @@ let subscription = new_subscription()
|
||||
.metadata("trial", "14_days")
|
||||
.metadata("source", "website_signup");
|
||||
|
||||
let subscription_id = subscription.create();
|
||||
print(`✅ Subscription ID: ${subscription_id}`);
|
||||
let subscription_result = subscription.create_async(STRIPE_API_KEY, "payment-example", "new_create_subscription_response", "new_create_subscription_error");
|
||||
print(`✅ Subscription creation dispatched: ${subscription_result}`);
|
||||
let subscription_id = "sub_demo_subscription_id";
|
||||
|
||||
print("\n🎯 Creating Multi-Item Subscription...");
|
||||
|
||||
@@ -130,8 +140,9 @@ let multi_subscription = new_subscription()
|
||||
.metadata("licenses", "5")
|
||||
.metadata("addons", "premium_support");
|
||||
|
||||
let multi_subscription_id = multi_subscription.create();
|
||||
print(`✅ Multi-Item Subscription ID: ${multi_subscription_id}`);
|
||||
let multi_result = multi_subscription.create_async(STRIPE_API_KEY, "payment-example", "new_create_subscription_response", "new_create_subscription_error");
|
||||
print(`✅ Multi-Item Subscription creation dispatched: ${multi_result}`);
|
||||
let multi_subscription_id = "sub_demo_multi_subscription_id";
|
||||
|
||||
print("\n💰 Creating Payment Intent with Coupon...");
|
||||
|
||||
@@ -145,8 +156,9 @@ let discounted_payment = new_payment_intent()
|
||||
.metadata("coupon_applied", percent_coupon_id)
|
||||
.metadata("discount_percent", "25");
|
||||
|
||||
let discounted_payment_id = discounted_payment.create();
|
||||
print(`✅ Discounted Payment Intent ID: ${discounted_payment_id}`);
|
||||
let discounted_result = discounted_payment.create_async(STRIPE_API_KEY, "payment-example", "new_create_payment_intent_response", "new_create_payment_intent_error");
|
||||
print(`✅ Discounted Payment Intent creation dispatched: ${discounted_result}`);
|
||||
let discounted_payment_id = "pi_demo_discounted_payment_id";
|
||||
|
||||
print("\n📊 Summary of Created Items:");
|
||||
print("================================");
|
||||
@@ -162,7 +174,12 @@ print(`Multi-Subscription ID: ${multi_subscription_id}`);
|
||||
print(`Discounted Payment ID: ${discounted_payment_id}`);
|
||||
|
||||
print("\n🎉 Payment workflow demonstration completed!");
|
||||
print("All Stripe objects have been created successfully using the builder pattern.");
|
||||
print("All Stripe object creation requests have been dispatched using the non-blocking pattern.");
|
||||
print("💡 In non-blocking mode:");
|
||||
print(" ✓ Functions return immediately with dispatch confirmations");
|
||||
print(" ✓ HTTP requests happen in background using tokio::spawn");
|
||||
print(" ✓ Results are handled by response/error scripts via RhaiDispatcher");
|
||||
print(" ✓ No thread blocking or waiting for API responses");
|
||||
|
||||
// Example of accessing object properties
|
||||
print("\n🔍 Accessing Object Properties:");
|
||||
|
Reference in New Issue
Block a user