190 lines
7.8 KiB
Plaintext
190 lines
7.8 KiB
Plaintext
// ===== Stripe Payment Integration Example =====
|
||
// This script demonstrates the complete payment workflow using Stripe
|
||
|
||
print("🔧 Configuring Stripe...");
|
||
// Configure Stripe with API key from environment variables
|
||
// The STRIPE_API_KEY is loaded from .env file by main.rs
|
||
let config_result = configure_stripe(STRIPE_API_KEY);
|
||
print(`Configuration result: ${config_result}`);
|
||
|
||
print("\n📦 Creating a Product...");
|
||
// Create a new product using builder pattern
|
||
let product = new_product()
|
||
.name("Premium Software License")
|
||
.description("A comprehensive software solution for businesses")
|
||
.metadata("category", "software")
|
||
.metadata("tier", "premium");
|
||
|
||
print(`Product created: ${product.name}`);
|
||
|
||
// Create the product in Stripe (non-blocking)
|
||
print("🔄 Dispatching product creation to Stripe...");
|
||
try {
|
||
let product_result = product.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
print(`✅ Product creation dispatched: ${product_result}`);
|
||
} catch(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.");
|
||
let product_id = "prod_demo_example_id"; // Continue with demo ID
|
||
}
|
||
|
||
print("\n💰 Creating Prices...");
|
||
|
||
// Create upfront price (one-time payment)
|
||
let upfront_price = new_price()
|
||
.amount(19999) // $199.99 in cents
|
||
.currency("usd")
|
||
.product(product_id)
|
||
.metadata("type", "upfront");
|
||
|
||
let upfront_result = upfront_price.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
print(`✅ Upfront Price creation dispatched: ${upfront_result}`);
|
||
let upfront_price_id = "price_demo_upfront_id";
|
||
|
||
// Create monthly subscription price
|
||
let monthly_price = new_price()
|
||
.amount(2999) // $29.99 in cents
|
||
.currency("usd")
|
||
.product(product_id)
|
||
.recurring("month")
|
||
.metadata("type", "monthly_subscription");
|
||
|
||
let monthly_result = monthly_price.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
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()
|
||
.amount(29999) // $299.99 in cents (2 months free)
|
||
.currency("usd")
|
||
.product(product_id)
|
||
.recurring("year")
|
||
.metadata("type", "annual_subscription")
|
||
.metadata("discount", "2_months_free");
|
||
|
||
let annual_result = annual_price.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
print(`✅ Annual Price creation dispatched: ${annual_result}`);
|
||
let annual_price_id = "price_demo_annual_id";
|
||
|
||
print("\n🎟️ Creating Discount Coupons...");
|
||
|
||
// Create a percentage-based coupon
|
||
let percent_coupon = new_coupon()
|
||
.duration("once")
|
||
.percent_off(25)
|
||
.metadata("campaign", "new_customer_discount")
|
||
.metadata("code", "WELCOME25");
|
||
|
||
let percent_result = percent_coupon.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
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()
|
||
.duration("repeating")
|
||
.duration_in_months(3)
|
||
.amount_off(500, "usd") // $5.00 off
|
||
.metadata("campaign", "loyalty_program")
|
||
.metadata("code", "LOYAL5");
|
||
|
||
let amount_result = amount_coupon.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
print(`✅ $5 Off Coupon creation dispatched: ${amount_result}`);
|
||
let amount_coupon_id = "coupon_demo_5dollar_id";
|
||
|
||
print("\n💳 Creating Payment Intent for Upfront Payment...");
|
||
|
||
// Create a payment intent for one-time payment
|
||
let payment_intent = new_payment_intent()
|
||
.amount(19999)
|
||
.currency("usd")
|
||
.customer("cus_example_customer_id")
|
||
.description("Premium Software License - One-time Payment")
|
||
.add_payment_method_type("card")
|
||
.add_payment_method_type("us_bank_account")
|
||
.metadata("product_id", product_id)
|
||
.metadata("price_id", upfront_price_id)
|
||
.metadata("payment_type", "upfront");
|
||
|
||
let payment_result = payment_intent.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
print(`✅ Payment Intent creation dispatched: ${payment_result}`);
|
||
let payment_intent_id = "pi_demo_payment_intent_id";
|
||
|
||
print("\n🔄 Creating Subscription...");
|
||
|
||
// Create a subscription for monthly billing
|
||
let subscription = new_subscription()
|
||
.customer("cus_example_customer_id")
|
||
.add_price(monthly_price_id)
|
||
.trial_days(14) // 14-day free trial
|
||
.coupon(percent_coupon_id) // Apply 25% discount
|
||
.metadata("plan", "monthly")
|
||
.metadata("trial", "14_days")
|
||
.metadata("source", "website_signup");
|
||
|
||
let subscription_result = subscription.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
print(`✅ Subscription creation dispatched: ${subscription_result}`);
|
||
let subscription_id = "sub_demo_subscription_id";
|
||
|
||
print("\n🎯 Creating Multi-Item Subscription...");
|
||
|
||
// Create a subscription with multiple items
|
||
let multi_subscription = new_subscription()
|
||
.customer("cus_example_enterprise_customer")
|
||
.add_price_with_quantity(monthly_price_id, 5) // 5 licenses
|
||
.add_price("price_addon_support_monthly") // Support addon
|
||
.trial_days(30) // 30-day trial for enterprise
|
||
.metadata("plan", "enterprise")
|
||
.metadata("licenses", "5")
|
||
.metadata("addons", "premium_support");
|
||
|
||
let multi_result = multi_subscription.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
print(`✅ Multi-Item Subscription creation dispatched: ${multi_result}`);
|
||
let multi_subscription_id = "sub_demo_multi_subscription_id";
|
||
|
||
print("\n💰 Creating Payment Intent with Coupon...");
|
||
|
||
// Create another payment intent with discount applied
|
||
let discounted_payment = new_payment_intent()
|
||
.amount(14999) // Discounted amount after coupon
|
||
.currency("usd")
|
||
.customer("cus_example_customer_2")
|
||
.description("Premium Software License - With 25% Discount")
|
||
.metadata("original_amount", "19999")
|
||
.metadata("coupon_applied", percent_coupon_id)
|
||
.metadata("discount_percent", "25");
|
||
|
||
let discounted_result = discounted_payment.create_async("payment-example", "payment-context", STRIPE_API_KEY);
|
||
print(`✅ Discounted Payment Intent creation dispatched: ${discounted_result}`);
|
||
let discounted_payment_id = "pi_demo_discounted_payment_id";
|
||
|
||
print("\n📊 Summary of Created Items:");
|
||
print("================================");
|
||
print(`Product ID: ${product_id}`);
|
||
print(`Upfront Price ID: ${upfront_price_id}`);
|
||
print(`Monthly Price ID: ${monthly_price_id}`);
|
||
print(`Annual Price ID: ${annual_price_id}`);
|
||
print(`25% Coupon ID: ${percent_coupon_id}`);
|
||
print(`$5 Coupon ID: ${amount_coupon_id}`);
|
||
print(`Payment Intent ID: ${payment_intent_id}`);
|
||
print(`Subscription ID: ${subscription_id}`);
|
||
print(`Multi-Subscription ID: ${multi_subscription_id}`);
|
||
print(`Discounted Payment ID: ${discounted_payment_id}`);
|
||
|
||
print("\n🎉 Payment workflow demonstration completed!");
|
||
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:");
|
||
print(`Product Name: ${product.name}`);
|
||
print(`Product Description: ${product.description}`);
|
||
print(`Upfront Price Amount: $${upfront_price.amount / 100}`);
|
||
print(`Monthly Price Currency: ${monthly_price.currency}`);
|
||
print(`Subscription Customer: ${subscription.customer}`);
|
||
print(`Payment Intent Amount: $${payment_intent.amount / 100}`);
|
||
print(`Percent Coupon Duration: ${percent_coupon.duration}`);
|
||
print(`Percent Coupon Discount: ${percent_coupon.percent_off}%`); |