refactor patron BSON handlers
parent
b6bf4689d4
commit
b1e732a9b3
|
@ -0,0 +1,28 @@
|
|||
package lila.stripe
|
||||
|
||||
import lila.db.dsl._
|
||||
import reactivemongo.bson._
|
||||
|
||||
private[stripe] object BsonHandlers {
|
||||
|
||||
implicit val CentsBSONHandler = intAnyValHandler[Cents](_.value, Cents.apply)
|
||||
implicit val ChargeIdBSONHandler = stringAnyValHandler[ChargeId](_.value, ChargeId.apply)
|
||||
implicit val CustomerIdBSONHandler = stringAnyValHandler[CustomerId](_.value, CustomerId.apply)
|
||||
|
||||
object PatronHandlers {
|
||||
import Patron._
|
||||
implicit val PayPalEmailBSONHandler = stringAnyValHandler[PayPal.Email](_.value, PayPal.Email.apply)
|
||||
implicit val PayPalSubIdBSONHandler = stringAnyValHandler[PayPal.SubId](_.value, PayPal.SubId.apply)
|
||||
implicit val PayPalBSONHandler = Macros.handler[PayPal]
|
||||
implicit val StripeBSONHandler = Macros.handler[Stripe]
|
||||
implicit val UserIdBSONHandler = stringAnyValHandler[UserId](_.value, UserId.apply)
|
||||
implicit val PatronBSONHandler = Macros.handler[Patron]
|
||||
}
|
||||
|
||||
object ChargeHandlers {
|
||||
import Charge._
|
||||
implicit val StripeBSONHandler = Macros.handler[Stripe]
|
||||
implicit val PayPalBSONHandler = Macros.handler[PayPal]
|
||||
implicit val ChargeBSONHandler = Macros.handler[Charge]
|
||||
}
|
||||
}
|
|
@ -25,15 +25,6 @@ object Charge {
|
|||
cents = cents,
|
||||
date = DateTime.now)
|
||||
|
||||
case class Stripe(customerId: String) extends AnyVal
|
||||
case class Stripe(chargeId: ChargeId, customerId: CustomerId)
|
||||
case class PayPal(email: Option[String], subId: Option[String])
|
||||
|
||||
private[stripe] object BSONHandlers {
|
||||
import reactivemongo.bson._
|
||||
import lila.db.dsl._
|
||||
implicit val CentsBSONHandler = intAnyValHandler[Cents](_.value, Cents.apply)
|
||||
implicit val StripeBSONHandler = Macros.handler[Stripe]
|
||||
implicit val PayPalBSONHandler = Macros.handler[PayPal]
|
||||
implicit val ChargeBSONHandler = Macros.handler[Charge]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ import play.api.libs.json._
|
|||
object JsonHandlers {
|
||||
|
||||
implicit val StripeCustomerId = Reads.of[String].map(CustomerId.apply)
|
||||
implicit val StripeChargeId = Reads.of[String].map(ChargeId.apply)
|
||||
implicit val StripeCents = Reads.of[Int].map(Cents.apply)
|
||||
implicit val StripePlanReads = Json.reads[StripePlan]
|
||||
implicit val StripeSubscriptionReads = Json.reads[StripeSubscription]
|
||||
implicit val StripeSubscriptionsReads = Json.reads[StripeSubscriptions]
|
||||
|
|
|
@ -20,9 +20,7 @@ object Patron {
|
|||
case class UserId(value: String) extends AnyVal
|
||||
|
||||
case class Stripe(customerId: CustomerId)
|
||||
object Stripe {
|
||||
case class CustomerId(value: String) extends AnyVal
|
||||
}
|
||||
|
||||
case class PayPal(
|
||||
email: Option[PayPal.Email],
|
||||
subId: Option[PayPal.SubId],
|
||||
|
@ -33,16 +31,4 @@ object Patron {
|
|||
case class Email(value: String) extends AnyVal
|
||||
case class SubId(value: String) extends AnyVal
|
||||
}
|
||||
|
||||
private[stripe] object BSONHandlers {
|
||||
import reactivemongo.bson._
|
||||
import lila.db.dsl._
|
||||
implicit val StripeCustomerIdBSONHandler = stringAnyValHandler[CustomerId](_.value, CustomerId.apply)
|
||||
implicit val StripeBSONHandler = Macros.handler[Stripe]
|
||||
implicit val PayPalEmailBSONHandler = stringAnyValHandler[PayPal.Email](_.value, PayPal.Email.apply)
|
||||
implicit val PayPalSubIdBSONHandler = stringAnyValHandler[PayPal.SubId](_.value, PayPal.SubId.apply)
|
||||
implicit val PayPalBSONHandler = Macros.handler[PayPal]
|
||||
implicit val UserIdBSONHandler = stringAnyValHandler[UserId](_.value, UserId.apply)
|
||||
implicit val PatronBSONHandler = Macros.handler[Patron]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,8 +11,9 @@ final class StripeApi(
|
|||
chargeColl: Coll,
|
||||
bus: lila.common.Bus) {
|
||||
|
||||
import Patron.BSONHandlers._
|
||||
import Charge.BSONHandlers._
|
||||
import BsonHandlers._
|
||||
import PatronHandlers._
|
||||
import ChargeHandlers._
|
||||
|
||||
def checkout(user: User, data: Checkout): Fu[StripeSubscription] =
|
||||
LichessPlan findUnder data.cents match {
|
||||
|
@ -45,8 +46,8 @@ final class StripeApi(
|
|||
customerIdPatron(charge.customer) flatMap { patronOption =>
|
||||
chargeColl.insert(Charge.make(
|
||||
userId = patronOption.map(_.userId.value),
|
||||
stripe = Charge.Stripe(charge.customer.value).some,
|
||||
cents = charge.cents)) >> {
|
||||
stripe = Charge.Stripe(charge.id, charge.customer).some,
|
||||
cents = charge.amount)) >> {
|
||||
patronOption match {
|
||||
case None => fufail(s"Charged unknown customer $charge")
|
||||
case Some(patron) if patron.canLevelUp =>
|
||||
|
@ -54,10 +55,10 @@ final class StripeApi(
|
|||
patronColl.updateField($id(patron.id), "lastLevelUp", DateTime.now) >>
|
||||
UserRepo.setPlan(user, user.plan.incMonths) >>- {
|
||||
logger.info(s"Charged $charge $patron")
|
||||
lila.mon.stripe.amount(charge.amount)
|
||||
lila.mon.stripe.amount(charge.amount.value)
|
||||
bus.publish(lila.hub.actorApi.stripe.ChargeEvent(
|
||||
username = user.username,
|
||||
amount = charge.amount), 'stripe)
|
||||
amount = charge.amount.value), 'stripe)
|
||||
}
|
||||
}
|
||||
case Some(patron) => fufail(s"Too early to level up $charge $patron")
|
||||
|
@ -121,9 +122,10 @@ final class StripeApi(
|
|||
}
|
||||
}
|
||||
|
||||
// returns true if the user should be reloaded from DB
|
||||
def sync(user: User): Fu[Boolean] = userPatron(user) flatMap {
|
||||
|
||||
case None if !user.plan.isEmpty =>
|
||||
case None if user.plan.active =>
|
||||
logger.warn(s"sync: disable plan of non-patron")
|
||||
UserRepo.setPlan(user, user.plan.disable) inject true
|
||||
|
||||
|
@ -145,10 +147,8 @@ final class StripeApi(
|
|||
}
|
||||
|
||||
case (_, Some(paypal)) =>
|
||||
if (paypal.isExpired && user.plan.active) {
|
||||
logger.warn(s"sync: disable plan of patron with expired paypal")
|
||||
UserRepo.setPlan(user, user.plan.disable) inject true
|
||||
}
|
||||
if (paypal.isExpired)
|
||||
patronColl.unsetField($id(user.id), "payPal") >> sync(user)
|
||||
else if (!paypal.isExpired && !user.plan.active) {
|
||||
logger.warn(s"sync: enable plan of customer with paypal")
|
||||
UserRepo.setPlan(user, user.plan.enable) inject true
|
||||
|
|
|
@ -3,6 +3,7 @@ package lila.stripe
|
|||
import org.joda.time.DateTime
|
||||
|
||||
case class CustomerId(value: String) extends AnyVal
|
||||
case class ChargeId(value: String) extends AnyVal
|
||||
|
||||
case class Source(value: String) extends AnyVal
|
||||
case class Usd(value: Int) extends AnyVal with Ordered[Usd] {
|
||||
|
@ -32,9 +33,7 @@ case class StripeCustomer(id: CustomerId, subscriptions: StripeSubscriptions) {
|
|||
def plan = firstSubscription.map(_.plan)
|
||||
}
|
||||
|
||||
case class StripeCharge(amount: Int, customer: CustomerId) {
|
||||
def cents = Cents(amount)
|
||||
}
|
||||
case class StripeCharge(id: ChargeId, amount: Cents, customer: CustomerId)
|
||||
|
||||
case class StripeInvoice(
|
||||
id: Option[String],
|
||||
|
|
Loading…
Reference in New Issue