Skip to content

Commit

Permalink
Merge pull request #1048 from amihaiemil/1045
Browse files Browse the repository at this point in the history
#1045 Invoice Takes Latest Payment
  • Loading branch information
amihaiemil authored Mar 9, 2021
2 parents 4ff853c + 19f3071 commit 036b347
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 152 deletions.
13 changes: 4 additions & 9 deletions self-api/src/main/java/com/selfxdsd/api/Invoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,11 @@ public interface Invoice {
LocalDateTime createdAt();

/**
* Timestamp of the payment.
* @return LocalDateTime or null if it's not paid.
* Latest Payment performed for this Invoice. If the Payment is successful,
* the Invoice is considered paid.
* @return Payment or null if no Payment as been performed yet.
*/
LocalDateTime paymentTime();

/**
* Returns the transaction ID of the payment.
* @return String or null if it's not paid.
*/
String transactionId();
Payment latest();

/**
* Who emitted the Invoice?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ public final class StoredInvoice implements Invoice {
private final LocalDateTime createdAt;

/**
* Time when this Invoice has been paid.
* Latest Payment performed for this Invoice. If it's successful,
* then this Invoice is considered paid.
*/
private final LocalDateTime paymentTime;

/**
* The payment's transaction ID.
*/
private final String transactionId;
private final Payment latest;

/**
* Who emitted this Invoice?
Expand Down Expand Up @@ -92,8 +88,7 @@ public final class StoredInvoice implements Invoice {
* @param id Invoice id.
* @param contract Contract.
* @param createdAt Invoice creation time.
* @param paymentTime Time when this Invoice has been paid.
* @param transactionId The payment's transaction ID.
* @param latest Latest Payment performed for this Invoice.
* @param billedBy Who emitted the Invoice.
* @param billedTo Who pays it.
* @param billedByCountry Country of the Contributor.
Expand All @@ -106,8 +101,7 @@ public StoredInvoice(
final int id,
final Contract contract,
final LocalDateTime createdAt,
final LocalDateTime paymentTime,
final String transactionId,
final Payment latest,
final String billedBy,
final String billedTo,
final String billedByCountry,
Expand All @@ -118,8 +112,7 @@ public StoredInvoice(
this.id = id;
this.contract = contract;
this.createdAt = createdAt;
this.paymentTime = paymentTime;
this.transactionId = transactionId;
this.latest = latest;
this.billedBy = billedBy;
this.billedTo = billedTo;
this.billedByCountry = billedByCountry;
Expand Down Expand Up @@ -171,13 +164,8 @@ public LocalDateTime createdAt() {
}

@Override
public LocalDateTime paymentTime() {
return this.paymentTime;
}

@Override
public String transactionId() {
return this.transactionId;
public Payment latest() {
return this.latest;
}

@Override
Expand Down Expand Up @@ -233,18 +221,21 @@ public InvoicedTasks tasks() {

@Override
public boolean isPaid() {
return this.paymentTime != null && this.transactionId != null;
return this.latest != null && this.latest.status().equalsIgnoreCase(
Payment.Status.SUCCESSFUL
);
}

@Override
public PlatformInvoice platformInvoice() {
final PlatformInvoice found;
if(this.isPaid()) {
if(this.transactionId.startsWith("fake_payment_")) {
final String transactionId = this.latest.transactionId();
if(transactionId.startsWith("fake_payment_")) {
found = null;
} else {
found = this.storage.platformInvoices().getByPayment(
this.transactionId, this.paymentTime
transactionId, this.latest.paymentTime()
);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.selfxdsd.api.exceptions.PaymentMethodsException;
import com.selfxdsd.api.storage.Storage;
import com.selfxdsd.core.contracts.invoices.StoredInvoice;
import com.selfxdsd.core.contracts.invoices.StoredPayment;
import com.stripe.model.SetupIntent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -111,7 +112,8 @@ public Payment pay(final Invoice invoice) {
+ " of Contract " + invoice.contract().contractId()
+ "..."
);
final BigDecimal newCash = this.limit.subtract(invoice.totalAmount());
final BigDecimal totalAmount = invoice.totalAmount();
final BigDecimal newCash = this.limit.subtract(totalAmount);
final String uuid = UUID.randomUUID().toString().replace("-", "");
final Payment payment = this.storage
.invoices()
Expand All @@ -120,8 +122,15 @@ public Payment pay(final Invoice invoice) {
invoice.invoiceId(),
invoice.contract(),
invoice.createdAt(),
LocalDateTime.now(),
"fake_payment_" + uuid,
new StoredPayment(
invoice,
"fake_payment_" + uuid,
LocalDateTime.now(),
totalAmount,
Payment.Status.SUCCESSFUL,
"",
this.storage
),
invoice.billedBy(),
invoice.billedTo(),
"FK Country",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public Payment pay(final Invoice invoice) {
payment = this.original.pay(invoice);
} catch (final WalletPaymentException failed) {
payment = invoice.payments().register(
invoice, null, LocalDateTime.now(),
invoice, "", LocalDateTime.now(),
invoice.totalAmount(),
Payment.Status.FAILED, failed.getMessage()
);
} catch (final IllegalStateException errored) {
payment = invoice.payments().register(
invoice, null, LocalDateTime.now(),
invoice, "", LocalDateTime.now(),
invoice.totalAmount(),
Payment.Status.ERROR, errored.getMessage()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.selfxdsd.api.storage.Storage;
import com.selfxdsd.core.Env;
import com.selfxdsd.core.contracts.invoices.StoredInvoice;
import com.selfxdsd.core.contracts.invoices.StoredPayment;
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Customer;
Expand Down Expand Up @@ -205,11 +206,12 @@ public Payment pay(final Invoice invoice) {
invoice.commission(),
contributorBilling
);
final BigDecimal totalAmount = invoice.totalAmount();
final PaymentIntent paymentIntent = PaymentIntent
.create(
PaymentIntentCreateParams.builder()
.setCurrency("eur")
.setAmount(invoice.totalAmount().longValueExact())
.setAmount(totalAmount.longValueExact())
.setCustomer(this.identifier)
.setPaymentMethod(paymentMethod.identifier())
.setTransferData(
Expand Down Expand Up @@ -244,8 +246,15 @@ public Payment pay(final Invoice invoice) {
invoice.invoiceId(),
invoice.contract(),
invoice.createdAt(),
paymentDate,
paymentIntent.getId(),
new StoredPayment(
invoice,
paymentIntent.getId(),
paymentDate,
totalAmount,
Payment.Status.SUCCESSFUL,
"",
this.storage
),
invoice.billedBy(),
invoice.billedTo(),
contributorBilling.country(),
Expand Down
Loading

0 comments on commit 036b347

Please sign in to comment.