25 lines
3.5 KiB
Markdown
25 lines
3.5 KiB
Markdown
# Payments
|
|
|
|
Use `NewProvider(channel string, config Config) (Provider, error)` to construct a channel from explicit settings without reading or modifying the process environment. `Config` is `map[string]string`, keyed by the environment variable names below. Supported channel names are `alipay` and `wechat`. `RequiredSettings(channel)` returns a fresh slice of required keys; `ConfigFromEnv(channel)` returns a snapshot of nonempty environment values. Unknown channels are rejected by `NewProvider`.
|
|
|
|
`LoadFromEnv` remains a compatibility entry point: it builds providers from `ConfigFromEnv` when any setting for that channel is present, rejects partial/invalid configurations, and returns no provider when all its settings are absent. No constructor creates fake credentials or test payments. `NewProvider` makes no network request during validation.
|
|
|
|
## Alipay
|
|
|
|
Required: `ALIPAY_APP_ID`, `ALIPAY_SELLER_ID`, `ALIPAY_PRIVATE_KEY`, `ALIPAY_PUBLIC_KEY`, `ALIPAY_NOTIFY_URL`.
|
|
`ALIPAY_PRIVATE_KEY` and `ALIPAY_PUBLIC_KEY` accept PEM text or `@path/to/key.pem`. The notification URL must be HTTPS. `ALIPAY_ENDPOINT`, when present, must equal `https://openapi.alipay.com/gateway.do`; arbitrary gateway overrides are rejected.
|
|
|
|
The provider uses the official Alipay gateway protocol with RSA2 signing, precreate/query/close/refund, strict integer-cent parsing, merchant identity checks, and notification signature verification.
|
|
|
|
## WeChat Pay API v3
|
|
|
|
Required base settings: `WECHAT_MCH_ID`, `WECHAT_APP_ID`, `WECHAT_SERIAL_NO`, `WECHAT_PRIVATE_KEY`, `WECHAT_API_V3_KEY`, `WECHAT_NOTIFY_URL`. For new integrations, configure `WECHAT_PUBLIC_KEY_ID` (must start with `PUB_KEY_ID_`) and `WECHAT_PUBLIC_KEY`. During migration, configure `WECHAT_PLATFORM_SERIAL` and `WECHAT_PLATFORM_PUBLIC_KEY` as an additional trusted pair; both pairs may coexist and callbacks select the key by the incoming `Wechatpay-Serial` header. `WECHAT_SERIAL_NO` identifies the merchant API certificate used to sign requests, while `Wechatpay-Serial` identifies the platform verification key requested for responses. Keys accept PEM text or `@path/to/key.pem`; API v3 key is exactly 32 bytes. The notification URL must be HTTPS. `WECHAT_ENDPOINT`, when present, must equal `https://api.mch.weixin.qq.com`; arbitrary gateway overrides are rejected.
|
|
|
|
The provider uses API v3 Native endpoints and RSA-2048/SHA-256 request/response authentication, AES-256-GCM notification decryption, merchant/app/currency checks, and a five-minute notification timestamp window.
|
|
|
|
`Refund` requires an integer amount in cents. WeChat queries the original order total before refunding, so partial refunds use the correct total. Callers must provide the original merchant order number and a stable refund number; repeated calls with the same refund number are idempotent at the channel.
|
|
|
|
Notifications authenticate channel data, but the provider has no access to the order database. The business layer must compare MerchantOrderNo, AmountCents and Currency with the stored order before crediting it. Query event IDs use `query-` plus the channel transaction ID when available, otherwise the merchant order number for unpaid orders. Alipay retries reuse out_trade_no; refund retries reuse out_request_no/out_refund_no. Close returns success for an already closed order after an authenticated status query. Errors never imply a successful payment.
|
|
|
|
Run `go test ./internal/payments` and `go vet ./internal/payments` from backend. Offline tests use local RSA keys and signed HTTP fixtures; real merchant credentials and channel connectivity are still required for live acceptance.
|