@@ -5,11 +5,11 @@ defmodule Algora.Payments do
5
5
6
6
alias Algora.Accounts
7
7
alias Algora.Accounts.User
8
+ alias Algora.Admin
8
9
alias Algora.Bounties
9
10
alias Algora.Bounties.Bounty
10
11
alias Algora.Bounties.Claim
11
12
alias Algora.Bounties.Tip
12
- alias Algora.Contracts.Contract
13
13
alias Algora.MoneyUtils
14
14
alias Algora.Payments.Account
15
15
alias Algora.Payments.Customer
@@ -617,7 +617,7 @@ defmodule Algora.Payments do
617
617
{ _ , txs } =
618
618
Repo . update_all ( from ( t in Transaction , where: t . group_id == ^ group_id , select: t ) ,
619
619
set: [
620
- status: :succeeded ,
620
+ status: :processing ,
621
621
succeeded_at: DateTime . utc_now ( ) ,
622
622
provider: "stripe" ,
623
623
provider_id: charge_id ,
@@ -627,18 +627,61 @@ defmodule Algora.Payments do
627
627
)
628
628
629
629
bounty_ids = txs |> Enum . map ( & & 1 . bounty_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
630
+
631
+ bounties =
632
+ from ( b in Bounty ,
633
+ where: b . id in ^ bounty_ids ,
634
+ join: u in assoc ( b , :owner ) ,
635
+ join: t in assoc ( b , :ticket ) ,
636
+ select: % { b | ticket: t , owner: u }
637
+ )
638
+ |> Repo . all ( )
639
+ |> Map . new ( & { & 1 . id , & 1 } )
640
+
641
+ { issue_bounty_ids , contract_bounty_ids } =
642
+ Enum . split_with ( bounty_ids , fn id ->
643
+ bounty = bounties [ id ]
644
+ bounty && bounty . ticket . repository_id
645
+ end )
646
+
630
647
tip_ids = txs |> Enum . map ( & & 1 . tip_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
631
- contract_ids = txs |> Enum . map ( & & 1 . contract_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
632
648
claim_ids = txs |> Enum . map ( & & 1 . claim_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
633
649
634
- Repo . update_all ( from ( b in Bounty , where: b . id in ^ bounty_ids ) , set: [ status: :paid ] )
650
+ Repo . update_all ( from ( b in Bounty , where: b . id in ^ issue_bounty_ids ) , set: [ status: :paid ] )
635
651
Repo . update_all ( from ( t in Tip , where: t . id in ^ tip_ids ) , set: [ status: :paid ] )
636
- Repo . update_all ( from ( c in Contract , where: c . id in ^ contract_ids ) , set: [ status: :paid ] )
637
652
# TODO: add and use a new "paid" status for claims
638
653
Repo . update_all ( from ( c in Claim , where: c . id in ^ claim_ids ) , set: [ status: :approved ] )
639
654
655
+ auto_txs =
656
+ Enum . filter ( txs , fn tx ->
657
+ bounty = bounties [ tx . bounty_id ]
658
+
659
+ contract? = tx . bounty_id in contract_bounty_ids
660
+
661
+ if contract? do
662
+ Admin . alert (
663
+ "Contract payment received. URL: #{ AlgoraWeb.Endpoint . url ( ) } /#{ bounty . owner . handle } /contracts/#{ bounty . id } " ,
664
+ :info
665
+ )
666
+ end
667
+
668
+ tx . type != :credit or not contract?
669
+ end )
670
+
671
+ Repo . update_all (
672
+ from ( t in Transaction , where: t . group_id == ^ group_id and t . id in ^ Enum . map ( auto_txs , & & 1 . id ) , select: t ) ,
673
+ set: [
674
+ status: :succeeded ,
675
+ succeeded_at: DateTime . utc_now ( ) ,
676
+ provider: "stripe" ,
677
+ provider_id: charge_id ,
678
+ provider_charge_id: charge_id ,
679
+ provider_payment_intent_id: payment_intent_id
680
+ ]
681
+ )
682
+
640
683
activities_result =
641
- txs
684
+ auto_txs
642
685
|> Enum . filter ( & ( & 1 . type == :credit ) )
643
686
|> Enum . reduce_while ( :ok , fn tx , :ok ->
644
687
case Repo . insert_activity ( tx , % { type: :transaction_succeeded , notify_users: [ tx . user_id ] } ) do
@@ -648,7 +691,7 @@ defmodule Algora.Payments do
648
691
end )
649
692
650
693
jobs_result =
651
- txs
694
+ auto_txs
652
695
|> Enum . filter ( & ( & 1 . type == :credit ) )
653
696
|> Enum . reduce_while ( :ok , fn credit , :ok ->
654
697
case fetch_active_account ( credit . user_id ) do
0 commit comments