@polymorphism-tech/morph-spec 11.1.1 → 11.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/README.md +3 -2
  2. package/bin/morph-spec.js +13 -23
  3. package/docs/CHEATSHEET.md +3 -3
  4. package/docs/QUICKSTART.md +1 -1
  5. package/framework/CLAUDE.md +3 -1
  6. package/framework/MORPH.md +8 -3
  7. package/framework/agents.json +4 -4
  8. package/framework/docs/CLI.md +28 -18
  9. package/framework/evals/review-checklist.md +35 -18
  10. package/framework/hooks/README.md +18 -11
  11. package/framework/hooks/claude-code/core/README.md +2 -2
  12. package/framework/hooks/claude-code/core/chain.js +9 -25
  13. package/framework/hooks/claude-code/core/comment-guard.js +3 -9
  14. package/framework/hooks/claude-code/core/conventional-commit.js +3 -3
  15. package/framework/hooks/claude-code/core/graph-refresh-kick.js +6 -6
  16. package/framework/hooks/claude-code/core/masked-exit.js +4 -3
  17. package/framework/hooks/claude-code/core/pre-bash-denylist.js +2 -2
  18. package/framework/hooks/claude-code/core/protect-spec-files.js +2 -4
  19. package/framework/hooks/claude-code/core/stack-changed.js +5 -4
  20. package/framework/hooks/claude-code/core/validate-completion.js +15 -12
  21. package/framework/hooks/shared/contract.js +22 -1
  22. package/framework/hooks/shared/git-paths.js +6 -0
  23. package/framework/hooks/shared/graph-freshness.js +3 -3
  24. package/framework/hooks/shared/graph-refresh.js +5 -10
  25. package/framework/hooks/shared/hook-response.js +1 -18
  26. package/framework/hooks/shared/payload-utils.js +12 -1
  27. package/framework/hooks/shared/phase-utils.js +4 -52
  28. package/framework/hooks/shared/report-progress.js +1 -1
  29. package/framework/mcp/registry.json +16 -32
  30. package/framework/rules/ai-agents-standards.md +9 -1
  31. package/framework/rules/dotnet-templates.md +32 -33
  32. package/framework/rules/gsap-standards.md +8 -0
  33. package/framework/rules/infrastructure-standards.md +9 -9
  34. package/framework/rules/mcp-standards.md +10 -0
  35. package/framework/rules/neon-standards.md +4 -4
  36. package/framework/rules/nextjs-templates.md +10 -11
  37. package/framework/rules/ui-design.md +2 -2
  38. package/framework/schemas/config.schema.json +11 -0
  39. package/framework/skills/core/create-umbraco-lp/SKILL.md +1 -1
  40. package/framework/skills/core/migrate-lp-to-umbraco/SKILL.md +1 -1
  41. package/framework/skills/core/morph-apply/SKILL.md +6 -4
  42. package/framework/skills/core/morph-plan/SKILL.md +1 -1
  43. package/framework/skills/core/morph-uiux/SKILL.md +5 -5
  44. package/framework/standards/STANDARDS.json +8 -4
  45. package/framework/standards/STANDARDS.meta.json +1 -1
  46. package/framework/standards/ai-agents/agent-archetypes.md +1 -1
  47. package/framework/standards/ai-agents/middleware-patterns.md +0 -2
  48. package/framework/standards/ai-agents/prompt-sources.md +2 -2
  49. package/framework/standards/ai-agents/providers/model-registry.md +9 -12
  50. package/framework/standards/ai-agents/rag-custom-pgvector.md +2 -9
  51. package/framework/standards/frontend/design-system/ai-image-generation.md +1 -1
  52. package/framework/standards/infrastructure/docker/local-compose-isolation.md +2 -2
  53. package/framework/standards/integration/mcp/mcp-tools.md +125 -267
  54. package/framework/templates/README.md +52 -82
  55. package/framework/templates/REGISTRY.json +1 -281
  56. package/framework/templates/code/sql/ai-agents/agent-prompts-migration.sql.template +3 -4
  57. package/framework/templates/context/design-system.md +1 -1
  58. package/framework/templates/docs/plan.md +2 -2
  59. package/framework/templates/dotnet/ai-kit/src/Morph.AiKit/Providers/model-registry.v2.json +1 -1
  60. package/framework/templates/dotnet/ai-kit/tests/Morph.AiKit.Tests/MediaDisclosureTests.cs +1 -1
  61. package/framework/templates/dotnet/ai-kit/tests/Morph.AiKit.Tests/Morph.AiKit.Tests.csproj +3 -5
  62. package/framework/templates/frontend/nextjs/gsap-scroll-section.tsx.hbs +13 -17
  63. package/framework/templates/infrastructure/docker/docker-compose.template.yml +15 -16
  64. package/framework/templates/infrastructure/github/README.md +10 -13
  65. package/package.json +7 -16
  66. package/src/commands/graph/build.js +2 -2
  67. package/src/commands/mcp/manual-setup.js +95 -0
  68. package/src/commands/mcp/mcp-setup.js +26 -192
  69. package/src/commands/project/doctor.js +48 -142
  70. package/src/commands/project/env.js +2 -1
  71. package/src/commands/project/init-contract.js +2 -2
  72. package/src/commands/project/init.js +34 -294
  73. package/src/commands/project/stack-scan.js +6 -14
  74. package/src/commands/project/stack-set.js +3 -1
  75. package/src/commands/project/standards.js +1 -2
  76. package/src/commands/project/update.js +87 -205
  77. package/src/commands/scaffold/scaffold.js +43 -5
  78. package/src/commands/validation/check.js +12 -10
  79. package/src/commands/validation/prove-red.js +61 -22
  80. package/src/core/paths/output-schema.js +8 -27
  81. package/src/core/templates/template-registry.js +8 -43
  82. package/src/core/templates/template-renderer.js +3 -200
  83. package/src/lib/ai/ai-checks.js +1 -1
  84. package/src/lib/ai/ai-project-scan.js +2 -5
  85. package/src/lib/ai/code-audit.js +3 -8
  86. package/src/lib/ai/registry-audit.js +2 -1
  87. package/src/lib/check/signals.js +25 -7
  88. package/src/lib/config/project-config.js +3 -2
  89. package/src/lib/contract/apply.js +2 -1
  90. package/src/lib/contract/defaults.js +2 -2
  91. package/src/lib/contract/merge.js +1 -0
  92. package/src/lib/contract/scan.js +5 -5
  93. package/src/lib/detectors/claude-config-detector.js +44 -98
  94. package/src/lib/detectors/mcp-config.js +2 -1
  95. package/src/lib/diff/diff-lines.js +4 -9
  96. package/src/lib/generators/scaffold-generator.js +5 -17
  97. package/src/lib/git/default-branch.js +5 -13
  98. package/src/lib/graph/graph-health.js +1 -1
  99. package/src/lib/graph/graph-loader.js +2 -1
  100. package/src/lib/graph/graphifyignore.js +5 -5
  101. package/src/lib/graph/sanitize.js +2 -2
  102. package/src/lib/installers/effects.js +0 -13
  103. package/src/lib/installers/install-manifest.js +56 -0
  104. package/src/lib/installers/mcp-installer.js +16 -19
  105. package/src/lib/installers/memory-installer.js +5 -8
  106. package/src/lib/installers/rules-installer.js +9 -15
  107. package/src/lib/installers/stack-assets.js +54 -7
  108. package/src/lib/installers/stack-sync.js +16 -16
  109. package/src/lib/installers/stack-tooling.js +4 -3
  110. package/src/lib/json-file.js +9 -0
  111. package/src/lib/plan/artifact-vocabulary.js +1 -1
  112. package/src/lib/plan/checks.js +4 -3
  113. package/src/lib/plan/media.js +6 -6
  114. package/src/lib/plan/plan-parser.js +6 -11
  115. package/src/lib/review-reports.js +13 -18
  116. package/src/lib/scan/worktree-dirs.js +1 -3
  117. package/src/lib/specs/adr-index.js +4 -4
  118. package/src/lib/specs/revocations.js +3 -3
  119. package/src/lib/stack/stack-scan.js +4 -4
  120. package/src/lib/standards/registry.js +1 -1
  121. package/src/lib/standards/verified-against.js +3 -3
  122. package/src/lib/tests-audit/audit.js +3 -3
  123. package/src/lib/text.js +5 -0
  124. package/src/scripts/global-install.js +41 -19
  125. package/src/scripts/setup-infra.js +68 -187
  126. package/src/utils/agents-installer.js +21 -60
  127. package/src/utils/claude-md-injector.js +16 -50
  128. package/src/utils/claude-settings-manager.js +29 -27
  129. package/src/utils/file-copier.js +3 -4
  130. package/src/utils/hooks-installer.js +83 -62
  131. package/src/utils/skills-installer.js +18 -78
  132. package/src/utils/version-checker.js +2 -31
  133. package/CLAUDE.md +0 -118
  134. package/framework/commands/create-umbraco-lp.md +0 -37
  135. package/framework/commands/migrate-lp-to-umbraco.md +0 -35
  136. package/framework/commands/morph-apply.md +0 -14
  137. package/framework/hooks/shared/spawn-command.js +0 -23
  138. package/framework/templates/CLAUDE.md.core.guide.md +0 -29
  139. package/framework/templates/CLAUDE.md.core.template +0 -22
  140. package/framework/templates/IDE-SNIPPETS.md +0 -262
  141. package/framework/templates/code/dotnet/ai-agents/AgentBootstrap.cs.template +0 -26
  142. package/framework/templates/code/dotnet/ai-agents/EmbeddingService.cs.template +0 -28
  143. package/framework/templates/code/dotnet/ai-agents/ModelRegistry.cs.template +0 -99
  144. package/framework/templates/code/dotnet/ai-agents/ResilientToolMiddleware.cs.template +0 -40
  145. package/framework/templates/code/dotnet/ai-agents/VectorSearchTool.cs.template +0 -54
  146. package/framework/templates/code/dotnet/ai-agents/model-registry.json.template +0 -53
  147. package/framework/templates/code/dotnet/contracts/README.md +0 -74
  148. package/framework/templates/code/dotnet/database/vector-entity.cs +0 -49
  149. package/framework/templates/code/sql/ai-agents/pgvector-embeddings-migration.sql.template +0 -91
  150. package/framework/templates/code/sql/postgresql-migration.template.sql +0 -108
  151. package/framework/templates/code/typescript/contracts.ts +0 -140
  152. package/framework/templates/context/ARCHITECTURE.md +0 -106
  153. package/framework/templates/examples/design-system-examples.md +0 -354
  154. package/framework/templates/infrastructure/docker/Dockerfile.template +0 -58
  155. package/framework/templates/infrastructure/github/actions/docker-build-push/action.yml.hbs +0 -45
  156. package/framework/templates/infrastructure/github/actions/health-check/action.yml.hbs +0 -27
  157. package/framework/templates/integrations/asaas-client.cs +0 -332
  158. package/framework/templates/integrations/asaas-webhook.cs +0 -290
  159. package/framework/templates/integrations/neon-auth-config.cs +0 -170
  160. package/framework/templates/ui/design-system.css +0 -226
  161. package/src/commands/context/context-refresh.js +0 -63
  162. package/src/lib/detectors/index.js +0 -178
  163. package/src/lib/generators/architecture-scanner.js +0 -543
  164. package/src/lib/generators/settings-generator.js +0 -97
  165. package/src/lib/graph/god-nodes.js +0 -65
  166. package/src/lib/installers/templates-reconcile.js +0 -26
@@ -1,332 +0,0 @@
1
- using System.Net.Http.Json;
2
- using System.Text.Json.Serialization;
3
- using Microsoft.Extensions.Logging;
4
- using Microsoft.Extensions.Options;
5
-
6
- namespace {{Namespace}}.Infrastructure.Services;
7
-
8
- public class AsaasOptions
9
- {
10
- public const string SectionName = "Asaas";
11
-
12
- // Produção: https://www.asaas.com/api/v3
13
- public string BaseUrl { get; set; } = "https://sandbox.asaas.com/api/v3";
14
-
15
- public string ApiKey { get; set; } = string.Empty;
16
- }
17
-
18
- public interface IAsaasClient
19
- {
20
- Task<AsaasCustomer> CreateCustomerAsync(CreateCustomerRequest request, CancellationToken ct = default);
21
- Task<AsaasCustomer?> GetCustomerByIdAsync(string customerId, CancellationToken ct = default);
22
- Task<AsaasCustomer?> GetCustomerByCpfCnpjAsync(string cpfCnpj, CancellationToken ct = default);
23
-
24
- Task<AsaasPayment> CreatePaymentAsync(CreatePaymentRequest request, CancellationToken ct = default);
25
- Task<AsaasPayment?> GetPaymentAsync(string paymentId, CancellationToken ct = default);
26
- Task<AsaasPixQrCode?> GetPixQrCodeAsync(string paymentId, CancellationToken ct = default);
27
-
28
- Task<AsaasSubscription> CreateSubscriptionAsync(CreateSubscriptionRequest request, CancellationToken ct = default);
29
- Task<AsaasSubscription?> GetSubscriptionAsync(string subscriptionId, CancellationToken ct = default);
30
- Task CancelSubscriptionAsync(string subscriptionId, CancellationToken ct = default);
31
- }
32
-
33
- public class AsaasClient : IAsaasClient
34
- {
35
- private readonly HttpClient _httpClient;
36
- private readonly ILogger<AsaasClient> _logger;
37
-
38
- public AsaasClient(HttpClient httpClient, ILogger<AsaasClient> logger)
39
- {
40
- _httpClient = httpClient;
41
- _logger = logger;
42
- }
43
-
44
- public async Task<AsaasCustomer> CreateCustomerAsync(CreateCustomerRequest request, CancellationToken ct = default)
45
- {
46
- _logger.LogInformation("Creating Asaas customer: {Name}", request.Name);
47
-
48
- var response = await _httpClient.PostAsJsonAsync("customers", request, ct);
49
- await EnsureSuccessAsync(response, "create customer");
50
-
51
- return await response.Content.ReadFromJsonAsync<AsaasCustomer>(ct)
52
- ?? throw new AsaasException("Failed to deserialize customer response");
53
- }
54
-
55
- public async Task<AsaasCustomer?> GetCustomerByIdAsync(string customerId, CancellationToken ct = default)
56
- {
57
- var response = await _httpClient.GetAsync($"customers/{customerId}", ct);
58
-
59
- if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
60
- return null;
61
-
62
- await EnsureSuccessAsync(response, "get customer");
63
- return await response.Content.ReadFromJsonAsync<AsaasCustomer>(ct);
64
- }
65
-
66
- public async Task<AsaasCustomer?> GetCustomerByCpfCnpjAsync(string cpfCnpj, CancellationToken ct = default)
67
- {
68
- var response = await _httpClient.GetAsync($"customers?cpfCnpj={cpfCnpj}", ct);
69
- await EnsureSuccessAsync(response, "search customer");
70
-
71
- var result = await response.Content.ReadFromJsonAsync<AsaasListResponse<AsaasCustomer>>(ct);
72
- return result?.Data?.FirstOrDefault();
73
- }
74
-
75
- public async Task<AsaasPayment> CreatePaymentAsync(CreatePaymentRequest request, CancellationToken ct = default)
76
- {
77
- _logger.LogInformation("Creating Asaas payment for customer {CustomerId}, type {BillingType}, value {Value}",
78
- request.Customer, request.BillingType, request.Value);
79
-
80
- var response = await _httpClient.PostAsJsonAsync("payments", request, ct);
81
- await EnsureSuccessAsync(response, "create payment");
82
-
83
- return await response.Content.ReadFromJsonAsync<AsaasPayment>(ct)
84
- ?? throw new AsaasException("Failed to deserialize payment response");
85
- }
86
-
87
- public async Task<AsaasPayment?> GetPaymentAsync(string paymentId, CancellationToken ct = default)
88
- {
89
- var response = await _httpClient.GetAsync($"payments/{paymentId}", ct);
90
-
91
- if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
92
- return null;
93
-
94
- await EnsureSuccessAsync(response, "get payment");
95
- return await response.Content.ReadFromJsonAsync<AsaasPayment>(ct);
96
- }
97
-
98
- public async Task<AsaasPixQrCode?> GetPixQrCodeAsync(string paymentId, CancellationToken ct = default)
99
- {
100
- var response = await _httpClient.GetAsync($"payments/{paymentId}/pixQrCode", ct);
101
-
102
- if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
103
- return null;
104
-
105
- await EnsureSuccessAsync(response, "get pix qrcode");
106
- return await response.Content.ReadFromJsonAsync<AsaasPixQrCode>(ct);
107
- }
108
-
109
- public async Task<AsaasSubscription> CreateSubscriptionAsync(CreateSubscriptionRequest request, CancellationToken ct = default)
110
- {
111
- _logger.LogInformation("Creating Asaas subscription for customer {CustomerId}", request.Customer);
112
-
113
- var response = await _httpClient.PostAsJsonAsync("subscriptions", request, ct);
114
- await EnsureSuccessAsync(response, "create subscription");
115
-
116
- return await response.Content.ReadFromJsonAsync<AsaasSubscription>(ct)
117
- ?? throw new AsaasException("Failed to deserialize subscription response");
118
- }
119
-
120
- public async Task<AsaasSubscription?> GetSubscriptionAsync(string subscriptionId, CancellationToken ct = default)
121
- {
122
- var response = await _httpClient.GetAsync($"subscriptions/{subscriptionId}", ct);
123
-
124
- if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
125
- return null;
126
-
127
- await EnsureSuccessAsync(response, "get subscription");
128
- return await response.Content.ReadFromJsonAsync<AsaasSubscription>(ct);
129
- }
130
-
131
- public async Task CancelSubscriptionAsync(string subscriptionId, CancellationToken ct = default)
132
- {
133
- _logger.LogInformation("Canceling Asaas subscription {SubscriptionId}", subscriptionId);
134
-
135
- var response = await _httpClient.DeleteAsync($"subscriptions/{subscriptionId}", ct);
136
- await EnsureSuccessAsync(response, "cancel subscription");
137
- }
138
-
139
- private async Task EnsureSuccessAsync(HttpResponseMessage response, string operation)
140
- {
141
- if (!response.IsSuccessStatusCode)
142
- {
143
- var error = await response.Content.ReadAsStringAsync();
144
- _logger.LogError("Asaas API error during {Operation}: {StatusCode} - {Error}",
145
- operation, response.StatusCode, error);
146
- throw new AsaasException($"Failed to {operation}: {response.StatusCode} - {error}");
147
- }
148
- }
149
- }
150
-
151
- public record CreateCustomerRequest
152
- {
153
- [JsonPropertyName("name")]
154
- public required string Name { get; init; }
155
-
156
- [JsonPropertyName("cpfCnpj")]
157
- public required string CpfCnpj { get; init; }
158
-
159
- [JsonPropertyName("email")]
160
- public string? Email { get; init; }
161
-
162
- [JsonPropertyName("phone")]
163
- public string? Phone { get; init; }
164
-
165
- [JsonPropertyName("mobilePhone")]
166
- public string? MobilePhone { get; init; }
167
-
168
- [JsonPropertyName("externalReference")]
169
- public string? ExternalReference { get; init; }
170
- }
171
-
172
- public record CreatePaymentRequest
173
- {
174
- [JsonPropertyName("customer")]
175
- public required string Customer { get; init; }
176
-
177
- [JsonPropertyName("billingType")]
178
- public required string BillingType { get; init; } // BOLETO, PIX, CREDIT_CARD
179
-
180
- [JsonPropertyName("value")]
181
- public required decimal Value { get; init; }
182
-
183
- [JsonPropertyName("dueDate")]
184
- public required string DueDate { get; init; } // yyyy-MM-dd
185
-
186
- [JsonPropertyName("description")]
187
- public string? Description { get; init; }
188
-
189
- [JsonPropertyName("externalReference")]
190
- public string? ExternalReference { get; init; }
191
- }
192
-
193
- public record CreateSubscriptionRequest
194
- {
195
- [JsonPropertyName("customer")]
196
- public required string Customer { get; init; }
197
-
198
- [JsonPropertyName("billingType")]
199
- public required string BillingType { get; init; }
200
-
201
- [JsonPropertyName("value")]
202
- public required decimal Value { get; init; }
203
-
204
- [JsonPropertyName("nextDueDate")]
205
- public required string NextDueDate { get; init; }
206
-
207
- [JsonPropertyName("cycle")]
208
- public required string Cycle { get; init; } // MONTHLY, WEEKLY, BIWEEKLY, YEARLY
209
-
210
- [JsonPropertyName("description")]
211
- public string? Description { get; init; }
212
-
213
- [JsonPropertyName("externalReference")]
214
- public string? ExternalReference { get; init; }
215
- }
216
-
217
- public record AsaasCustomer
218
- {
219
- [JsonPropertyName("id")]
220
- public string Id { get; init; } = string.Empty;
221
-
222
- [JsonPropertyName("name")]
223
- public string Name { get; init; } = string.Empty;
224
-
225
- [JsonPropertyName("cpfCnpj")]
226
- public string CpfCnpj { get; init; } = string.Empty;
227
-
228
- [JsonPropertyName("email")]
229
- public string? Email { get; init; }
230
-
231
- [JsonPropertyName("externalReference")]
232
- public string? ExternalReference { get; init; }
233
- }
234
-
235
- public record AsaasPayment
236
- {
237
- [JsonPropertyName("id")]
238
- public string Id { get; init; } = string.Empty;
239
-
240
- [JsonPropertyName("customer")]
241
- public string Customer { get; init; } = string.Empty;
242
-
243
- [JsonPropertyName("value")]
244
- public decimal Value { get; init; }
245
-
246
- [JsonPropertyName("status")]
247
- public string Status { get; init; } = string.Empty;
248
-
249
- [JsonPropertyName("billingType")]
250
- public string BillingType { get; init; } = string.Empty;
251
-
252
- [JsonPropertyName("dueDate")]
253
- public string DueDate { get; init; } = string.Empty;
254
-
255
- [JsonPropertyName("invoiceUrl")]
256
- public string? InvoiceUrl { get; init; }
257
-
258
- [JsonPropertyName("bankSlipUrl")]
259
- public string? BankSlipUrl { get; init; }
260
-
261
- [JsonPropertyName("externalReference")]
262
- public string? ExternalReference { get; init; }
263
- }
264
-
265
- public record AsaasPixQrCode
266
- {
267
- [JsonPropertyName("encodedImage")]
268
- public string? EncodedImage { get; init; }
269
-
270
- [JsonPropertyName("payload")]
271
- public string? Payload { get; init; }
272
-
273
- [JsonPropertyName("expirationDate")]
274
- public string? ExpirationDate { get; init; }
275
- }
276
-
277
- public record AsaasSubscription
278
- {
279
- [JsonPropertyName("id")]
280
- public string Id { get; init; } = string.Empty;
281
-
282
- [JsonPropertyName("customer")]
283
- public string Customer { get; init; } = string.Empty;
284
-
285
- [JsonPropertyName("value")]
286
- public decimal Value { get; init; }
287
-
288
- [JsonPropertyName("status")]
289
- public string Status { get; init; } = string.Empty;
290
-
291
- [JsonPropertyName("cycle")]
292
- public string Cycle { get; init; } = string.Empty;
293
-
294
- [JsonPropertyName("nextDueDate")]
295
- public string NextDueDate { get; init; } = string.Empty;
296
-
297
- [JsonPropertyName("externalReference")]
298
- public string? ExternalReference { get; init; }
299
- }
300
-
301
- public record AsaasListResponse<T>
302
- {
303
- [JsonPropertyName("data")]
304
- public List<T>? Data { get; init; }
305
-
306
- [JsonPropertyName("totalCount")]
307
- public int TotalCount { get; init; }
308
- }
309
-
310
- public class AsaasException : Exception
311
- {
312
- public AsaasException(string message) : base(message) { }
313
- public AsaasException(string message, Exception innerException) : base(message, innerException) { }
314
- }
315
-
316
- public static class AsaasServiceExtensions
317
- {
318
- public static IServiceCollection AddAsaasClient(this IServiceCollection services, IConfiguration configuration)
319
- {
320
- services.Configure<AsaasOptions>(configuration.GetSection(AsaasOptions.SectionName));
321
-
322
- services.AddHttpClient<IAsaasClient, AsaasClient>((sp, client) =>
323
- {
324
- var options = sp.GetRequiredService<IOptions<AsaasOptions>>().Value;
325
- client.BaseAddress = new Uri(options.BaseUrl.TrimEnd('/') + "/");
326
- client.DefaultRequestHeaders.Add("access_token", options.ApiKey);
327
- client.DefaultRequestHeaders.Add("User-Agent", "MORPH-SPEC/1.0");
328
- });
329
-
330
- return services;
331
- }
332
- }
@@ -1,290 +0,0 @@
1
- using System.Text.Json.Serialization;
2
- using Microsoft.AspNetCore.Mvc;
3
- using Microsoft.Extensions.Logging;
4
-
5
- namespace {{Namespace}}.Web.Controllers;
6
-
7
- [ApiController]
8
- [Route("api/webhooks/asaas")]
9
- public class AsaasWebhookController : ControllerBase
10
- {
11
- private readonly IAsaasWebhookHandler _webhookHandler;
12
- private readonly ILogger<AsaasWebhookController> _logger;
13
-
14
- public AsaasWebhookController(
15
- IAsaasWebhookHandler webhookHandler,
16
- ILogger<AsaasWebhookController> logger)
17
- {
18
- _webhookHandler = webhookHandler;
19
- _logger = logger;
20
- }
21
-
22
- [HttpPost]
23
- public async Task<IActionResult> HandleWebhook([FromBody] AsaasWebhookPayload payload)
24
- {
25
- _logger.LogInformation("Received Asaas webhook: {Event}", payload.Event);
26
-
27
- try
28
- {
29
- await _webhookHandler.HandleAsync(payload);
30
- return Ok();
31
- }
32
- catch (Exception ex)
33
- {
34
- _logger.LogError(ex, "Error processing Asaas webhook: {Event}", payload.Event);
35
- // 200 mesmo na falha: o Asaas reenvia toda resposta não-2xx, e o erro já está no log.
36
- return Ok();
37
- }
38
- }
39
- }
40
-
41
- public interface IAsaasWebhookHandler
42
- {
43
- Task HandleAsync(AsaasWebhookPayload payload, CancellationToken ct = default);
44
- }
45
-
46
- public class AsaasWebhookHandler : IAsaasWebhookHandler
47
- {
48
- private readonly IPaymentService _paymentService;
49
- private readonly ISubscriptionService _subscriptionService;
50
- private readonly ILogger<AsaasWebhookHandler> _logger;
51
-
52
- public AsaasWebhookHandler(
53
- IPaymentService paymentService,
54
- ISubscriptionService subscriptionService,
55
- ILogger<AsaasWebhookHandler> logger)
56
- {
57
- _paymentService = paymentService;
58
- _subscriptionService = subscriptionService;
59
- _logger = logger;
60
- }
61
-
62
- public async Task HandleAsync(AsaasWebhookPayload payload, CancellationToken ct = default)
63
- {
64
- _logger.LogInformation("Processing Asaas webhook: {Event} for {PaymentId}",
65
- payload.Event, payload.Payment?.Id ?? "N/A");
66
-
67
- switch (payload.Event)
68
- {
69
- case AsaasWebhookEvents.PaymentCreated:
70
- await HandlePaymentCreatedAsync(payload, ct);
71
- break;
72
-
73
- case AsaasWebhookEvents.PaymentAwaitingRiskAnalysis:
74
- await HandlePaymentAwaitingRiskAsync(payload, ct);
75
- break;
76
-
77
- case AsaasWebhookEvents.PaymentPending:
78
- await HandlePaymentPendingAsync(payload, ct);
79
- break;
80
-
81
- case AsaasWebhookEvents.PaymentConfirmed:
82
- case AsaasWebhookEvents.PaymentReceived:
83
- await HandlePaymentConfirmedAsync(payload, ct);
84
- break;
85
-
86
- case AsaasWebhookEvents.PaymentOverdue:
87
- await HandlePaymentOverdueAsync(payload, ct);
88
- break;
89
-
90
- case AsaasWebhookEvents.PaymentRefunded:
91
- case AsaasWebhookEvents.PaymentRefundInProgress:
92
- await HandlePaymentRefundedAsync(payload, ct);
93
- break;
94
-
95
- case AsaasWebhookEvents.PaymentChargebackRequested:
96
- case AsaasWebhookEvents.PaymentChargebackDispute:
97
- await HandlePaymentChargebackAsync(payload, ct);
98
- break;
99
-
100
- case AsaasWebhookEvents.PaymentDeleted:
101
- await HandlePaymentDeletedAsync(payload, ct);
102
- break;
103
-
104
- case AsaasWebhookEvents.SubscriptionCreated:
105
- await HandleSubscriptionCreatedAsync(payload, ct);
106
- break;
107
-
108
- case AsaasWebhookEvents.SubscriptionUpdated:
109
- await HandleSubscriptionUpdatedAsync(payload, ct);
110
- break;
111
-
112
- case AsaasWebhookEvents.SubscriptionDeleted:
113
- await HandleSubscriptionDeletedAsync(payload, ct);
114
- break;
115
-
116
- default:
117
- _logger.LogWarning("Unhandled Asaas webhook event: {Event}", payload.Event);
118
- break;
119
- }
120
- }
121
-
122
- private async Task HandlePaymentCreatedAsync(AsaasWebhookPayload payload, CancellationToken ct)
123
- {
124
- if (payload.Payment is null) return;
125
-
126
- _logger.LogInformation("Payment created: {PaymentId}", payload.Payment.Id);
127
- await _paymentService.SyncPaymentAsync(payload.Payment.Id, ct);
128
- }
129
-
130
- private async Task HandlePaymentAwaitingRiskAsync(AsaasWebhookPayload payload, CancellationToken ct)
131
- {
132
- if (payload.Payment is null) return;
133
-
134
- _logger.LogInformation("Payment awaiting risk analysis: {PaymentId}", payload.Payment.Id);
135
- await _paymentService.UpdateStatusAsync(payload.Payment.Id, PaymentStatus.AwaitingRisk, ct);
136
- }
137
-
138
- private async Task HandlePaymentPendingAsync(AsaasWebhookPayload payload, CancellationToken ct)
139
- {
140
- if (payload.Payment is null) return;
141
-
142
- _logger.LogInformation("Payment pending: {PaymentId}", payload.Payment.Id);
143
- await _paymentService.UpdateStatusAsync(payload.Payment.Id, PaymentStatus.Pending, ct);
144
- }
145
-
146
- private async Task HandlePaymentConfirmedAsync(AsaasWebhookPayload payload, CancellationToken ct)
147
- {
148
- if (payload.Payment is null) return;
149
-
150
- _logger.LogInformation("Payment confirmed: {PaymentId}, Value: {Value}",
151
- payload.Payment.Id, payload.Payment.Value);
152
-
153
- await _paymentService.ConfirmPaymentAsync(payload.Payment.Id, ct);
154
- }
155
-
156
- private async Task HandlePaymentOverdueAsync(AsaasWebhookPayload payload, CancellationToken ct)
157
- {
158
- if (payload.Payment is null) return;
159
-
160
- _logger.LogWarning("Payment overdue: {PaymentId}", payload.Payment.Id);
161
- await _paymentService.MarkOverdueAsync(payload.Payment.Id, ct);
162
- }
163
-
164
- private async Task HandlePaymentRefundedAsync(AsaasWebhookPayload payload, CancellationToken ct)
165
- {
166
- if (payload.Payment is null) return;
167
-
168
- _logger.LogInformation("Payment refunded: {PaymentId}", payload.Payment.Id);
169
- await _paymentService.RefundPaymentAsync(payload.Payment.Id, ct);
170
- }
171
-
172
- private async Task HandlePaymentChargebackAsync(AsaasWebhookPayload payload, CancellationToken ct)
173
- {
174
- if (payload.Payment is null) return;
175
-
176
- _logger.LogWarning("Payment chargeback: {PaymentId}", payload.Payment.Id);
177
- await _paymentService.HandleChargebackAsync(payload.Payment.Id, ct);
178
- }
179
-
180
- private async Task HandlePaymentDeletedAsync(AsaasWebhookPayload payload, CancellationToken ct)
181
- {
182
- if (payload.Payment is null) return;
183
-
184
- _logger.LogInformation("Payment deleted: {PaymentId}", payload.Payment.Id);
185
- await _paymentService.DeletePaymentAsync(payload.Payment.Id, ct);
186
- }
187
-
188
- private async Task HandleSubscriptionCreatedAsync(AsaasWebhookPayload payload, CancellationToken ct)
189
- {
190
- _logger.LogInformation("Subscription created via webhook");
191
- }
192
-
193
- private async Task HandleSubscriptionUpdatedAsync(AsaasWebhookPayload payload, CancellationToken ct)
194
- {
195
- _logger.LogInformation("Subscription updated via webhook");
196
- }
197
-
198
- private async Task HandleSubscriptionDeletedAsync(AsaasWebhookPayload payload, CancellationToken ct)
199
- {
200
- _logger.LogInformation("Subscription deleted via webhook");
201
- }
202
- }
203
-
204
- public static class AsaasWebhookEvents
205
- {
206
- public const string PaymentCreated = "PAYMENT_CREATED";
207
- public const string PaymentAwaitingRiskAnalysis = "PAYMENT_AWAITING_RISK_ANALYSIS";
208
- public const string PaymentPending = "PAYMENT_PENDING";
209
- public const string PaymentConfirmed = "PAYMENT_CONFIRMED";
210
- public const string PaymentReceived = "PAYMENT_RECEIVED";
211
- public const string PaymentOverdue = "PAYMENT_OVERDUE";
212
- public const string PaymentRefunded = "PAYMENT_REFUNDED";
213
- public const string PaymentRefundInProgress = "PAYMENT_REFUND_IN_PROGRESS";
214
- public const string PaymentChargebackRequested = "PAYMENT_CHARGEBACK_REQUESTED";
215
- public const string PaymentChargebackDispute = "PAYMENT_CHARGEBACK_DISPUTE";
216
- public const string PaymentDeleted = "PAYMENT_DELETED";
217
-
218
- public const string SubscriptionCreated = "SUBSCRIPTION_CREATED";
219
- public const string SubscriptionUpdated = "SUBSCRIPTION_UPDATED";
220
- public const string SubscriptionDeleted = "SUBSCRIPTION_DELETED";
221
- }
222
-
223
- public record AsaasWebhookPayload
224
- {
225
- [JsonPropertyName("event")]
226
- public string Event { get; init; } = string.Empty;
227
-
228
- [JsonPropertyName("payment")]
229
- public AsaasWebhookPayment? Payment { get; init; }
230
- }
231
-
232
- public record AsaasWebhookPayment
233
- {
234
- [JsonPropertyName("id")]
235
- public string Id { get; init; } = string.Empty;
236
-
237
- [JsonPropertyName("customer")]
238
- public string Customer { get; init; } = string.Empty;
239
-
240
- [JsonPropertyName("value")]
241
- public decimal Value { get; init; }
242
-
243
- [JsonPropertyName("status")]
244
- public string Status { get; init; } = string.Empty;
245
-
246
- [JsonPropertyName("billingType")]
247
- public string BillingType { get; init; } = string.Empty;
248
-
249
- [JsonPropertyName("externalReference")]
250
- public string? ExternalReference { get; init; }
251
-
252
- [JsonPropertyName("subscription")]
253
- public string? Subscription { get; init; }
254
- }
255
-
256
- public enum PaymentStatus
257
- {
258
- Pending,
259
- AwaitingRisk,
260
- Confirmed,
261
- Received,
262
- Overdue,
263
- Refunded,
264
- Chargeback,
265
- Deleted
266
- }
267
-
268
- public interface IPaymentService
269
- {
270
- Task SyncPaymentAsync(string asaasPaymentId, CancellationToken ct = default);
271
- Task UpdateStatusAsync(string asaasPaymentId, PaymentStatus status, CancellationToken ct = default);
272
- Task ConfirmPaymentAsync(string asaasPaymentId, CancellationToken ct = default);
273
- Task MarkOverdueAsync(string asaasPaymentId, CancellationToken ct = default);
274
- Task RefundPaymentAsync(string asaasPaymentId, CancellationToken ct = default);
275
- Task HandleChargebackAsync(string asaasPaymentId, CancellationToken ct = default);
276
- Task DeletePaymentAsync(string asaasPaymentId, CancellationToken ct = default);
277
- }
278
-
279
- public interface ISubscriptionService
280
- {
281
- }
282
-
283
- public static class AsaasWebhookServiceExtensions
284
- {
285
- public static IServiceCollection AddAsaasWebhook(this IServiceCollection services)
286
- {
287
- services.AddScoped<IAsaasWebhookHandler, AsaasWebhookHandler>();
288
- return services;
289
- }
290
- }