killbill-memoizeit

Added sql for payment dao

1/13/2012 4:37:25 AM

Changes

Details

diff --git a/api/src/main/java/com/ning/billing/invoice/api/InvoicePaymentApi.java b/api/src/main/java/com/ning/billing/invoice/api/InvoicePaymentApi.java
index c9b9e27..b339d0f 100644
--- a/api/src/main/java/com/ning/billing/invoice/api/InvoicePaymentApi.java
+++ b/api/src/main/java/com/ning/billing/invoice/api/InvoicePaymentApi.java
@@ -23,9 +23,10 @@ import java.util.UUID;
 import org.joda.time.DateTime;
 
 import com.ning.billing.catalog.api.Currency;
+import com.ning.billing.payment.api.InvoicePayment;
 
 public interface InvoicePaymentApi {
-    public void paymentSuccessful(UUID invoiceId, BigDecimal amount, Currency currency, UUID paymentAttemptId, DateTime paymentAttemptDate);
+    public void paymentSuccessful(UUID invoiceId, BigDecimal amount, Currency currency, UUID paymentAttemptId, DateTime paymentDate);
 
     public void paymentFailed(UUID invoiceId, UUID paymentAttemptId, DateTime paymentAttemptDate);
 
@@ -34,4 +35,6 @@ public interface InvoicePaymentApi {
     public Invoice getInvoice(UUID invoiceId);
 
     public Invoice getInvoiceForPaymentAttemptId(UUID paymentAttemptId);
+
+    InvoicePayment getInvoicePayment(UUID paymentAttemptId);
 }
diff --git a/api/src/main/java/com/ning/billing/payment/api/PaymentApi.java b/api/src/main/java/com/ning/billing/payment/api/PaymentApi.java
index c37ee01..1d62eb5 100644
--- a/api/src/main/java/com/ning/billing/payment/api/PaymentApi.java
+++ b/api/src/main/java/com/ning/billing/payment/api/PaymentApi.java
@@ -38,6 +38,8 @@ public interface PaymentApi {
     List<Either<PaymentError, PaymentInfo>> createPayment(String accountKey, List<String> invoiceIds);
     List<Either<PaymentError, PaymentInfo>> createPayment(Account account, List<String> invoiceIds);
 
+    List<Either<PaymentError, PaymentInfo>> createRefund(Account account, List<String> invoiceIds); //TODO
+
     Either<PaymentError, PaymentProviderAccount> getPaymentProviderAccount(String accountKey);
 
     Either<PaymentError, PaymentProviderAccount> createPaymentProviderAccount(PaymentProviderAccount account);
diff --git a/api/src/main/java/com/ning/billing/payment/api/PaymentInfo.java b/api/src/main/java/com/ning/billing/payment/api/PaymentInfo.java
index 1ccd255..fce8d45 100644
--- a/api/src/main/java/com/ning/billing/payment/api/PaymentInfo.java
+++ b/api/src/main/java/com/ning/billing/payment/api/PaymentInfo.java
@@ -26,10 +26,9 @@ import com.google.common.base.Objects;
 import com.ning.billing.util.eventbus.EventBusNotification;
 
 public class PaymentInfo implements EventBusNotification {
-    private final String id;
+    private final String paymentId;
     private final BigDecimal amount;
     private final BigDecimal refundAmount;
-    private final BigDecimal appliedCreditBalanceAmount;
     private final String paymentNumber;
     private final String bankIdentificationNumber;
     private final String status;
@@ -40,10 +39,9 @@ public class PaymentInfo implements EventBusNotification {
     private final DateTime updatedDate;
 
     public PaymentInfo(PaymentInfo src) {
-        this.id = src.id;
+        this.paymentId = src.paymentId;
         this.amount = src.amount;
         this.refundAmount = src.refundAmount;
-        this.appliedCreditBalanceAmount = src.appliedCreditBalanceAmount;
         this.paymentNumber = src.paymentNumber;
         this.bankIdentificationNumber = src.bankIdentificationNumber;
         this.status = src.status;
@@ -55,26 +53,24 @@ public class PaymentInfo implements EventBusNotification {
     }
 
     @JsonCreator
-    public PaymentInfo(@JsonProperty("id") String id,
+    public PaymentInfo(@JsonProperty("paymentId") String paymentId,
                        @JsonProperty("amount") BigDecimal amount,
-                       @JsonProperty("appliedCreditBalanceAmount") BigDecimal appliedCreditBalanceAmount,
+                       @JsonProperty("refundAmount") BigDecimal refundAmount,
                        @JsonProperty("bankIdentificationNumber") String bankIdentificationNumber,
                        @JsonProperty("paymentNumber") String paymentNumber,
-                       @JsonProperty("refundAmount") BigDecimal refundAmount,
                        @JsonProperty("status") String status,
                        @JsonProperty("type") String type,
                        @JsonProperty("referenceId") String referenceId,
                        @JsonProperty("effectiveDate") DateTime effectiveDate,
                        @JsonProperty("createdDate") DateTime createdDate,
                        @JsonProperty("updatedDate") DateTime updatedDate) {
-        this.id = id;
+        this.paymentId = paymentId;
         this.amount = amount;
-        this.appliedCreditBalanceAmount = appliedCreditBalanceAmount;
+        this.refundAmount = refundAmount;
         this.bankIdentificationNumber = bankIdentificationNumber;
         this.effectiveDate = effectiveDate;
         this.paymentNumber = paymentNumber;
         this.referenceId = referenceId;
-        this.refundAmount = refundAmount;
         this.status = status;
         this.type = type;
         this.createdDate = createdDate;
@@ -85,18 +81,14 @@ public class PaymentInfo implements EventBusNotification {
         return new Builder(this);
     }
 
-    public String getId() {
-        return id;
+    public String getPaymentId() {
+        return paymentId;
     }
 
     public BigDecimal getAmount() {
         return amount;
     }
 
-    public BigDecimal getAppliedCreditBalanceAmount() {
-        return appliedCreditBalanceAmount;
-    }
-
     public String getBankIdentificationNumber() {
         return bankIdentificationNumber;
     }
@@ -134,7 +126,7 @@ public class PaymentInfo implements EventBusNotification {
     }
 
     public static class Builder {
-        private String id;
+        private String paymentId;
         private BigDecimal amount;
         private BigDecimal refundAmount;
         private BigDecimal appliedCreditBalanceAmount;
@@ -151,10 +143,9 @@ public class PaymentInfo implements EventBusNotification {
         }
 
         public Builder(PaymentInfo src) {
-            this.id = src.id;
+            this.paymentId = src.paymentId;
             this.amount = src.amount;
             this.refundAmount = src.refundAmount;
-            this.appliedCreditBalanceAmount = src.appliedCreditBalanceAmount;
             this.paymentNumber = src.paymentNumber;
             this.bankIdentificationNumber = src.bankIdentificationNumber;
             this.type = src.type;
@@ -165,8 +156,8 @@ public class PaymentInfo implements EventBusNotification {
             this.updatedDate = src.updatedDate;
         }
 
-        public Builder setId(String id) {
-            this.id = id;
+        public Builder setPaymentId(String paymentId) {
+            this.paymentId = paymentId;
             return this;
         }
 
@@ -226,12 +217,11 @@ public class PaymentInfo implements EventBusNotification {
         }
 
         public PaymentInfo build() {
-            return new PaymentInfo(id,
+            return new PaymentInfo(paymentId,
                                    amount,
                                    refundAmount,
                                    bankIdentificationNumber,
                                    paymentNumber,
-                                   appliedCreditBalanceAmount,
                                    type,
                                    status,
                                    referenceId,
@@ -244,11 +234,10 @@ public class PaymentInfo implements EventBusNotification {
     @Override
     public int hashCode() {
         return Objects.hashCode(amount,
-                                appliedCreditBalanceAmount,
                                 bankIdentificationNumber,
                                 createdDate,
                                 effectiveDate,
-                                id,
+                                paymentId,
                                 paymentNumber,
                                 referenceId,
                                 refundAmount,
@@ -266,11 +255,10 @@ public class PaymentInfo implements EventBusNotification {
             }
             else {
                 return Objects.equal(amount, other.amount) &&
-                       Objects.equal(appliedCreditBalanceAmount, other.appliedCreditBalanceAmount) &&
                        Objects.equal(bankIdentificationNumber, other.bankIdentificationNumber) &&
                        Objects.equal(createdDate, other.createdDate) &&
                        Objects.equal(effectiveDate, other.effectiveDate) &&
-                       Objects.equal(id, other.id) &&
+                       Objects.equal(paymentId, other.paymentId) &&
                        Objects.equal(paymentNumber, other.paymentNumber) &&
                        Objects.equal(referenceId, other.referenceId) &&
                        Objects.equal(refundAmount, other.refundAmount) &&
@@ -284,6 +272,6 @@ public class PaymentInfo implements EventBusNotification {
 
     @Override
     public String toString() {
-        return "PaymentInfo [id=" + id + ", amount=" + amount + ", refundAmount=" + refundAmount + ", appliedCreditBalanceAmount=" + appliedCreditBalanceAmount + ", paymentNumber=" + paymentNumber + ", bankIdentificationNumber=" + bankIdentificationNumber + ", status=" + status + ", type=" + type + ", referenceId=" + referenceId + ", effectiveDate=" + effectiveDate + ", createdDate=" + createdDate + ", updatedDate=" + updatedDate + "]";
+        return "PaymentInfo [paymentId=" + paymentId + ", amount=" + amount + ", refundAmount=" + refundAmount + ", paymentNumber=" + paymentNumber + ", bankIdentificationNumber=" + bankIdentificationNumber + ", status=" + status + ", type=" + type + ", referenceId=" + referenceId + ", effectiveDate=" + effectiveDate + ", createdDate=" + createdDate + ", updatedDate=" + updatedDate + "]";
     }
 }
diff --git a/invoice/src/main/java/com/ning/billing/invoice/api/invoice/DefaultInvoicePaymentApi.java b/invoice/src/main/java/com/ning/billing/invoice/api/invoice/DefaultInvoicePaymentApi.java
index c8879e8..36a2a68 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/api/invoice/DefaultInvoicePaymentApi.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/api/invoice/DefaultInvoicePaymentApi.java
@@ -28,6 +28,7 @@ import com.ning.billing.catalog.api.Currency;
 import com.ning.billing.invoice.api.Invoice;
 import com.ning.billing.invoice.api.InvoicePaymentApi;
 import com.ning.billing.invoice.dao.InvoiceDao;
+import com.ning.billing.payment.api.InvoicePayment;
 
 public class DefaultInvoicePaymentApi implements InvoicePaymentApi {
     private final InvoiceDao dao;
@@ -60,7 +61,12 @@ public class DefaultInvoicePaymentApi implements InvoicePaymentApi {
     @Override
     public Invoice getInvoiceForPaymentAttemptId(UUID paymentAttemptId) {
         String invoiceIdStr = dao.getInvoiceIdByPaymentAttemptId(paymentAttemptId);
-        return dao.getById(invoiceIdStr);
+        return invoiceIdStr == null ? null : dao.getById(invoiceIdStr);
+    }
+
+    @Override
+    public InvoicePayment getInvoicePayment(UUID paymentAttemptId) {
+        return dao.getInvoicePayment(paymentAttemptId);
     }
 
 }
diff --git a/invoice/src/main/java/com/ning/billing/invoice/api/user/DefaultInvoiceCreationNotification.java b/invoice/src/main/java/com/ning/billing/invoice/api/user/DefaultInvoiceCreationNotification.java
index 094e6f1..5c6785c 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/api/user/DefaultInvoiceCreationNotification.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/api/user/DefaultInvoiceCreationNotification.java
@@ -18,7 +18,9 @@ package com.ning.billing.invoice.api.user;
 
 import java.math.BigDecimal;
 import java.util.UUID;
+
 import org.joda.time.DateTime;
+
 import com.ning.billing.catalog.api.Currency;
 import com.ning.billing.invoice.api.InvoiceCreationNotification;
 
@@ -61,4 +63,10 @@ public class DefaultInvoiceCreationNotification implements InvoiceCreationNotifi
     public DateTime getInvoiceCreationDate() {
         return invoiceCreationDate;
     }
+
+    @Override
+    public String toString() {
+        return "DefaultInvoiceCreationNotification [invoiceId=" + invoiceId + ", accountId=" + accountId + ", amountOwed=" + amountOwed + ", currency=" + currency + ", invoiceCreationDate=" + invoiceCreationDate + "]";
+    }
+
 }
diff --git a/invoice/src/main/java/com/ning/billing/invoice/dao/DefaultInvoiceDao.java b/invoice/src/main/java/com/ning/billing/invoice/dao/DefaultInvoiceDao.java
index 31b525d..016028c 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/dao/DefaultInvoiceDao.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/dao/DefaultInvoiceDao.java
@@ -32,6 +32,7 @@ import com.ning.billing.invoice.api.Invoice;
 import com.ning.billing.invoice.api.InvoiceCreationNotification;
 import com.ning.billing.invoice.api.InvoiceItem;
 import com.ning.billing.invoice.api.user.DefaultInvoiceCreationNotification;
+import com.ning.billing.payment.api.InvoicePayment;
 import com.ning.billing.util.eventbus.EventBus;
 
 public class DefaultInvoiceDao implements InvoiceDao {
@@ -155,4 +156,9 @@ public class DefaultInvoiceDao implements InvoiceDao {
     public String getInvoiceIdByPaymentAttemptId(UUID paymentAttemptId) {
         return invoiceDao.getInvoiceIdByPaymentAttemptId(paymentAttemptId.toString());
     }
+
+    @Override
+    public InvoicePayment getInvoicePayment(UUID paymentAttemptId) {
+        return invoiceDao.getInvoicePayment(paymentAttemptId);
+    }
 }
diff --git a/invoice/src/main/java/com/ning/billing/invoice/dao/InvoiceDao.java b/invoice/src/main/java/com/ning/billing/invoice/dao/InvoiceDao.java
index e80a17e..043cb0f 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/dao/InvoiceDao.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/dao/InvoiceDao.java
@@ -22,6 +22,7 @@ import java.util.List;
 import java.util.UUID;
 
 import com.ning.billing.invoice.api.Invoice;
+import com.ning.billing.payment.api.InvoicePayment;
 
 public interface InvoiceDao {
     void create(Invoice invoice);
@@ -51,4 +52,6 @@ public interface InvoiceDao {
 
     void test();
 
+    InvoicePayment getInvoicePayment(UUID paymentAttemptId);
+
 }
diff --git a/invoice/src/main/java/com/ning/billing/invoice/dao/InvoiceSqlDao.java b/invoice/src/main/java/com/ning/billing/invoice/dao/InvoiceSqlDao.java
index f9553c9..2759236 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/dao/InvoiceSqlDao.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/dao/InvoiceSqlDao.java
@@ -50,6 +50,7 @@ import com.ning.billing.catalog.api.Currency;
 import com.ning.billing.invoice.api.Invoice;
 import com.ning.billing.invoice.api.InvoiceItem;
 import com.ning.billing.invoice.model.DefaultInvoice;
+import com.ning.billing.payment.api.InvoicePayment;
 import com.ning.billing.util.UuidMapper;
 import com.ning.billing.util.entity.EntityDao;
 
@@ -77,6 +78,9 @@ public interface InvoiceSqlDao extends EntityDao<Invoice>, Transactional<Invoice
     List<UUID> getInvoicesForPayment(@Bind("targetDate") final Date targetDate,
                                      @Bind("numberOfDays") final int numberOfDays);
 
+    @SqlQuery
+    InvoicePayment getInvoicePayment(@Bind("paymentAttemptId") UUID paymentAttemptId);
+
     @SqlUpdate
     void notifySuccessfulPayment(@Bind("invoiceId") final String invoiceId,
                                  @Bind("amount") final BigDecimal paymentAmount,
diff --git a/invoice/src/main/resources/com/ning/billing/invoice/dao/InvoiceSqlDao.sql.stg b/invoice/src/main/resources/com/ning/billing/invoice/dao/InvoiceSqlDao.sql.stg
index 12a5412..c8fbad3 100644
--- a/invoice/src/main/resources/com/ning/billing/invoice/dao/InvoiceSqlDao.sql.stg
+++ b/invoice/src/main/resources/com/ning/billing/invoice/dao/InvoiceSqlDao.sql.stg
@@ -57,6 +57,13 @@ create() ::= <<
   VALUES (:id, :accountId, :invoiceDate, :targetDate, :currency);
 >>
 
+getInvoiceIdByPaymentAttemptId() ::= <<
+  SELECT i.id
+    FROM invoices i, invoice_payments ip
+   WHERE ip.invoice_id = i.id
+     AND ip.payment_attempt_id = :paymentAttemptId
+>>
+
 update() ::= <<
   UPDATE invoices
   SET account_id = :accountId, invoice_date = :invoiceDate, target_date = :targetDate, currency = :currency
@@ -64,20 +71,19 @@ update() ::= <<
 >>
 
 notifySuccessfulPayment() ::= <<
-  INSERT INTO invoice_payments(invoice_id, payment_id, payment_date, amount, currency)
-  VALUES(:invoiceId, :paymentId, :paymentDate, :amount, :currency);
+  INSERT INTO invoice_payments(invoice_id, payment_attempt_id, payment_date, amount, currency)
+  VALUES(:invoice_id, :payment_attempt_id, :payment_date, :amount, :currency);
 >>
 
-getInvoiceIdByPaymentAttemptId() ::= <<
-  SELECT i.id
-    FROM invoices i, invoice_payments ip
-   WHERE ip.invoice_id = i.id
-     AND ip.payment_attempt_id = :paymentAttemptId
+notifyFailedPayment() ::= <<
+  INSERT INTO invoice_payments(invoice_id, payment_attempt_id)
+  VALUES(:invoice_id, :payment_attempt_id);
 >>
 
-notifyFailedPayment() ::= <<
-  INSERT INTO invoice_payments(invoice_id, payment_id, payment_date)
-  VALUES(:invoiceId, :paymentId, :paymentAttemptDate);
+getInvoicePayment() ::= <<
+    SELECT invoice_id, payment_id, payment_date, amount, currency
+      FROM invoice_payments
+     WHERE payment_id = :payment_id
 >>
 
 test() ::= <<
diff --git a/invoice/src/main/resources/com/ning/billing/invoice/ddl.sql b/invoice/src/main/resources/com/ning/billing/invoice/ddl.sql
index a3ff37d..330e82c 100644
--- a/invoice/src/main/resources/com/ning/billing/invoice/ddl.sql
+++ b/invoice/src/main/resources/com/ning/billing/invoice/ddl.sql
@@ -29,13 +29,14 @@ CREATE INDEX invoices_account_id ON invoices(account_id ASC);
 DROP TABLE IF EXISTS invoice_payments;
 CREATE TABLE invoice_payments (
   invoice_id char(36) NOT NULL,
-  payment_id char(36) NOT NULL,
-  payment_date datetime NOT NULL,
+  payment_attempt_id char(36) NOT NULL,
+  payment_date datetime,
+  payment_attempt_date datetime,
   amount numeric(10,4),
   currency char(3),
-  PRIMARY KEY(invoice_id, payment_id)
+  PRIMARY KEY(invoice_id, payment_attempt_id)
 ) ENGINE=innodb;
-CREATE UNIQUE INDEX invoice_payments_unique ON invoice_payments(invoice_id, payment_id);
+CREATE UNIQUE INDEX invoice_payments_unique ON invoice_payments(invoice_id, payment_attempt_id);
 
 DROP VIEW IF EXISTS invoice_payment_summary;
 CREATE VIEW invoice_payment_summary AS
diff --git a/invoice/src/test/java/com/ning/billing/invoice/api/MockInvoicePaymentApi.java b/invoice/src/test/java/com/ning/billing/invoice/api/MockInvoicePaymentApi.java
index 8a536a8..821b880 100644
--- a/invoice/src/test/java/com/ning/billing/invoice/api/MockInvoicePaymentApi.java
+++ b/invoice/src/test/java/com/ning/billing/invoice/api/MockInvoicePaymentApi.java
@@ -83,17 +83,21 @@ public class MockInvoicePaymentApi implements InvoicePaymentApi
 
     @Override
     public Invoice getInvoiceForPaymentAttemptId(UUID paymentAttemptId) {
-        for (InvoicePayment existingInvoicePayment : invoicePayments) {
-            if (existingInvoicePayment.getPaymentAttemptId().equals(paymentAttemptId)) {
-                return getInvoice(existingInvoicePayment.getInvoiceId());
+        for (InvoicePayment invoicePayment : invoicePayments) {
+            if (invoicePayment.getPaymentAttemptId().equals(paymentAttemptId)) {
+                return getInvoice(invoicePayment.getInvoiceId());
             }
         }
         return null;
     }
 
+    @Override
     public InvoicePayment getInvoicePayment(UUID paymentAttemptId) {
-        // TODO Auto-generated method stub
+        for (InvoicePayment invoicePayment : invoicePayments) {
+            if (paymentAttemptId.equals(invoicePayment.getPaymentAttemptId())) {
+                return invoicePayment;
+            }
+        }
         return null;
     }
-
 }
diff --git a/invoice/src/test/java/com/ning/billing/invoice/dao/MockInvoiceDao.java b/invoice/src/test/java/com/ning/billing/invoice/dao/MockInvoiceDao.java
index bf8b8c8..c549a61 100644
--- a/invoice/src/test/java/com/ning/billing/invoice/dao/MockInvoiceDao.java
+++ b/invoice/src/test/java/com/ning/billing/invoice/dao/MockInvoiceDao.java
@@ -181,4 +181,11 @@ public class MockInvoiceDao implements InvoiceDao {
         }
         return null;
     }
+
+    @Override
+    public InvoicePayment getInvoicePayment(UUID paymentAttemptId) {
+        synchronized(monitor) {
+            return invoicePayments.get(paymentAttemptId);
+        }
+    }
 }
diff --git a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java
index 943d04f..ad91c85 100644
--- a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java
+++ b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java
@@ -130,19 +130,26 @@ public class DefaultPaymentApi implements PaymentApi {
             if (invoice.getAmountOutstanding().compareTo(BigDecimal.ZERO) == 0 ) {
             // TODO: send a notification that invoice was ignored ?
             }
+            else if (invoiceId.equals(paymentDao.getPaymentAttemptForInvoiceId(invoiceId))) {
+
+            }
             else {
                 PaymentAttempt paymentAttempt = paymentDao.createPaymentAttempt(invoice);
                 Either<PaymentError, PaymentInfo> paymentOrError = plugin.processInvoice(account, invoice);
                 processedPaymentsOrErrors.add(paymentOrError);
 
                 if (paymentOrError.isRight()) {
-                    PaymentInfo info = paymentOrError.getRight();
+                    PaymentInfo paymentInfo = paymentOrError.getRight();
 
-                    paymentDao.savePaymentInfo(info);
-                    paymentDao.updatePaymentAttemptWithPaymentId(paymentAttempt.getPaymentAttemptId(), info.getId());
+                    paymentDao.savePaymentInfo(paymentInfo);
+
+                    if (paymentInfo.getPaymentId() != null) {
+                        paymentDao.updatePaymentAttemptWithPaymentId(paymentAttempt.getPaymentAttemptId(), paymentInfo.getPaymentId());
+                    }
 
                     invoicePaymentApi.paymentSuccessful(invoice.getId(),
-                                                        invoice.getAmountOutstanding(),
+                                                        paymentInfo.getAmount(),
+//                                                      info.getRefundAmount(), TODO
                                                         invoice.getCurrency(),
                                                         paymentAttempt.getPaymentAttemptId(),
                                                         paymentAttempt.getPaymentAttemptDate());
@@ -175,4 +182,10 @@ public class DefaultPaymentApi implements PaymentApi {
         return paymentDao.getPaymentAttemptForPaymentId(id);
     }
 
+    @Override
+    public List<Either<PaymentError, PaymentInfo>> createRefund(Account account, List<String> invoiceIds) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
 }
diff --git a/payment/src/main/java/com/ning/billing/payment/dao/DefaultPaymentDao.java b/payment/src/main/java/com/ning/billing/payment/dao/DefaultPaymentDao.java
index f14dee1..ceba7a7 100644
--- a/payment/src/main/java/com/ning/billing/payment/dao/DefaultPaymentDao.java
+++ b/payment/src/main/java/com/ning/billing/payment/dao/DefaultPaymentDao.java
@@ -39,6 +39,11 @@ public class DefaultPaymentDao implements PaymentDao {
     }
 
     @Override
+    public PaymentAttempt getPaymentAttemptForInvoiceId(String invoiceId) {
+        return sqlDao.getPaymentAttemptForInvoiceId(invoiceId);
+    }
+
+    @Override
     public PaymentAttempt createPaymentAttempt(Invoice invoice) {
         final PaymentAttempt paymentAttempt = new PaymentAttempt(UUID.randomUUID(), invoice);
 
@@ -52,8 +57,8 @@ public class DefaultPaymentDao implements PaymentDao {
     }
 
     @Override
-    public void updatePaymentAttemptWithPaymentId(UUID paymentAttemptId, String id) {
-        sqlDao.updatePaymentAttemptWithPaymentId(paymentAttemptId.toString(), id);
+    public void updatePaymentAttemptWithPaymentId(UUID paymentAttemptId, String paymentId) {
+        sqlDao.updatePaymentAttemptWithPaymentId(paymentAttemptId.toString(), paymentId);
     }
 
 }
diff --git a/payment/src/main/java/com/ning/billing/payment/dao/PaymentDao.java b/payment/src/main/java/com/ning/billing/payment/dao/PaymentDao.java
index db8e011..d40b264 100644
--- a/payment/src/main/java/com/ning/billing/payment/dao/PaymentDao.java
+++ b/payment/src/main/java/com/ning/billing/payment/dao/PaymentDao.java
@@ -32,4 +32,6 @@ public interface PaymentDao {
 
     void updatePaymentAttemptWithPaymentId(UUID paymentAttemptId, String paymentId);
 
+    PaymentAttempt getPaymentAttemptForInvoiceId(String invoiceId);
+
 }
diff --git a/payment/src/main/java/com/ning/billing/payment/dao/PaymentSqlDao.java b/payment/src/main/java/com/ning/billing/payment/dao/PaymentSqlDao.java
index 8f38958..0c19196 100644
--- a/payment/src/main/java/com/ning/billing/payment/dao/PaymentSqlDao.java
+++ b/payment/src/main/java/com/ning/billing/payment/dao/PaymentSqlDao.java
@@ -51,10 +51,15 @@ public interface PaymentSqlDao extends Transactional<PaymentSqlDao>, CloseMe, Tr
 
     @SqlQuery
     @Mapper(PaymentAttemptMapper.class)
-    PaymentAttempt getPaymentAttemptForPaymentId(@Bind("paymentId") String paymentId);
+    PaymentAttempt getPaymentAttemptForPaymentId(@Bind("payment_id") String paymentId);
+
+    @SqlQuery
+    @Mapper(PaymentAttemptMapper.class)
+    PaymentAttempt getPaymentAttemptForInvoiceId(@Bind("invoice_id") String invoiceId);
 
     @SqlUpdate
-    void updatePaymentAttemptWithPaymentId(String paymentAttemptId, String id);
+    void updatePaymentAttemptWithPaymentId(@Bind("payment_attempt_id") String paymentAttemptId,
+                                           @Bind("payment_id") String payment_id);
 
     @SqlUpdate
     void insertPaymentInfo(@Bind(binder = PaymentInfoBinder.class) PaymentInfo paymentInfo);
@@ -69,10 +74,11 @@ public interface PaymentSqlDao extends Transactional<PaymentSqlDao>, CloseMe, Tr
         public void bind(@SuppressWarnings("rawtypes") SQLStatement stmt, Bind bind, PaymentAttempt paymentAttempt) {
             Invoice invoice = paymentAttempt.getInvoice();
 
-            stmt.bind("id", paymentAttempt.getPaymentAttemptId().toString());
+            stmt.bind("payment_attempt_id", paymentAttempt.getPaymentAttemptId().toString());
             stmt.bind("payment_id", paymentAttempt.getPaymentId());
             stmt.bind("payment_attempt_dt", getDate(paymentAttempt.getPaymentAttemptDate()));
             stmt.bind("invoice_id", invoice.getId().toString());
+            stmt.bind("payment_attempt_amount", invoice.getAmountOutstanding()); //TODO: suppport partial payments
             stmt.bind("account_id", invoice.getAccountId().toString());
             stmt.bind("invoice_dt", getDate(invoice.getInvoiceDate()));
             stmt.bind("target_dt", getDate(invoice.getTargetDate()));
@@ -93,17 +99,16 @@ public interface PaymentSqlDao extends Transactional<PaymentSqlDao>, CloseMe, Tr
         @Override
         public PaymentAttempt map(int index, ResultSet rs, StatementContext ctx) throws SQLException {
 
-            UUID paymentAttemptId = UUID.fromString(rs.getString("id"));
-            String paymentId = rs.getString("payment_id");
-            DateTime paymentAttemptDate = getDate(rs, "payment_attempt_dt");
-
+            UUID paymentAttemptId = UUID.fromString(rs.getString("payment_attempt_id"));
             UUID invoiceId = UUID.fromString(rs.getString("invoice_id"));
             UUID accountId = UUID.fromString(rs.getString("account_id"));
-            DateTime invoiceDate = getDate(rs, "invoice_dt");
-            DateTime targetDate = getDate(rs, "target_dt");
+            String paymentId = rs.getString("payment_id");
+            BigDecimal amountPaid = rs.getBigDecimal("amount_paid");
             Currency currency = Currency.valueOf(rs.getString("currency"));
+            DateTime targetDate = getDate(rs, "target_dt");
+            DateTime invoiceDate = getDate(rs, "invoice_dt");
+            DateTime paymentAttemptDate = getDate(rs, "payment_attempt_dt");
             DateTime lastPaymentAttempt = getDate(rs, "last_payment_attempt");
-            BigDecimal amountPaid = rs.getBigDecimal("amount_paid");
 
             Invoice invoice = new DefaultInvoice(invoiceId, accountId, invoiceDate, targetDate, currency, lastPaymentAttempt, amountPaid);
 
@@ -119,14 +124,13 @@ public interface PaymentSqlDao extends Transactional<PaymentSqlDao>, CloseMe, Tr
 
         @Override
         public void bind(@SuppressWarnings("rawtypes") SQLStatement stmt, Bind bind, PaymentInfo paymentInfo) {
-            stmt.bind("id", paymentInfo.getId().toString());
+            stmt.bind("payment_id", paymentInfo.getPaymentId().toString());
             stmt.bind("amount", paymentInfo.getAmount());
             stmt.bind("refund_amount", paymentInfo.getRefundAmount());
-            stmt.bind("applied_credit_balance_amount", paymentInfo.getAppliedCreditBalanceAmount());
             stmt.bind("payment_number", paymentInfo.getPaymentNumber());
             stmt.bind("bank_identification_number", paymentInfo.getBankIdentificationNumber());
             stmt.bind("status", paymentInfo.getStatus());
-            stmt.bind("type", paymentInfo.getType());
+            stmt.bind("payment_type", paymentInfo.getType());
             stmt.bind("reference_id", paymentInfo.getReferenceId());
             stmt.bind("effective_dt", getDate(paymentInfo.getEffectiveDate()));
             stmt.bind("created_dt", getDate(paymentInfo.getCreatedDate()));
@@ -144,25 +148,23 @@ public interface PaymentSqlDao extends Transactional<PaymentSqlDao>, CloseMe, Tr
         @Override
         public PaymentInfo map(int index, ResultSet rs, StatementContext ctx) throws SQLException {
 
-            String id = rs.getString("id");
+            String paymentId = rs.getString("payment_id");
             BigDecimal amount = rs.getBigDecimal("amount");
             BigDecimal refundAmount = rs.getBigDecimal("refund_amount");
-            BigDecimal appliedCreditBalanceAmount = rs.getBigDecimal("applied_credit_balance_amount");
             String paymentNumber = rs.getString("payment_number");
             String bankIdentificationNumber = rs.getString("bank_identification_number");
             String status = rs.getString("status");
-            String type = rs.getString("type");
+            String type = rs.getString("payment_type");
             String referenceId = rs.getString("reference_id");
             DateTime effectiveDate = getDate(rs, "effective_dt");
             DateTime createdDate = getDate(rs, "created_dt");
             DateTime updatedDate = getDate(rs, "updated_dt");
 
-            return new PaymentInfo(id,
+            return new PaymentInfo(paymentId,
                                    amount,
-                                   appliedCreditBalanceAmount,
+                                   refundAmount,
                                    bankIdentificationNumber,
                                    paymentNumber,
-                                   refundAmount,
                                    status,
                                    type,
                                    referenceId,
@@ -171,4 +173,5 @@ public interface PaymentSqlDao extends Transactional<PaymentSqlDao>, CloseMe, Tr
                                    updatedDate);
         }
     }
+
 }
diff --git a/payment/src/main/java/com/ning/billing/payment/setup/PaymentModule.java b/payment/src/main/java/com/ning/billing/payment/setup/PaymentModule.java
index 8b40409..3c05c13 100644
--- a/payment/src/main/java/com/ning/billing/payment/setup/PaymentModule.java
+++ b/payment/src/main/java/com/ning/billing/payment/setup/PaymentModule.java
@@ -23,7 +23,7 @@ import org.skife.config.ConfigurationObjectFactory;
 import com.google.inject.AbstractModule;
 import com.ning.billing.payment.api.DefaultPaymentApi;
 import com.ning.billing.payment.api.PaymentApi;
-import com.ning.billing.payment.dao.MockPaymentDao;
+import com.ning.billing.payment.dao.DefaultPaymentDao;
 import com.ning.billing.payment.dao.PaymentDao;
 import com.ning.billing.payment.provider.PaymentProviderPluginRegistry;
 
@@ -38,6 +38,10 @@ public class PaymentModule extends AbstractModule {
         this.props = props;
     }
 
+    protected void installPaymentDao() {
+        bind(PaymentDao.class).to(DefaultPaymentDao.class).asEagerSingleton();
+    }
+
     protected void installPaymentProviderPlugins(PaymentConfig config) {
     }
 
@@ -49,8 +53,7 @@ public class PaymentModule extends AbstractModule {
         bind(PaymentConfig.class).toInstance(paymentConfig);
         bind(PaymentProviderPluginRegistry.class).asEagerSingleton();
         bind(PaymentApi.class).to(DefaultPaymentApi.class).asEagerSingleton();
-        bind(MockPaymentDao.class).asEagerSingleton();
-        bind(PaymentDao.class).to(MockPaymentDao.class);
         installPaymentProviderPlugins(paymentConfig);
+        installPaymentDao();
     }
 }
diff --git a/payment/src/main/resources/com/ning/billing/payment/dao/PaymentSqlDao.sql.stg b/payment/src/main/resources/com/ning/billing/payment/dao/PaymentSqlDao.sql.stg
index 553877e..af172e5 100644
--- a/payment/src/main/resources/com/ning/billing/payment/dao/PaymentSqlDao.sql.stg
+++ b/payment/src/main/resources/com/ning/billing/payment/dao/PaymentSqlDao.sql.stg
@@ -1,2 +1,54 @@
 group PaymentSqlDao;
 
+paymentAttemptFields(prefix) ::= <<
+    <prefix>payment_attempt_id,
+    <prefix>account_id,
+    <prefix>invoice_id,
+    <prefix>payment_attempt_amount,
+    <prefix>payment_attempt_dt,
+    <prefix>created_dt,
+    <prefix>updated_dt
+>>
+
+paymentInfoFields(prefix) ::= <<
+    <prefix>payment_id,
+    <prefix>amount,
+    <prefix>refund_amount,
+    <prefix>bank_identification_number,
+    <prefix>payment_number,
+    <prefix>payment_type,
+    <prefix>status,
+    <prefix>reference_id,
+    <prefix>effective_dt,
+    <prefix>created_dt,
+    <prefix>updated_dt
+>>
+
+insertPaymentAttempt() ::= <<
+    INSERT INTO payment_attempts (<paymentAttemptFields()>)
+    VALUES (:payment_attempt_id, :account_id, :invoice_id, :payment_attempt_amount, :payment_attempt_dt, NOW(), NOW());
+>>
+
+getPaymentAttemptForPaymentId() ::= <<
+    SELECT <paymentAttemptFields()>
+      FROM payment_attempts
+     WHERE payment_id = :payment_id
+>>
+
+getPaymentAttemptForInvoiceId() ::= <<
+    SELECT <paymentAttemptFields()>
+      FROM payment_attempts
+     WHERE invoice_id = :invoice_id
+>>
+
+updatePaymentAttemptWithPaymentId() ::= <<
+    UPDATE payment_attempts
+       SET payment_id = :payment_id,
+           updated_dt = NOW()
+     WHERE payment_attempt_id = :payment_attempt_id
+>>
+
+insertPaymentInfo() ::= <<
+    INSERT INTO payments (<paymentInfoFields()>)
+    VALUES (:payment_id, :amount, :refund_amount, :bank_identification_number, :payment_number, :payment_type, :status, :reference_id, :effective_dt, NOW(), NOW());
+>>
diff --git a/payment/src/main/resources/com/ning/billing/payment/ddl.sql b/payment/src/main/resources/com/ning/billing/payment/ddl.sql
new file mode 100644
index 0000000..a118254
--- /dev/null
+++ b/payment/src/main/resources/com/ning/billing/payment/ddl.sql
@@ -0,0 +1,28 @@
+DROP TABLE IF EXISTS payment_attempts;
+CREATE TABLE payment_attempts (
+      payment_attempt_id varchar(36) COLLATE utf8_bin NOT NULL,
+      payment_id varchar(36) COLLATE utf8_bin,
+      account_id varchar(36) COLLATE utf8_bin NOT NULL,
+      invoice_id varchar(36) COLLATE utf8_bin NOT NULL,
+      payment_attempt_amount decimal(8,2) NOT NULL,
+      payment_attempt_dt datetime NOT NULL,
+      created_dt datetime NOT NULL,
+      updated_dt datetime NOT NULL,
+      PRIMARY KEY (payment_attempt_id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
+
+DROP TABLE IF EXISTS payments; 
+CREATE TABLE payments (
+      payment_id varchar(36) COLLATE utf8_bin NOT NULL,
+      amount decimal(8,2),
+      refund_amount decimal(8,2),
+      payment_number varchar(36) COLLATE utf8_bin,
+      bank_identification_number varchar(36) COLLATE utf8_bin,
+      status varchar(20) COLLATE utf8_bin,
+      payment_type varchar(20) COLLATE utf8_bin,
+      reference_id varchar(36) COLLATE utf8_bin,
+      effective_dt datetime,
+      created_dt datetime NOT NULL,
+      updated_dt datetime NOT NULL,
+      PRIMARY KEY (payment_id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
diff --git a/payment/src/test/java/com/ning/billing/payment/api/TestPaymentApi.java b/payment/src/test/java/com/ning/billing/payment/api/TestPaymentApi.java
index 7f1c53b..1268eef 100644
--- a/payment/src/test/java/com/ning/billing/payment/api/TestPaymentApi.java
+++ b/payment/src/test/java/com/ning/billing/payment/api/TestPaymentApi.java
@@ -94,7 +94,7 @@ public abstract class TestPaymentApi {
 
         PaymentInfo paymentInfo = results.get(0).getRight();
 
-        assertNotNull(paymentInfo.getId());
+        assertNotNull(paymentInfo.getPaymentId());
         assertEquals(paymentInfo.getAmount().doubleValue(), amount.doubleValue());
     }
 }
diff --git a/payment/src/test/java/com/ning/billing/payment/dao/MockPaymentDao.java b/payment/src/test/java/com/ning/billing/payment/dao/MockPaymentDao.java
index ca46ca3..d3196b7 100644
--- a/payment/src/test/java/com/ning/billing/payment/dao/MockPaymentDao.java
+++ b/payment/src/test/java/com/ning/billing/payment/dao/MockPaymentDao.java
@@ -47,7 +47,7 @@ public class MockPaymentDao implements PaymentDao {
 
     @Override
     public void savePaymentInfo(PaymentInfo paymentInfo) {
-        payments.put(paymentInfo.getId(), paymentInfo);
+        payments.put(paymentInfo.getPaymentId(), paymentInfo);
     }
 
     @Override
@@ -61,4 +61,14 @@ public class MockPaymentDao implements PaymentDao {
         }
     }
 
+    @Override
+    public PaymentAttempt getPaymentAttemptForInvoiceId(String invoiceId) {
+        for (PaymentAttempt paymentAttempt : paymentAttempts.values()) {
+            if (invoiceId.equals(paymentAttempt.getInvoice().getId())) {
+                return paymentAttempt;
+            }
+        }
+        return null;
+    }
+
 }
diff --git a/payment/src/test/java/com/ning/billing/payment/provider/MockPaymentProviderPlugin.java b/payment/src/test/java/com/ning/billing/payment/provider/MockPaymentProviderPlugin.java
index 23ff1d3..321631e 100644
--- a/payment/src/test/java/com/ning/billing/payment/provider/MockPaymentProviderPlugin.java
+++ b/payment/src/test/java/com/ning/billing/payment/provider/MockPaymentProviderPlugin.java
@@ -38,7 +38,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
 
     @Override
     public Either<PaymentError, PaymentInfo> processInvoice(Account account, Invoice invoice) {
-        PaymentInfo payment = new PaymentInfo.Builder().setId(UUID.randomUUID().toString())
+        PaymentInfo payment = new PaymentInfo.Builder().setPaymentId(UUID.randomUUID().toString())
                                             .setAmount(invoice.getAmountOutstanding())
                                             .setStatus("Processed")
                                             .setBankIdentificationNumber("1234")
@@ -49,7 +49,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
                                             .setType("Electronic")
                                             .build();
 
-        payments.put(payment.getId(), payment);
+        payments.put(payment.getPaymentId(), payment);
         return Either.right(payment);
     }
 
diff --git a/payment/src/test/java/com/ning/billing/payment/setup/PaymentTestModule.java b/payment/src/test/java/com/ning/billing/payment/setup/PaymentTestModule.java
index 599390e..53d4a5f 100644
--- a/payment/src/test/java/com/ning/billing/payment/setup/PaymentTestModule.java
+++ b/payment/src/test/java/com/ning/billing/payment/setup/PaymentTestModule.java
@@ -23,6 +23,8 @@ import com.ning.billing.account.api.AccountUserApi;
 import com.ning.billing.account.api.MockAccountUserApi;
 import com.ning.billing.invoice.api.InvoicePaymentApi;
 import com.ning.billing.invoice.api.MockInvoicePaymentApi;
+import com.ning.billing.payment.dao.MockPaymentDao;
+import com.ning.billing.payment.dao.PaymentDao;
 import com.ning.billing.payment.provider.MockPaymentProviderPluginModule;
 import com.ning.billing.util.eventbus.EventBus;
 import com.ning.billing.util.eventbus.MemoryEventBus;
@@ -33,6 +35,11 @@ public class PaymentTestModule extends PaymentModule {
     }
 
     @Override
+    protected void installPaymentDao() {
+        bind(PaymentDao.class).to(MockPaymentDao.class).asEagerSingleton();
+    }
+
+    @Override
     protected void installPaymentProviderPlugins(PaymentConfig config) {
         install(new MockPaymentProviderPluginModule("my-mock"));
     }
diff --git a/payment/src/test/java/com/ning/billing/payment/TestPaymentInvoiceIntegration.java b/payment/src/test/java/com/ning/billing/payment/TestPaymentInvoiceIntegration.java
index d9bc54e..4568936 100644
--- a/payment/src/test/java/com/ning/billing/payment/TestPaymentInvoiceIntegration.java
+++ b/payment/src/test/java/com/ning/billing/payment/TestPaymentInvoiceIntegration.java
@@ -90,10 +90,11 @@ public class TestPaymentInvoiceIntegration {
         final String accountddl = IOUtils.toString(MysqlTestingHelper.class.getResourceAsStream("/com/ning/billing/account/ddl.sql"));
         final String utilddl = IOUtils.toString(MysqlTestingHelper.class.getResourceAsStream("/com/ning/billing/util/ddl.sql"));
         final String invoiceddl = IOUtils.toString(MysqlTestingHelper.class.getResourceAsStream("/com/ning/billing/invoice/ddl.sql"));
+        final String paymentddl = IOUtils.toString(MysqlTestingHelper.class.getResourceAsStream("/com/ning/billing/payment/ddl.sql"));
 
         helper = new MysqlTestingHelper();
         helper.startMysql();
-        helper.initDb(accountddl + "\n" + invoiceddl + "\n" + utilddl);
+        helper.initDb(accountddl + "\n" + invoiceddl + "\n" + utilddl + "\n" + paymentddl);
         dbi = helper.getDBI();
     }
 
@@ -187,9 +188,16 @@ public class TestPaymentInvoiceIntegration {
         assertTrue(paymentInfoReceiver.getErrors().isEmpty());
 
         List<PaymentInfo> payments = paymentInfoReceiver.getProcessedPayments();
-        PaymentAttempt paymentAttempt = paymentApi.getPaymentAttemptForPaymentId(payments.get(0).getId());
+        PaymentAttempt paymentAttempt = paymentApi.getPaymentAttemptForPaymentId(payments.get(0).getPaymentId());
         Assert.assertNotNull(paymentAttempt);
 
         Invoice invoiceForPayment = invoicePaymentApi.getInvoiceForPaymentAttemptId(paymentAttempt.getPaymentAttemptId());
+
+        Assert.assertNotNull(invoiceForPayment);
+        Assert.assertEquals(invoiceForPayment.getId(), invoice.getId());
+        Assert.assertEquals(invoiceForPayment.getAccountId(), account.getId());
+        Assert.assertEquals(invoiceForPayment.getLastPaymentAttempt(), paymentAttempt.getPaymentAttemptDate());
+        Assert.assertEquals(invoiceForPayment.getAmountOutstanding(), new BigDecimal("0"));
+        Assert.assertEquals(invoiceForPayment.getAmountPaid(), amount);
     }
 }
diff --git a/payment/src/test/java/com/ning/billing/payment/TestPaymentProvider.java b/payment/src/test/java/com/ning/billing/payment/TestPaymentProvider.java
index 5ceba69..d5c71b0 100644
--- a/payment/src/test/java/com/ning/billing/payment/TestPaymentProvider.java
+++ b/payment/src/test/java/com/ning/billing/payment/TestPaymentProvider.java
@@ -102,7 +102,7 @@ public class TestPaymentProvider {
         assertTrue(paymentInfoReceiver.getErrors().isEmpty());
 
         final PaymentInfo paymentInfo = paymentInfoReceiver.getProcessedPayments().get(0);
-        final PaymentInfoRequest paymentInfoRequest = new PaymentInfoRequest(account.getId(), paymentInfo.getId());
+        final PaymentInfoRequest paymentInfoRequest = new PaymentInfoRequest(account.getId(), paymentInfo.getPaymentId());
 
         paymentInfoReceiver.clear();
         eventBus.post(paymentInfoRequest);