Search in sources :

Example 1 with RefundResponse

use of uk.gov.pay.api.model.RefundResponse in project pay-publicapi by alphagov.

the class GetPaymentRefundServiceTest method testGetPayment.

@Test
@PactVerification({ "connector" })
@Pacts(pacts = { "publicapi-connector-get-payment-refund" })
public void testGetPayment() {
    Account account = new Account(ACCOUNT_ID, TokenPaymentType.CARD, "a-token-link");
    RefundResponse refund = getPaymentService.getConnectorPaymentRefund(account, CHARGE_ID, REFUND_ID);
    assertThat(refund.getRefundId(), is(REFUND_ID));
    assertThat(refund.getStatus(), is("success"));
    assertThat(refund.getAmount(), is(100L));
    assertThat(refund.getCreatedDate(), is("2018-09-22T10:14:16.067Z"));
    assertThat(refund.getLinks().getPayment().getHref(), is("http://publicapi.test.localhost/v1/payments/123456789"));
    assertThat(refund.getLinks().getSelf().getHref(), is("http://publicapi.test.localhost/v1/payments/123456789/refunds/r_123abc456def"));
}
Also used : Account(uk.gov.pay.api.auth.Account) RefundResponse(uk.gov.pay.api.model.RefundResponse) PactVerification(au.com.dius.pact.consumer.PactVerification) Test(org.junit.Test) Pacts(uk.gov.service.payments.commons.testing.pact.consumers.Pacts)

Example 2 with RefundResponse

use of uk.gov.pay.api.model.RefundResponse in project pay-publicapi by alphagov.

the class GetPaymentRefundServiceTest method testGetLedgerPaymentRefund.

@Test
@PactVerification({ "ledger" })
@Pacts(pacts = { "publicapi-ledger-get-payment-refund" })
public void testGetLedgerPaymentRefund() {
    Account account = new Account(ACCOUNT_ID, TokenPaymentType.CARD, "a-token-link");
    RefundResponse refund = getPaymentService.getLedgerPaymentRefund(account, CHARGE_ID, REFUND_ID);
    assertThat(refund.getRefundId(), is(REFUND_ID));
    assertThat(refund.getStatus(), is("success"));
    assertThat(refund.getAmount(), is(100L));
    assertThat(refund.getCreatedDate(), is("2018-09-22T10:14:16.067Z"));
    assertThat(refund.getLinks().getPayment().getHref(), is("http://publicapi.test.localhost/v1/payments/123456789"));
    assertThat(refund.getLinks().getSelf().getHref(), is("http://publicapi.test.localhost/v1/payments/123456789/refunds/r_123abc456def"));
}
Also used : Account(uk.gov.pay.api.auth.Account) RefundResponse(uk.gov.pay.api.model.RefundResponse) PactVerification(au.com.dius.pact.consumer.PactVerification) Test(org.junit.Test) Pacts(uk.gov.service.payments.commons.testing.pact.consumers.Pacts)

Example 3 with RefundResponse

use of uk.gov.pay.api.model.RefundResponse in project pay-publicapi by alphagov.

the class GetPaymentRefundsServiceTest method shouldReturnRefundsForPaymentCorrectlyFromLedger.

@Test
@PactVerification("ledger")
@Pacts(pacts = { "publicapi-ledger-get-payment-refunds" })
public void shouldReturnRefundsForPaymentCorrectlyFromLedger() {
    Account account = new Account(ACCOUNT_ID, TokenPaymentType.CARD, "a-token-link");
    RefundsResponse response = getPaymentRefundsService.getLedgerTransactionTransactions(account, "ch_123abc456xyz");
    assertThat(response.getPaymentId(), is("ch_123abc456xyz"));
    assertThat(response.getLinks().getSelf().getHref(), is("http://publicapi.test.localhost/v1/payments/ch_123abc456xyz/refunds"));
    assertThat(response.getLinks().getPayment().getHref(), is("http://publicapi.test.localhost/v1/payments/ch_123abc456xyz"));
    List<RefundResponse> refunds = response.getEmbedded().getRefunds();
    assertThat(refunds.size(), is(2));
    assertThat(refunds.get(0).getRefundId(), is("refund-transaction-id1"));
    assertThat(refunds.get(0).getStatus(), is("submitted"));
    assertThat(refunds.get(0).getAmount(), is(100L));
    assertThat(refunds.get(0).getCreatedDate(), is("2018-09-22T10:14:16.067Z"));
    assertThat(refunds.get(0).getLinks().getPayment().getHref(), is("http://publicapi.test.localhost/v1/payments/ch_123abc456xyz"));
    assertThat(refunds.get(0).getLinks().getSelf().getHref(), is("http://publicapi.test.localhost/v1/payments/ch_123abc456xyz/refunds/refund-transaction-id1"));
    assertThat(refunds.get(1).getRefundId(), is("refund-transaction-id2"));
    assertThat(refunds.get(1).getStatus(), is("error"));
    assertThat(refunds.get(1).getAmount(), is(200L));
    assertThat(refunds.get(1).getCreatedDate(), is("2018-09-22T10:16:16.067Z"));
    assertThat(refunds.get(1).getLinks().getPayment().getHref(), is("http://publicapi.test.localhost/v1/payments/ch_123abc456xyz"));
    assertThat(refunds.get(1).getLinks().getSelf().getHref(), is("http://publicapi.test.localhost/v1/payments/ch_123abc456xyz/refunds/refund-transaction-id2"));
}
Also used : Account(uk.gov.pay.api.auth.Account) RefundResponse(uk.gov.pay.api.model.RefundResponse) RefundsResponse(uk.gov.pay.api.model.RefundsResponse) PactVerification(au.com.dius.pact.consumer.PactVerification) Test(org.junit.Test) Pacts(uk.gov.service.payments.commons.testing.pact.consumers.Pacts)

Example 4 with RefundResponse

use of uk.gov.pay.api.model.RefundResponse in project pay-publicapi by alphagov.

the class GetPaymentRefundsServiceTest method shouldReturnRefundsForPaymentCorrectlyFromConnector.

@Test
@PactVerification("connector")
@Pacts(pacts = { "publicapi-connector-get-payment-refunds" })
public void shouldReturnRefundsForPaymentCorrectlyFromConnector() {
    Account account = new Account(ACCOUNT_ID, TokenPaymentType.CARD, "a-token-link");
    RefundsResponse response = getPaymentRefundsService.getConnectorPaymentRefunds(account, "charge8133029783750222");
    assertThat(response.getPaymentId(), is("charge8133029783750222"));
    assertThat(response.getLinks().getSelf().getHref(), is("http://publicapi.test.localhost/v1/payments/charge8133029783750222/refunds"));
    assertThat(response.getLinks().getPayment().getHref(), is("http://publicapi.test.localhost/v1/payments/charge8133029783750222"));
    List<RefundResponse> refunds = response.getEmbedded().getRefunds();
    assertThat(refunds.size(), is(2));
    assertThat(refunds.get(0).getRefundId(), is("di0qnu9ucdo7aslhatci6h90jk"));
    assertThat(refunds.get(0).getStatus(), is("success"));
    assertThat(refunds.get(0).getAmount(), is(1L));
    assertThat(refunds.get(0).getCreatedDate(), is("2016-01-25T13:23:55.000Z"));
    assertThat(refunds.get(0).getLinks().getPayment().getHref(), is("http://publicapi.test.localhost/v1/payments/charge8133029783750222"));
    assertThat(refunds.get(0).getLinks().getSelf().getHref(), is("http://publicapi.test.localhost/v1/payments/charge8133029783750222/refunds/di0qnu9ucdo7aslhatci6h90jk"));
    assertThat(refunds.get(1).getRefundId(), is("m16ufgc3t23l766ljhv9eicsn5"));
    assertThat(refunds.get(1).getStatus(), is("error"));
    assertThat(refunds.get(1).getAmount(), is(1L));
    assertThat(refunds.get(1).getCreatedDate(), is("2016-01-25T16:23:55.000Z"));
    assertThat(refunds.get(1).getLinks().getPayment().getHref(), is("http://publicapi.test.localhost/v1/payments/charge8133029783750222"));
    assertThat(refunds.get(1).getLinks().getSelf().getHref(), is("http://publicapi.test.localhost/v1/payments/charge8133029783750222/refunds/m16ufgc3t23l766ljhv9eicsn5"));
}
Also used : Account(uk.gov.pay.api.auth.Account) RefundResponse(uk.gov.pay.api.model.RefundResponse) RefundsResponse(uk.gov.pay.api.model.RefundsResponse) PactVerification(au.com.dius.pact.consumer.PactVerification) Test(org.junit.Test) Pacts(uk.gov.service.payments.commons.testing.pact.consumers.Pacts)

Example 5 with RefundResponse

use of uk.gov.pay.api.model.RefundResponse in project pay-publicapi by alphagov.

the class PaymentRefundsResource method submitRefund.

@POST
@Timed
@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
@Operation(security = { @SecurityRequirement(name = "BearerAuth") }, operationId = "Submit a refund for a payment", summary = "Submit a refund for a payment", description = "Return issued refund information. " + "The Authorisation token needs to be specified in the 'authorization' header as 'authorization: Bearer YOUR_API_KEY_HERE'", responses = { @ApiResponse(responseCode = "200", description = "successful operation", content = @Content(schema = @Schema(implementation = RefundResult.class))), @ApiResponse(responseCode = "202", description = "ACCEPTED"), @ApiResponse(responseCode = "401", description = "Credentials are required to access this resource"), @ApiResponse(responseCode = "404", description = "Not found", content = @Content(schema = @Schema(implementation = PaymentError.class))), @ApiResponse(responseCode = "412", description = "Refund amount available mismatch"), @ApiResponse(responseCode = "429", description = "Too many requests", content = @Content(schema = @Schema(implementation = ApiErrorResponse.class))), @ApiResponse(responseCode = "500", description = "Downstream system error", content = @Content(schema = @Schema(implementation = PaymentError.class))) })
public Response submitRefund(@Parameter(hidden = true) @Auth Account account, @PathParam("paymentId") String paymentId, @Parameter(required = true, description = "requestPayload") CreatePaymentRefundRequest requestPayload) {
    var strategy = new GetOnePaymentStrategy("", account, paymentId, getPaymentService);
    logger.info("Create a refund for payment request - paymentId={}", paymentId);
    Integer refundAmountAvailable = requestPayload.getRefundAmountAvailable().orElseGet(() -> Optional.of((CardPayment) strategy.validateAndExecute().getPayment()).map(p -> p.getRefundSummary().map(RefundSummary::getAmountAvailable).orElse(0L)).map(Long::intValue).orElse(0));
    ImmutableMap<String, Object> payloadMap = ImmutableMap.of("amount", requestPayload.getAmount(), "refund_amount_available", refundAmountAvailable);
    String connectorPayload = new GsonBuilder().create().toJson(payloadMap);
    Response connectorResponse = client.target(getConnectorUrl(format("/v1/api/accounts/%s/charges/%s/refunds", account.getAccountId(), paymentId))).request().post(json(connectorPayload));
    if (connectorResponse.getStatus() == ACCEPTED.getStatusCode()) {
        RefundFromConnector refundFromConnector = connectorResponse.readEntity(RefundFromConnector.class);
        logger.debug("created refund returned - [ {} ]", refundFromConnector);
        RefundResponse refundResponse = RefundResponse.valueOf(refundFromConnector, paymentId, baseUrl);
        return Response.accepted(refundResponse).build();
    }
    throw new CreateRefundException(connectorResponse);
}
Also used : Account(uk.gov.pay.api.auth.Account) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) Client(javax.ws.rs.client.Client) Auth(io.dropwizard.auth.Auth) UriBuilder.fromPath(javax.ws.rs.core.UriBuilder.fromPath) RefundResponse(uk.gov.pay.api.model.RefundResponse) GsonBuilder(com.google.gson.GsonBuilder) RefundResult(uk.gov.pay.api.model.search.card.RefundResult) RefundFromConnector(uk.gov.pay.api.model.RefundFromConnector) Inject(javax.inject.Inject) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) CardPayment(uk.gov.pay.api.model.CardPayment) Consumes(javax.ws.rs.Consumes) PublicApiConfig(uk.gov.pay.api.app.config.PublicApiConfig) GetPaymentService(uk.gov.pay.api.service.GetPaymentService) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) HeaderParam(javax.ws.rs.HeaderParam) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) ApiErrorResponse(uk.gov.pay.api.resources.error.ApiErrorResponse) SecurityRequirement(io.swagger.v3.oas.annotations.security.SecurityRequirement) PaymentError(uk.gov.pay.api.model.PaymentError) RefundSummary(uk.gov.pay.api.model.RefundSummary) Schema(io.swagger.v3.oas.annotations.media.Schema) Entity.json(javax.ws.rs.client.Entity.json) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ImmutableMap(com.google.common.collect.ImmutableMap) RefundForSearchResult(uk.gov.pay.api.model.search.card.RefundForSearchResult) CreatePaymentRefundRequest(uk.gov.pay.api.model.CreatePaymentRefundRequest) String.format(java.lang.String.format) ACCEPTED(javax.ws.rs.core.Response.Status.ACCEPTED) Parameter(io.swagger.v3.oas.annotations.Parameter) Timed(com.codahale.metrics.annotation.Timed) GetPaymentRefundsService(uk.gov.pay.api.service.GetPaymentRefundsService) Response(javax.ws.rs.core.Response) Tag(io.swagger.v3.oas.annotations.tags.Tag) CreateRefundException(uk.gov.pay.api.exception.CreateRefundException) Optional(java.util.Optional) ConnectorService(uk.gov.pay.api.service.ConnectorService) GetPaymentRefundService(uk.gov.pay.api.service.GetPaymentRefundService) RefundsResponse(uk.gov.pay.api.model.RefundsResponse) GsonBuilder(com.google.gson.GsonBuilder) RefundFromConnector(uk.gov.pay.api.model.RefundFromConnector) RefundResponse(uk.gov.pay.api.model.RefundResponse) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) ApiErrorResponse(uk.gov.pay.api.resources.error.ApiErrorResponse) Response(javax.ws.rs.core.Response) RefundsResponse(uk.gov.pay.api.model.RefundsResponse) CreateRefundException(uk.gov.pay.api.exception.CreateRefundException) RefundResponse(uk.gov.pay.api.model.RefundResponse) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

RefundResponse (uk.gov.pay.api.model.RefundResponse)8 Account (uk.gov.pay.api.auth.Account)5 PactVerification (au.com.dius.pact.consumer.PactVerification)4 Test (org.junit.Test)4 RefundsResponse (uk.gov.pay.api.model.RefundsResponse)3 Pacts (uk.gov.service.payments.commons.testing.pact.consumers.Pacts)3 Timed (com.codahale.metrics.annotation.Timed)2 Operation (io.swagger.v3.oas.annotations.Operation)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 UriBuilder.fromPath (javax.ws.rs.core.UriBuilder.fromPath)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 GsonBuilder (com.google.gson.GsonBuilder)1 Auth (io.dropwizard.auth.Auth)1 Parameter (io.swagger.v3.oas.annotations.Parameter)1 Content (io.swagger.v3.oas.annotations.media.Content)1 Schema (io.swagger.v3.oas.annotations.media.Schema)1 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)1 SecurityRequirement (io.swagger.v3.oas.annotations.security.SecurityRequirement)1