@@ -9,6 +9,7 @@ defmodule Algora.Payments do
99 alias Algora.Bounties
1010 alias Algora.Bounties.Bounty
1111 alias Algora.Bounties.Claim
12+ alias Algora.Bounties.Jobs.PromptPayoutConnect
1213 alias Algora.Bounties.Tip
1314 alias Algora.Jobs.JobPosting
1415 alias Algora.MoneyUtils
@@ -737,7 +738,7 @@ defmodule Algora.Payments do
737738
738739 { :error , :no_active_account } ->
739740 case % { credit_id: credit . id }
740- |> Bounties.Jobs. PromptPayoutConnect. new ( )
741+ |> PromptPayoutConnect . new ( )
741742 |> Oban . insert ( ) do
742743 { :ok , _job } -> { :cont , :ok }
743744 error -> { :halt , error }
@@ -761,4 +762,81 @@ defmodule Algora.Payments do
761762 end
762763 end )
763764 end
765+
766+ def process_release (
767+ % Stripe.Charge { id: charge_id , captured: true , payment_intent: payment_intent_id } ,
768+ group_id ,
769+ amount ,
770+ recipient
771+ ) do
772+ Repo . transact ( fn ->
773+ tx = Repo . get_by ( Transaction , group_id: group_id , type: :charge , status: :succeeded )
774+
775+ user = Repo . get_by ( User , id: tx . user_id )
776+ bounty = Repo . get_by ( Bounty , id: tx . bounty_id )
777+
778+ Algora.Admin . alert (
779+ "Release #{ amount } escrow to #{ recipient . handle } for #{ AlgoraWeb.Endpoint . url ( ) } /#{ user . handle } /contracts/#{ bounty . id } " ,
780+ :critical
781+ )
782+
783+ debit_id = Nanoid . generate ( )
784+ credit_id = Nanoid . generate ( )
785+
786+ with { :ok , debit0 } <- Repo . fetch_by ( Transaction , group_id: group_id , type: :debit , status: :requires_release ) ,
787+ { :ok , _ } <-
788+ debit0
789+ |> change ( % {
790+ net_amount: Money . sub! ( debit0 . net_amount , amount ) ,
791+ gross_amount: Money . sub! ( debit0 . gross_amount , amount )
792+ } )
793+ |> Repo . update ( ) ,
794+ { :ok , credit0 } <- Repo . fetch_by ( Transaction , group_id: group_id , type: :credit , status: :requires_release ) ,
795+ { :ok , _ } <-
796+ credit0
797+ |> change ( % {
798+ net_amount: Money . add! ( credit0 . net_amount , amount ) ,
799+ gross_amount: Money . add! ( credit0 . gross_amount , amount )
800+ } )
801+ |> Repo . update ( ) ,
802+ { :ok , _debit } <-
803+ Repo . insert ( % Transaction {
804+ id: debit_id ,
805+ provider: "stripe" ,
806+ provider_id: charge_id ,
807+ provider_charge_id: charge_id ,
808+ provider_payment_intent_id: payment_intent_id ,
809+ type: :debit ,
810+ status: :succeeded ,
811+ succeeded_at: DateTime . utc_now ( ) ,
812+ bounty_id: tx . bounty_id ,
813+ user_id: tx . user_id ,
814+ gross_amount: amount ,
815+ net_amount: amount ,
816+ total_fee: Money . zero ( :USD ) ,
817+ linked_transaction_id: credit_id ,
818+ group_id: group_id
819+ } ) ,
820+ { :ok , _credit } <-
821+ Repo . insert ( % Transaction {
822+ id: credit_id ,
823+ provider: "stripe" ,
824+ provider_id: charge_id ,
825+ provider_charge_id: charge_id ,
826+ provider_payment_intent_id: payment_intent_id ,
827+ type: :credit ,
828+ status: :initialized ,
829+ succeeded_at: DateTime . utc_now ( ) ,
830+ bounty_id: tx . bounty_id ,
831+ user_id: recipient . id ,
832+ gross_amount: amount ,
833+ net_amount: amount ,
834+ total_fee: Money . zero ( :USD ) ,
835+ linked_transaction_id: debit_id ,
836+ group_id: group_id
837+ } ) do
838+ { :ok , nil }
839+ end
840+ end )
841+ end
764842end
0 commit comments