Example #1
0
        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 HydrateExternalJson(int shopifyJsonType, long shopifyId)
        {
            if (shopifyJsonType == ShopifyJsonType.Location)
            {
                var json = _inventoryApi.RetrieveLocation(shopifyId);
                UpdateRecord(shopifyJsonType, shopifyId, json);
            }
            if (shopifyJsonType == ShopifyJsonType.Customer)
            {
                var json = _customerApi.Retrieve(shopifyId);
                UpdateRecord(shopifyJsonType, shopifyId, json);
            }
            if (shopifyJsonType == ShopifyJsonType.Product)
            {
                var json = _productApi.RetrieveProducts(shopifyId);
                UpdateRecord(shopifyJsonType, shopifyId, json);
            }
            if (shopifyJsonType == ShopifyJsonType.Variant)
            {
                var json = _productApi.RetrieveVariant(shopifyId);
                UpdateRecord(shopifyJsonType, shopifyId, json);
            }
            if (shopifyJsonType == ShopifyJsonType.Order)
            {
                var json = _orderApi.Retrieve(shopifyId);
                UpdateRecord(shopifyJsonType, shopifyId, json);
            }
            if (shopifyJsonType == ShopifyJsonType.Transaction)
            {
                var transactions =
                    _orderApi.RetrieveTransactions(shopifyId)
                    .DeserializeFromJson <List <Push.Shopify.Api.Transactions.Transaction> >();

                foreach (var transaction in transactions)
                {
                    UpdateRecord(shopifyJsonType, transaction.id, transaction.SerializeToJson());
                }
            }

            throw new NotImplementedException();
        }