@@ -245,8 +245,6 @@ defmodule Algora.Bounties do
245
245
) ::
246
246
{ :ok , String . t ( ) } | { :error , atom ( ) }
247
247
def create_tip ( % { creator: creator , owner: owner , recipient: recipient , amount: amount } , opts \\ [ ] ) do
248
- ticket_ref = opts [ :ticket_ref ]
249
-
250
248
changeset =
251
249
Tip . changeset ( % Tip { } , % {
252
250
amount: amount ,
@@ -255,6 +253,39 @@ defmodule Algora.Bounties do
255
253
recipient_id: recipient . id
256
254
} )
257
255
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
+
258
289
# Initialize transaction IDs
259
290
charge_id = Nanoid . generate ( )
260
291
debit_id = Nanoid . generate ( )
@@ -279,12 +310,7 @@ defmodule Algora.Bounties do
279
310
currency: currency ,
280
311
product_data: % {
281
312
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 ] } " ) ,
288
314
images: [ recipient . avatar_url ]
289
315
}
290
316
} ,
@@ -309,11 +335,12 @@ defmodule Algora.Bounties do
309
335
]
310
336
311
337
Repo . transact ( fn ->
312
- with { :ok , tip } <- Repo . insert ( changeset ) ,
313
- { :ok , _charge } <-
338
+ with { :ok , _charge } <-
314
339
initialize_charge ( % {
315
340
id: charge_id ,
316
- tip: tip ,
341
+ tip_id: opts [ :tip_id ] ,
342
+ bounty_id: opts [ :bounty_id ] ,
343
+ claim_id: opts [ :claim_id ] ,
317
344
user_id: creator . id ,
318
345
gross_amount: gross_amount ,
319
346
net_amount: amount ,
@@ -324,7 +351,9 @@ defmodule Algora.Bounties do
324
351
{ :ok , _debit } <-
325
352
initialize_debit ( % {
326
353
id: debit_id ,
327
- tip: tip ,
354
+ tip_id: opts [ :tip_id ] ,
355
+ bounty_id: opts [ :bounty_id ] ,
356
+ claim_id: opts [ :claim_id ] ,
328
357
amount: amount ,
329
358
user_id: creator . id ,
330
359
linked_transaction_id: credit_id ,
@@ -333,16 +362,17 @@ defmodule Algora.Bounties do
333
362
{ :ok , _credit } <-
334
363
initialize_credit ( % {
335
364
id: credit_id ,
336
- tip: tip ,
365
+ tip_id: opts [ :tip_id ] ,
366
+ bounty_id: opts [ :bounty_id ] ,
367
+ claim_id: opts [ :claim_id ] ,
337
368
amount: amount ,
338
369
user_id: recipient . id ,
339
370
linked_transaction_id: debit_id ,
340
371
group_id: tx_group_id
341
372
} ) ,
342
373
{ :ok , session } <-
343
374
Payments . create_stripe_session ( line_items , % {
344
- # Mandatory for some countries like India
345
- description: "Tip payment for OSS contributions" ,
375
+ description: description ,
346
376
metadata: % { "version" => "2" , "group_id" => tx_group_id }
347
377
} ) do
348
378
{ :ok , session . url }
@@ -352,7 +382,9 @@ defmodule Algora.Bounties do
352
382
353
383
defp initialize_charge ( % {
354
384
id: id ,
355
- tip: tip ,
385
+ tip_id: tip_id ,
386
+ bounty_id: bounty_id ,
387
+ claim_id: claim_id ,
356
388
user_id: user_id ,
357
389
gross_amount: gross_amount ,
358
390
net_amount: net_amount ,
@@ -366,7 +398,9 @@ defmodule Algora.Bounties do
366
398
provider: "stripe" ,
367
399
type: :charge ,
368
400
status: :initialized ,
369
- tip_id: tip . id ,
401
+ tip_id: tip_id ,
402
+ bounty_id: bounty_id ,
403
+ claim_id: claim_id ,
370
404
user_id: user_id ,
371
405
gross_amount: gross_amount ,
372
406
net_amount: net_amount ,
@@ -377,14 +411,18 @@ defmodule Algora.Bounties do
377
411
|> Algora.Validations . validate_positive ( :gross_amount )
378
412
|> Algora.Validations . validate_positive ( :net_amount )
379
413
|> Algora.Validations . validate_positive ( :total_fee )
380
- |> foreign_key_constraint ( :tip_id )
381
414
|> foreign_key_constraint ( :user_id )
415
+ |> foreign_key_constraint ( :tip_id )
416
+ |> foreign_key_constraint ( :bounty_id )
417
+ |> foreign_key_constraint ( :claim_id )
382
418
|> Repo . insert ( )
383
419
end
384
420
385
421
defp initialize_debit ( % {
386
422
id: id ,
387
- tip: tip ,
423
+ tip_id: tip_id ,
424
+ bounty_id: bounty_id ,
425
+ claim_id: claim_id ,
388
426
amount: amount ,
389
427
user_id: user_id ,
390
428
linked_transaction_id: linked_transaction_id ,
@@ -396,7 +434,9 @@ defmodule Algora.Bounties do
396
434
provider: "stripe" ,
397
435
type: :debit ,
398
436
status: :initialized ,
399
- tip_id: tip . id ,
437
+ tip_id: tip_id ,
438
+ bounty_id: bounty_id ,
439
+ claim_id: claim_id ,
400
440
user_id: user_id ,
401
441
gross_amount: amount ,
402
442
net_amount: amount ,
@@ -406,14 +446,18 @@ defmodule Algora.Bounties do
406
446
} )
407
447
|> Algora.Validations . validate_positive ( :gross_amount )
408
448
|> Algora.Validations . validate_positive ( :net_amount )
409
- |> foreign_key_constraint ( :tip_id )
410
449
|> foreign_key_constraint ( :user_id )
450
+ |> foreign_key_constraint ( :tip_id )
451
+ |> foreign_key_constraint ( :bounty_id )
452
+ |> foreign_key_constraint ( :claim_id )
411
453
|> Repo . insert ( )
412
454
end
413
455
414
456
defp initialize_credit ( % {
415
457
id: id ,
416
- tip: tip ,
458
+ tip_id: tip_id ,
459
+ bounty_id: bounty_id ,
460
+ claim_id: claim_id ,
417
461
amount: amount ,
418
462
user_id: user_id ,
419
463
linked_transaction_id: linked_transaction_id ,
@@ -425,7 +469,9 @@ defmodule Algora.Bounties do
425
469
provider: "stripe" ,
426
470
type: :credit ,
427
471
status: :initialized ,
428
- tip_id: tip . id ,
472
+ tip_id: tip_id ,
473
+ bounty_id: bounty_id ,
474
+ claim_id: claim_id ,
429
475
user_id: user_id ,
430
476
gross_amount: amount ,
431
477
net_amount: amount ,
@@ -435,8 +481,10 @@ defmodule Algora.Bounties do
435
481
} )
436
482
|> Algora.Validations . validate_positive ( :gross_amount )
437
483
|> Algora.Validations . validate_positive ( :net_amount )
438
- |> foreign_key_constraint ( :tip_id )
439
484
|> foreign_key_constraint ( :user_id )
485
+ |> foreign_key_constraint ( :tip_id )
486
+ |> foreign_key_constraint ( :bounty_id )
487
+ |> foreign_key_constraint ( :claim_id )
440
488
|> Repo . insert ( )
441
489
end
442
490
0 commit comments