public ShopifyCustomer UpsertCustomer(Customer customer) { using (var transaction = _orderRepository.BeginTransaction()) { var existingCustomer = _orderRepository.RetrieveCustomer(customer.id); if (existingCustomer == null) { var newCustomer = new ShopifyCustomer(); newCustomer.ShopifyCustomerId = customer.id; newCustomer.ShopifyPrimaryEmail = customer.email; newCustomer.DateCreated = DateTime.UtcNow; newCustomer.LastUpdated = DateTime.UtcNow; newCustomer.NeedsCustomerPut = true; _orderRepository.InsertCustomer(newCustomer); _shopifyJsonService.Upsert(ShopifyJsonType.Customer, customer.id, customer.SerializeToJson()); transaction.Commit(); return(newCustomer); } else { existingCustomer.ShopifyPrimaryEmail = customer.email; existingCustomer.NeedsCustomerPut = true; existingCustomer.LastUpdated = DateTime.UtcNow; _orderRepository.SaveChanges(); _shopifyJsonService.Upsert(ShopifyJsonType.Customer, customer.id, customer.SerializeToJson()); transaction.Commit(); return(existingCustomer); } } }
private void PullTransactionsFromShopify(ShopifyOrder orderRecord) { var transactionsJson = _orderApi.RetrieveTransactions(orderRecord.ShopifyOrderId); var transactions = transactionsJson.DeserializeFromJson <TransactionList>(); var order = _shopifyJsonService.RetrieveOrder(orderRecord.ShopifyOrderId); foreach (var transaction in transactions.transactions) { var transactionRecord = _orderRepository.RetrieveTransaction(transaction.id); if (transactionRecord != null) { transactionRecord.LastUpdated = DateTime.UtcNow; _orderRepository.SaveChanges(); continue; } using (var dbTransaction = _orderRepository.BeginTransaction()) { var record = new ShopifyTransaction(); if (transaction.kind == TransactionKind.Refund) { record.IsSyncableToPayment = true; var refund = order.RefundByTransaction(transaction.id); if (refund != null) { record.ShopifyRefundId = refund.id; record.IsPureCancel = refund.IsPureCancel; } } if (transaction.kind == TransactionKind.Capture || transaction.kind == TransactionKind.Sale) { record.IsSyncableToPayment = true; } record.ShopifyOrderId = transaction.order_id; record.ShopifyTransactionId = transaction.id; record.ShopifyStatus = transaction.status; record.ShopifyKind = transaction.kind; record.ShopifyGateway = transaction.gateway; record.ShopifyAmount = transaction.amount; record.ShopifyOrderMonsterId = orderRecord.MonsterId; record.DateCreated = DateTime.UtcNow; record.LastUpdated = DateTime.UtcNow; _logService.Log(LogBuilder.DetectedNewShopifyTransaction(record)); _orderRepository.InsertTransaction(record); _shopifyJsonService.Upsert( ShopifyJsonType.Transaction, transaction.id, transaction.SerializeToJson()); dbTransaction.Commit(); } } orderRecord.NeedsTransactionGet = false; _orderRepository.SaveChanges(); }
private void UpsertOrderAndCustomer(Order order) { var monsterCustomerRecord = _shopifyCustomerPull.UpsertCustomer(order.customer); using (var transaction = _orderRepository.BeginTransaction()) { var existingOrder = _orderRepository.RetrieveOrder(order.id); if (existingOrder == null) { var newOrder = new ShopifyOrder(); newOrder.ShopifyOrderId = order.id; newOrder.ShopifyOrderNumber = order.name; newOrder.ShopifyTotalPrice = order.total_price; newOrder.ShopifyFinancialStatus = order.financial_status; newOrder.ShopifyFulfillmentStatus = order.fulfillment_status; newOrder.ShopifyIsCancelled = order.IsCancelled; newOrder.ShopifyAreAllItemsRefunded = order.AreAllLineItemsRefunded; newOrder.ShopifyTotalQuantity = order.NetOrderedQuantity; newOrder.NeedsOrderPut = false; newOrder.NeedsTransactionGet = true; newOrder.ErrorCount = 0; newOrder.Ignore = false; newOrder.CustomerMonsterId = monsterCustomerRecord.MonsterId; newOrder.DateCreated = DateTime.UtcNow; newOrder.LastUpdated = DateTime.UtcNow; _executionLogService.Log(LogBuilder.DetectedNewShopifyOrder(newOrder)); _orderRepository.InsertOrder(newOrder); } else { existingOrder.ShopifyTotalPrice = order.total_price; existingOrder.ShopifyFinancialStatus = order.financial_status; existingOrder.ShopifyFulfillmentStatus = order.fulfillment_status; existingOrder.ShopifyIsCancelled = order.IsCancelled; existingOrder.ShopifyAreAllItemsRefunded = order.AreAllLineItemsRefunded; existingOrder.ShopifyTotalQuantity = order.NetOrderedQuantity; if (existingOrder.StatusChangeDetected(order)) { existingOrder.NeedsOrderPut = true; } existingOrder.NeedsTransactionGet = true; existingOrder.LastUpdated = DateTime.UtcNow; _executionLogService.Log(LogBuilder.DetectedUpdateShopifyOrder(existingOrder)); _orderRepository.SaveChanges(); } _shopifyJsonService.Upsert(ShopifyJsonType.Order, order.id, order.SerializeToJson()); transaction.Commit(); } }