Skip to content

Commit

Permalink
Refund changes
Browse files Browse the repository at this point in the history
Adds percentage to refund amount, also adds percentage and amount_in_cents to individual adjustments.
  • Loading branch information
paulorbpinho-fullstacklabs committed Aug 20, 2024
1 parent a93a23a commit 993245a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/ning/billing/recurly/model/AdjustmentRefund.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class AdjustmentRefund extends RecurlyObject {
@XmlElement(name = "quantity_decimal")
private BigDecimal quantityDecimal;

@XmlElement(name = "percentage")
private Integer percentage;

@XmlElement(name = "amount_in_cents")
private Integer amountInCents;

@XmlElement(name = "prorate")
private Boolean prorate;

Expand All @@ -46,6 +52,22 @@ public void setUuid(final Object uuid) {
this.uuid = stringOrNull(uuid);
}

public void setAmountInCents(final Object amountInCents) {
this.amountInCents = integerOrNull(amountInCents);
}

public Integer getAmountInCents() {
return this.amountInCents;
}

public void setPercentage(final Object percentage) {
this.percentage = integerOrNull(percentage);
}

public Integer getPercentage() {
return this.percentage;
}

public Integer getQuantity() {
return quantity;
}
Expand Down Expand Up @@ -77,6 +99,8 @@ public String toString() {
sb.append("{uuid='").append(uuid).append('\'');
sb.append(", quantity=").append(quantity);
sb.append(", quantity_decimal=").append(quantityDecimal);
sb.append(", percentage=").append(percentage);
sb.append(", amountInCents=").append(amountInCents);
sb.append(", prorate=").append(prorate);
sb.append('}');
return sb.toString();
Expand All @@ -98,6 +122,12 @@ public boolean equals(final Object o) {
if (quantityDecimal != null ? !quantityDecimal.equals(that.quantityDecimal) : that.quantityDecimal != null) {
return false;
}
if (amountInCents != null ? !amountInCents.equals(that.amountInCents) : that.amountInCents != null) {
return false;
}
if (percentage != null ? !percentage.equals(that.percentage) : that.percentage != null) {
return false;
}
if (uuid != null ? !uuid.equals(that.uuid) : that.uuid != null) {
return false;
}
Expand All @@ -111,6 +141,8 @@ public int hashCode() {
prorate,
quantity,
quantityDecimal,
amountInCents,
percentage,
uuid
);
}
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/com/ning/billing/recurly/model/InvoiceRefund.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class InvoiceRefund extends RecurlyObject {
@XmlElement(name = "amount_in_cents")
private Integer amountInCents;

@XmlElement(name = "percentage")
private Integer percentage;

@XmlElementWrapper(name = "line_items")
@XmlElement(name = "adjustment")
private List<AdjustmentRefund> lineItems;
Expand Down Expand Up @@ -68,6 +71,14 @@ public Integer getAmountInCents() {
return this.amountInCents;
}

public void setPercentage(final Object percentage) {
this.percentage = integerOrNull(percentage);
}

public Integer getPercentage() {
return this.percentage;
}

public void setLineItems(final List<AdjustmentRefund> lineItems) {
this.lineItems = lineItems;
}
Expand Down Expand Up @@ -118,7 +129,11 @@ public DateTime getRefundedAt() {

@Override
public int hashCode() {
return Objects.hashCode(refundMethod, amountInCents);
return Objects.hashCode(
refundMethod,
amountInCents,
percentage
);
}

@Override
Expand All @@ -131,6 +146,9 @@ public boolean equals(final Object o) {
if (amountInCents != null ? !amountInCents.equals(refund.amountInCents) : refund.amountInCents != null) {
return false;
}
if (percentage != null ? !percentage.equals(refund.percentage) : refund.percentage != null) {
return false;
}
if (externalRefund != null ? !externalRefund.equals(refund.externalRefund) : refund.externalRefund != null) {
return false;
}
Expand All @@ -155,6 +173,7 @@ public boolean equals(final Object o) {
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("InvoiceRefund{");
sb.append("percentage=").append(percentage);
sb.append("amountInCents=").append(amountInCents);
sb.append(", refundMethod='").append(refundMethod).append('\'');
sb.append(", externalRefund='").append(externalRefund).append('\'');
Expand Down
43 changes: 38 additions & 5 deletions src/test/java/com/ning/billing/recurly/TestRecurlyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2001,12 +2001,19 @@ public void testInvoices() throws Exception {

recurlyClient.createAccountAdjustment(account.getAccountCode(), adjustmentData2);

final Adjustment adjustmentData3 = new Adjustment();
adjustmentData3.setCurrency("USD");
adjustmentData3.setUnitAmountInCents(50);
adjustmentData3.setDescription("A description of an account adjustment3");

recurlyClient.createAccountAdjustment(account.getAccountCode(), adjustmentData2);

final Invoice invoiceData = new Invoice();
invoiceData.setCollectionMethod("automatic");

final Invoice invoice = recurlyClient.postAccountInvoice(account.getAccountCode(), invoiceData).getChargeInvoice();

Assert.assertEquals(invoice.getTotalInCents(), new Integer(200));
Assert.assertEquals(invoice.getTotalInCents(), new Integer(250));

// wait for the invoice to be marked paid
// has to happen asynchronously on the server
Expand Down Expand Up @@ -2036,6 +2043,18 @@ public void testInvoices() throws Exception {
Assert.assertEquals(refundInvoice.getSubtotalInCents(), new Integer(-100));
Assert.assertEquals(refundInvoice.getTransactions().get(0).getAction(), "refund");

final InvoiceRefund refundOptions2 = new InvoiceRefund();
refundOptions2.setRefundMethod(RefundMethod.transaction_first);
refundOptions2.setPercentage(100);
refundOptions2.setCreditCustomerNotes("Credit Customer Notes");
refundOptions2.setExternalRefund(true);
refundOptions2.setPaymentMethod("credit_card");
final Invoice refundInvoice2 = recurlyClient.refundInvoice(invoice.getId(), refundOptions2);

Assert.assertEquals(refundInvoice.getTotalInCents(), new Integer(-150));
Assert.assertEquals(refundInvoice.getSubtotalInCents(), new Integer(-150));
Assert.assertEquals(refundInvoice.getTransactions().get(0).getAction(), "refund");

// The refundInvoice should have an original_invoices of the original invoice
final Invoices originalInvoices = recurlyClient.getOriginalInvoices(refundInvoice.getId());
Assert.assertEquals(originalInvoices.get(0).getId(), invoice.getId());
Expand Down Expand Up @@ -2073,12 +2092,19 @@ public void testLineItemInvoiceRefund() throws Exception {

recurlyClient.createAccountAdjustment(account.getAccountCode(), adjustmentData2);

final Adjustment adjustmentData3 = new Adjustment();
adjustmentData3.setCurrency("USD");
adjustmentData3.setUnitAmountInCents(50);
adjustmentData3.setDescription("A description of an account adjustment3");

recurlyClient.createAccountAdjustment(account.getAccountCode(), adjustmentData3);

final Invoice invoiceData = new Invoice();
invoiceData.setCollectionMethod("automatic");

Invoice invoice = recurlyClient.postAccountInvoice(account.getAccountCode(), invoiceData).getChargeInvoice();

Assert.assertEquals(invoice.getTotalInCents(), new Integer(200));
Assert.assertEquals(invoice.getTotalInCents(), new Integer(250));

// wait for the invoice to be marked paid
// has to happen asynchronously on the server
Expand All @@ -2093,21 +2119,28 @@ public void testLineItemInvoiceRefund() throws Exception {
// we can use "toAdjustmentRefund" on the adjustment to turn it
// into an AdjustmentRefund
final AdjustmentRefund adjustmentRefund = invoice.getLineItems().get(0).toAdjustmentRefund();
final AdjustmentRefund adjustmentRefund2 = invoice.getLineItems().get(1).toAdjustmentRefund();
final AdjustmentRefund adjustmentRefund3 = invoice.getLineItems().get(2).toAdjustmentRefund();

// we could change the quantity here or the prorating settings if we want, defaults to full quantity
// adjustmentRefund.setQuantity(1);
adjustmentRefund.setPercentage(100);
lineItems.add(adjustmentRefund);
adjustmentRefund2.setPercentage(100);
lineItems.add(adjustmentRefund2);
adjustmentRefund3.setPercentage(100);
lineItems.add(adjustmentRefund3);

final InvoiceRefund refundOptions = new InvoiceRefund();
refundOptions.setRefundMethod(RefundMethod.transaction_first);
refundOptions.setLineItems(lineItems);
final Invoice refundInvoice = recurlyClient.refundInvoice(invoice.getId(), refundOptions);

Assert.assertEquals(refundInvoice.getTotalInCents(), new Integer(-100));
Assert.assertEquals(refundInvoice.getSubtotalInCents(), new Integer(-100));
Assert.assertEquals(refundInvoice.getTotalInCents(), new Integer(-250));
Assert.assertEquals(refundInvoice.getSubtotalInCents(), new Integer(-250));
Assert.assertEquals(refundInvoice.getTransactions().get(0).getAction(), "refund");

Assert.assertEquals(refundInvoice.getLineItems().size(), 1);
Assert.assertEquals(refundInvoice.getLineItems().size(), 3);
final Adjustment lineItem = refundInvoice.getLineItems().get(0);
Assert.assertEquals(lineItem.getQuantity(), new Integer(1));
} finally {
Expand Down

0 comments on commit 993245a

Please sign in to comment.