Skip to content

Commit

Permalink
[PH][MIBM-126] tax due is a long amount in pence (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHodgson authored Oct 12, 2020
1 parent 6ce5acb commit 15e39c9
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@
package uk.gov.hmrc.merchandiseinbaggagefrontend.controllers

import javax.inject.{Inject, Singleton}
import play.api.data.Form
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.{Answers, CheckYourAnswersFormProvider}
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.CheckYourAnswersForm.form
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.CheckYourAnswersPage

@Singleton
class CheckYourAnswersController @Inject()(override val controllerComponents: MessagesControllerComponents,
actionProvider: DeclarationJourneyActionProvider,
formProvider: CheckYourAnswersFormProvider,
page: CheckYourAnswersPage)
(implicit appConfig: AppConfig) extends DeclarationJourneyController {
val form: Form[Answers] = formProvider()

val onPageLoad: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
request.declarationJourney.declarationIfRequiredAndComplete.fold(actionProvider.invalidRequest){ declaration =>
Ok(page(form, declaration))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import play.api.mvc._
import uk.gov.hmrc.http.HttpClient
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.{AppConfig, ErrorHandler}
import uk.gov.hmrc.merchandiseinbaggagefrontend.connectors.PaymentConnector
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.CheckYourAnswersFormProvider
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.CheckYourAnswersForm.form
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.api._
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.AmountInPence
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.PaymentPage
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController

Expand All @@ -33,12 +32,9 @@ import scala.concurrent.{ExecutionContext, Future}
class PaymentController @Inject()(
mcc: MessagesControllerComponents,
paymentPage: PaymentPage,
formProvider: CheckYourAnswersFormProvider,
override val httpClient: HttpClient)(implicit val ec: ExecutionContext, appConfig: AppConfig, errorHandler: ErrorHandler)
extends FrontendController(mcc) with PaymentConnector {

private val form = formProvider()

val onPageLoad: Action[AnyContent] = Action.async { implicit request =>
Future.successful(Ok(paymentPage()))
}
Expand All @@ -47,14 +43,13 @@ class PaymentController @Inject()(
def onError(): Future[Result] = Future successful BadRequest("something WRONG")

form.bindFromRequest().fold(_ => onError(),
json => {

answers => {
//TODO hard coded data for now
val body = PayApiRequest(
MibReference("MIBI1234567890"),
AmountInPence(json.taxDue.toLong),
AmountInPence(json.taxDue.toLong),
AmountInPence(json.taxDue.toLong),
answers.taxDue,
answers.taxDue,
answers.taxDue,
TraderDetails("Trader Inc, 239 Old Street, Berlin, Germany, EC1V 9EY"),
MerchandiseDetails("Parts and technical crew for the forest moon")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,23 @@
package uk.gov.hmrc.merchandiseinbaggagefrontend.forms

import play.api.data.Form
import play.api.data.Forms.mapping
import play.api.data.Forms._
import play.api.data.Forms.{mapping, _}
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.mappings.Mappings
import play.api.data.format.Formats._
import CheckYourAnswersFormProvider._
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.AmountInPence

object CheckYourAnswersFormProvider {
object CheckYourAnswersForm extends Mappings {
val taxDue = "taxDue"
}

class CheckYourAnswersFormProvider extends Mappings {

def apply(): Form[Answers] =
val form: Form[Answers] =
Form(
mapping(
taxDue -> of(doubleFormat)
)(Answers.apply)(Answers.unapply)
taxDue -> longNumber
)(Answers.apply)(answers => Some(answers.taxDue.value))
)

}

case class Answers(taxDue: AmountInPence)

case class Answers(taxDue: Double)
object Answers {
def apply(taxDue: Long): Answers = Answers(AmountInPence(taxDue))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
@import uk.gov.hmrc.merchandiseinbaggagefrontend.controllers.routes._
@import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.Declaration
@import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.CheckYourAnswersFormProvider._
@import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.CheckYourAnswersForm.taxDue

@this(layout: Layout, formHelper: FormWithCSRF, errorSummary: components.errorSummary, button: components.button)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import play.api.test.FakeRequest
import play.api.test.Helpers._
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpResponse}
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.ErrorHandler
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.CheckYourAnswersFormProvider
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.api.PayApiRequest
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.{ErrorTemplate, PaymentPage}

Expand All @@ -35,36 +34,35 @@ class PaymentControllerSpec extends DeclarationJourneyControllerSpec {
private lazy val httpClient = app.injector.instanceOf[HttpClient]
private lazy val component = app.injector.instanceOf[MessagesControllerComponents]
private lazy val errorHandlerTemplate = app.injector.instanceOf[ErrorTemplate]
private lazy val formProvider = app.injector.instanceOf[CheckYourAnswersFormProvider]
private implicit lazy val errorHandler: ErrorHandler = app.injector.instanceOf[ErrorHandler]

private def messages[A](fakeRequest: FakeRequest[A]): Messages = messagesApi.preferred(fakeRequest)

"on page load will render PaymentPage template" in {
val controller = new PaymentController(component, view, formProvider, httpClient)
val controller = new PaymentController(component, view, httpClient)
val getRequest = buildGet(routes.PaymentController.onPageLoad().url)

contentAsString(controller.onPageLoad(getRequest)) mustBe view()(getRequest, messages(getRequest), appConfig).toString
}

"on submit will trigger a call to pay-api to make payment and render the response" in {
val stubbedApiResponse = s"""{"journeyId":"5f3b","nextUrl":"http://host"}"""
val controller = new PaymentController(component, view, formProvider, httpClient) {
val controller = new PaymentController(component, view, httpClient) {
override def makePayment(requestBody: PayApiRequest)
(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] =
Future.successful(HttpResponse(201, stubbedApiResponse))
}

val postRequest = buildPost(routes.PaymentController.onSubmit().url)
.withFormUrlEncodedBody("taxDue" -> "10.11")
.withFormUrlEncodedBody("taxDue" -> "1011")
val eventualResult = controller.onSubmit()(postRequest)

status(eventualResult) mustBe 303
redirectLocation(eventualResult) mustBe Some("http://host")
}

"on submit will return error page if call to pay-api fails" in {
val controller = new PaymentController(component, view, formProvider, httpClient) {
val controller = new PaymentController(component, view, httpClient) {
override def makePayment(requestBody: PayApiRequest)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] =
Future.failed(new Exception("Something wrong"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,14 @@ package uk.gov.hmrc.merchandiseinbaggagefrontend.forms

import play.api.data.FormError
import uk.gov.hmrc.merchandiseinbaggagefrontend.BaseSpec
import CheckYourAnswersFormProvider._
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.CheckYourAnswersForm._

class CheckYourAnswersFormProviderSpec extends BaseSpec {


"bind data to the form" in {
val provider = new CheckYourAnswersFormProvider()

provider().bind(Map(taxDue -> "30.12")).value mustBe Some(Answers(30.12))
"bind tax due in pence to the form" in {
form.bind(Map(taxDue -> "3012")).value mustBe Some(Answers(3012))
}

"return error if incorrect" in {
val provider = new CheckYourAnswersFormProvider()

provider().bind(Map[String, String]()).errors mustBe List(FormError(taxDue, List("error.required"),List()))
form.bind(Map[String, String]()).errors mustBe List(FormError(taxDue, List("error.required"),List()))
}

}

0 comments on commit 15e39c9

Please sign in to comment.