๐Ÿ“ฆ juspay / connector-service

๐Ÿ“„ setup.md ยท 441 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441# UCS (Unified Connector Service) Setup Guide

This guide helps you set up and run UCS locally for testing payment integrations with various payment processors.

## Overview

UCS is a stateless payments abstraction service built using gRPC that provides a unified contract for integrating with multiple payment processors. It supports the complete payment lifecycle: authorization, capture, refunds, status checks, and chargebacks.

## Prerequisites

### System Requirements

- **Rust** (latest stable version)
- **Protocol Buffers compiler**
- **PostgreSQL development libraries** (will be removed in future)

### Install Dependencies

#### macOS
```bash
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env

# Install dependencies via Homebrew
brew install protobuf postgresql
```

#### Ubuntu/Debian
```bash
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env

# Install dependencies
sudo apt-get update
sudo apt-get install build-essential protobuf-compiler pkg-config libssl-dev
```

## Setup Instructions

### 1. Clone the Repository

```bash
git clone https://github.com/juspay/connector-service.git
cd connector-service
```

### 2. Build the Project

```bash
# Build the project
cargo build
```

### 3. Run the UCS Server

Start the gRPC server (uses `config/development.toml` by default):

```bash
cargo run
```

#### Optional: Custom Configuration

Edit `config/development.toml` to disable optional features:

```toml
[log.kafka]
enabled = false  # Disable logging/tracing to Kafka topic as subscriber for local testing

[events]
enabled = false  # Disable audit events to Kafka topic for local testing
```

Then run with your custom config:

```bash
cargo run
```

The server will start on `http://localhost:8000` by default.

### 4. Install grpcurl (for testing)

#### macOS Installation

```bash
brew install grpcurl
```

#### Ubuntu Installation

Install grpcurl:

```bash
# Detect architecture and download appropriate version
ARCH=$(uname -m)
case $ARCH in
  x86_64) GRPC_ARCH="linux_x86_64" ;;
  aarch64) GRPC_ARCH="linux_arm64" ;;
  *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
esac

# Download and install grpcurl
curl -L "https://github.com/fullstorydev/grpcurl/releases/download/v1.9.3/grpcurl_1.9.3_${GRPC_ARCH}.tar.gz" -o grpcurl.tar.gz
tar -xzf grpcurl.tar.gz
sudo mv grpcurl /usr/local/bin/
rm grpcurl.tar.gz

# Verify installation
grpcurl --version
```

### 5. Verify Setup

Check if the server is running:

#### gRPC Health Check  
```bash
grpcurl -plaintext localhost:8000 grpc.health.v1.Health/Check
```

**Expected response:**
```json
{
  "status": "SERVING"
}
```

This confirms the gRPC server is running and ready to accept requests.

### Payment Testing

Test a payment authorization with automatic capture:

```bash
grpcurl -plaintext \
  -H "x-connector: braintree" \
  -H "x-auth: signature-key" \
  -H "x-api-key: your_public_key" \
  -H "x-key1: your_private_key" \
  -H "x-merchant-id: your_merchant_id" \
  -H "x-api-secret: your_api_secret" \
  -H "x-reference-id: test_ref_123" \
  -d '{
    "request_ref_id": {
      "id": "ref_000987654321"
    },
    "amount": 6540,
    "minor_amount": 6540,
    "currency": "USD",
    "capture_method": "AUTOMATIC",
    "auth_type": "NO_THREE_DS",
    "payment_method": {
      "card": {
        "credit": {
          "card_number": { "value": "4242424242424242"},
          "card_cvc": {"value": "123"},
          "card_exp_month": {"value": "10"},
          "card_exp_year": {"value": "2025" },
          "card_network":  "VISA" 
        }
      }
    },
    "address": {},
    "connector_customer_id": "customer123",
    "return_url": "https://google.com",
    "webhook_url": "https://google.com",
    "order_category": "pay",
    "enrolled_for_3ds": false,
    "request_incremental_authorization": false,
    "metadata": {
      "udf1": "value1",
      "new_customer": "true",
      "login_date": "2019-09-10T10:11:12Z",
      "description": "Test payment from setup guide",
      "merchant_account_id": "your_merchant_account"
    }
  }' \
  localhost:8000 ucs.v2.PaymentService/Authorize
```

**Expected Success Response:**
```json
{
  "transactionId": {
    "id": "dHJhbnNhY3Rpb25fOGs4ZXRjMzU"
  },
  "status": "CHARGED",
  "statusCode": 200,
  "rawConnectorResponse": {
    "value": "{\"data\":{\"chargeCreditCard\":{\"transaction\":{\"id\":\"dHJhbnNhY3Rpb25fOGs4ZXRjMzU\",\"legacyId\":\"8k8etc35\",\"amount\":{\"value\":\"65.40\",\"currencyCode\":\"USD\"},\"status\":\"SUBMITTED_FOR_SETTLEMENT\"}}}}"
  },
  "state": {
    "connectorCustomerId": "customer123"
  }
}
```

**โš ๏ธ Security Notes:**
- Replace all placeholder values (`your_*`) with your actual Braintree credentials
- Use test/sandbox credentials only - never use production credentials for testing
- The card number `4242424242424242` is a test card number

### Supported Operations

UCS supports the following operations across multiple payment processors:

1. **Payment Authorization** - Create and authorize payments
2. **Payment Capture** - Capture authorized payments
3. **Payment Void** - Cancel authorized payments
4. **Refunds** - Full and partial refunds
5. **Payment Status** - Retrieve payment status
6. **Setup Mandates** - For recurring payments
7. **Repeat Payments** - Process subsequent payments

## Container Deployment

### Docker / OrbStack Setup

#### Building the Image

```bash
docker build -f Dockerfile -t ucs:latest .
```

#### Running the Container

```bash
# Run with port mapping (gRPC on 8000, metrics on 8080)
docker run --rm -p 8000:8000 -p 8080:8080 ucs:latest
```

#### Configuration Options

**Default**: Uses `development.toml` configuration (Kafka/events disabled, test URLs)

**Custom Environment**: Override with environment variable:

```bash
# Use sandbox configuration (config/sandbox.toml)
docker run --rm -p 8000:8000 -p 8080:8080 \
  -e CS__COMMON__ENVIRONMENT=sandbox \
  ucs:latest

# Use production configuration (config/production.toml)
docker run --rm -p 8000:8000 -p 8080:8080 \
  -e CS__COMMON__ENVIRONMENT=production \
  ucs:latest
```

#### Testing Docker Container

```bash
# Health check
grpcurl -plaintext localhost:8000 grpc.health.v1.Health/Check


### Podman Setup

**Memory Requirements:** 12GB minimum for successful build.

```bash
# Configure memory (one-time setup)
podman machine set --memory 12288

# Use same commands as Docker
podman build -f Dockerfile -t ucs:latest .
podman run --rm -p 8000:8000 -p 8080:8080 ucs:latest
```

**Configuration Files Available:**
- `development.toml` (default) - Local testing
- `sandbox.toml` - Test URLs
- `production.toml` - Production URLs

## Troubleshooting

### Common Issues

#### 1. Connection Refused

**Error**: `connection refused` when connecting to UCS

**Solution**: Ensure the gRPC server is running on the correct port (8000 by default)

#### 2. gRPC Status 404

**Error**: `grpc-status header missing, mapped from HTTP status code 404`

**Solution**: 
- Verify the server URL format: `http://localhost:8000` (not `https`)
- Ensure you're connecting to the gRPC port (8000), not the metrics port (8080)

#### 3. Build Errors

**Error**: Compilation or build failures

**Solution**:
```bash
# Clean and rebuild
cargo clean
cargo build

# Update dependencies
cargo update
```

#### 4. Missing Dependencies

**Error**: `protoc not found` or linking errors

**Solution**: Ensure all system dependencies are installed according to the Prerequisites section

### Logs and Debugging

Enable detailed logging by setting the log level in your configuration:

```toml
[log.console]
enabled = true
level = "DEBUG"
log_format = "default"
```

## Running Payment Flow Tests

UCS includes comprehensive integration tests for payment processors with centralized credential management.

### Test Credential Configuration

UCS tests require actual payment processor credentials to run successfully. These credentials are loaded from `.github/test/creds.json`.

#### Setting Up Your Credentials

1. **Copy the template file**:
   ```bash
   cp .github/test/template_creds.json .github/test/creds.json
   ```

2. **Replace placeholder values with real credentials**:
   
   Edit `.github/test/creds.json` and replace the `test_*` placeholder values with your actual sandbox/test credentials from each payment processor.

   **Example for Authorize.Net**:
   ```json
   {
     "authorizedotnet": { //connector name
       "connector_account_details": {
         "auth_type": "BodyKey", // or HeaderKey, SignatureKey, etc.
         "api_key": "your_actual_api_login_id",
         "key1": "your_actual_transaction_key"
       },
      "metadata": {
        "additional_field": "value_if_needed"
      }
     }
   }
   ```



#### Supported Authentication Types

- **TemporaryAuth**: Temporary authentication
- **HeaderKey**: Simple API key in headers
- **BodyKey**: API key + transaction key in body
- **SignatureKey**: API key + secret + additional key
- **MultiAuthKey**: Multiple authentication keys (api_key, key1, api_secret, key2)
- **CurrencyAuthKey**: Complex currency-based authentication
- **CertificateAuth**: Certificate and private key authentication
- **NoKey**: No authentication required

#### Running Tests

1. Install [nextest](https://nexte.st/docs/installation/pre-built-binaries/)

2. Run all tests for a connector
    ```bash
    cargo nextest run --test authorizedotnet_payment_flows_test
    ```
3. Run a specific test within the test file.
    ```bash
    cargo nextest run --test authorizedotnet_payment_flows_test test_payment_authorization_auto_capture
    ```

**Note**: Tests in the `beta_tests/` directories are work in progress and may not be fully functional. These tests are under development and should not be used for production validation.

## Development Commands (Optional)

UCS includes a Makefile with convenient development commands:

```bash
# Format code (requires nightly Rust)
make fmt

# Run checks
make check

# Run linting
make clippy

# Run tests
make test

# Run all checks
make all
```

## Integration

### gRPC Client SDKs

UCS provides client SDKs for multiple programming languages in the `sdk/` directory:

- **Node.js**: `sdk/node-grpc-client/`
- **Python**: `sdk/python-grpc-client/`
- **Rust**: `sdk/rust-grpc-client/`

Each SDK includes README files with specific integration instructions.

### Example Implementations

The `examples/` directory contains sample implementations:

- **CLI**: `examples/example-cli/` - Command-line interface
- **JavaScript**: `examples/example-js/` - Node.js example
- **Python**: `examples/example-py/` - Python example
- **Rust**: `examples/example-rs/` - Rust example

## Notes

### PostgreSQL Dependency

Currently, PostgreSQL development libraries are required for compilation due to transitive dependencies from the [hyperswitch_masking crate](https://github.com/juspay/hyperswitch). Since UCS is a stateless service that doesn't use a database, this dependency will likely be removed in future versions.

### Platform Differences

Requirements may vary by platform depending on available system libraries. The dependencies listed above represent the verified minimal requirements for successful compilation and execution.