Skip to content

Commit

Permalink
fix Payment.get... methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Osiris-Team committed Feb 15, 2023
1 parent 83b9dad commit b19119e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 34 deletions.
53 changes: 29 additions & 24 deletions src/main/java/com/osiris/jsqlgen/payhook/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -773,23 +773,16 @@ public static List<Payment> getForUser(String userId){
return get("userId=?", userId);
}

public static List<Payment> getSubscriptionPaymentsForUser(String userId){
public static List<Payment> getUserSubscriptionPayments(String userId){
return whereUserId().is(userId)
.and(wherePaypalSubscriptionId().isNotNull()
.or(whereStripeSubscriptionId().isNotNull()))
.get();
// TODO ADD NEW PAYMENT PROCESSOR
}

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

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

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

public static List<Payment> getUserAuthorizedPayments(String userId) {
return getAuthorizedPayments("userId=?", userId);
}

/**
* List of payments that have been authorized/completed/paid.
*
* @return list of payments, where {@link Payment#timestampAuthorized} is not null.
* @see PayHook#onPaymentAuthorized
*/
public static List<Payment> getAuthorizedPayments() throws Exception {
public static List<Payment> getAuthorizedPayments() {
return getAuthorizedPayments(null);
}

Expand All @@ -829,8 +826,12 @@ public static List<Payment> getAuthorizedPayments() throws Exception {
* @return list of payments, where {@link Payment#timestampAuthorized} is not null.
* @see PayHook#onPaymentAuthorized
*/
public static List<Payment> getAuthorizedPayments(String where, Object... objs) throws Exception {
return get("timestampAuthorized != 0 " + (where != null ? " AND " + where : ""), objs);
public static List<Payment> getAuthorizedPayments(String where, Object... objs) {
return get("WHERE timestampAuthorized != 0 " + (where != null ? " AND " + where : ""), objs);
}

public static List<Payment> getUserCancelledPayments(String userId) {
return getCancelledPayments("userId=?", userId);
}

/**
Expand All @@ -839,7 +840,7 @@ public static List<Payment> getAuthorizedPayments(String where, Object... objs)
* @return list of payments, where {@link Payment#timestampCancelled} is not null.
* @see PayHook#onPaymentCancelled
*/
public static List<Payment> getCancelledPayments() throws Exception {
public static List<Payment> getCancelledPayments() {
return getCancelledPayments(null);
}

Expand All @@ -849,26 +850,30 @@ public static List<Payment> getCancelledPayments() throws Exception {
* @return list of payments, where {@link Payment#timestampCancelled} is not null.
* @see PayHook#onPaymentCancelled
*/
public static List<Payment> getCancelledPayments(String where, Object... objs) throws Exception {
return get("timestampCancelled != 0 " + (where != null ? " AND " + where : ""), objs);
public static List<Payment> getCancelledPayments(String where, Object... objs) {
return get("WHERE timestampCancelled != 0 " + (where != null ? " AND " + where : ""), objs);
}

public static List<Payment> getUserRefundedPayments(String userId) {
return getRefundedPayments("userId=?", userId);
}

/**
* List of payments that have been refunded.
*
* @return list of payments, where {@link Payment#charge} is smaller than 0.
* @return list of payments, where {@link Payment#timestampRefunded} is not 0.
*/
public static List<Payment> getRefundedPayments() throws Exception {
public static List<Payment> getRefundedPayments() {
return getRefundedPayments(null);
}

/**
* List of payments that have been refunded.
*
* @return list of payments, where {@link Payment#charge} is smaller than 0.
* @return list of payments, where {@link Payment#timestampRefunded} is not 0.
*/
public static List<Payment> getRefundedPayments(String where, Object... objs) throws Exception {
return get("charge <= 0 " + (where != null ? " AND " + where : ""), objs);
public static List<Payment> getRefundedPayments(String where, Object... objs) {
return get("WHERE timestampRefunded != 0 " + (where != null ? " AND " + where : ""), objs);
}

public PaymentProcessor getPaymentProcessor() {
Expand Down
40 changes: 30 additions & 10 deletions src/test/java/com/osiris/jsqlgen/payhook/PaymentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,49 @@ void getSubscriptionPaymentsForUser() throws ManagedProcessException {
Payment.whereUserId().is("test").remove();
Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.MONTHLY);
assertEquals(0, Payment.getSubscriptionPaymentsForUser("test").size());
assertEquals(0, Payment.getUserSubscriptionPayments("test").size());
p.paypalSubscriptionId = "not-null";
Payment.update(p);
assertEquals(1, Payment.getSubscriptionPaymentsForUser("test").size());
assertEquals(1, Payment.getUserSubscriptionPayments("test").size());
}

@Test
void getPendingFuturePayments() {
}

@Test
void getPendingPayments() {
void getPendingPayments() throws ManagedProcessException {
DatabaseTest.init();
Payment.whereUserId().is("test").remove();
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
p.timestampCreated = now - 1000;
Payment.update(p);
assertEquals(1, Payment.getUserPendingPayments("test").size());
}

@Test
void getAuthorizedPayments() {
void getAuthorizedPayments() throws ManagedProcessException {
DatabaseTest.init();
Payment.whereUserId().is("test").remove();
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
p.timestampAuthorized = now - 1000;
Payment.update(p);
assertEquals(1, Payment.getUserAuthorizedPayments("test").size());
}

@Test
void getCancelledPayments() {
void getCancelledPayments() throws ManagedProcessException {
DatabaseTest.init();
Payment.whereUserId().is("test").remove();
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
p.timestampCancelled = now - 1000;
Payment.update(p);
assertEquals(1, Payment.getUserCancelledPayments("test").size());
}

@Test
void getRefundedPayments() {
void getRefundedPayments() throws ManagedProcessException {
DatabaseTest.init();
Payment.whereUserId().is("test").remove();
Payment p = Payment.createAndAdd("test", 100, "EUR", Payment.Interval.NONE);
p.timestampRefunded = now - 1000;
Payment.update(p);
assertEquals(1, Payment.getUserRefundedPayments("test").size());
}
}

0 comments on commit b19119e

Please sign in to comment.