|
1 | 1 | defmodule Chargebeex.Subscription do |
2 | 2 | use TypedStruct |
3 | | - use Chargebeex.Resource, resource: "subscription", only: [:list, :retrieve, :update] |
| 3 | + |
| 4 | + @resource "subscription" |
| 5 | + use Chargebeex.Resource, resource: @resource, only: [:list, :retrieve, :update] |
| 6 | + |
| 7 | + alias Chargebeex.Builder |
| 8 | + alias Chargebeex.Client |
4 | 9 |
|
5 | 10 | @typedoc """ |
6 | 11 | "future" | "in_trial" | "active" | "non_renewing" | "paused" | "cancelled" |
@@ -99,4 +104,105 @@ defmodule Chargebeex.Subscription do |
99 | 104 | |> super() |
100 | 105 | |> Chargebeex.Resource.add_custom_fields(raw_data) |
101 | 106 | end |
| 107 | + |
| 108 | + @doc """ |
| 109 | + Creates a Subcription with items for a Customer |
| 110 | +
|
| 111 | + ## Examples |
| 112 | +
|
| 113 | + iex> Chargebeex.Subscription.create_with_items("169ljDT1Op0yuxET", %{ |
| 114 | + subscription_items: [ |
| 115 | + %{ |
| 116 | + item_price_id: "DEF789LMN012", |
| 117 | + quantity: 1 |
| 118 | + } |
| 119 | + ] |
| 120 | + }) |
| 121 | + {:ok, %Chargebeex.Subscription{ |
| 122 | + id: "GHI456OPQ789", |
| 123 | + contract_term: %{}, |
| 124 | + referral_info: %{}, |
| 125 | + shipping_address: %{}, |
| 126 | + coupons: nil, |
| 127 | + charged_items: nil, |
| 128 | + item_tiers: nil, |
| 129 | + subscription_items: [ |
| 130 | + %{ |
| 131 | + "amount" => 100, |
| 132 | + "free_quantity" => 0, |
| 133 | + "item_price_id" => "DEF789LMN012", |
| 134 | + "item_type" => "plan", |
| 135 | + "object" => "subscription_item", |
| 136 | + "quantity" => 1, |
| 137 | + "trial_end" => 1705877999, |
| 138 | + "unit_price" => 100 |
| 139 | + } |
| 140 | + ], |
| 141 | + business_entity_id: "ABC123XYZ456", |
| 142 | + auto_close_invoices: true, |
| 143 | + custom_fields: %{}, |
| 144 | + metadata: %{}, |
| 145 | + create_pending_invoices: false, |
| 146 | + free_period_unit: nil, |
| 147 | + free_period: nil, |
| 148 | + cancel_reason_code: nil, |
| 149 | + changes_scheduled_at: nil, |
| 150 | + deleted: false, |
| 151 | + invoice_notes: nil, |
| 152 | + base_currency_code: nil, |
| 153 | + exchange_rate: nil, |
| 154 | + mrr: nil, |
| 155 | + total_dues: nil, |
| 156 | + due_since: nil, |
| 157 | + due_invoices_count: 0, |
| 158 | + net_term_days: nil, |
| 159 | + channel: "web", |
| 160 | + cancel_schedule_created_at: nil, |
| 161 | + plan_amount_in_decimal: nil, |
| 162 | + plan_free_quantity_in_decimal: nil, |
| 163 | + payment_source_id: nil, |
| 164 | + has_scheduled_changes: false, |
| 165 | + has_scheduled_advance_invoices: false, |
| 166 | + updated_at: 1705785896, |
| 167 | + resource_version: 1705785896428, |
| 168 | + created_from_ip: nil, |
| 169 | + cancel_reason: nil, |
| 170 | + cancelled_at: nil, |
| 171 | + resume_date: nil, |
| 172 | + pause_date: nil, |
| 173 | + override_relationship: false, |
| 174 | + contract_term_billing_cycle_on_renewal: nil, |
| 175 | + activated_at: nil, |
| 176 | + started_at: 1705705200, |
| 177 | + created_at: 1705785896, |
| 178 | + next_billing_at: 1705878000, |
| 179 | + current_term_end: nil, |
| 180 | + current_term_start: nil, |
| 181 | + trial_end_action: nil, |
| 182 | + currency_code: nil, |
| 183 | + start_date: nil, |
| 184 | + trial_end: nil, |
| 185 | + billing_period: nil, |
| 186 | + billing_period_unit: nil, |
| 187 | + object: nil, |
| 188 | + remaining_billing_cycles: nil, |
| 189 | + po_number: nil, |
| 190 | + plan_quantity_in_decimal: nil, |
| 191 | + plan_unit_price_in_decimal: nil, |
| 192 | + customer_id: nil, |
| 193 | + status: nil, |
| 194 | + trial_start: nil |
| 195 | + }} |
| 196 | + """ |
| 197 | + def create_with_items(customer_id, params, opts \\ []) do |
| 198 | + with path <- |
| 199 | + Chargebeex.Action.nested_resource_path_generic_without_id( |
| 200 | + [customer: customer_id], |
| 201 | + "subscription_for_items" |
| 202 | + ), |
| 203 | + {:ok, _status_code, _headers, content} <- Client.post(path, params, opts), |
| 204 | + builded <- Builder.build(content) do |
| 205 | + {:ok, Map.get(builded, @resource)} |
| 206 | + end |
| 207 | + end |
102 | 208 | end |
0 commit comments