Skip to content

Commit b19119e

Browse files
committed
fix Payment.get... methods
1 parent 83b9dad commit b19119e

File tree

2 files changed

+59
-34
lines changed

2 files changed

+59
-34
lines changed

src/main/java/com/osiris/jsqlgen/payhook/Payment.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -773,23 +773,16 @@ public static List<Payment> getForUser(String userId){
773773
return get("userId=?", userId);
774774
}
775775

776-
public static List<Payment> getSubscriptionPaymentsForUser(String userId){
776+
public static List<Payment> getUserSubscriptionPayments(String userId){
777777
return whereUserId().is(userId)
778778
.and(wherePaypalSubscriptionId().isNotNull()
779779
.or(whereStripeSubscriptionId().isNotNull()))
780780
.get();
781781
// TODO ADD NEW PAYMENT PROCESSOR
782782
}
783783

784-
/**
785-
* List of payments that haven't been authorized or cancelled (or expired) yet and are in the future.
786-
*
787-
* @return list of payments, where {@link Payment#timestampAuthorized} is null, and
788-
* {@link Payment#timestampCancelled} is null, and {@link Payment#timestampCreated} is bigger than now.
789-
*/
790-
public static List<Payment> getPendingFuturePayments(String where, Object... objs) throws Exception {
791-
return get("timestampAuthorized = 0 AND timestampCancelled = 0 AND timestampCreated > " + System.currentTimeMillis() +
792-
(where != null ? " AND " + where : ""), objs);
784+
public static List<Payment> getUserPendingPayments(String userId){
785+
return getPendingPayments("userId=?", userId);
793786
}
794787

795788
/**
@@ -798,7 +791,7 @@ public static List<Payment> getPendingFuturePayments(String where, Object... obj
798791
* @return list of payments, where {@link Payment#timestampAuthorized} is null, and
799792
* {@link Payment#timestampCancelled} is null, and {@link Payment#timestampCreated} is smaller than now and {@link Payment#timestampExpires} is bigger than now.
800793
*/
801-
public static List<Payment> getPendingPayments() throws Exception {
794+
public static List<Payment> getPendingPayments() {
802795
return getPendingPayments(null);
803796
}
804797

@@ -808,18 +801,22 @@ public static List<Payment> getPendingPayments() throws Exception {
808801
* @return list of payments, where {@link Payment#timestampAuthorized} is null, and
809802
* {@link Payment#timestampCancelled} is null, and {@link Payment#timestampCreated} is smaller than now and {@link Payment#timestampExpires} is bigger than now.
810803
*/
811-
public static List<Payment> getPendingPayments(String where, Object... objs) throws Exception {
812-
return get("timestampAuthorized = 0 AND timestampCancelled = 0 "
804+
public static List<Payment> getPendingPayments(String where, Object... objs) {
805+
return get("WHERE timestampAuthorized = 0 AND timestampCancelled = 0 "
813806
+ (where != null ? " AND " + where : ""), objs);
814807
}
815808

809+
public static List<Payment> getUserAuthorizedPayments(String userId) {
810+
return getAuthorizedPayments("userId=?", userId);
811+
}
812+
816813
/**
817814
* List of payments that have been authorized/completed/paid.
818815
*
819816
* @return list of payments, where {@link Payment#timestampAuthorized} is not null.
820817
* @see PayHook#onPaymentAuthorized
821818
*/
822-
public static List<Payment> getAuthorizedPayments() throws Exception {
819+
public static List<Payment> getAuthorizedPayments() {
823820
return getAuthorizedPayments(null);
824821
}
825822

@@ -829,8 +826,12 @@ public static List<Payment> getAuthorizedPayments() throws Exception {
829826
* @return list of payments, where {@link Payment#timestampAuthorized} is not null.
830827
* @see PayHook#onPaymentAuthorized
831828
*/
832-
public static List<Payment> getAuthorizedPayments(String where, Object... objs) throws Exception {
833-
return get("timestampAuthorized != 0 " + (where != null ? " AND " + where : ""), objs);
829+
public static List<Payment> getAuthorizedPayments(String where, Object... objs) {
830+
return get("WHERE timestampAuthorized != 0 " + (where != null ? " AND " + where : ""), objs);
831+
}
832+
833+
public static List<Payment> getUserCancelledPayments(String userId) {
834+
return getCancelledPayments("userId=?", userId);
834835
}
835836

836837
/**
@@ -839,7 +840,7 @@ public static List<Payment> getAuthorizedPayments(String where, Object... objs)
839840
* @return list of payments, where {@link Payment#timestampCancelled} is not null.
840841
* @see PayHook#onPaymentCancelled
841842
*/
842-
public static List<Payment> getCancelledPayments() throws Exception {
843+
public static List<Payment> getCancelledPayments() {
843844
return getCancelledPayments(null);
844845
}
845846

@@ -849,26 +850,30 @@ public static List<Payment> getCancelledPayments() throws Exception {
849850
* @return list of payments, where {@link Payment#timestampCancelled} is not null.
850851
* @see PayHook#onPaymentCancelled
851852
*/
852-
public static List<Payment> getCancelledPayments(String where, Object... objs) throws Exception {
853-
return get("timestampCancelled != 0 " + (where != null ? " AND " + where : ""), objs);
853+
public static List<Payment> getCancelledPayments(String where, Object... objs) {
854+
return get("WHERE timestampCancelled != 0 " + (where != null ? " AND " + where : ""), objs);
855+
}
856+
857+
public static List<Payment> getUserRefundedPayments(String userId) {
858+
return getRefundedPayments("userId=?", userId);
854859
}
855860

856861
/**
857862
* List of payments that have been refunded.
858863
*
859-
* @return list of payments, where {@link Payment#charge} is smaller than 0.
864+
* @return list of payments, where {@link Payment#timestampRefunded} is not 0.
860865
*/
861-
public static List<Payment> getRefundedPayments() throws Exception {
866+
public static List<Payment> getRefundedPayments() {
862867
return getRefundedPayments(null);
863868
}
864869

865870
/**
866871
* List of payments that have been refunded.
867872
*
868-
* @return list of payments, where {@link Payment#charge} is smaller than 0.
873+
* @return list of payments, where {@link Payment#timestampRefunded} is not 0.
869874
*/
870-
public static List<Payment> getRefundedPayments(String where, Object... objs) throws Exception {
871-
return get("charge <= 0 " + (where != null ? " AND " + where : ""), objs);
875+
public static List<Payment> getRefundedPayments(String where, Object... objs) {
876+
return get("WHERE timestampRefunded != 0 " + (where != null ? " AND " + where : ""), objs);
872877
}
873878

874879
public PaymentProcessor getPaymentProcessor() {

src/test/java/com/osiris/jsqlgen/payhook/PaymentTest.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,49 @@ void getSubscriptionPaymentsForUser() throws ManagedProcessException {
1616
Payment.whereUserId().is("test").remove();
1717
Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
1818
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.MONTHLY);
19-
assertEquals(0, Payment.getSubscriptionPaymentsForUser("test").size());
19+
assertEquals(0, Payment.getUserSubscriptionPayments("test").size());
2020
p.paypalSubscriptionId = "not-null";
2121
Payment.update(p);
22-
assertEquals(1, Payment.getSubscriptionPaymentsForUser("test").size());
22+
assertEquals(1, Payment.getUserSubscriptionPayments("test").size());
2323
}
2424

2525
@Test
26-
void getPendingFuturePayments() {
27-
}
28-
29-
@Test
30-
void getPendingPayments() {
26+
void getPendingPayments() throws ManagedProcessException {
27+
DatabaseTest.init();
28+
Payment.whereUserId().is("test").remove();
29+
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
30+
p.timestampCreated = now - 1000;
31+
Payment.update(p);
32+
assertEquals(1, Payment.getUserPendingPayments("test").size());
3133
}
3234

3335
@Test
34-
void getAuthorizedPayments() {
36+
void getAuthorizedPayments() throws ManagedProcessException {
37+
DatabaseTest.init();
38+
Payment.whereUserId().is("test").remove();
39+
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
40+
p.timestampAuthorized = now - 1000;
41+
Payment.update(p);
42+
assertEquals(1, Payment.getUserAuthorizedPayments("test").size());
3543
}
3644

3745
@Test
38-
void getCancelledPayments() {
46+
void getCancelledPayments() throws ManagedProcessException {
47+
DatabaseTest.init();
48+
Payment.whereUserId().is("test").remove();
49+
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
50+
p.timestampCancelled = now - 1000;
51+
Payment.update(p);
52+
assertEquals(1, Payment.getUserCancelledPayments("test").size());
3953
}
4054

4155
@Test
42-
void getRefundedPayments() {
56+
void getRefundedPayments() throws ManagedProcessException {
57+
DatabaseTest.init();
58+
Payment.whereUserId().is("test").remove();
59+
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
60+
p.timestampRefunded = now - 1000;
61+
Payment.update(p);
62+
assertEquals(1, Payment.getUserRefundedPayments("test").size());
4363
}
4464
}

0 commit comments

Comments
 (0)