Skip to content

Commit

Permalink
Merge pull request #416 from ostelco/feature/recurring-payment-purcha…
Browse files Browse the repository at this point in the history
…se-and-time-remaining

Feature/recurring payment purchase and time remaining (WIP)
  • Loading branch information
Kjell M. Myksvoll authored Dec 19, 2018
2 parents e0f3823 + 7714924 commit e65a0b7
Show file tree
Hide file tree
Showing 17 changed files with 432 additions and 248 deletions.
1 change: 1 addition & 0 deletions acceptance-tests/script/wait.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ java -cp '/acceptance-tests.jar' org.junit.runner.JUnitCore \
org.ostelco.at.jersey.BundlesAndPurchasesTest \
org.ostelco.at.jersey.SourceTest \
org.ostelco.at.jersey.PurchaseTest \
org.ostelco.at.jersey.PlanTest \
org.ostelco.at.jersey.AnalyticsTest \
org.ostelco.at.jersey.ConsentTest \
org.ostelco.at.jersey.ProfileTest \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1191,10 +1191,12 @@ class PlanTest {
.amount(100)
.currency("nok")
val plan = Plan()
.name("test")
.name("PLAN_1_NOK_PER_DAY")
.price(price)
.interval(Plan.IntervalEnum.DAY)
.intervalCount(1)
.properties(emptyMap<String, Any>())
.presentation(emptyMap<String, Any>())

post<Plan> {
path = "/plans"
Expand Down Expand Up @@ -1239,6 +1241,8 @@ class PlanTest {
.price(price)
.interval(Plan.IntervalEnum.DAY)
.intervalCount(1)
.properties(emptyMap<String, Any>())
.presentation(emptyMap<String, Any>())

try {
// Create subscriber with payment source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,14 @@ class ProfilesResource {
@Produces("application/json")
fun getPlans(@PathParam("email") email: String): Response {
return storage.getPlans(email).fold(
{ apiError -> Response.status(apiError.status).entity(asJson(apiError)) },
{ Response.status(Response.Status.OK).entity(asJson(it)) })
.build()
{
val err = ApiErrorMapper.mapStorageErrorToApiError("Failed to fetch plans",
ApiErrorCode.FAILED_TO_FETCH_PLANS_FOR_SUBSCRIBER,
it)
Response.status(err.status).entity(asJson(err))
},
{ Response.status(Response.Status.OK).entity(asJson(it)) }
).build()
}

/**
Expand All @@ -210,10 +215,15 @@ class ProfilesResource {
fun attachPlan(@PathParam("email") email: String,
@PathParam("planId") planId: String,
@QueryParam("trial_end") trialEnd: Long): Response {
return storage.attachPlan(email, planId, trialEnd).fold(
{ apiError -> Response.status(apiError.status).entity(asJson(apiError)) },
{ Response.status(Response.Status.CREATED) })
.build()
return storage.subscribeToPlan(email, planId, trialEnd).fold(
{
val err = ApiErrorMapper.mapStorageErrorToApiError("Failed to store subscription",
ApiErrorCode.FAILED_TO_STORE_SUBSCRIPTION,
it)
Response.status(err.status).entity(asJson(err))
},
{ Response.status(Response.Status.CREATED) }
).build()
}

/**
Expand All @@ -224,10 +234,15 @@ class ProfilesResource {
@Produces("application/json")
fun detachPlan(@PathParam("email") email: String,
@PathParam("planId") planId: String): Response {
return storage.detachPlan(email, planId).fold(
{ apiError -> Response.status(apiError.status).entity(asJson(apiError)) },
{ Response.status(Response.Status.OK) })
.build()
return storage.unsubscribeFromPlan(email, planId).fold(
{
val err = ApiErrorMapper.mapStorageErrorToApiError("Failed to remove subscription",
ApiErrorCode.FAILED_TO_REMOVE_SUBSCRIPTION,
it)
Response.status(err.status).entity(asJson(err))
},
{ Response.status(Response.Status.OK) }
).build()
}
}

Expand Down Expand Up @@ -430,6 +445,7 @@ class NotifyResource {
@Path("/plans")
class PlanResource() {

private val logger by getLogger()
private val storage by lazy { getResource<AdminDataSource>() }

/**
Expand All @@ -441,9 +457,14 @@ class PlanResource() {
fun get(@NotNull
@PathParam("planId") planId: String): Response {
return storage.getPlan(planId).fold(
{ apiError -> Response.status(apiError.status).entity(asJson(apiError)) },
{ Response.status(Response.Status.OK).entity(asJson(it)) })
.build()
{
val err = ApiErrorMapper.mapStorageErrorToApiError("Failed to fetch plan",
ApiErrorCode.FAILED_TO_FETCH_PLAN,
it)
Response.status(err.status).entity(asJson(err))
},
{ Response.status(Response.Status.OK).entity(asJson(it)) }
).build()
}

/**
Expand All @@ -452,11 +473,16 @@ class PlanResource() {
@POST
@Produces("application/json")
@Consumes("application/json")
fun create(plan: Plan) : Response {
fun create(plan: Plan): Response {
return storage.createPlan(plan).fold(
{ apiError -> Response.status(apiError.status).entity(asJson(apiError))},
{ Response.status(Response.Status.CREATED).entity(asJson(it))})
.build()
{
val err = ApiErrorMapper.mapStorageErrorToApiError("Failed to store plan",
ApiErrorCode.FAILED_TO_STORE_PLAN,
it)
Response.status(err.status).entity(asJson(err))
},
{ Response.status(Response.Status.CREATED).entity(asJson(it)) }
).build()
}

/**
Expand All @@ -469,8 +495,13 @@ class PlanResource() {
fun delete(@NotNull
@PathParam("planId") planId: String) : Response {
return storage.deletePlan(planId).fold(
{ apiError -> Response.status(apiError.status).entity(asJson(apiError)) },
{ Response.status(Response.Status.OK).entity(asJson(it))})
.build()
{
val err = ApiErrorMapper.mapStorageErrorToApiError("Failed to remove plan",
ApiErrorCode.FAILED_TO_REMOVE_PLAN,
it)
Response.status(err.status).entity(asJson(err))
},
{ Response.status(Response.Status.OK).entity(asJson(it))}
).build()
}
}
4 changes: 2 additions & 2 deletions model/src/main/kotlin/org/ostelco/prime/model/Entities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ data class Plan(
val price: Price,
val interval: String,
val intervalCount: Long = 1L,
val planId: String = "",
val productId: String = "") : HasId {
val properties: Map<String, String> = emptyMap(),
val presentation: Map<String, String> = emptyMap()) : HasId {

override val id: String
@JsonIgnore
Expand Down
Loading

0 comments on commit e65a0b7

Please sign in to comment.