| 1 |
|
defmodule DaProductApp.SaasKit.Billing do |
| 2 |
|
@moduledoc """ |
| 3 |
|
Module for handling billing operations through SaaS Kit. |
| 4 |
|
""" |
| 5 |
|
|
| 6 |
|
alias DaProductApp.SaasKit.Client |
| 7 |
|
|
| 8 |
|
@doc """ |
| 9 |
|
Create a new customer |
| 10 |
|
""" |
| 11 |
:-( |
def create_customer(user_id, params \\ %{}) do |
| 12 |
:-( |
body = %{ |
| 13 |
|
user_id: user_id |
| 14 |
|
} |
| 15 |
|
|> Map.merge(params) |
| 16 |
|
|
| 17 |
:-( |
Client.post("/customers", body) |
| 18 |
|
end |
| 19 |
|
|
| 20 |
|
@doc """ |
| 21 |
|
Get customer information |
| 22 |
|
""" |
| 23 |
|
def get_customer(customer_id) do |
| 24 |
:-( |
Client.get("/customers/#{customer_id}") |
| 25 |
|
end |
| 26 |
|
|
| 27 |
|
@doc """ |
| 28 |
|
Update customer information |
| 29 |
|
""" |
| 30 |
|
def update_customer(customer_id, params) do |
| 31 |
:-( |
Client.put("/customers/#{customer_id}", params) |
| 32 |
|
end |
| 33 |
|
|
| 34 |
|
@doc """ |
| 35 |
|
Get customer's billing history |
| 36 |
|
""" |
| 37 |
:-( |
def get_billing_history(customer_id, params \\ %{}) do |
| 38 |
:-( |
Client.get("/customers/#{customer_id}/billing-history", params) |
| 39 |
|
end |
| 40 |
|
|
| 41 |
|
@doc """ |
| 42 |
|
Create a payment method for a customer |
| 43 |
|
""" |
| 44 |
|
def create_payment_method(customer_id, payment_method_data) do |
| 45 |
:-( |
Client.post("/customers/#{customer_id}/payment-methods", payment_method_data) |
| 46 |
|
end |
| 47 |
|
|
| 48 |
|
@doc """ |
| 49 |
|
List customer's payment methods |
| 50 |
|
""" |
| 51 |
|
def list_payment_methods(customer_id) do |
| 52 |
:-( |
Client.get("/customers/#{customer_id}/payment-methods") |
| 53 |
|
end |
| 54 |
|
|
| 55 |
|
@doc """ |
| 56 |
|
Delete a payment method |
| 57 |
|
""" |
| 58 |
|
def delete_payment_method(customer_id, payment_method_id) do |
| 59 |
:-( |
Client.delete("/customers/#{customer_id}/payment-methods/#{payment_method_id}") |
| 60 |
|
end |
| 61 |
|
|
| 62 |
|
@doc """ |
| 63 |
|
Create an invoice |
| 64 |
|
""" |
| 65 |
|
def create_invoice(customer_id, items) do |
| 66 |
:-( |
body = %{ |
| 67 |
|
customer_id: customer_id, |
| 68 |
|
items: items |
| 69 |
|
} |
| 70 |
|
|
| 71 |
:-( |
Client.post("/invoices", body) |
| 72 |
|
end |
| 73 |
|
|
| 74 |
|
@doc """ |
| 75 |
|
Get invoice details |
| 76 |
|
""" |
| 77 |
|
def get_invoice(invoice_id) do |
| 78 |
:-( |
Client.get("/invoices/#{invoice_id}") |
| 79 |
|
end |
| 80 |
|
|
| 81 |
|
@doc """ |
| 82 |
|
List invoices for a customer |
| 83 |
|
""" |
| 84 |
:-( |
def list_customer_invoices(customer_id, params \\ %{}) do |
| 85 |
:-( |
Client.get("/invoices", Map.put(params, :customer_id, customer_id)) |
| 86 |
|
end |
| 87 |
|
|
| 88 |
|
@doc """ |
| 89 |
|
Process a payment |
| 90 |
|
""" |
| 91 |
|
def process_payment(payment_data) do |
| 92 |
:-( |
Client.post("/payments", payment_data) |
| 93 |
|
end |
| 94 |
|
|
| 95 |
|
@doc """ |
| 96 |
|
Get payment details |
| 97 |
|
""" |
| 98 |
|
def get_payment(payment_id) do |
| 99 |
:-( |
Client.get("/payments/#{payment_id}") |
| 100 |
|
end |
| 101 |
|
|
| 102 |
|
@doc """ |
| 103 |
|
Refund a payment |
| 104 |
|
""" |
| 105 |
:-( |
def refund_payment(payment_id, amount \\ nil) do |
| 106 |
:-( |
body = if amount, do: %{amount: amount}, else: %{} |
| 107 |
:-( |
Client.post("/payments/#{payment_id}/refund", body) |
| 108 |
|
end |
| 109 |
|
end |