<commit_before>package stripe

import (
	"socialapi/workers/payment/models"

	stripe "github.com/stripe/stripe-go"
	stripeSub "github.com/stripe/stripe-go/sub"
)

func Subscribe(token, accId, email, planTitle, planInterval string) error {
	plan, err := FindPlanByTitleAndInterval(planTitle, planInterval)
	if err != nil {
		return err
	}

	customer, err := FindCustomerByOldId(accId)
	if err != nil && err != ErrCustomerNotFound {
		return err
	}

	if customer == nil {
		if IsEmpty(token) {
			return ErrTokenIsEmpty
		}

		customer, err = CreateCustomer(token, accId, email)
		if err != nil {
			return err
		}
	}

	subscriptions, err := FindCustomerActiveSubscriptions(customer)
	if err != nil {
		return err
	}

	if IsNoSubscriptions(subscriptions) {
		_, err = CreateSubscription(customer, plan)
		return err
	}

	if IsOverSubscribed(subscriptions) {
		return ErrCustomerHasTooManySubscriptions
	}

	var currentSubscription = subscriptions[0]

	if IsSubscribedToPlan(currentSubscription, plan) {
		return ErrCustomerAlreadySubscribedToPlan
	}

	if !IsFreePlan(plan) {
		err = UpdateSubscriptionForCustomer(customer, subscriptions, plan)
		return err
	}

	err = CancelSubscription(customer, &currentSubscription)<commit_msg>payment: fix bug where user downgrades to free and cc is removed and subscribes again<commit_after>package stripe

import (
	"socialapi/workers/payment/models"

	stripe "github.com/stripe/stripe-go"
	stripeSub "github.com/stripe/stripe-go/sub"
)

func Subscribe(token, accId, email, planTitle, planInterval string) error {
	plan, err := FindPlanByTitleAndInterval(planTitle, planInterval)
	if err != nil {
		return err
	}

	customer, err := FindCustomerByOldId(accId)
	if err != nil && err != ErrCustomerNotFound {
		return err
	}

	if customer == nil {
		if IsEmpty(token) {
			return ErrTokenIsEmpty
		}

		customer, err = CreateCustomer(token, accId, email)
		if err != nil {
			return err
		}
	}

	resp, err := GetCreditCard(customer.OldId)
	if err != nil {
		return err
	}

	if IsEmpty(resp.LastFour) {
		if IsEmpty(token) {
			return ErrTokenIsEmpty
		}

		err := UpdateCreditCard(customer.OldId, token)
		if err != nil {
			return err
		}
	}

	subscriptions, err := FindCustomerActiveSubscriptions(customer)
	if err != nil {
		return err
	}

	if IsNoSubscriptions(subscriptions) {
		_, err = CreateSubscription(customer, plan)
		return err
	}

	if IsOverSubscribed(subscriptions) {
		return ErrCustomerHasTooManySubscriptions
	}

	var currentSubscription = subscriptions[0]

	if IsSubscribedToPlan(currentSubscription, plan) {
		return ErrCustomerAlreadySubscribedToPlan
	}

	if !IsFreePlan(plan) {
		err = UpdateSubscriptionForCustomer(customer, subscriptions, plan)
		return err
	}

	err = CancelSubscription(customer, &currentSubscription)
{'fraction_non_alphanumeric': 0.0943464169967591, 'fraction_numerical': 0.0007202016564638099, 'mean_word_length': 4.556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '930647', 'n_tokens_mistral': 961, 'n_tokens_neox': 892, 'n_words': 215}