@@ -129,77 +129,87 @@ defmodule AlgoraWeb.Webhooks.StripeController do
129
129
end
130
130
131
131
defp process_charge_succeeded (
132
- % Stripe.Event { type: "charge.succeeded" , data: % { object: % Stripe.Charge { id: charge_id } } } ,
132
+ % Stripe.Event { type: "charge.succeeded" , data: % { object: % Stripe.Charge { id: charge_id , captured: captured } } } ,
133
133
group_id
134
134
)
135
135
when is_binary ( group_id ) do
136
136
Repo . transact ( fn ->
137
+ status = if captured , do: :succeeded , else: :requires_capture
138
+ succeeded_at = if captured , do: DateTime . utc_now ( )
139
+
137
140
{ _ , txs } =
138
141
Repo . update_all ( from ( t in Transaction , where: t . group_id == ^ group_id , select: t ) ,
139
- set: [ status: :succeeded , succeeded_at: DateTime . utc_now ( ) ]
142
+ set: [
143
+ status: status ,
144
+ succeeded_at: succeeded_at ,
145
+ provider: "stripe" ,
146
+ provider_id: charge_id ,
147
+ provider_charge_id: charge_id
148
+ ]
140
149
)
141
150
142
- Repo . update_all ( from ( t in Transaction , where: t . group_id == ^ group_id ) ,
143
- set: [ provider: "stripe" , provider_id: charge_id ]
144
- )
145
-
146
- bounty_ids = txs |> Enum . map ( & & 1 . bounty_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
147
- tip_ids = txs |> Enum . map ( & & 1 . tip_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
148
- contract_ids = txs |> Enum . map ( & & 1 . contract_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
149
- claim_ids = txs |> Enum . map ( & & 1 . claim_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
150
-
151
- Repo . update_all ( from ( b in Bounty , where: b . id in ^ bounty_ids ) , set: [ status: :paid ] )
152
- Repo . update_all ( from ( t in Tip , where: t . id in ^ tip_ids ) , set: [ status: :paid ] )
153
- Repo . update_all ( from ( c in Contract , where: c . id in ^ contract_ids ) , set: [ status: :paid ] )
154
- # TODO: add and use a new "paid" status for claims
155
- Repo . update_all ( from ( c in Claim , where: c . id in ^ claim_ids ) , set: [ status: :approved ] )
156
-
157
- activities_result =
158
- txs
159
- |> Enum . filter ( & ( & 1 . type == :credit ) )
160
- |> Enum . reduce_while ( :ok , fn tx , :ok ->
161
- case Repo . insert_activity ( tx , % { type: :transaction_succeeded , notify_users: [ tx . user_id ] } ) do
162
- { :ok , _ } -> { :cont , :ok }
163
- error -> { :halt , error }
164
- end
165
- end )
166
-
167
- jobs_result =
168
- txs
169
- |> Enum . filter ( & ( & 1 . type == :credit ) )
170
- |> Enum . reduce_while ( :ok , fn credit , :ok ->
171
- case Payments . fetch_active_account ( credit . user_id ) do
172
- { :ok , _account } ->
173
- case % { credit_id: credit . id }
174
- |> Payments.Jobs.ExecutePendingTransfer . new ( )
175
- |> Oban . insert ( ) do
176
- { :ok , _job } -> { :cont , :ok }
177
- error -> { :halt , error }
178
- end
179
-
180
- { :error , :no_active_account } ->
181
- case % { credit_id: credit . id }
182
- |> Bounties.Jobs.PromptPayoutConnect . new ( )
183
- |> Oban . insert ( ) do
184
- { :ok , _job } -> { :cont , :ok }
185
- error -> { :halt , error }
186
- end
187
- end
188
- end )
189
-
190
- with txs when txs != [ ] <- txs ,
191
- :ok <- activities_result ,
192
- :ok <- jobs_result do
151
+ if status == :succeeded do
152
+ bounty_ids = txs |> Enum . map ( & & 1 . bounty_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
153
+ tip_ids = txs |> Enum . map ( & & 1 . tip_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
154
+ contract_ids = txs |> Enum . map ( & & 1 . contract_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
155
+ claim_ids = txs |> Enum . map ( & & 1 . claim_id ) |> Enum . reject ( & is_nil / 1 ) |> Enum . uniq ( )
156
+
157
+ Repo . update_all ( from ( b in Bounty , where: b . id in ^ bounty_ids ) , set: [ status: :paid ] )
158
+ Repo . update_all ( from ( t in Tip , where: t . id in ^ tip_ids ) , set: [ status: :paid ] )
159
+ Repo . update_all ( from ( c in Contract , where: c . id in ^ contract_ids ) , set: [ status: :paid ] )
160
+ # TODO: add and use a new "paid" status for claims
161
+ Repo . update_all ( from ( c in Claim , where: c . id in ^ claim_ids ) , set: [ status: :approved ] )
162
+
163
+ activities_result =
164
+ txs
165
+ |> Enum . filter ( & ( & 1 . type == :credit ) )
166
+ |> Enum . reduce_while ( :ok , fn tx , :ok ->
167
+ case Repo . insert_activity ( tx , % { type: :transaction_succeeded , notify_users: [ tx . user_id ] } ) do
168
+ { :ok , _ } -> { :cont , :ok }
169
+ error -> { :halt , error }
170
+ end
171
+ end )
172
+
173
+ jobs_result =
174
+ txs
175
+ |> Enum . filter ( & ( & 1 . type == :credit ) )
176
+ |> Enum . reduce_while ( :ok , fn credit , :ok ->
177
+ case Payments . fetch_active_account ( credit . user_id ) do
178
+ { :ok , _account } ->
179
+ case % { credit_id: credit . id }
180
+ |> Payments.Jobs.ExecutePendingTransfer . new ( )
181
+ |> Oban . insert ( ) do
182
+ { :ok , _job } -> { :cont , :ok }
183
+ error -> { :halt , error }
184
+ end
185
+
186
+ { :error , :no_active_account } ->
187
+ case % { credit_id: credit . id }
188
+ |> Bounties.Jobs.PromptPayoutConnect . new ( )
189
+ |> Oban . insert ( ) do
190
+ { :ok , _job } -> { :cont , :ok }
191
+ error -> { :halt , error }
192
+ end
193
+ end
194
+ end )
195
+
196
+ with txs when txs != [ ] <- txs ,
197
+ :ok <- activities_result ,
198
+ :ok <- jobs_result do
199
+ Payments . broadcast ( )
200
+ { :ok , nil }
201
+ else
202
+ { :error , reason } ->
203
+ Logger . error ( "Failed to update transactions: #{ inspect ( reason ) } " )
204
+ { :error , :failed_to_update_transactions }
205
+
206
+ _error ->
207
+ Logger . error ( "Failed to update transactions" )
208
+ { :error , :failed_to_update_transactions }
209
+ end
210
+ else
193
211
Payments . broadcast ( )
194
212
{ :ok , nil }
195
- else
196
- { :error , reason } ->
197
- Logger . error ( "Failed to update transactions: #{ inspect ( reason ) } " )
198
- { :error , :failed_to_update_transactions }
199
-
200
- _error ->
201
- Logger . error ( "Failed to update transactions" )
202
- { :error , :failed_to_update_transactions }
203
213
end
204
214
end )
205
215
end
0 commit comments