@@ -404,7 +404,6 @@ func StripeWebhook(w http.ResponseWriter, r *http.Request) {
404404 http .Error (w , "error parsing stripe webhook JSON" , http .StatusInternalServerError )
405405 return
406406 }
407-
408407 if subscription .Items == nil {
409408 utils .LogError (nil , fmt .Errorf ("error updating subscription no items found %v" , subscription ), 0 )
410409 http .Error (w , "error updating subscription no items found" , http .StatusBadRequest )
@@ -419,7 +418,6 @@ func StripeWebhook(w http.ResponseWriter, r *http.Request) {
419418 priceID := subscription .Items .Data [0 ].Price .ID
420419
421420 currSub , err := db .StripeGetSubscription (subscription .ID )
422-
423421 if err != nil {
424422 logger .WithError (err ).Error ("error getting subscription from database with id " , subscription .ID )
425423 http .Error (w , "error updating subscription could not get current subscription err:" + err .Error (), http .StatusInternalServerError )
@@ -435,18 +433,59 @@ func StripeWebhook(w http.ResponseWriter, r *http.Request) {
435433
436434 err = db .StripeUpdateSubscription (tx , priceID , subscription .ID , event .Data .Raw )
437435 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" )
439437 http .Error (w , "error updating user subscription, customer: " + subscription .Customer .ID , http .StatusInternalServerError )
440438 return
441439 }
442440
443441 if utils .GetPurchaseGroup (priceID ) == utils .GROUP_MOBILE || utils .GetPurchaseGroup (priceID ) == utils .GROUP_ADDON {
444- err := db .ChangeProductIDFromStripe (tx , subscription .ID , utils .PriceIdToProductId (priceID ))
442+ appSubID , err := db .GetUserSubscriptionIDByStripe (subscription .ID )
443+ if err != nil {
444+ if err == sql .ErrNoRows {
445+ // subscription changed from nonmobile to mobile, insert mobile subscription
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 ))
445459 if err != nil {
446460 logger .WithError (err ).Error ("error updating stripe mobile subscription" , subscription .ID )
447461 http .Error (w , "error updating stripe mobile subscription customer: " + subscription .Customer .ID , http .StatusInternalServerError )
448462 return
449463 }
464+ err = db .UpdateUserSubscription (tx , appSubID , true , 0 , "" )
465+ if err != nil {
466+ logger .WithError (err ).Error ("error updating stripe mobile subscription (sub updated)" , subscription .ID )
467+ http .Error (w , "error updating subscription" , http .StatusInternalServerError )
468+ return
469+ }
470+ } else {
471+ appSubID , err := db .GetUserSubscriptionIDByStripe (subscription .ID )
472+ if err != nil {
473+ if err == sql .ErrNoRows {
474+ // no mobile-subscription found for this stripe-subscription, nothing to do
475+ } else {
476+ logger .WithError (err ).WithField ("subscription.ID" , subscription .ID ).Error ("error updating user subscription, calling db.GetUserSubscriptionIDByStripe" )
477+ http .Error (w , "error updating subscription" , http .StatusInternalServerError )
478+ return
479+ }
480+ } else {
481+ // subscription changed from mobile to nonmobile, deactivate mobile subscription
482+ err = db .UpdateUserSubscription (tx , appSubID , false , time .Now ().Unix (), "user_canceled" )
483+ if err != nil {
484+ logger .WithError (err ).Error ("error updating stripe mobile subscription (sub updated)" , subscription .ID )
485+ http .Error (w , "error updating subscription" , http .StatusInternalServerError )
486+ return
487+ }
488+ }
450489 }
451490
452491 err = tx .Commit ()
@@ -602,31 +641,40 @@ func createNewStripeSubscription(subscription stripe.Subscription, event stripe.
602641 if err != nil {
603642 return err
604643 }
605-
606644 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 )
645+ err = insertMobileSubscription (tx , subscription )
623646 if err != nil {
624647 return err
625648 }
626649 }
627650 err = tx .Commit ()
651+ if err != nil {
652+ return err
653+ }
654+ return nil
655+ }
628656
629- return err
657+ func insertMobileSubscription (tx * sql.Tx , subscription stripe.Subscription ) error {
658+ userID , err := db .StripeGetCustomerUserId (subscription .Customer .ID )
659+ if err != nil {
660+ return err
661+ }
662+ details := types.MobileSubscription {
663+ ProductID : utils .PriceIdToProductId (subscription .Items .Data [0 ].Price .ID ),
664+ PriceMicros : uint64 (subscription .Items .Data [0 ].Price .UnitAmount ),
665+ Currency : string (subscription .Items .Data [0 ].Price .Currency ),
666+ Transaction : types.MobileSubscriptionTransactionGeneric {
667+ Type : "stripe" ,
668+ Receipt : subscription .ID ,
669+ ID : subscription .Items .Data [0 ].Price .ID ,
670+ },
671+ Valid : false ,
672+ }
673+ err = db .InsertMobileSubscription (tx , userID , details , details .Transaction .Type , details .Transaction .Receipt , 0 , "" , subscription .ID )
674+ if err != nil {
675+ return err
676+ }
677+ return nil
630678}
631679
632680func emailCustomerAboutFailedPayment (email string ) {
0 commit comments