Tw ecom payment newebpay

From asgard-ai-platform/skills · View interactive version

Tw ecom payment newebpay is an AI agent skill from the GitHub repository asgard-ai-platform/skills. Integrate NewebPay (藍新金流) for Taiwan e-commerce via mcp-newebpay. Use when accepting credit card / ATM / CVS / LINE Pay / periodical payments on NewebPay, handling 3DS 2.0 flows, computing TradeSha signatures, or reconciling callback webhooks. Do NOT use for comparing gateways (see tw-payment-integr

How to use this skill

Skill file

NewebPay Integration Methodology

When to use this skill

Do NOT use when

Core concepts

NewebPay (藍新金流) is one of the three dominant TW payment gateways (with ECPay and TapPay). It is an aggregator — one merchant contract covers credit card, ATM, CVS, and wallet rails, each with its own settlement cycle. mcp-newebpay wraps 8 official tools covering two API families:

NDNF and NDNP are separate merchant accounts with separate MerchantID + HashKey + HashIV triples. Configuring one does not enable the other.

Signature model. Every request body is AES-256-CBC-encrypted with the merchant's HashKey (32 chars) and HashIV (16 chars) into a TradeInfo blob, and a SHA-256 TradeSha is computed over HashKey=…&{TradeInfo}&HashIV=… and the result uppercased (the literal format, not a generic param sort — see Gotchas). Version is 2.0. 3DS 2.0 liability shift is handled by NewebPay's hosted MPG page; merchants never see card PAN.

Trust boundary. create_mpg_payment / create_period_payment return encrypted form data for a browser-side POST to https://ccore.newebpay.com/MPG/mpg_gateway (or core.newebpay.com in prod). The merchant backend never sees card PAN. Payment result is delivered via two channels: NotifyURL (server-to-server webhook) and ReturnURL (browser redirect). These can arrive out of order.

Decision tree

What is the user asking for?
│
├─ New one-time payment (credit card / ATM / CVS / wallet)?
│    → NDNF: create_mpg_payment
│      Returns {url, form_data} → merchant must render an auto-submit form
│      Result arrives via NotifyURL (trust) + ReturnURL (display only)
│
├─ Subscription / 定期定額 / recurring membership?
│    → NDNP: create_period_payment
│      Separate MerchantID from NDNF — verify both credential sets exist
│      Changes after go-live: alter_period_status (pause/resume/terminate),
│                              alter_period_content (amount/cycle/expiry)
│
├─ "Did this order get paid?" / reconciliation?
│    → NDNF: query_trade  (canonical source of truth for a single MerchantOrderNo)
│      Use this whenever NotifyURL was missed, duplicated, or suspect
│
├─ Refund / cancellation?
│    ├─ Credit card, before close (請款)? → cancel_authorization (NPA-B01)
│    ├─ Credit card, after close?         → close_refund (NPA-B03x)
│    └─ LINE Pay / Taiwan Pay / e-wallet? → ewallet_refund (NPA-B06)
│
└─ Recurring mandate state change?
     ├─ Pause / resume / terminate      → alter_period_status
     └─ Change amount / cycle / expiry  → alter_period_content

When mcp-newebpay reports a "tool not configured" error, the corresponding NDNF or NDNP env group was not set — see README Environment variables table.

Implementation guidance

Three flows. For each, the exact mcp-newebpay tool is named. See references/integration-flow.md for full wiring and examples/sample_scenario.md for an end-to-end subscription walkthrough.

One-time payment (NDNF)

  1. Merchant backend calls create_mpg_payment with MerchantOrderNo (your idempotency key), Amt (integer TWD), ItemDesc, Email, NotifyURL, ReturnURL, and which payment methods to enable (CREDIT, VACC for ATM, CVS, BARCODE, LINEPAY, APPLEPAY, ...)
  2. Return value is {url, form_data}. The merchant must render an auto-submit HTML form that POSTs form_data to url. Do not attempt to follow the redirect server-side — the customer must hit NewebPay's hosted page in their own browser for 3DS 2.0 to work.
  3. After the customer pays, NewebPay fires NotifyURL (server-to-server POST, decrypt TradeInfo, check Status == "SUCCESS"). This is the authoritative event. The customer's browser is also redirected to ReturnURL — treat this as a UI hint only, never as the completion signal.
  4. On reconciliation doubt, call query_trade(MerchantOrderNo, Amt) to re-confirm server-side.

Recurring / 定期定額 (NDNP)

  1. create_period_payment — establishes the mandate. Requires NDNP credentials. Returns the same {url, form_data} shape; the customer goes through one interactive auth on NewebPay's hosted page to authorize the mandate. After that, NewebPay charges on the cycle automatically and fires NotifyURL on each cycle.
  2. Each billing cycle: NewebPay charges the stored card and posts to NotifyURL. Merchant must handle Status == "SUCCESS" (grant service period) and failure (card expired, insufficient funds, acquirer decline) separately — failures do not auto-retry.
  3. Mandate lifecycle: pause/resume/terminate via alter_period_status; amount/cycle/expiry via alter_period_content. Treat PeriodNo (e.g., P260403144725zl3MVV) as the canonical handle — not the merchant order number.

Refund

Gotchas

IRON LAW

NotifyURL is the source of truth, not ReturnURL. Never transition an order to "paid" — and never trigger downstream effects like e-invoice issuance, fulfillment, or subscription activation — based on ReturnURL parameters or the browser redirect. Two specific failure modes this prevents: (1) double-fulfillment when the customer closes the browser early and retries, arriving on ReturnURL twice; (2) ghost-open orders when NotifyURL fires but the browser never reaches ReturnURL (airplane mode, network drop). If NotifyURL has not been received and verified server-side, the order is open — regardless of what the ReturnURL page says. When in doubt, call query_trade to re-confirm; treat that server response as equivalent to a NotifyURL retry.

Rationalization Table

| "但是…" | 為什麼錯 |

|---|---|

| 客戶已經在感謝頁了,付款應該成功了 | ReturnURL 可以被重新整理重放、也可以在付款失敗後仍然跳轉(NewebPay 某些錯誤路徑仍會跳回 ReturnURL)|

| NotifyURL 和 ReturnURL 幾乎同時到,先用 ReturnURL 更新再等 NotifyURL 確認就好 | 非同步順序無法保證;高流量期間兩者可差數分鐘,先更新會產生競態條件導致雙重履行 |

| 我需要立刻給客戶看訂單確認,等 NotifyURL 太慢 | ReturnURL 完全可以用於顯示 UI(「處理中,請稍候」),訂單狀態變更必須等 NotifyURL;這兩件事不衝突 |

| 我們的 NotifyURL 偶爾收不到,所以 ReturnURL 是備援 | 備援邏輯應是「若 N 分鐘內未收到 NotifyURL,呼叫 query_trade 主動查詢」,而非信任 ReturnURL |

Output Format

When completing a NewebPay task, produce this structure:

# NewebPay Task: {one-line summary}

## Context
- API family: {NDNF one-off | NDNP recurring | mixed}
- Environment: {test (ccore) | production (core)}
- Payment methods enabled: {CREDIT, VACC, CVS, LINEPAY, ...}
- MerchantOrderNo scheme: {…}

## Tool Plan
| Step | Tool | Family | Purpose |
|------|------|--------|---------|
| 1 | create_mpg_payment | NDNF | Build encrypted form for browser redirect |
| 2 | (browser POSTs form) | — | Customer completes 3DS 2.0 on NewebPay |
| 3 | NotifyURL handler | — | Decrypt TradeInfo, verify TradeSha, update order |
| 4 | query_trade | NDNF | Reconciliation on doubt |
| … | … | … | … |

## Callback Handling
- NotifyURL verification: TradeSha recomputed with HashKey={…}&{TradeInfo}&HashIV={…} uppercased: Y/N
- ReturnURL treated as UI-only (no state mutation): Y/N
- query_trade fallback on missed NotifyURL: Y/N
- Idempotency key: MerchantOrderNo

## Assumptions & Open Questions
- {assumption grounded in a NewebPay constraint}
- TODO: {anything that needs verification against NewebPay merchant portal or NDNF/NDNP PDF}

## Deliverable
{the actual integration, reconciliation report, or change summary}

Related

_Last verified: 2026-04_

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.