@@ -18,7 +18,7 @@ defmodule AlgoraWeb.ContractLive do
1818
1919 require Logger
2020
21- defp tip_options , do: [ { "None" , 0 } , { "10%" , 10 } , { "20%" , 20 } , { "50%" , 50 } ]
21+ # defp tip_options, do: [{"None", 0}, {"10%", 10}, {"20%", 20}, {"50%", 50}]
2222
2323 defmodule RewardBountyForm do
2424 @ moduledoc false
@@ -109,7 +109,7 @@ defmodule AlgoraWeb.ContractLive do
109109 ticket_body_html = Algora.Markdown . render ( bounty . ticket . description )
110110
111111 reward_changeset =
112- RewardBountyForm . changeset ( % RewardBountyForm { } , % { tip_percentage: 0 } )
112+ RewardBountyForm . changeset ( % RewardBountyForm { } , % { amount: bounty . amount } )
113113
114114 { :ok , thread } = Chat . get_or_create_bounty_thread ( bounty )
115115 messages = thread . id |> Chat . list_messages ( ) |> Repo . preload ( :sender )
@@ -233,6 +233,18 @@ defmodule AlgoraWeb.ContractLive do
233233 end
234234 end
235235
236+ @ impl true
237+ def handle_event ( "authorize_with_stripe" , _params , socket ) do
238+ case authorize_payment ( socket , socket . assigns . bounty ) do
239+ { :ok , session_url } ->
240+ { :noreply , redirect ( socket , external: session_url ) }
241+
242+ { :error , reason } ->
243+ Logger . error ( "Failed to create payment session: #{ inspect ( reason ) } " )
244+ { :noreply , put_flash ( socket , :error , "Something went wrong" ) }
245+ end
246+ end
247+
236248 @ impl true
237249 def handle_event ( _event , _params , socket ) do
238250 { :noreply , socket }
@@ -508,13 +520,13 @@ defmodule AlgoraWeb.ContractLive do
508520
509521 < . drawer :if = { @ current_user } show = { @ show_reward_modal } on_cancel = "close_drawer " >
510522 < . drawer_header >
511- < . drawer_title > Pay Contract </ . drawer_title >
523+ < . drawer_title > Authorize payment </ . drawer_title >
512524 < . drawer_description >
513- You can pay any amount at any time .
525+ You will be charged once { @ contractor . name } accepts the contract .
514526 </ . drawer_description >
515527 </ . drawer_header >
516528 < . drawer_content class = "mt-4 " >
517- < . form for = { @ reward_form } phx-change = "validate_reward " phx-submit = "pay_with_stripe " >
529+ < . form for = { @ reward_form } phx-change = "validate_reward " phx-submit = "authorize_with_stripe " >
518530 < div class = "flex flex-col gap-8 " >
519531 < div class = "grid grid-cols-2 gap-8 " >
520532 < . card >
@@ -527,9 +539,10 @@ defmodule AlgoraWeb.ContractLive do
527539 label = "Amount "
528540 icon = "tabler-currency-dollar "
529541 field = { @ reward_form [ :amount ] }
542+ disabled
530543 />
531544
532- < div >
545+ <%!-- < div>
533546 <.label>Tip</.label>
534547 <div class="mt-2">
535548 <.radio_group
@@ -538,7 +551,7 @@ defmodule AlgoraWeb.ContractLive do
538551 options={tip_options()}
539552 />
540553 </div>
541- </ div >
554+ </div> --%>
542555 </ div >
543556 </ . card_content >
544557 </ . card >
@@ -590,7 +603,7 @@ defmodule AlgoraWeb.ContractLive do
590603 Cancel
591604 </ . button >
592605 < . button type = "submit " >
593- Pay with Stripe < . icon name = "tabler-arrow-right " class = "-mr-1 ml-2 h-4 w-4 " />
606+ Authorize with Stripe < . icon name = "tabler-arrow-right " class = "-mr-1 ml-2 h-4 w-4 " />
594607 </ . button >
595608 </ div >
596609 </ div >
@@ -625,6 +638,14 @@ defmodule AlgoraWeb.ContractLive do
625638 )
626639 end
627640
641+ defp authorize_payment ( socket , bounty ) do
642+ Bounties . authorize_payment (
643+ % { owner: bounty . owner , amount: bounty . amount , bounty: bounty , claims: [ ] } ,
644+ ticket_ref: socket . assigns . ticket_ref ,
645+ recipient: socket . assigns . contractor
646+ )
647+ end
648+
628649 defp calculate_final_amount ( data_or_changeset ) do
629650 tip_percentage = get_field ( data_or_changeset , :tip_percentage ) || Decimal . new ( 0 )
630651 amount = get_field ( data_or_changeset , :amount ) || Money . zero ( :USD , no_fraction_if_integer: true )
0 commit comments