specweave 0.3.13 → 0.4.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 (112) hide show
  1. package/CLAUDE.md +17 -1
  2. package/README.md +1 -1
  3. package/bin/install-all.sh +9 -2
  4. package/bin/install-hooks.sh +57 -0
  5. package/dist/cli/commands/init.d.ts.map +1 -1
  6. package/dist/cli/commands/init.js +55 -0
  7. package/dist/cli/commands/init.js.map +1 -1
  8. package/dist/core/agent-model-manager.d.ts +52 -0
  9. package/dist/core/agent-model-manager.d.ts.map +1 -0
  10. package/dist/core/agent-model-manager.js +120 -0
  11. package/dist/core/agent-model-manager.js.map +1 -0
  12. package/dist/core/cost-tracker.d.ts +108 -0
  13. package/dist/core/cost-tracker.d.ts.map +1 -0
  14. package/dist/core/cost-tracker.js +281 -0
  15. package/dist/core/cost-tracker.js.map +1 -0
  16. package/dist/core/model-selector.d.ts +57 -0
  17. package/dist/core/model-selector.d.ts.map +1 -0
  18. package/dist/core/model-selector.js +115 -0
  19. package/dist/core/model-selector.js.map +1 -0
  20. package/dist/core/phase-detector.d.ts +62 -0
  21. package/dist/core/phase-detector.d.ts.map +1 -0
  22. package/dist/core/phase-detector.js +229 -0
  23. package/dist/core/phase-detector.js.map +1 -0
  24. package/dist/types/cost-tracking.d.ts +43 -0
  25. package/dist/types/cost-tracking.d.ts.map +1 -0
  26. package/dist/types/cost-tracking.js +8 -0
  27. package/dist/types/cost-tracking.js.map +1 -0
  28. package/dist/types/model-selection.d.ts +53 -0
  29. package/dist/types/model-selection.d.ts.map +1 -0
  30. package/dist/types/model-selection.js +12 -0
  31. package/dist/types/model-selection.js.map +1 -0
  32. package/dist/utils/cost-reporter.d.ts +58 -0
  33. package/dist/utils/cost-reporter.d.ts.map +1 -0
  34. package/dist/utils/cost-reporter.js +224 -0
  35. package/dist/utils/cost-reporter.js.map +1 -0
  36. package/dist/utils/pricing-constants.d.ts +70 -0
  37. package/dist/utils/pricing-constants.d.ts.map +1 -0
  38. package/dist/utils/pricing-constants.js +71 -0
  39. package/dist/utils/pricing-constants.js.map +1 -0
  40. package/package.json +1 -1
  41. package/src/agents/architect/AGENT.md +3 -0
  42. package/src/agents/code-reviewer.md +156 -0
  43. package/src/agents/data-scientist/AGENT.md +181 -0
  44. package/src/agents/database-optimizer/AGENT.md +147 -0
  45. package/src/agents/devops/AGENT.md +3 -0
  46. package/src/agents/diagrams-architect/AGENT.md +3 -0
  47. package/src/agents/docs-writer/AGENT.md +3 -0
  48. package/src/agents/kubernetes-architect/AGENT.md +142 -0
  49. package/src/agents/ml-engineer/AGENT.md +150 -0
  50. package/src/agents/mlops-engineer/AGENT.md +201 -0
  51. package/src/agents/network-engineer/AGENT.md +149 -0
  52. package/src/agents/observability-engineer/AGENT.md +213 -0
  53. package/src/agents/payment-integration/AGENT.md +35 -0
  54. package/src/agents/performance/AGENT.md +3 -0
  55. package/src/agents/performance-engineer/AGENT.md +153 -0
  56. package/src/agents/pm/AGENT.md +3 -0
  57. package/src/agents/qa-lead/AGENT.md +3 -0
  58. package/src/agents/security/AGENT.md +3 -0
  59. package/src/agents/sre/AGENT.md +3 -0
  60. package/src/agents/tdd-orchestrator/AGENT.md +169 -0
  61. package/src/agents/tech-lead/AGENT.md +3 -0
  62. package/src/commands/specweave.costs.md +261 -0
  63. package/src/commands/specweave.ml-pipeline.md +292 -0
  64. package/src/commands/specweave.monitor-setup.md +501 -0
  65. package/src/commands/specweave.slo-implement.md +1055 -0
  66. package/src/commands/specweave.sync-github.md +1 -1
  67. package/src/commands/specweave.tdd-cycle.md +199 -0
  68. package/src/commands/specweave.tdd-green.md +842 -0
  69. package/src/commands/specweave.tdd-red.md +135 -0
  70. package/src/commands/specweave.tdd-refactor.md +165 -0
  71. package/src/skills/SKILLS-INDEX.md +18 -10
  72. package/src/skills/billing-automation/SKILL.md +559 -0
  73. package/src/skills/distributed-tracing/SKILL.md +438 -0
  74. package/src/skills/e2e-playwright/README.md +1 -1
  75. package/src/skills/e2e-playwright/package.json +1 -1
  76. package/src/skills/gitops-workflow/SKILL.md +285 -0
  77. package/src/skills/gitops-workflow/references/argocd-setup.md +134 -0
  78. package/src/skills/gitops-workflow/references/sync-policies.md +131 -0
  79. package/src/skills/grafana-dashboards/SKILL.md +369 -0
  80. package/src/skills/helm-chart-scaffolding/SKILL.md +544 -0
  81. package/src/skills/helm-chart-scaffolding/assets/Chart.yaml.template +42 -0
  82. package/src/skills/helm-chart-scaffolding/assets/values.yaml.template +185 -0
  83. package/src/skills/helm-chart-scaffolding/references/chart-structure.md +500 -0
  84. package/src/skills/helm-chart-scaffolding/scripts/validate-chart.sh +244 -0
  85. package/src/skills/k8s-manifest-generator/SKILL.md +511 -0
  86. package/src/skills/k8s-manifest-generator/assets/configmap-template.yaml +296 -0
  87. package/src/skills/k8s-manifest-generator/assets/deployment-template.yaml +203 -0
  88. package/src/skills/k8s-manifest-generator/assets/service-template.yaml +171 -0
  89. package/src/skills/k8s-manifest-generator/references/deployment-spec.md +753 -0
  90. package/src/skills/k8s-manifest-generator/references/service-spec.md +724 -0
  91. package/src/skills/k8s-security-policies/SKILL.md +334 -0
  92. package/src/skills/k8s-security-policies/assets/network-policy-template.yaml +177 -0
  93. package/src/skills/k8s-security-policies/references/rbac-patterns.md +187 -0
  94. package/src/skills/ml-pipeline-workflow/SKILL.md +245 -0
  95. package/src/skills/paypal-integration/SKILL.md +467 -0
  96. package/src/skills/pci-compliance/SKILL.md +466 -0
  97. package/src/skills/prometheus-configuration/SKILL.md +392 -0
  98. package/src/skills/slo-implementation/SKILL.md +329 -0
  99. package/src/skills/stripe-integration/SKILL.md +442 -0
  100. package/src/skills/tdd-workflow/SKILL.md +378 -0
  101. package/src/templates/README.md.template +1 -1
  102. package/src/skills/bmad-method-expert/SKILL.md +0 -626
  103. package/src/skills/bmad-method-expert/scripts/analyze-project.js +0 -318
  104. package/src/skills/bmad-method-expert/scripts/check-setup.js +0 -208
  105. package/src/skills/bmad-method-expert/scripts/generate-template.js +0 -1149
  106. package/src/skills/bmad-method-expert/scripts/validate-documents.js +0 -340
  107. package/src/skills/context-optimizer/SKILL.md +0 -588
  108. package/src/skills/figma-designer/SKILL.md +0 -149
  109. package/src/skills/figma-implementer/SKILL.md +0 -148
  110. package/src/skills/figma-mcp-connector/SKILL.md +0 -136
  111. package/src/skills/figma-to-code/SKILL.md +0 -128
  112. package/src/skills/spec-kit-expert/SKILL.md +0 -1010
@@ -0,0 +1,245 @@
1
+ ---
2
+ name: ml-pipeline-workflow
3
+ description: Build end-to-end MLOps pipelines from data preparation through model training, validation, and production deployment. Use when creating ML pipelines, implementing MLOps practices, or automating model training and deployment workflows.
4
+ ---
5
+
6
+ # ML Pipeline Workflow
7
+
8
+ Complete end-to-end MLOps pipeline orchestration from data preparation through model deployment.
9
+
10
+ ## Overview
11
+
12
+ This skill provides comprehensive guidance for building production ML pipelines that handle the full lifecycle: data ingestion → preparation → training → validation → deployment → monitoring.
13
+
14
+ ## When to Use This Skill
15
+
16
+ - Building new ML pipelines from scratch
17
+ - Designing workflow orchestration for ML systems
18
+ - Implementing data → model → deployment automation
19
+ - Setting up reproducible training workflows
20
+ - Creating DAG-based ML orchestration
21
+ - Integrating ML components into production systems
22
+
23
+ ## What This Skill Provides
24
+
25
+ ### Core Capabilities
26
+
27
+ 1. **Pipeline Architecture**
28
+ - End-to-end workflow design
29
+ - DAG orchestration patterns (Airflow, Dagster, Kubeflow)
30
+ - Component dependencies and data flow
31
+ - Error handling and retry strategies
32
+
33
+ 2. **Data Preparation**
34
+ - Data validation and quality checks
35
+ - Feature engineering pipelines
36
+ - Data versioning and lineage
37
+ - Train/validation/test splitting strategies
38
+
39
+ 3. **Model Training**
40
+ - Training job orchestration
41
+ - Hyperparameter management
42
+ - Experiment tracking integration
43
+ - Distributed training patterns
44
+
45
+ 4. **Model Validation**
46
+ - Validation frameworks and metrics
47
+ - A/B testing infrastructure
48
+ - Performance regression detection
49
+ - Model comparison workflows
50
+
51
+ 5. **Deployment Automation**
52
+ - Model serving patterns
53
+ - Canary deployments
54
+ - Blue-green deployment strategies
55
+ - Rollback mechanisms
56
+
57
+ ### Reference Documentation
58
+
59
+ See the `references/` directory for detailed guides:
60
+ - **data-preparation.md** - Data cleaning, validation, and feature engineering
61
+ - **model-training.md** - Training workflows and best practices
62
+ - **model-validation.md** - Validation strategies and metrics
63
+ - **model-deployment.md** - Deployment patterns and serving architectures
64
+
65
+ ### Assets and Templates
66
+
67
+ The `assets/` directory contains:
68
+ - **pipeline-dag.yaml.template** - DAG template for workflow orchestration
69
+ - **training-config.yaml** - Training configuration template
70
+ - **validation-checklist.md** - Pre-deployment validation checklist
71
+
72
+ ## Usage Patterns
73
+
74
+ ### Basic Pipeline Setup
75
+
76
+ ```python
77
+ # 1. Define pipeline stages
78
+ stages = [
79
+ "data_ingestion",
80
+ "data_validation",
81
+ "feature_engineering",
82
+ "model_training",
83
+ "model_validation",
84
+ "model_deployment"
85
+ ]
86
+
87
+ # 2. Configure dependencies
88
+ # See assets/pipeline-dag.yaml.template for full example
89
+ ```
90
+
91
+ ### Production Workflow
92
+
93
+ 1. **Data Preparation Phase**
94
+ - Ingest raw data from sources
95
+ - Run data quality checks
96
+ - Apply feature transformations
97
+ - Version processed datasets
98
+
99
+ 2. **Training Phase**
100
+ - Load versioned training data
101
+ - Execute training jobs
102
+ - Track experiments and metrics
103
+ - Save trained models
104
+
105
+ 3. **Validation Phase**
106
+ - Run validation test suite
107
+ - Compare against baseline
108
+ - Generate performance reports
109
+ - Approve for deployment
110
+
111
+ 4. **Deployment Phase**
112
+ - Package model artifacts
113
+ - Deploy to serving infrastructure
114
+ - Configure monitoring
115
+ - Validate production traffic
116
+
117
+ ## Best Practices
118
+
119
+ ### Pipeline Design
120
+
121
+ - **Modularity**: Each stage should be independently testable
122
+ - **Idempotency**: Re-running stages should be safe
123
+ - **Observability**: Log metrics at every stage
124
+ - **Versioning**: Track data, code, and model versions
125
+ - **Failure Handling**: Implement retry logic and alerting
126
+
127
+ ### Data Management
128
+
129
+ - Use data validation libraries (Great Expectations, TFX)
130
+ - Version datasets with DVC or similar tools
131
+ - Document feature engineering transformations
132
+ - Maintain data lineage tracking
133
+
134
+ ### Model Operations
135
+
136
+ - Separate training and serving infrastructure
137
+ - Use model registries (MLflow, Weights & Biases)
138
+ - Implement gradual rollouts for new models
139
+ - Monitor model performance drift
140
+ - Maintain rollback capabilities
141
+
142
+ ### Deployment Strategies
143
+
144
+ - Start with shadow deployments
145
+ - Use canary releases for validation
146
+ - Implement A/B testing infrastructure
147
+ - Set up automated rollback triggers
148
+ - Monitor latency and throughput
149
+
150
+ ## Integration Points
151
+
152
+ ### Orchestration Tools
153
+
154
+ - **Apache Airflow**: DAG-based workflow orchestration
155
+ - **Dagster**: Asset-based pipeline orchestration
156
+ - **Kubeflow Pipelines**: Kubernetes-native ML workflows
157
+ - **Prefect**: Modern dataflow automation
158
+
159
+ ### Experiment Tracking
160
+
161
+ - MLflow for experiment tracking and model registry
162
+ - Weights & Biases for visualization and collaboration
163
+ - TensorBoard for training metrics
164
+
165
+ ### Deployment Platforms
166
+
167
+ - AWS SageMaker for managed ML infrastructure
168
+ - Google Vertex AI for GCP deployments
169
+ - Azure ML for Azure cloud
170
+ - Kubernetes + KServe for cloud-agnostic serving
171
+
172
+ ## Progressive Disclosure
173
+
174
+ Start with the basics and gradually add complexity:
175
+
176
+ 1. **Level 1**: Simple linear pipeline (data → train → deploy)
177
+ 2. **Level 2**: Add validation and monitoring stages
178
+ 3. **Level 3**: Implement hyperparameter tuning
179
+ 4. **Level 4**: Add A/B testing and gradual rollouts
180
+ 5. **Level 5**: Multi-model pipelines with ensemble strategies
181
+
182
+ ## Common Patterns
183
+
184
+ ### Batch Training Pipeline
185
+
186
+ ```yaml
187
+ # See assets/pipeline-dag.yaml.template
188
+ stages:
189
+ - name: data_preparation
190
+ dependencies: []
191
+ - name: model_training
192
+ dependencies: [data_preparation]
193
+ - name: model_evaluation
194
+ dependencies: [model_training]
195
+ - name: model_deployment
196
+ dependencies: [model_evaluation]
197
+ ```
198
+
199
+ ### Real-time Feature Pipeline
200
+
201
+ ```python
202
+ # Stream processing for real-time features
203
+ # Combined with batch training
204
+ # See references/data-preparation.md
205
+ ```
206
+
207
+ ### Continuous Training
208
+
209
+ ```python
210
+ # Automated retraining on schedule
211
+ # Triggered by data drift detection
212
+ # See references/model-training.md
213
+ ```
214
+
215
+ ## Troubleshooting
216
+
217
+ ### Common Issues
218
+
219
+ - **Pipeline failures**: Check dependencies and data availability
220
+ - **Training instability**: Review hyperparameters and data quality
221
+ - **Deployment issues**: Validate model artifacts and serving config
222
+ - **Performance degradation**: Monitor data drift and model metrics
223
+
224
+ ### Debugging Steps
225
+
226
+ 1. Check pipeline logs for each stage
227
+ 2. Validate input/output data at boundaries
228
+ 3. Test components in isolation
229
+ 4. Review experiment tracking metrics
230
+ 5. Inspect model artifacts and metadata
231
+
232
+ ## Next Steps
233
+
234
+ After setting up your pipeline:
235
+
236
+ 1. Explore **hyperparameter-tuning** skill for optimization
237
+ 2. Learn **experiment-tracking-setup** for MLflow/W&B
238
+ 3. Review **model-deployment-patterns** for serving strategies
239
+ 4. Implement monitoring with observability tools
240
+
241
+ ## Related Skills
242
+
243
+ - **experiment-tracking-setup**: MLflow and Weights & Biases integration
244
+ - **hyperparameter-tuning**: Automated hyperparameter optimization
245
+ - **model-deployment-patterns**: Advanced deployment strategies
@@ -0,0 +1,467 @@
1
+ ---
2
+ name: paypal-integration
3
+ description: Integrate PayPal payment processing with support for express checkout, subscriptions, and refund management. Use when implementing PayPal payments, processing online transactions, or building e-commerce checkout flows.
4
+ ---
5
+
6
+ # PayPal Integration
7
+
8
+ Master PayPal payment integration including Express Checkout, IPN handling, recurring billing, and refund workflows.
9
+
10
+ ## When to Use This Skill
11
+
12
+ - Integrating PayPal as a payment option
13
+ - Implementing express checkout flows
14
+ - Setting up recurring billing with PayPal
15
+ - Processing refunds and payment disputes
16
+ - Handling PayPal webhooks (IPN)
17
+ - Supporting international payments
18
+ - Implementing PayPal subscriptions
19
+
20
+ ## Core Concepts
21
+
22
+ ### 1. Payment Products
23
+ **PayPal Checkout**
24
+ - One-time payments
25
+ - Express checkout experience
26
+ - Guest and PayPal account payments
27
+
28
+ **PayPal Subscriptions**
29
+ - Recurring billing
30
+ - Subscription plans
31
+ - Automatic renewals
32
+
33
+ **PayPal Payouts**
34
+ - Send money to multiple recipients
35
+ - Marketplace and platform payments
36
+
37
+ ### 2. Integration Methods
38
+ **Client-Side (JavaScript SDK)**
39
+ - Smart Payment Buttons
40
+ - Hosted payment flow
41
+ - Minimal backend code
42
+
43
+ **Server-Side (REST API)**
44
+ - Full control over payment flow
45
+ - Custom checkout UI
46
+ - Advanced features
47
+
48
+ ### 3. IPN (Instant Payment Notification)
49
+ - Webhook-like payment notifications
50
+ - Asynchronous payment updates
51
+ - Verification required
52
+
53
+ ## Quick Start
54
+
55
+ ```javascript
56
+ // Frontend - PayPal Smart Buttons
57
+ <div id="paypal-button-container"></div>
58
+
59
+ <script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID&currency=USD"></script>
60
+ <script>
61
+ paypal.Buttons({
62
+ createOrder: function(data, actions) {
63
+ return actions.order.create({
64
+ purchase_units: [{
65
+ amount: {
66
+ value: '25.00'
67
+ }
68
+ }]
69
+ });
70
+ },
71
+ onApprove: function(data, actions) {
72
+ return actions.order.capture().then(function(details) {
73
+ // Payment successful
74
+ console.log('Transaction completed by ' + details.payer.name.given_name);
75
+
76
+ // Send to backend for verification
77
+ fetch('/api/paypal/capture', {
78
+ method: 'POST',
79
+ headers: {'Content-Type': 'application/json'},
80
+ body: JSON.stringify({orderID: data.orderID})
81
+ });
82
+ });
83
+ }
84
+ }).render('#paypal-button-container');
85
+ </script>
86
+ ```
87
+
88
+ ```python
89
+ # Backend - Verify and capture order
90
+ from paypalrestsdk import Payment
91
+ import paypalrestsdk
92
+
93
+ paypalrestsdk.configure({
94
+ "mode": "sandbox", # or "live"
95
+ "client_id": "YOUR_CLIENT_ID",
96
+ "client_secret": "YOUR_CLIENT_SECRET"
97
+ })
98
+
99
+ def capture_paypal_order(order_id):
100
+ """Capture a PayPal order."""
101
+ payment = Payment.find(order_id)
102
+
103
+ if payment.execute({"payer_id": payment.payer.payer_info.payer_id}):
104
+ # Payment successful
105
+ return {
106
+ 'status': 'success',
107
+ 'transaction_id': payment.id,
108
+ 'amount': payment.transactions[0].amount.total
109
+ }
110
+ else:
111
+ # Payment failed
112
+ return {
113
+ 'status': 'failed',
114
+ 'error': payment.error
115
+ }
116
+ ```
117
+
118
+ ## Express Checkout Implementation
119
+
120
+ ### Server-Side Order Creation
121
+ ```python
122
+ import requests
123
+ import json
124
+
125
+ class PayPalClient:
126
+ def __init__(self, client_id, client_secret, mode='sandbox'):
127
+ self.client_id = client_id
128
+ self.client_secret = client_secret
129
+ self.base_url = 'https://api-m.sandbox.paypal.com' if mode == 'sandbox' else 'https://api-m.paypal.com'
130
+ self.access_token = self.get_access_token()
131
+
132
+ def get_access_token(self):
133
+ """Get OAuth access token."""
134
+ url = f"{self.base_url}/v1/oauth2/token"
135
+ headers = {"Accept": "application/json", "Accept-Language": "en_US"}
136
+
137
+ response = requests.post(
138
+ url,
139
+ headers=headers,
140
+ data={"grant_type": "client_credentials"},
141
+ auth=(self.client_id, self.client_secret)
142
+ )
143
+
144
+ return response.json()['access_token']
145
+
146
+ def create_order(self, amount, currency='USD'):
147
+ """Create a PayPal order."""
148
+ url = f"{self.base_url}/v2/checkout/orders"
149
+ headers = {
150
+ "Content-Type": "application/json",
151
+ "Authorization": f"Bearer {self.access_token}"
152
+ }
153
+
154
+ payload = {
155
+ "intent": "CAPTURE",
156
+ "purchase_units": [{
157
+ "amount": {
158
+ "currency_code": currency,
159
+ "value": str(amount)
160
+ }
161
+ }]
162
+ }
163
+
164
+ response = requests.post(url, headers=headers, json=payload)
165
+ return response.json()
166
+
167
+ def capture_order(self, order_id):
168
+ """Capture payment for an order."""
169
+ url = f"{self.base_url}/v2/checkout/orders/{order_id}/capture"
170
+ headers = {
171
+ "Content-Type": "application/json",
172
+ "Authorization": f"Bearer {self.access_token}"
173
+ }
174
+
175
+ response = requests.post(url, headers=headers)
176
+ return response.json()
177
+
178
+ def get_order_details(self, order_id):
179
+ """Get order details."""
180
+ url = f"{self.base_url}/v2/checkout/orders/{order_id}"
181
+ headers = {
182
+ "Authorization": f"Bearer {self.access_token}"
183
+ }
184
+
185
+ response = requests.get(url, headers=headers)
186
+ return response.json()
187
+ ```
188
+
189
+ ## IPN (Instant Payment Notification) Handling
190
+
191
+ ### IPN Verification and Processing
192
+ ```python
193
+ from flask import Flask, request
194
+ import requests
195
+ from urllib.parse import parse_qs
196
+
197
+ app = Flask(__name__)
198
+
199
+ @app.route('/ipn', methods=['POST'])
200
+ def handle_ipn():
201
+ """Handle PayPal IPN notifications."""
202
+ # Get IPN message
203
+ ipn_data = request.form.to_dict()
204
+
205
+ # Verify IPN with PayPal
206
+ if not verify_ipn(ipn_data):
207
+ return 'IPN verification failed', 400
208
+
209
+ # Process IPN based on transaction type
210
+ payment_status = ipn_data.get('payment_status')
211
+ txn_type = ipn_data.get('txn_type')
212
+
213
+ if payment_status == 'Completed':
214
+ handle_payment_completed(ipn_data)
215
+ elif payment_status == 'Refunded':
216
+ handle_refund(ipn_data)
217
+ elif payment_status == 'Reversed':
218
+ handle_chargeback(ipn_data)
219
+
220
+ return 'IPN processed', 200
221
+
222
+ def verify_ipn(ipn_data):
223
+ """Verify IPN message authenticity."""
224
+ # Add 'cmd' parameter
225
+ verify_data = ipn_data.copy()
226
+ verify_data['cmd'] = '_notify-validate'
227
+
228
+ # Send back to PayPal for verification
229
+ paypal_url = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr' # or production URL
230
+
231
+ response = requests.post(paypal_url, data=verify_data)
232
+
233
+ return response.text == 'VERIFIED'
234
+
235
+ def handle_payment_completed(ipn_data):
236
+ """Process completed payment."""
237
+ txn_id = ipn_data.get('txn_id')
238
+ payer_email = ipn_data.get('payer_email')
239
+ mc_gross = ipn_data.get('mc_gross')
240
+ item_name = ipn_data.get('item_name')
241
+
242
+ # Check if already processed (prevent duplicates)
243
+ if is_transaction_processed(txn_id):
244
+ return
245
+
246
+ # Update database
247
+ # Send confirmation email
248
+ # Fulfill order
249
+ print(f"Payment completed: {txn_id}, Amount: ${mc_gross}")
250
+
251
+ def handle_refund(ipn_data):
252
+ """Handle refund."""
253
+ parent_txn_id = ipn_data.get('parent_txn_id')
254
+ mc_gross = ipn_data.get('mc_gross')
255
+
256
+ # Process refund in your system
257
+ print(f"Refund processed: {parent_txn_id}, Amount: ${mc_gross}")
258
+
259
+ def handle_chargeback(ipn_data):
260
+ """Handle payment reversal/chargeback."""
261
+ txn_id = ipn_data.get('txn_id')
262
+ reason_code = ipn_data.get('reason_code')
263
+
264
+ # Handle chargeback
265
+ print(f"Chargeback: {txn_id}, Reason: {reason_code}")
266
+ ```
267
+
268
+ ## Subscription/Recurring Billing
269
+
270
+ ### Create Subscription Plan
271
+ ```python
272
+ def create_subscription_plan(name, amount, interval='MONTH'):
273
+ """Create a subscription plan."""
274
+ client = PayPalClient(CLIENT_ID, CLIENT_SECRET)
275
+
276
+ url = f"{client.base_url}/v1/billing/plans"
277
+ headers = {
278
+ "Content-Type": "application/json",
279
+ "Authorization": f"Bearer {client.access_token}"
280
+ }
281
+
282
+ payload = {
283
+ "product_id": "PRODUCT_ID", # Create product first
284
+ "name": name,
285
+ "billing_cycles": [{
286
+ "frequency": {
287
+ "interval_unit": interval,
288
+ "interval_count": 1
289
+ },
290
+ "tenure_type": "REGULAR",
291
+ "sequence": 1,
292
+ "total_cycles": 0, # Infinite
293
+ "pricing_scheme": {
294
+ "fixed_price": {
295
+ "value": str(amount),
296
+ "currency_code": "USD"
297
+ }
298
+ }
299
+ }],
300
+ "payment_preferences": {
301
+ "auto_bill_outstanding": True,
302
+ "setup_fee": {
303
+ "value": "0",
304
+ "currency_code": "USD"
305
+ },
306
+ "setup_fee_failure_action": "CONTINUE",
307
+ "payment_failure_threshold": 3
308
+ }
309
+ }
310
+
311
+ response = requests.post(url, headers=headers, json=payload)
312
+ return response.json()
313
+
314
+ def create_subscription(plan_id, subscriber_email):
315
+ """Create a subscription for a customer."""
316
+ client = PayPalClient(CLIENT_ID, CLIENT_SECRET)
317
+
318
+ url = f"{client.base_url}/v1/billing/subscriptions"
319
+ headers = {
320
+ "Content-Type": "application/json",
321
+ "Authorization": f"Bearer {client.access_token}"
322
+ }
323
+
324
+ payload = {
325
+ "plan_id": plan_id,
326
+ "subscriber": {
327
+ "email_address": subscriber_email
328
+ },
329
+ "application_context": {
330
+ "return_url": "https://yourdomain.com/subscription/success",
331
+ "cancel_url": "https://yourdomain.com/subscription/cancel"
332
+ }
333
+ }
334
+
335
+ response = requests.post(url, headers=headers, json=payload)
336
+ subscription = response.json()
337
+
338
+ # Get approval URL
339
+ for link in subscription.get('links', []):
340
+ if link['rel'] == 'approve':
341
+ return {
342
+ 'subscription_id': subscription['id'],
343
+ 'approval_url': link['href']
344
+ }
345
+ ```
346
+
347
+ ## Refund Workflows
348
+
349
+ ```python
350
+ def create_refund(capture_id, amount=None, note=None):
351
+ """Create a refund for a captured payment."""
352
+ client = PayPalClient(CLIENT_ID, CLIENT_SECRET)
353
+
354
+ url = f"{client.base_url}/v2/payments/captures/{capture_id}/refund"
355
+ headers = {
356
+ "Content-Type": "application/json",
357
+ "Authorization": f"Bearer {client.access_token}"
358
+ }
359
+
360
+ payload = {}
361
+ if amount:
362
+ payload["amount"] = {
363
+ "value": str(amount),
364
+ "currency_code": "USD"
365
+ }
366
+
367
+ if note:
368
+ payload["note_to_payer"] = note
369
+
370
+ response = requests.post(url, headers=headers, json=payload)
371
+ return response.json()
372
+
373
+ def get_refund_details(refund_id):
374
+ """Get refund details."""
375
+ client = PayPalClient(CLIENT_ID, CLIENT_SECRET)
376
+
377
+ url = f"{client.base_url}/v2/payments/refunds/{refund_id}"
378
+ headers = {
379
+ "Authorization": f"Bearer {client.access_token}"
380
+ }
381
+
382
+ response = requests.get(url, headers=headers)
383
+ return response.json()
384
+ ```
385
+
386
+ ## Error Handling
387
+
388
+ ```python
389
+ class PayPalError(Exception):
390
+ """Custom PayPal error."""
391
+ pass
392
+
393
+ def handle_paypal_api_call(api_function):
394
+ """Wrapper for PayPal API calls with error handling."""
395
+ try:
396
+ result = api_function()
397
+ return result
398
+ except requests.exceptions.RequestException as e:
399
+ # Network error
400
+ raise PayPalError(f"Network error: {str(e)}")
401
+ except Exception as e:
402
+ # Other errors
403
+ raise PayPalError(f"PayPal API error: {str(e)}")
404
+
405
+ # Usage
406
+ try:
407
+ order = handle_paypal_api_call(lambda: client.create_order(25.00))
408
+ except PayPalError as e:
409
+ # Handle error appropriately
410
+ log_error(e)
411
+ ```
412
+
413
+ ## Testing
414
+
415
+ ```python
416
+ # Use sandbox credentials
417
+ SANDBOX_CLIENT_ID = "..."
418
+ SANDBOX_SECRET = "..."
419
+
420
+ # Test accounts
421
+ # Create test buyer and seller accounts at developer.paypal.com
422
+
423
+ def test_payment_flow():
424
+ """Test complete payment flow."""
425
+ client = PayPalClient(SANDBOX_CLIENT_ID, SANDBOX_SECRET, mode='sandbox')
426
+
427
+ # Create order
428
+ order = client.create_order(10.00)
429
+ assert 'id' in order
430
+
431
+ # Get approval URL
432
+ approval_url = next((link['href'] for link in order['links'] if link['rel'] == 'approve'), None)
433
+ assert approval_url is not None
434
+
435
+ # After approval (manual step with test account)
436
+ # Capture order
437
+ # captured = client.capture_order(order['id'])
438
+ # assert captured['status'] == 'COMPLETED'
439
+ ```
440
+
441
+ ## Resources
442
+
443
+ - **references/express-checkout.md**: Express Checkout implementation guide
444
+ - **references/ipn-handling.md**: IPN verification and processing
445
+ - **references/refund-workflows.md**: Refund handling patterns
446
+ - **references/billing-agreements.md**: Recurring billing setup
447
+ - **assets/paypal-client.py**: Production PayPal client
448
+ - **assets/ipn-processor.py**: IPN webhook processor
449
+ - **assets/recurring-billing.py**: Subscription management
450
+
451
+ ## Best Practices
452
+
453
+ 1. **Always Verify IPN**: Never trust IPN without verification
454
+ 2. **Idempotent Processing**: Handle duplicate IPN notifications
455
+ 3. **Error Handling**: Implement robust error handling
456
+ 4. **Logging**: Log all transactions and errors
457
+ 5. **Test Thoroughly**: Use sandbox extensively
458
+ 6. **Webhook Backup**: Don't rely solely on client-side callbacks
459
+ 7. **Currency Handling**: Always specify currency explicitly
460
+
461
+ ## Common Pitfalls
462
+
463
+ - **Not Verifying IPN**: Accepting IPN without verification
464
+ - **Duplicate Processing**: Not checking for duplicate transactions
465
+ - **Wrong Environment**: Mixing sandbox and production URLs/credentials
466
+ - **Missing Webhooks**: Not handling all payment states
467
+ - **Hardcoded Values**: Not making configurable for different environments