feat: Add new Rhai example demonstrating payment flow
- Add a new Rhai example showcasing complete payment flow for company registration. This includes company creation, payment record creation, successful payment processing, status updates, and handling failed and refunded payments. - Add new example demonstrating payment integration in Rhai scripting. This example showcases the usage of various payment status methods and verifies data from the database after processing payments. - Add new examples to Cargo.toml to facilitate building and running the examples. This makes it easier to integrate and test payment functionality using Rhai scripting.
This commit is contained in:
229
heromodels/examples/biz_rhai/payment_flow.rhai
Normal file
229
heromodels/examples/biz_rhai/payment_flow.rhai
Normal file
@@ -0,0 +1,229 @@
|
||||
// Payment Flow Rhai Example
|
||||
// This script demonstrates the complete payment flow for company registration
|
||||
// using the Rhai scripting interface.
|
||||
|
||||
print("=== Payment Flow Rhai Example ===");
|
||||
print("Demonstrating company registration with payment integration");
|
||||
print("");
|
||||
|
||||
// --- Step 1: Create Company with Pending Payment Status ---
|
||||
print("Step 1: Creating company with pending payment status");
|
||||
|
||||
let company_name = "InnovateTech Solutions";
|
||||
let company_reg = "REG-ITS-2024-001";
|
||||
let company_inc_date = 1704067200; // Jan 1, 2024
|
||||
|
||||
// Create company (status defaults to PendingPayment)
|
||||
let company = new_company(company_name, company_reg, company_inc_date)
|
||||
.email("contact@innovatetech.com")
|
||||
.phone("+1-555-0199")
|
||||
.website("https://innovatetech.com")
|
||||
.address("456 Innovation Blvd, Tech Valley, TV 67890")
|
||||
.business_type(BusinessTypeConstants::Starter)
|
||||
.industry("Software Development")
|
||||
.description("Cutting-edge software solutions for modern businesses")
|
||||
.fiscal_year_end("12-31");
|
||||
|
||||
print(` Company: ${company.name}`);
|
||||
print(` Status: ${company.status} (default for new companies)`);
|
||||
print(` Registration: ${company.registration_number}`);
|
||||
print(` Email: ${company.email}`);
|
||||
|
||||
// Save company to database
|
||||
company = set_company(company);
|
||||
print(` Saved with ID: ${company.id}`);
|
||||
print("");
|
||||
|
||||
// --- Step 2: Create Payment Record ---
|
||||
print("Step 2: Creating payment record");
|
||||
|
||||
let payment_intent_id = `pi_rhai_${timestamp()}`;
|
||||
let payment_plan = "yearly";
|
||||
let setup_fee = 750.0;
|
||||
let monthly_fee = 149.0;
|
||||
let total_amount = setup_fee + (monthly_fee * 12.0); // Setup + 12 months
|
||||
|
||||
let payment = new_payment(
|
||||
payment_intent_id,
|
||||
company.id,
|
||||
payment_plan,
|
||||
setup_fee,
|
||||
monthly_fee,
|
||||
total_amount
|
||||
);
|
||||
|
||||
print(` Payment Intent ID: ${payment.payment_intent_id}`);
|
||||
print(` Company ID: ${payment.company_id}`);
|
||||
print(` Payment Plan: ${payment.payment_plan}`);
|
||||
print(` Setup Fee: $${payment.setup_fee}`);
|
||||
print(` Monthly Fee: $${payment.monthly_fee}`);
|
||||
print(` Total Amount: $${payment.total_amount}`);
|
||||
print(` Status: ${payment.status} (default for new payments)`);
|
||||
|
||||
// Save payment to database
|
||||
payment = set_payment(payment);
|
||||
print(` Saved with ID: ${payment.id}`);
|
||||
print("");
|
||||
|
||||
// --- Step 3: Process Payment Successfully ---
|
||||
print("Step 3: Processing payment...");
|
||||
|
||||
// Simulate payment processing
|
||||
print(" Contacting payment processor...");
|
||||
print(" Validating payment details...");
|
||||
print(" Processing transaction...");
|
||||
|
||||
// Complete the payment with Stripe customer ID
|
||||
let stripe_customer_id = `cus_rhai_${timestamp()}`;
|
||||
payment = payment.complete_payment(stripe_customer_id);
|
||||
|
||||
print(" Payment completed successfully!");
|
||||
print(` New Status: ${payment.status}`);
|
||||
print(` Stripe Customer ID: ${payment.stripe_customer_id}`);
|
||||
|
||||
// Save updated payment
|
||||
payment = set_payment(payment);
|
||||
print("");
|
||||
|
||||
// --- Step 4: Update Company Status to Active ---
|
||||
print("Step 4: Updating company status to Active");
|
||||
|
||||
company = company.status(CompanyStatusConstants::Active);
|
||||
print(` Company: ${company.name}`);
|
||||
print(` New Status: ${company.status}`);
|
||||
|
||||
// Save updated company
|
||||
company = set_company(company);
|
||||
print(" Company status updated successfully!");
|
||||
print("");
|
||||
|
||||
// --- Step 5: Verify Payment Status ---
|
||||
print("Step 5: Payment status verification");
|
||||
print(` Is payment completed? ${payment.is_completed()}`);
|
||||
print(` Is payment pending? ${payment.is_pending()}`);
|
||||
print(` Has payment failed? ${payment.has_failed()}`);
|
||||
print(` Is payment refunded? ${payment.is_refunded()}`);
|
||||
print("");
|
||||
|
||||
// --- Step 6: Retrieve Data from Database ---
|
||||
print("Step 6: Verifying data from database");
|
||||
|
||||
let retrieved_company = get_company_by_id(company.id);
|
||||
print(` Retrieved Company: ${retrieved_company.name}`);
|
||||
print(` Status: ${retrieved_company.status}`);
|
||||
|
||||
let retrieved_payment = get_payment_by_id(payment.id);
|
||||
print(` Retrieved Payment: ${retrieved_payment.payment_intent_id}`);
|
||||
print(` Status: ${retrieved_payment.status}`);
|
||||
print(` Total: $${retrieved_payment.total_amount}`);
|
||||
print("");
|
||||
|
||||
// --- Step 7: Demonstrate Failed Payment Scenario ---
|
||||
print("Step 7: Demonstrating failed payment scenario");
|
||||
|
||||
// Create another company for failed payment demo
|
||||
let failed_company = new_company(
|
||||
"FailureDemo Corp",
|
||||
"REG-FDC-2024-002",
|
||||
1704067200
|
||||
)
|
||||
.email("demo@failurecorp.com")
|
||||
.business_type(BusinessTypeConstants::Single)
|
||||
.industry("Consulting");
|
||||
|
||||
failed_company = set_company(failed_company);
|
||||
print(` Created company: ${failed_company.name}`);
|
||||
print(` Status: ${failed_company.status} (pending payment)`);
|
||||
|
||||
// Create payment that will fail
|
||||
let failed_payment_intent = `pi_fail_${timestamp()}`;
|
||||
let failed_payment = new_payment(
|
||||
failed_payment_intent,
|
||||
failed_company.id,
|
||||
"monthly",
|
||||
300.0,
|
||||
59.0,
|
||||
359.0
|
||||
);
|
||||
|
||||
failed_payment = set_payment(failed_payment);
|
||||
|
||||
// Simulate payment failure
|
||||
print(" Simulating payment failure...");
|
||||
failed_payment = failed_payment.fail_payment();
|
||||
failed_payment = set_payment(failed_payment);
|
||||
|
||||
print(` Failed Company: ${failed_company.name}`);
|
||||
print(` Company Status: ${failed_company.status} (remains pending)`);
|
||||
print(` Payment Status: ${failed_payment.status}`);
|
||||
print(` Payment failed: ${failed_payment.has_failed()}`);
|
||||
print("");
|
||||
|
||||
// --- Step 8: Demonstrate Payment Refund ---
|
||||
print("Step 8: Demonstrating payment refund scenario");
|
||||
|
||||
// Create a payment to refund
|
||||
let refund_company = new_company(
|
||||
"RefundDemo Inc",
|
||||
"REG-RDI-2024-003",
|
||||
1704067200
|
||||
)
|
||||
.email("refund@demo.com")
|
||||
.business_type(BusinessTypeConstants::Twin);
|
||||
|
||||
refund_company = set_company(refund_company);
|
||||
|
||||
let refund_payment = new_payment(
|
||||
`pi_refund_${timestamp()}`,
|
||||
refund_company.id,
|
||||
"monthly",
|
||||
200.0,
|
||||
39.0,
|
||||
239.0
|
||||
);
|
||||
|
||||
refund_payment = set_payment(refund_payment);
|
||||
|
||||
// First complete the payment
|
||||
refund_payment = refund_payment.complete_payment(`cus_refund_${timestamp()}`);
|
||||
refund_payment = set_payment(refund_payment);
|
||||
print(` Payment completed: ${refund_payment.is_completed()}`);
|
||||
|
||||
// Then refund it
|
||||
refund_payment = refund_payment.refund_payment();
|
||||
refund_payment = set_payment(refund_payment);
|
||||
|
||||
print(` Payment refunded: ${refund_payment.is_refunded()}`);
|
||||
print(` Refund Status: ${refund_payment.status}`);
|
||||
print("");
|
||||
|
||||
// --- Summary ---
|
||||
print("=== Payment Flow Example Complete ===");
|
||||
print("Summary of demonstrated features:");
|
||||
print("✓ Company creation with PendingPayment status (default)");
|
||||
print("✓ Payment record creation and database persistence");
|
||||
print("✓ Successful payment processing and status updates");
|
||||
print("✓ Company status transition from PendingPayment to Active");
|
||||
print("✓ Payment status verification methods");
|
||||
print("✓ Database retrieval and verification");
|
||||
print("✓ Failed payment scenario handling");
|
||||
print("✓ Payment refund processing");
|
||||
print("");
|
||||
print("Key Payment Statuses:");
|
||||
print("- Pending: Initial state for new payments");
|
||||
print("- Completed: Payment successfully processed");
|
||||
print("- Failed: Payment processing failed");
|
||||
print("- Refunded: Previously completed payment was refunded");
|
||||
print("");
|
||||
print("Key Company Statuses:");
|
||||
print("- PendingPayment: Default for new companies (awaiting payment)");
|
||||
print("- Active: Payment completed, company is operational");
|
||||
print("- Suspended: Company temporarily suspended");
|
||||
print("- Inactive: Company deactivated");
|
||||
|
||||
// Helper function to get current timestamp
|
||||
fn timestamp() {
|
||||
// This would normally return current timestamp
|
||||
// For demo purposes, we'll use a static value
|
||||
1704067200
|
||||
}
|
Reference in New Issue
Block a user