{"record":{"id":"bc4db7b0885b718a","repo":"nopSolutions/nopCommerce","slug":"purchase-was-not-initiated-by-customer","errorCode":null,"errorMessage":"Purchase was not initiated by customer","messagePattern":"Purchase was not initiated by customer","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Plugins/Nop.Plugin.Widgets.FacebookPixel/Services/FacebookPixelService.cs","lineNumber":721,"sourceCode":"        };\n    }\n\n    /// <summary>\n    /// Prepare purchase event model\n    /// </summary>\n    /// <param name=\"order\">Order</param>\n    /// <returns>\n    /// A task that represents the asynchronous operation\n    /// The task result contains the ConversionsEvent model\n    /// </returns>\n    protected async Task<ConversionsEvent> PreparePurchaseModelAsync(Order order)\n    {\n        ArgumentNullException.ThrowIfNull(order);\n\n        //check whether the purchase was initiated by the customer\n        var customer = await _workContext.GetCurrentCustomerAsync();\n        if (order.CustomerId != customer.Id)\n            throw new NopException(\"Purchase was not initiated by customer\");\n\n        //prepare event object\n        var currency = await _currencyService.GetCurrencyByIdAsync(_currencySettings.PrimaryStoreCurrencyId);\n        var contentsProperties = await (await _orderService.GetOrderItemsAsync(order.Id)).SelectAwait(async item =>\n        {\n            var product = await _productService.GetProductByIdAsync(item.ProductId);\n            var sku = product != null ? await _productService.FormatSkuAsync(product, item.AttributesXml) : string.Empty;\n            var quantity = product != null ? (int?)item.Quantity : null;\n            return new { id = sku, quantity = quantity };\n        }).Cast<object>().ToListAsync();\n        var eventObject = new ConversionsEventCustomData\n        {\n            ContentType = \"product\",\n            Contents = contentsProperties,\n            Currency = currency?.CurrencyCode,\n            Value = order.OrderTotal\n        };\n","sourceCodeStart":703,"sourceCodeEnd":739,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Plugins/Nop.Plugin.Widgets.FacebookPixel/Services/FacebookPixelService.cs#L703-L739","documentation":"Thrown by PreparePurchaseModelAsync when order.CustomerId does not equal the current customer's Id. Same ownership guard as the cart event: the purchase ConversionsEvent is only built when the order belongs to the customer active in the work context.","triggerScenarios":"An Order belonging to customer A is passed while the work context resolves customer B: admin order view, backend order-processing job, post-checkout context where the session customer changed.","commonSituations":"Admin viewing/reprocessing an order placed by a customer; a background integration job iterating orders without impersonating each order's customer; a webhook handler reconstructing the work context incorrectly.","solutions":["Impersonate (set the work context to) the order's customer before preparing the event, or bypass it.","Skip the event (return null) instead of throwing for admin/backend order views.","Run purchase-event preparation inline in the checkout flow where the customer context is guaranteed correct."],"exampleFix":"// before\nvar customer = await _workContext.GetCurrentCustomerAsync();\nif (order.CustomerId != customer.Id)\n    throw new NopException(\"Purchase was not initiated by customer\");\n\n// after — only emit during the live checkout context\nvar customer = await _workContext.GetCurrentCustomerAsync();\nif (order.CustomerId != customer.Id)\n    return null; // not the purchaser's own session — skip silently","handlingStrategy":"validation","validationCode":"var current = await _workContext.GetCurrentCustomerAsync();\nif (order.CustomerId != current.Id) return null; // not the purchaser's session","typeGuard":"static bool IsOrderOwner(Order order, Customer current) => order.CustomerId == current.Id;","tryCatchPattern":"try { await PreparePurchaseModelAsync(order); }\ncatch (NopException ex) when (ex.Message.Contains(\"not initiated by customer\"))\n{ _logger.Debug(ex, \"Skipping purchase event for non-owner order\"); }","preventionTips":["Emit purchase events during the live checkout session where context is correct.","Impersonate the order's customer in backend jobs, or skip event emission.","Skip silently rather than throwing for admin order views."],"tags":["facebook-pixel","conversions-api","authorization","customer-context","nopcommerce"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}