diff --git a/EbayListingTool/Services/EbayListingService.cs b/EbayListingTool/Services/EbayListingService.cs index a8403aa..0ca60ab 100644 --- a/EbayListingTool/Services/EbayListingService.cs +++ b/EbayListingTool/Services/EbayListingService.cs @@ -1,4 +1,4 @@ -using System.Net.Http; +using System.Net.Http; using System.Net.Http.Headers; using System.Text; using EbayListingTool.Models; @@ -174,6 +174,35 @@ public class EbayListingService } } + /// Parses an eBay error JSON body into a user-friendly message. + private static string ExtractEbayError(string json, string policyType) + { + try + { + var errors = JObject.Parse(json)["errors"] as JArray; + var first = errors?.FirstOrDefault() as JObject; + if (first != null) + { + var errorId = first["errorId"]?.Value() ?? 0; + var longMsg = first["longMessage"]?.ToString() ?? first["message"]?.ToString() ?? ""; + + // 20403 = account not opted in to Business Policies + if (errorId == 20403 || longMsg.Contains("not eligible for Business Policy")) + return $"Your eBay account is not set up for Business Policies, which are required to post listings via the API.\n\n" + + $"To fix this:\n" + + $"1. Log in to the eBay Seller Hub (or sandbox Seller Hub)\n" + + $"2. Go to Account \u2192 Business policies\n" + + $"3. Create at least one Postage, Payment and Returns policy\n\n" + + $"Once done, click Post to eBay again."; + + if (!string.IsNullOrWhiteSpace(longMsg)) + return $"eBay {policyType} error: {longMsg}"; + } + } + catch { } + return $"Could not fetch {policyType} from eBay. Please check your account settings."; + } + private async Task CreateMerchantLocationAsync(string token, string postcode) { if (string.IsNullOrWhiteSpace(postcode))