-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a57b2a
commit b88bbe9
Showing
9 changed files
with
706 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/com/impactupgrade/nucleus/controller/AccountingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.impactupgrade.nucleus.controller; | ||
|
||
import com.impactupgrade.nucleus.environment.Environment; | ||
import com.impactupgrade.nucleus.environment.EnvironmentFactory; | ||
import com.impactupgrade.nucleus.model.PaymentGatewayDeposit; | ||
import com.impactupgrade.nucleus.security.SecurityUtil; | ||
import com.impactupgrade.nucleus.service.segment.PaymentGatewayService; | ||
import org.apache.commons.collections.CollectionUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.FormParam; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
@Path("/accounting") | ||
public class AccountingController { | ||
|
||
private static final Logger log = LogManager.getLogger(AccountingController.class); | ||
private static final String DATE_FORMAT = "yyyy-MM-dd"; | ||
|
||
protected final EnvironmentFactory environmentFactory; | ||
|
||
public AccountingController(EnvironmentFactory environmentFactory) { | ||
this.environmentFactory = environmentFactory; | ||
} | ||
|
||
@Path("/sync-transactions") | ||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Response syncTransactions( | ||
@FormParam("start_date") String start, | ||
@FormParam("end_date") String end, | ||
@Context HttpServletRequest request | ||
) throws Exception { | ||
|
||
Environment environment = environmentFactory.init(request); | ||
SecurityUtil.verifyApiKey(environment); | ||
|
||
Date startDate = new SimpleDateFormat(DATE_FORMAT).parse(start); | ||
Date endDate = new SimpleDateFormat(DATE_FORMAT).parse(end); | ||
|
||
Runnable runnable = () -> { | ||
// Get all payment services available (Stripe is it you?) | ||
List<PaymentGatewayService> paymentGatewayServices = environment.allPaymentGatewayServices(); | ||
|
||
if (CollectionUtils.isEmpty(paymentGatewayServices)) { | ||
log.info("Payment Services not defined for environment! Returning..."); | ||
return; | ||
} | ||
|
||
List<PaymentGatewayDeposit> deposits = new ArrayList<>(); | ||
|
||
try { | ||
for (PaymentGatewayService paymentGatewayService : paymentGatewayServices) { | ||
String serviceName = paymentGatewayService.name(); | ||
log.info("Payment service '{}' running...", serviceName); | ||
deposits.addAll(paymentGatewayService.getDeposits(startDate, endDate)); | ||
log.info("Payment service '{}' done.", serviceName); | ||
} | ||
} catch (Exception e) { | ||
log.error("Failed to get deposits! {}", e.getMessage()); | ||
return; | ||
} | ||
|
||
environment.accountingService().addDeposits(deposits); | ||
}; | ||
|
||
// Away from the main thread | ||
new Thread(runnable).start(); | ||
|
||
return Response.ok().build(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.