# Payment Gateway Integration Example # # This example demonstrates how to integrate the Payment Gateway # into your CloudLayer application. defmodule MyApp.PaymentExample do @moduledoc """ Example module showing how to use the Payment Gateway API. """ @doc """ Example: Process a payment for a user purchase. """ def process_purchase(user_id, amount, currency \\ "USD") do # Check if Payment Gateway is available if payment_gateway_available?() do case PaymentGatewayApp.start_payment(amount, currency, user_id) do {:ok, payment} -> # Store payment ID in your database # save_payment_record(user_id, payment.id) {:ok, %{ payment_id: payment.id, status: payment.status, message: "Payment initiated successfully" }} {:error, reason} -> {:error, "Payment failed: #{inspect(reason)}"} end else {:error, "Payment Gateway not available"} end end @doc """ Example: Check payment status and update order. """ def check_payment_and_update_order(payment_id, order_id) do if payment_gateway_available?() do case PaymentGatewayApp.get_payment_status(payment_id) do {:ok, status} -> # Update your order based on payment status case status.status do "completed" -> # complete_order(order_id) {:ok, "Order completed"} "failed" -> # cancel_order(order_id) {:ok, "Order cancelled"} "pending" -> {:ok, "Payment still pending"} _ -> {:ok, "Payment status: #{status.status}"} end {:error, reason} -> {:error, "Failed to check status: #{inspect(reason)}"} end else {:error, "Payment Gateway not available"} end end @doc """ Example: Webhook handler for payment notifications. """ def handle_payment_webhook(payload) do # Verify webhook signature (implement based on your provider) # verify_webhook_signature(payload) payment_id = payload["payment_id"] status = payload["status"] # Update your database # update_payment_status(payment_id, status) # Send notification to user # notify_user_payment_status(payment_id, status) {:ok, "Webhook processed"} end # Helper function to check if Payment Gateway is available defp payment_gateway_available? do Code.ensure_loaded?(PaymentGatewayApp) and Application.get_env(:da_product_app, :enable_payment_gateway, false) end end