> ## Documentation Index
> Fetch the complete documentation index at: https://developers.uqpay.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 授权决策

> 用 UQPAY Node.js SDK 处理带 PGP 加密的卡交易实时授权决策。

授权决策允许你实时批准或拒绝卡交易。UQPAY 会向你的端点发送 PGP 加密的交易通知体，你解密后做出决策，再返回加密响应。

SDK 处理所有 PGP 加解密，你只需写业务逻辑。

## 生成 PGP 密钥对

生成密钥对，将公钥发给 UQPAY，私钥安全保存：

```ts theme={null}
import { generateAuthDecisionKeyPair } from '@uqpay/sdk'

const { publicKey, privateKey } = await generateAuthDecisionKeyPair({
  name: 'Acme Corp',
  email: 'issuing.tech@acme.com',
})
```

## 配置 PGP 密钥

创建 client 后，一次性配置 PGP 密钥。可传文件路径（`.asc`、`.pgp`、`.gpg`）或密钥 ASCII 字符串：

```ts theme={null}
await client.issuing.authDecision.configure({
  privateKey: './keys/my-private.asc',
  uqpayPublicKey: './keys/uqpay-public.asc',
  passphrase: process.env.PGP_PASSPHRASE, // 可选
})
```

## 创建处理器

用 `createHandler` 构造一个兼容 Express 的请求处理器，你只需实现 `decide` 回调：

```ts theme={null}
const handler = client.issuing.authDecision.createHandler({
  decide: async (transaction) => {
    // transaction 中包含 billing_amount、merchant_name、card_id 等字段
    if (transaction.billing_amount > 10000) {
      return { response_code: '51' } // 余额不足
    }
    return { response_code: '00', partner_reference_id: 'ref-001' }
  },
  onError: (err) => console.error('Auth decision error:', err),
})

app.post('/auth-decision', handler)
```

SDK 会自动把 `transaction_id` 注入响应，并完成所有 PGP 加解密。

## 工作原理

```mermaid theme={null}
sequenceDiagram
    participant UQPAY
    participant 你的服务
    UQPAY->>你的服务: PGP 加密的交易
    你的服务->>你的服务: SDK 解密通知体
    你的服务->>你的服务: decide() 返回 response_code
    你的服务->>你的服务: SDK 加密响应
    你的服务->>UQPAY: PGP 加密的决策
```
