Skip to content

Commit e0efa61

Browse files
committed
impl create_payment_session
1 parent 1410b54 commit e0efa61

File tree

1 file changed

+72
-24
lines changed

1 file changed

+72
-24
lines changed

lib/algora/bounties/bounties.ex

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ defmodule Algora.Bounties do
245245
) ::
246246
{:ok, String.t()} | {:error, atom()}
247247
def create_tip(%{creator: creator, owner: owner, recipient: recipient, amount: amount}, opts \\ []) do
248-
ticket_ref = opts[:ticket_ref]
249-
250248
changeset =
251249
Tip.changeset(%Tip{}, %{
252250
amount: amount,
@@ -255,6 +253,39 @@ defmodule Algora.Bounties do
255253
recipient_id: recipient.id
256254
})
257255

256+
Repo.transact(fn ->
257+
with {:ok, tip} <- Repo.insert(changeset) do
258+
create_payment_session(
259+
%{
260+
creator: creator,
261+
owner: owner,
262+
recipient: recipient,
263+
amount: amount,
264+
description: "Tip payment for OSS contributions"
265+
},
266+
ticket_ref: opts[:ticket_ref],
267+
tip_id: tip.id
268+
)
269+
end
270+
end)
271+
end
272+
273+
@spec create_payment_session(
274+
%{creator: User.t(), owner: User.t(), recipient: User.t(), amount: Money.t(), description: String.t()},
275+
opts :: [
276+
ticket_ref: %{owner: String.t(), repo: String.t(), number: integer()},
277+
tip_id: String.t(),
278+
bounty_id: String.t(),
279+
claim_id: String.t()
280+
]
281+
) ::
282+
{:ok, String.t()} | {:error, atom()}
283+
def create_payment_session(
284+
%{creator: creator, owner: owner, recipient: recipient, amount: amount, description: description},
285+
opts \\ []
286+
) do
287+
ticket_ref = opts[:ticket_ref]
288+
258289
# Initialize transaction IDs
259290
charge_id = Nanoid.generate()
260291
debit_id = Nanoid.generate()
@@ -279,12 +310,7 @@ defmodule Algora.Bounties do
279310
currency: currency,
280311
product_data: %{
281312
name: "Payment to @#{recipient.provider_login}",
282-
# TODO:
283-
description:
284-
if(ticket_ref,
285-
do: "#{ticket_ref[:owner]}/#{ticket_ref[:repo]}##{ticket_ref[:number]}",
286-
else: "Tip to @#{recipient.provider_login}"
287-
),
313+
description: if(ticket_ref, do: "#{ticket_ref[:owner]}/#{ticket_ref[:repo]}##{ticket_ref[:number]}"),
288314
images: [recipient.avatar_url]
289315
}
290316
},
@@ -309,11 +335,12 @@ defmodule Algora.Bounties do
309335
]
310336

311337
Repo.transact(fn ->
312-
with {:ok, tip} <- Repo.insert(changeset),
313-
{:ok, _charge} <-
338+
with {:ok, _charge} <-
314339
initialize_charge(%{
315340
id: charge_id,
316-
tip: tip,
341+
tip_id: opts[:tip_id],
342+
bounty_id: opts[:bounty_id],
343+
claim_id: opts[:claim_id],
317344
user_id: creator.id,
318345
gross_amount: gross_amount,
319346
net_amount: amount,
@@ -324,7 +351,9 @@ defmodule Algora.Bounties do
324351
{:ok, _debit} <-
325352
initialize_debit(%{
326353
id: debit_id,
327-
tip: tip,
354+
tip_id: opts[:tip_id],
355+
bounty_id: opts[:bounty_id],
356+
claim_id: opts[:claim_id],
328357
amount: amount,
329358
user_id: creator.id,
330359
linked_transaction_id: credit_id,
@@ -333,16 +362,17 @@ defmodule Algora.Bounties do
333362
{:ok, _credit} <-
334363
initialize_credit(%{
335364
id: credit_id,
336-
tip: tip,
365+
tip_id: opts[:tip_id],
366+
bounty_id: opts[:bounty_id],
367+
claim_id: opts[:claim_id],
337368
amount: amount,
338369
user_id: recipient.id,
339370
linked_transaction_id: debit_id,
340371
group_id: tx_group_id
341372
}),
342373
{:ok, session} <-
343374
Payments.create_stripe_session(line_items, %{
344-
# Mandatory for some countries like India
345-
description: "Tip payment for OSS contributions",
375+
description: description,
346376
metadata: %{"version" => "2", "group_id" => tx_group_id}
347377
}) do
348378
{:ok, session.url}
@@ -352,7 +382,9 @@ defmodule Algora.Bounties do
352382

353383
defp initialize_charge(%{
354384
id: id,
355-
tip: tip,
385+
tip_id: tip_id,
386+
bounty_id: bounty_id,
387+
claim_id: claim_id,
356388
user_id: user_id,
357389
gross_amount: gross_amount,
358390
net_amount: net_amount,
@@ -366,7 +398,9 @@ defmodule Algora.Bounties do
366398
provider: "stripe",
367399
type: :charge,
368400
status: :initialized,
369-
tip_id: tip.id,
401+
tip_id: tip_id,
402+
bounty_id: bounty_id,
403+
claim_id: claim_id,
370404
user_id: user_id,
371405
gross_amount: gross_amount,
372406
net_amount: net_amount,
@@ -377,14 +411,18 @@ defmodule Algora.Bounties do
377411
|> Algora.Validations.validate_positive(:gross_amount)
378412
|> Algora.Validations.validate_positive(:net_amount)
379413
|> Algora.Validations.validate_positive(:total_fee)
380-
|> foreign_key_constraint(:tip_id)
381414
|> foreign_key_constraint(:user_id)
415+
|> foreign_key_constraint(:tip_id)
416+
|> foreign_key_constraint(:bounty_id)
417+
|> foreign_key_constraint(:claim_id)
382418
|> Repo.insert()
383419
end
384420

385421
defp initialize_debit(%{
386422
id: id,
387-
tip: tip,
423+
tip_id: tip_id,
424+
bounty_id: bounty_id,
425+
claim_id: claim_id,
388426
amount: amount,
389427
user_id: user_id,
390428
linked_transaction_id: linked_transaction_id,
@@ -396,7 +434,9 @@ defmodule Algora.Bounties do
396434
provider: "stripe",
397435
type: :debit,
398436
status: :initialized,
399-
tip_id: tip.id,
437+
tip_id: tip_id,
438+
bounty_id: bounty_id,
439+
claim_id: claim_id,
400440
user_id: user_id,
401441
gross_amount: amount,
402442
net_amount: amount,
@@ -406,14 +446,18 @@ defmodule Algora.Bounties do
406446
})
407447
|> Algora.Validations.validate_positive(:gross_amount)
408448
|> Algora.Validations.validate_positive(:net_amount)
409-
|> foreign_key_constraint(:tip_id)
410449
|> foreign_key_constraint(:user_id)
450+
|> foreign_key_constraint(:tip_id)
451+
|> foreign_key_constraint(:bounty_id)
452+
|> foreign_key_constraint(:claim_id)
411453
|> Repo.insert()
412454
end
413455

414456
defp initialize_credit(%{
415457
id: id,
416-
tip: tip,
458+
tip_id: tip_id,
459+
bounty_id: bounty_id,
460+
claim_id: claim_id,
417461
amount: amount,
418462
user_id: user_id,
419463
linked_transaction_id: linked_transaction_id,
@@ -425,7 +469,9 @@ defmodule Algora.Bounties do
425469
provider: "stripe",
426470
type: :credit,
427471
status: :initialized,
428-
tip_id: tip.id,
472+
tip_id: tip_id,
473+
bounty_id: bounty_id,
474+
claim_id: claim_id,
429475
user_id: user_id,
430476
gross_amount: amount,
431477
net_amount: amount,
@@ -435,8 +481,10 @@ defmodule Algora.Bounties do
435481
})
436482
|> Algora.Validations.validate_positive(:gross_amount)
437483
|> Algora.Validations.validate_positive(:net_amount)
438-
|> foreign_key_constraint(:tip_id)
439484
|> foreign_key_constraint(:user_id)
485+
|> foreign_key_constraint(:tip_id)
486+
|> foreign_key_constraint(:bounty_id)
487+
|> foreign_key_constraint(:claim_id)
440488
|> Repo.insert()
441489
end
442490

0 commit comments

Comments
 (0)