Commit fdd9b7c3 by Nilu

stripe - credit based job openings (webhook to receive invoice paid event)

parent 80f0a11b
......@@ -24,6 +24,8 @@
<column name="name_on_card" type="String" nullable="true" length="100"/>
<column name="card_post_code" type="String" nullable="true" length="10"/>
<column name="card_id" type="String" nullable="true" length="100"/>
<column name="plan_renewed_on" type="Date" nullable="true"/>
<column name="used_credits" type="Long" nullable="true"/>
<column name="added_by_user_id" type="Long" length="11" nullable="false"/>
<column name="payment_plan_id" type="Long" length="11" nullable="true"/>
</NODE>
......
......@@ -24,6 +24,8 @@ CREATE TABLE tl_company (
name_on_card varchar(100) NULL,
card_post_code varchar(10) NULL,
card_id varchar(100) NULL,
plan_renewed_on datetime NULL,
used_credits numeric(12) NULL,
added_by_user_id numeric(12) NOT NULL,
payment_plan_id numeric(12) NULL
);
......
......@@ -25,6 +25,8 @@ CREATE TABLE tl_company (
name_on_card varchar2(100) NULL,
card_post_code varchar2(10) NULL,
card_id varchar2(100) NULL,
plan_renewed_on date NULL,
used_credits number(12) NULL,
added_by_user_id number(12) NOT NULL,
payment_plan_id number(12) NULL
);
......
......@@ -25,6 +25,8 @@ CREATE TABLE tl_company (
name_on_card varchar(100) NULL,
card_post_code varchar(10) NULL,
card_id varchar(100) NULL,
plan_renewed_on timestamp NULL,
used_credits numeric(12) NULL,
added_by_user_id numeric(12) NOT NULL,
payment_plan_id numeric(12) NULL
);
......
......@@ -91,13 +91,6 @@ public class MakePaymentFP extends SaveFP
company.setCardID(card.getId());
}
if(!ppj && company.getPaymentPlan() != null)
{
// cannot subscribe to a plan without card details
StripeUtils.updatePlan(company);
}
job.setApplyBy(DateDiff.add(DateDiff.getToday(), Calendar.DATE, 30));
job.setOpenDate(new Date());
job.setJobStatus(JobStatus.OPEN);
......@@ -108,6 +101,15 @@ public class MakePaymentFP extends SaveFP
job.createShortenedURL();
}
if(!ppj && company.getPaymentPlan() != null)
{
// cannot subscribe to a plan without card details
company.setPlanRenewedOn(DateDiff.getToday());
company.setUsedCredits(1);
StripeUtils.updatePlan(company);
}
// restarting process as custom attributes needs to be updated to intercom
completeProcessRestartAndRestoreAttribs(process, request);
......
......@@ -49,6 +49,8 @@ public class SaveJobFP extends SaveFP
job.setApplyBy(DateDiff.add(DateDiff.getToday(), Calendar.DATE, 30));
job.setOpenDate(new Date());
company.setUsedCredits(company.getUsedCredits() + 1);
if(status == JobStatus.OPEN)
{
if(company.getSelectedPaymentPlan() != null)
......
......@@ -147,9 +147,7 @@ public class Company extends BaseCompany
public boolean canCreateJob()
{
Job[] openJobs = getOpenJobs();
return getPaymentPlan() != null && getPaymentPlan().getActiveJobCount() > openJobs.length;
return getPaymentPlan() != null && getPaymentPlan().getActiveJobCount() > getUsedCredits();
}
......
......@@ -34,14 +34,16 @@
<ATTRIB name="PostCode" type="String" dbcol="post_code" mandatory="false" length="10"/>
<ATTRIB name="City" type="String" dbcol="city" mandatory="false" length="100"/>
<ATTRIB name="HasClientSupport" type="Boolean" dbcol="has_client_support" mandatory="false" defaultValue="Boolean.FALSE"/>
<ATTRIB name="StripeReference" type="String" dbcol="stripe_reference" length="100"/>
<ATTRIB name="StripeSubscription" type="String" dbcol="stripe_subscription" length="100"/>
<ATTRIB name="NameOnCard" type="String" dbcol="name_on_card" length="100"/>
<ATTRIB name="CardPostCode" type="String" dbcol="card_post_code" length="10"/>
<ATTRIB name="CardID" type="String" dbcol="card_id" length="100"/>
<SINGLEREFERENCE name="AddedByUser" type="CompanyUser" dbcol="added_by_user_id" mandatory="true"/>
<SINGLEREFERENCE name="PaymentPlan" type="PaymentPlan" dbcol="payment_plan_id" mandatory="false"/>
<ATTRIB name="StripeReference" type="String" dbcol="stripe_reference" length="100" />
<ATTRIB name="StripeSubscription" type="String" dbcol="stripe_subscription" length="100" />
<ATTRIB name="NameOnCard" type="String" dbcol="name_on_card" length="100" />
<ATTRIB name="CardPostCode" type="String" dbcol="card_post_code" length="10" />
<ATTRIB name="CardID" type="String" dbcol="card_id" length="100" />
<ATTRIB name="PlanRenewedOn" type="Date" dbcol="plan_renewed_on" mandatory="false" />
<ATTRIB name="UsedCredits" type="Integer" dbcol="used_credits" />
<SINGLEREFERENCE name="AddedByUser" type="CompanyUser" dbcol="added_by_user_id" mandatory="true" />
<SINGLEREFERENCE name="PaymentPlan" type="PaymentPlan" dbcol="payment_plan_id" mandatory="false" />
</TABLE>
......
......@@ -8,25 +8,32 @@ import com.stripe.exception.CardException;
import com.stripe.exception.InvalidRequestException;
import com.stripe.model.Card;
import com.stripe.model.Customer;
import com.stripe.model.Event;
import com.stripe.model.Invoice;
import com.stripe.model.Plan;
import com.stripe.model.Subscription;
import com.stripe.net.APIResource;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import oneit.appservices.config.ConfigMgr;
import oneit.logging.LogLevel;
import oneit.logging.LogMgr;
import oneit.logging.LoggingArea;
import oneit.objstore.ObjectTransaction;
import oneit.objstore.StorageException;
import oneit.objstore.rdbms.filters.EqualsFilter;
import oneit.security.SecUser;
import oneit.utils.DateDiff;
import oneit.utils.parsers.FieldException;
import performa.orm.Company;
import performa.orm.CompanyUser;
import performa.orm.PaymentPlan;
import spark.utils.IOUtils;
public class StripeUtils
......@@ -212,4 +219,51 @@ public class StripeUtils
LogMgr.log(LoggingArea.ALL, LogLevel.PROCESSING1, ex, "Error while creating subscrition in stripe");
}
}
public static void handleWebhook(HttpServletRequest request, ObjectTransaction objTran) throws FieldException
{
try
{
String rawJson = IOUtils.toString(request.getInputStream());
Event event = APIResource.GSON.fromJson(rawJson, Event.class);
LogMgr.log(LoggingArea.ALL, LogLevel.PROCESSING1, "Event received from stripe : ", event);
if (event != null && event.getType().equals("invoice.payment_succeeded"))
{
Invoice invoice = (Invoice) event.getData().getObject();
LogMgr.log(LoggingArea.ALL, LogLevel.PROCESSING1, "Payment Succeeded event received from stripe with invoice : ", invoice);
if(invoice.getBilling().equals("charge_automatically"))
{
Company[] companies = Company.SearchByAll().andStripeReference(new EqualsFilter<>(invoice.getCustomer())).search(objTran);
if(companies != null && companies.length > 0)
{
Company company = companies[0];
Date invoiceDate = new Date(invoice.getDate() * 1000L);
if(company.getPlanRenewedOn() == null || !DateDiff.startOfDay(invoiceDate).equals(DateDiff.startOfDay(company.getPlanRenewedOn())))
{
if(company.getPlanRenewedOn() != null)
{
company.setUsedCredits(0);
}
company.setPlanRenewedOn(invoiceDate);
}
LogMgr.log(LoggingArea.ALL, LogLevel.PROCESSING1, "Setting company with reset plan details : ", company);
}
}
}
}
catch (IOException ex)
{
LogMgr.log(LoggingArea.ALL, LogLevel.PROCESSING1, ex, "Error while handling webhook");
}
}
}
......@@ -130,6 +130,10 @@
<INHERITS factory="Named" nodename="CoreORMAdminNoPriv"/>
</NODE>
<NODE name="webhook_jsp" factory="Participant">
<INHERITS factory="Named" nodename="CoreORMAdminNoPriv"/>
</NODE>
<NODE name="ORMErrorConfig::ADMIN_PORTAL" factory="Participant" class="oneit.servlets.forms.ErrorReportConfig">
<format item="field.*.error.pageHeader_performa_errorPrefix">
<![CDATA[<div class="error-message message-common"><img src="${contextRoot}/images/error-alert.png" class="alert-icon" /><span class="message-txt" style="font-weight: bold">${translateLabel:Errors_Occurred:Errors occurred, please correct them and try again}</span><br/>]]>
......
<?xml version="1.0" encoding="UTF-8"?>
<!-- @AutoRun -->
<OBJECTS name="" xmlns:oneit="http://www.1iT.com.au">
<NODE name="Script" factory="Vector">
<NODE name="DDL" factory="Participant" class="oneit.sql.transfer.RedefineTableOperation">
<tableName factory="String">tl_company</tableName>
<column name="plan_renewed_on" type="Date" nullable="true"/>
<column name="used_credits" type="Long" nullable="true"/>
</NODE>
</NODE>
</OBJECTS>
\ No newline at end of file
<%@page import="performa.utils.StripeUtils"%>
<%@page import="oneit.objstore.ObjectTransaction"%>
<%@page import="oneit.servlets.process.ProcessDecorator"%>
<%@page import="oneit.servlets.process.ORMProcessState"%>
<%@ page extends="oneit.servlets.jsp.FormJSP" %>
<%! protected String getName (ServletConfig config) { return "webhook_jsp"; } %>
<%
ORMProcessState process = (ORMProcessState) ProcessDecorator.getDefaultProcess(request);
ObjectTransaction objTran = process.getTransaction ();
StripeUtils.handleWebhook(request, objTran);
process.complete();
%>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment