105 lines
4.3 KiB
Plaintext
105 lines
4.3 KiB
Plaintext
// Example 11: Payment Provider - Pesapal Integration
|
|
|
|
print("=== Payment Provider & Pesapal Example ===\n");
|
|
|
|
// Get a context to work with
|
|
print("1. Getting context...");
|
|
let ctx = get_context(["alice", "bob"]);
|
|
print(" ✓ Context created: " + ctx.context_id());
|
|
|
|
// Create Pesapal payment client (sandbox mode for testing)
|
|
print("\n2. Creating Pesapal payment client (sandbox)...");
|
|
let payment_client = new_payment_client_pesapal_sandbox(
|
|
1, // Client ID
|
|
"qkio1BGGYAXTu2JOfm7XSXNruoZsrqEW", // Consumer key
|
|
"osGQ364R49cXKeOYSpaOnT++rHs=" // Consumer secret
|
|
);
|
|
print(" ✓ Payment client created");
|
|
|
|
// Create a payment request
|
|
print("\n3. Creating payment request...");
|
|
let payment_request = new_payment_request(
|
|
"ORDER_" + timestamp(), // Merchant reference
|
|
2500.00, // Amount
|
|
"KES", // Currency (Kenyan Shillings)
|
|
"Payment for Premium Subscription", // Description
|
|
"https://example.com/payment/callback" // Callback URL
|
|
);
|
|
|
|
// Add customer details
|
|
payment_request
|
|
.customer_email("customer@example.com")
|
|
.customer_phone("+254712345678")
|
|
.customer_name("John", "Doe")
|
|
.redirect_url("https://example.com/payment/success");
|
|
|
|
print(" ✓ Payment request created");
|
|
print(" Merchant Reference: ORDER_" + timestamp());
|
|
print(" Amount: 2500.00 KES");
|
|
print(" Description: Payment for Premium Subscription");
|
|
|
|
// Create payment link
|
|
print("\n4. Creating payment link...");
|
|
let payment_response = payment_client.create_payment_link(payment_request);
|
|
print(" ✓ Payment link created successfully!");
|
|
print(" Payment URL: " + payment_response.get_payment_url());
|
|
print(" Order Tracking ID: " + payment_response.get_order_tracking_id());
|
|
print(" Merchant Reference: " + payment_response.get_merchant_reference());
|
|
print(" Status: " + payment_response.get_status());
|
|
|
|
// In a real scenario, you would:
|
|
// 1. Redirect the user to payment_response.get_payment_url()
|
|
// 2. User completes payment on Pesapal
|
|
// 3. Pesapal sends callback to your callback URL
|
|
// 4. You check payment status using the order tracking ID
|
|
|
|
// Simulate checking payment status
|
|
print("\n5. Checking payment status...");
|
|
let order_tracking_id = payment_response.get_order_tracking_id();
|
|
let payment_status = payment_client.get_payment_status(order_tracking_id);
|
|
print(" ✓ Payment status retrieved");
|
|
print(" Order Tracking ID: " + order_tracking_id);
|
|
print(" Status: " + payment_status.get_status());
|
|
print(" Amount: " + payment_status.get_amount() + " " + payment_status.get_currency());
|
|
print(" Payment Method: " + payment_status.get_payment_method());
|
|
print(" Transaction ID: " + payment_status.get_transaction_id());
|
|
|
|
// Create another payment for a different scenario
|
|
print("\n6. Creating another payment (smaller amount)...");
|
|
let small_payment = new_payment_request(
|
|
"ORDER_SMALL_" + timestamp(),
|
|
150.00,
|
|
"KES",
|
|
"Coffee Purchase",
|
|
"https://example.com/payment/callback"
|
|
);
|
|
|
|
small_payment
|
|
.customer_email("coffee@example.com")
|
|
.customer_phone("+254798765432");
|
|
|
|
let small_payment_response = payment_client.create_payment_link(small_payment);
|
|
print(" ✓ Small payment link created");
|
|
print(" Amount: 150.00 KES");
|
|
print(" Payment URL: " + small_payment_response.get_payment_url());
|
|
|
|
// Example with production client (commented out - requires real credentials)
|
|
print("\n7. Production client example (not executed)...");
|
|
print(" // For production, use:");
|
|
print(" // let prod_client = new_payment_client_pesapal(");
|
|
print(" // \"your_production_consumer_key\",");
|
|
print(" // \"your_production_consumer_secret\"");
|
|
print(" // );");
|
|
|
|
print("\n=== Payment Provider & Pesapal Example Complete ===");
|
|
print("\nNote: This example makes REAL API calls to Pesapal sandbox.");
|
|
print("The credentials used are for testing purposes only.");
|
|
print("\nPesapal Payment Flow:");
|
|
print("1. Create payment request with amount, currency, and customer details");
|
|
print("2. Get payment URL from Pesapal");
|
|
print("3. Redirect customer to payment URL");
|
|
print("4. Customer completes payment (M-PESA, Card, etc.)");
|
|
print("5. Pesapal sends callback to your server");
|
|
print("6. Check payment status using order tracking ID");
|
|
print("7. Fulfill order if payment is successful");
|