Skip to content

Commit

Permalink
Merge pull request #90 from cesmii/feature/eCommerce
Browse files Browse the repository at this point in the history
Feature/e commerce
  • Loading branch information
scoxen1 authored May 23, 2024
2 parents 33483b8 + b877f91 commit 05e283c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
4 changes: 3 additions & 1 deletion api/CESMII.Marketplace.DAL/CartDAL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ protected override CartModel MapToModel(Cart entity, bool verbose = false)
Status = entity.Status,
Items = MapToModelCartItems(entity.Items),
IsActive = entity.IsActive,
SessionId = entity.SessionId
SessionId = entity.SessionId,
CreditsApplied = entity.CreditsApplied
};

return result;
Expand Down Expand Up @@ -241,6 +242,7 @@ protected override void MapToEntity(ref Cart entity, CartModel model)
entity.Completed = model.Completed;
entity.Status = model.Status;
entity.SessionId = model.SessionId;
entity.CreditsApplied = model.CreditsApplied;
entity.Items = MapToEntityCartItems(model.Items);
entity.CheckoutUser = model.CheckoutUser;
}
Expand Down
1 change: 1 addition & 0 deletions api/CESMII.Marketplace.Data/Entities/Cart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Cart : MarketplaceAbstractEntity
public List<CartItem> Items { get; set; }

public string SessionId { get; set; }
public long? CreditsApplied { get; set; }

/// <summary>
/// This is a simplified form of the user which contains basic
Expand Down
18 changes: 11 additions & 7 deletions api/CESMII.Marketplace.JobManager/Jobs/JobOntimeEdgePurchase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using System.Net.Mail;
using System.Net.Http;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

using CESMII.Marketplace.Data.Repositories;
using CESMII.Marketplace.Data.Entities;
using CESMII.Marketplace.DAL;
using CESMII.Marketplace.DAL.Models;
Expand All @@ -26,13 +28,14 @@ namespace CESMII.Marketplace.JobManager.Jobs
public class JobOntimeEdgePurchase : JobBase
{
public JobOntimeEdgePurchase(
IServiceScopeFactory serviceScopeFactory,
ILogger<IJob> logger,
IHttpApiFactory httpFactory,
IHttpApiFactory httpFactory,
IDal<JobLog, JobLogModel> dalJobLog,
UserDAL dalUser,
IConfiguration configuration,
MailRelayService mailRelayService) :
base(logger, httpFactory, dalJobLog, dalUser, configuration, mailRelayService)
MailRelayService mailRelayService) :
base(serviceScopeFactory, logger, httpFactory, dalJobLog, dalUser, configuration, mailRelayService)
{
//wire up run async event
base.JobRun += JobInitiateLicensing;
Expand All @@ -48,7 +51,6 @@ private async Task<string> JobInitiateLicensing(object sender, JobEventArgs e)
{
//3 sources of data - job config - static data, runtime payload data - user entered data and user data - user profile data
//extract out job config params from payload and convert from JSON to an object we can use within this job
//if smip settings is null, still allow purchase to go through but inform user
var jobConfig = JsonConvert.DeserializeObject<JobOnTimeEdgeConfig>(e.Config.Data);
var payload = JsonConvert.DeserializeObject<ECommerceOnCompleteModel>(e.Payload);

Expand Down Expand Up @@ -78,6 +80,7 @@ private async Task<string> JobInitiateLicensing(object sender, JobEventArgs e)
//call the Zapier catch hook API to intialize start purchase flow
var config = new HttpApiConfig()
{
BaseAddress = "",
Url = jobConfig.ApogeanApi.Url,
Body = new StringContent(JsonConvert.SerializeObject(req)),
ContentType = "application/json",
Expand Down Expand Up @@ -150,7 +153,8 @@ private bool ValidateJobConfig(JobOnTimeEdgeConfig jobConfig, out string message
sbResult.AppendLine("The ApogeanApi config section is missing. Please contact the system administrator.");
_logger.LogError($"JobOntimeEdgePurchase|ValidateJobConfig|ApogeanApi section is missing.");
}
if (string.IsNullOrEmpty(jobConfig.ApogeanApi.Url)) {
if (string.IsNullOrEmpty(jobConfig.ApogeanApi.Url))
{
sbResult.AppendLine("The OnTime Edge url is missing. Please contact the system administrator.");
_logger.LogError($"JobOntimeEdgePurchase|ValidateJobConfig|The jobConfig.Url is missing.");
}
Expand All @@ -172,7 +176,7 @@ private bool ValidatePayload(ECommerceOnCompleteModel payload, out string messag
sbResult.AppendLine("The purchaser information is missing. Please contact the system administrator.");
_logger.LogError($"JobOntimeEdgePurchase|ValidatePayload|payload is missing.");
}

if (string.IsNullOrEmpty(payload.CheckoutUser?.FirstName))
{
sbResult.AppendLine("First Name is required.");
Expand Down Expand Up @@ -231,7 +235,7 @@ private async Task GenerateNotifyEmailHTML(MarketplaceItemSimpleModel marketplac
}
*/
var title = isSuccess ? "Purchase Fulfillment Submitted Successfully" : "Purchase Fulfillment Failed";

//generate email body
System.Text.StringBuilder sbBody = new System.Text.StringBuilder();
sbBody.AppendLine("<div class='row mb-2'>");
Expand Down
16 changes: 8 additions & 8 deletions api/CESMII.Marketplace.Service/StripeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task<CheckoutInitModel> DoCheckout(CartModel cart)
//append check out session id
ReturnUrl = cart.ReturnUrl.TrimEnd(Convert.ToChar("/")) + "/{CHECKOUT_SESSION_ID}",
LineItems = new List<SessionLineItemOptions>(),
ExpiresAt = DateTime.UtcNow.AddMinutes(30) //TODO: make this configurable in appSettings
ExpiresAt = DateTime.Now.AddMinutes(30) //TODO: make this configurable in appSettings
};

// For anonymous users, save the cart details first into the database and use that after successful checkout.
Expand Down Expand Up @@ -195,7 +195,7 @@ await _stripeLogDal.Add(new StripeAuditLogModel
// Update/deduct organization credits to deduct amount used.
if (cart.CreditsApplied.HasValue)
{
organization.Credits -= cart.CreditsApplied.Value;
organization.Credits = Convert.ToInt64(organization.Credits + "00") - cart.CreditsApplied.Value;
}
await _organizationService.Update(organization, cart.CreatedById);
}
Expand Down Expand Up @@ -411,14 +411,14 @@ private async Task SendMailItemPurchased(Controller controller, MarketplaceItemC
/// <returns></returns>
private async Task OnCheckoutCompleteJob(CartItemModel item, MarketplaceItemCheckoutModel marketplaceItem, UserCheckoutModel user)
{
//check if job is assigned for this it
var job = _dalJobDefinition.Where(x => x.MarketplaceItemId.ToString().Equals(marketplaceItem.ID) &&
x.ActionType == (int)JobActionTypeEnum.ECommerceOnComplete, null, 1).Data?.First();

if (job == null) return;

try
{
//check if job is assigned for this it
var job = _dalJobDefinition.Where(x => x.MarketplaceItemId.ToString().Equals(marketplaceItem.ID) &&
x.ActionType == (int)JobActionTypeEnum.ECommerceOnComplete, null, 1).Data.FirstOrDefault();

if (job == null) return;

if (job.RequiresAuthentication && string.IsNullOrEmpty(user?.ID))
{
_logger.LogError($"StripeService|onCheckoutCompleteJob|Job requires an authenticated user");
Expand Down

0 comments on commit 05e283c

Please sign in to comment.