@@ -404,7 +404,6 @@ func StripeWebhook(w http.ResponseWriter, r *http.Request) {
404
404
http .Error (w , "error parsing stripe webhook JSON" , http .StatusInternalServerError )
405
405
return
406
406
}
407
-
408
407
if subscription .Items == nil {
409
408
utils .LogError (nil , fmt .Errorf ("error updating subscription no items found %v" , subscription ), 0 )
410
409
http .Error (w , "error updating subscription no items found" , http .StatusBadRequest )
@@ -419,7 +418,6 @@ func StripeWebhook(w http.ResponseWriter, r *http.Request) {
419
418
priceID := subscription .Items .Data [0 ].Price .ID
420
419
421
420
currSub , err := db .StripeGetSubscription (subscription .ID )
422
-
423
421
if err != nil {
424
422
logger .WithError (err ).Error ("error getting subscription from database with id " , subscription .ID )
425
423
http .Error (w , "error updating subscription could not get current subscription err:" + err .Error (), http .StatusInternalServerError )
@@ -435,13 +433,29 @@ func StripeWebhook(w http.ResponseWriter, r *http.Request) {
435
433
436
434
err = db .StripeUpdateSubscription (tx , priceID , subscription .ID , event .Data .Raw )
437
435
if err != nil {
438
- logger .WithError (err ).Error ("error updating user subscription" , subscription . ID )
436
+ logger .WithError (err ).WithField ( "subscription.ID" , subscription . ID ). Error ("error updating user subscription" )
439
437
http .Error (w , "error updating user subscription, customer: " + subscription .Customer .ID , http .StatusInternalServerError )
440
438
return
441
439
}
442
440
443
441
if utils .GetPurchaseGroup (priceID ) == utils .GROUP_MOBILE || utils .GetPurchaseGroup (priceID ) == utils .GROUP_ADDON {
444
- err := db .ChangeProductIDFromStripe (tx , subscription .ID , utils .PriceIdToProductId (priceID ))
442
+ _ , err := db .GetUserSubscriptionIDByStripe (subscription .ID )
443
+ if err != nil {
444
+ if err == sql .ErrNoRows {
445
+ // subscription changed from one group to another, entry does not exist yet
446
+ err = insertMobileSubscription (tx , subscription )
447
+ if err != nil {
448
+ logger .WithError (err ).WithField ("subscription.ID" , subscription .ID ).Error ("error updating stripe mobile subscription, no users_app_subs id found for subscription id" )
449
+ http .Error (w , "error updating stripe mobile subscription, no users_app_subs id found for subscription id, customer: " + subscription .Customer .ID , http .StatusInternalServerError )
450
+ return
451
+ }
452
+ } else {
453
+ logger .WithError (err ).WithField ("subscription.ID" , subscription .ID ).Error ("error updating user subscription, calling db.GetUserSubscriptionIDByStripe" )
454
+ http .Error (w , "error updating user subscription, customer: " + subscription .Customer .ID , http .StatusInternalServerError )
455
+ return
456
+ }
457
+ }
458
+ err = db .ChangeProductIDFromStripe (tx , subscription .ID , utils .PriceIdToProductId (priceID ))
445
459
if err != nil {
446
460
logger .WithError (err ).Error ("error updating stripe mobile subscription" , subscription .ID )
447
461
http .Error (w , "error updating stripe mobile subscription customer: " + subscription .Customer .ID , http .StatusInternalServerError )
@@ -602,31 +616,40 @@ func createNewStripeSubscription(subscription stripe.Subscription, event stripe.
602
616
if err != nil {
603
617
return err
604
618
}
605
-
606
619
if utils .GetPurchaseGroup (subscription .Items .Data [0 ].Price .ID ) == utils .GROUP_MOBILE || utils .GetPurchaseGroup (subscription .Items .Data [0 ].Price .ID ) == utils .GROUP_ADDON {
607
- userID , err := db .StripeGetCustomerUserId (subscription .Customer .ID )
608
- if err != nil {
609
- return err
610
- }
611
- details := types.MobileSubscription {
612
- ProductID : utils .PriceIdToProductId (subscription .Items .Data [0 ].Price .ID ),
613
- PriceMicros : uint64 (subscription .Items .Data [0 ].Price .UnitAmount ),
614
- Currency : string (subscription .Items .Data [0 ].Price .Currency ),
615
- Transaction : types.MobileSubscriptionTransactionGeneric {
616
- Type : "stripe" ,
617
- Receipt : subscription .ID ,
618
- ID : subscription .Items .Data [0 ].Price .ID ,
619
- },
620
- Valid : false ,
621
- }
622
- err = db .InsertMobileSubscription (tx , userID , details , details .Transaction .Type , details .Transaction .Receipt , 0 , "" , subscription .ID )
620
+ err = insertMobileSubscription (tx , subscription )
623
621
if err != nil {
624
622
return err
625
623
}
626
624
}
627
625
err = tx .Commit ()
626
+ if err != nil {
627
+ return err
628
+ }
629
+ return nil
630
+ }
628
631
629
- return err
632
+ func insertMobileSubscription (tx * sql.Tx , subscription stripe.Subscription ) error {
633
+ userID , err := db .StripeGetCustomerUserId (subscription .Customer .ID )
634
+ if err != nil {
635
+ return err
636
+ }
637
+ details := types.MobileSubscription {
638
+ ProductID : utils .PriceIdToProductId (subscription .Items .Data [0 ].Price .ID ),
639
+ PriceMicros : uint64 (subscription .Items .Data [0 ].Price .UnitAmount ),
640
+ Currency : string (subscription .Items .Data [0 ].Price .Currency ),
641
+ Transaction : types.MobileSubscriptionTransactionGeneric {
642
+ Type : "stripe" ,
643
+ Receipt : subscription .ID ,
644
+ ID : subscription .Items .Data [0 ].Price .ID ,
645
+ },
646
+ Valid : false ,
647
+ }
648
+ err = db .InsertMobileSubscription (tx , userID , details , details .Transaction .Type , details .Transaction .Receipt , 0 , "" , subscription .ID )
649
+ if err != nil {
650
+ return err
651
+ }
652
+ return nil
630
653
}
631
654
632
655
func emailCustomerAboutFailedPayment (email string ) {
0 commit comments