Рассчитать пропорциональную стоимость в полосе с помощью эликсира - PullRequest
0 голосов
/ 04 июня 2018

Я пытаюсь рассчитать стоимость пропорциональности в Stripe, используя Elixir.Я использую библиотеку Stripity_Stripe https://github.com/code-corps/stripity_stripe/tree/2.0-alpha

Вот что я сделал.

  1. Получить текущее время как время Unix
proration_date = DateTime.to_unix(DateTime.utc_now)
Получить предстоящий счет с использованием Stripe.Invoice
params = %{customer: customer_id,
           subscription: subscription_id,
           subscription_items: [%{plan: plan_id}],
           subscription_proration_date: proration_date}

upcoming_invoice = Invoice.upcoming(params, opts)

Я заменяю customer_id на идентификатор клиента и subscription_id на subscription_id клиента и plan_id на новый plan_id (который нужно сделатьизменить) и subscription_proration_date как proration_date

возвращается как

{:ok,
 %Stripe.Invoice{
   amount_due: 17226,
   application_fee: nil,
   attempt_count: 0,
   attempted: false,
   charge: nil,
   closed: false,
   currency: "usd",
   customer: "cus_CxY2gOrcBX568N",
   date: 1530382817,
   description: nil,
   discount: nil,
   ending_balance: nil,
   forgiven: false,
   id: nil,
   lines: %Stripe.List{
     data: [
       %{
         amount: 5235,
         currency: "usd",
         description: "Remaining time on Gold after 04 Jun 2018",
         discountable: false,
         id: "ii_1CZJOQE0U7CBdOnaJ2JiTU3p",
         invoice_item: "ii_1CZJOQE0U7CBdOnaJ2JiTU3p",
         livemode: false,
         metadata: %{},
         object: "line_item",
         period: %{end: 1530382817, start: 1528121306},
         plan: %Stripe.Plan{
           amount: 6000,
           created: 1527787728,
           currency: "usd",
           id: "plan_CxqGQzrlZKzsyX",
           interval: "month",
           interval_count: 1,
           livemode: false,
           metadata: %{},
           name: "Gold",
           object: "plan",
           statement_descriptor: "Gold",
           trial_period_days: nil
         },
         proration: true,
         quantity: 1,
         subscription: "sub_Cxr5eVusfDmwpD",
         subscription_item: "si_CzHykruREBJmer",
         type: "invoiceitem"
       },
       %{
         amount: -1991,
         currency: "usd",
         description: "Unused time on Bronze after 31 May 2018",
         discountable: false,
         id: "ii_1CXyMGE0U7CBdOna7pMyz65c",
         invoice_item: "ii_1CXyMGE0U7CBdOna7pMyz65c",
         livemode: false,
         metadata: %{},
         object: "line_item",
         period: %{end: 1530382817, start: 1527802256},
         plan: %Stripe.Plan{
           amount: 2000,
           created: 1527789541,
           currency: "usd",
           id: "plan_CxqkbcqfX4hDB8",
           interval: "month",
           interval_count: 1,
           livemode: false,
           metadata: %{},
           name: "Bronze",
           object: "plan",
           statement_descriptor: nil,
           trial_period_days: nil
         },
         proration: true,
         quantity: 1,
         subscription: "sub_Cxr5eVusfDmwpD",
         subscription_item: "si_Cxr5FLLMsPXBhq",
         type: "invoiceitem"
       },
       %{
         amount: 3982,
         currency: "usd",
         description: "Remaining time on Silver after 31 May 2018",
         discountable: false,
         id: "ii_1CXyMGE0U7CBdOnazohKmlP6",
         invoice_item: "ii_1CXyMGE0U7CBdOnazohKmlP6",
         livemode: false,
         metadata: %{},
         object: "line_item",
         period: %{end: 1530382817, start: 1527802256},
         plan: %Stripe.Plan{
           amount: 4000,
           created: 1527787680,
           currency: "usd",
           id: "plan_CxqFiVIY2FqTL3",
           interval: "month",
           interval_count: 1,
           livemode: false,
           metadata: %{},
           name: "Silver",
           object: "plan",
           statement_descriptor: "Silver",
           trial_period_days: nil
         },
         proration: true,
         quantity: 1,
         subscription: "sub_Cxr5eVusfDmwpD",
         subscription_item: "si_Cxr5FLLMsPXBhq",
         type: "invoiceitem"
       },
       %{
         amount: 4000,
         currency: "usd",
         description: nil,
         discountable: true,
         id: "sub_Cxr5eVusfDmwpD",
         livemode: false, 
         metadata: %{},
         object: "line_item",
         period: %{end: 1533061217, start: 1530382817},
         plan: %Stripe.Plan{
           amount: 4000,
           created: 1527787680,
           currency: "usd",
           id: "plan_CxqFiVIY2FqTL3",
           interval: "month",
           interval_count: 1,
           livemode: false,
           metadata: %{},
           name: "Silver",
           object: "plan", 
           statement_descriptor: "Silver",
           trial_period_days: nil
         },
         proration: false,
         quantity: 1,
         subscription: nil,
         subscription_item: "si_Cxr5FLLMsPXBhq",
         type: "subscription"
       },
       %{
         amount: 6000,
         currency: "usd",
         description: nil,
         discountable: true,
         id: "sub_Cxr5eVusfDmwpD",
         livemode: false,
         metadata: %{},
         object: "line_item",
         period: %{end: 1533061217, start: 1530382817},
         plan: %Stripe.Plan{
           amount: 6000,
           created: 1527787728,
           currency: "usd",
           id: "plan_CxqGQzrlZKzsyX",
           interval: "month",
           interval_count: 1,
           livemode: false,
           metadata: %{},
           name: "Gold",
           object: "plan",
           statement_descriptor: "Gold",
           trial_period_days: nil
         },
         proration: false,
         quantity: 1,
         subscription: nil,
         subscription_item: "si_CzHykruREBJmer",
         type: "subscription"
       }
     ],
     has_more: false,
     object: "list",
     total_count: 5,
     url: "/v1/invoices/upcoming/lines?customer=cus_CxY2gOrcBX568N&subscription=sub_Cxr5eVusfDmwpD&subscription_proration_date=1528121306"
   },
   livemode: false, 
   metadata: %{},
   next_payment_attempt: 1530386417,
   object: "invoice",
   paid: false,
   period_end: 1530382817,
   period_start: 1527790817,
   receipt_number: nil,
   starting_balance: 0,
   statement_descriptor: nil,
   subscription: "sub_Cxr5eVusfDmwpD",
   subscription_proration_date: 1528121306,
   subtotal: 17226,
   tax: nil,
   tax_percent: nil,
   total: 17226,
   webhooks_delivered_at: nil
 }}
Итак, я сделал сопоставление с шаблоном, чтобы получить элементы данных, как это (https://stripe.com/docs/billing/subscriptions/prorations)

{: ok,% Stripe.Invoice {lines:% Stripe.List {data: data}}} = upcoming_invoice

Тогда я попробовал это

data |> Enum.find (& & 1.period.start == proration_date)

возвращает это

%{
  amount: 5235,
  currency: "usd",
  description: "Remaining time on Gold after 04 Jun 2018",
  discountable: false,
  id: "ii_1CZJPIE0U7CBdOna36eKJQfg",
  invoice_item: "ii_1CZJPIE0U7CBdOna36eKJQfg",
  livemode: false,
  metadata: %{},
  object: "line_item",
  period: %{end: 1530382817, start: 1528121306},
  plan: %Stripe.Plan{
    amount: 6000,
    created: 1527787728,
    currency: "usd",
    id: "plan_CxqGQzrlZKzsyX",
    interval: "month",
    interval_count: 1,
    livemode: false,
    metadata: %{},
    name: "Gold",
    object: "plan",
    statement_descriptor: "Gold",
    trial_period_days: nil
  },
  proration: true,
  quantity: 1,
  subscription: "sub_Cxr5eVusfDmwpD",
  subscription_item: "si_CzHzbTUPBrfqPA",
  type: "invoiceitem"
}

Тогда я не знаю, что делать дальше.В документах с полосами, использующих python, сделайте это следующим образом.

current_prorations = [ii for ii in invoice.lines.data if ii.period.start == proration_date]
cost = sum([p.amount for p in current_prorations])

, но мой код возвращает только 1 результат, и я обновил план, чтобы проверить, возвращает ли мой код правильную стоимость, но она не была правильной.

Чего мне не хватает?

1 Ответ

0 голосов
/ 05 июня 2018

Самый простой перевод ваших двух строк кода Python будет выглядеть так:

current_prorations = for ii <- invoice.lines.data, ii.period.start == proration_date, do: ii
cost = for(p <- current_prorations, do: p.amount) |> Enum.sum()
...