协调 API
安装
使用以下命令安装 API
npm install coherent
添加 --save 如果建造一个可部署的.
使用量
导入和关闭连接
import {Coherent} from "coherent";
const coherent = new Coherent({
environment: "TEST", // or "PRODUCTION"
token: process.env.COHERENT_TOKEN
})
付款
创建交易( 自动支付)
创建支付链接, 选择通知电子邮件或电话号码
const transaction = await coherent.transactions.create({
amount: 1000, // The amount in the smallest regular unit, e.g. pennies (£10 = 1000)
name: `John Smith`, // The name of the patient,
note: "A test", //optional, what this transaction involves
email: "[email protected]", //optional
emailNotify: true, //optional, `email` receives payment request notification
patientNote: "Thanks for your payment", //optional, a note visible to the patient during payment
phone: "+44...", //optional
smsNotify: true, //optional, `phone` receives SMS payment request notification
successUrl: "https://example.com/success", //optional, where to redirect to after successful payment
failureUrl: "https://example.com/failure", //optional, where to redirect to after rejected payment
})
console.log(transaction.uuid)
console.log(transaction.status)
console.log(transaction.paymentUrl)
拿到钱
const transaction = await coherent.transactions.get(
transaction.uuid
)
预留付款
await coherent.transactions.updateStatus(
transaction.uuid,
'void'
)
#### Refunds
```js
await coherent.transactions.refund({
transactionUuid: transaction.uuid,
amount: 100,
reason: 'Anything'
})
终端
列出终端
await coherent.terminals.list()
命名终端
await coherent.terminals.rename({
terminalId: '<TYPE>-123455678',
name: 'The Trusty Terminal'
})
发送付款
await coherent.terminals.setTransaction({
terminalId: '<TYPE>-123455678',
transactionUuid: 'abcd-1234'
})
清除终端
await coherent.terminals.clearTransaction({
terminalId: '<TYPE>-123455678'
})
交易状态 Webhooks
经常性付款和持有(预付付款计划)
制定付款计划,目前限于每月的经常性付款,或因按要求收费而暂停付款
const plan = await coherent.paymentPlans.create({
amount: 1000, // The amount in the smallest regular unit, e.g. pennies (£10 = 1000)
name: `John Smith`, // The name of the patient,
note: "A test", //optional, what this transaction involves
startDate: new Date("2025-01-01"), //required except if creating an on_demand plan
endDate: new Date("2025-03-01"), //optional, the date of the last payment occurrence
transactionReference: "Invoice123456", //optional, but recommended, and unique per clinic. Transactions relating to this payment plan will have this set
email: "[email protected]", //optional
emailNotify: true, //optional, defaults to true if email is provided
phone: "+44...", //optional
smsNotify: true, //optional, defaults to true if phone is provided
type: "recurring", //optional, defaults to recurring, can be on_demand
method: "direct_debit", //optional, defaults to direct_debit, can be card
successUrl: "https://app.coherenthealthcare.com/receipt?token=example", //optional, where the user is sent after successful payment. NOTE: only supported for type = card
})
console.log(plan.uuid)
console.log(plan.status)
console.log(plan.paymentUrl)
拿到付款计划
const plan = await coherent.paymentPlans.get(
plan.uuid
)
暂停或取消付款计划
await coherent.paymentPlans.setStatus(
plan.uuid,
PaymentPlanStatus.Void
// OR PaymentPlanStatus.Paused
// OR PaymentPlanStatus.Resumed
)
您可以将第二个参数设置为:
- Void: 取消付款计划和相关时间表
- 暂停: 暂停支付计划的时间表, 计划暂停时的意外事件会被永远跳过
- 复会:恢复支付计划时间表,恢复到停发或误发前的状态.
重新发送通知
await coherent.paymentPlans.notify(
plan.uuid
)
按需付费计划
await coherent.paymentPlans.charge(
plan.uuid,
{ amount: 2000, reference: "INVOICE-1234" } // optional
)
收回一项直接调试计划,该计划因资金不足而未能收款(PaymentPlanStatus.InsufficientFunds() 恢复计划,并立即向本批付款发出一次呼吁:
const { transactionUuid } = await coherent.paymentPlans.resumeAndCharge(
plan.uuid
)
将支付计划与交易链接
每一次重复一次付款计划,就会产生交易,然后会像往常一样触发交易网.
为了将产生的交易与付款计划联系起来,设置 transactionReference 支付计划的制定.
您在收到 Webhook 时不会有与给定的 UUID 交易的记录, 但当您收到交易时, 它应该包含较早时设定的引用 {}.paymentPlan.transactionReference。 。 。 .
合伙人商业管理
合作伙伴可以管理他们的商家,包括列出商家,管理API密钥,并配置webhook URL.
说明: API 键和webhook URL在入职时自动创建( )
startOnboarding) (中文(简体) ). 只有在登入后想要旋转密钥或更改webhook URL时,才需要下面的终点.
列出属于此伙伴的所有商家
const merchants = await coherent.merchants.listMerchants()
// Returns: [{ uuid, name, email, phone?, onboarded }]
API 密钥管理
为商家创建新的 API 密钥
const result = await coherent.merchants.createApiKey('merchant-uuid')
console.log(result.apiKey) // Store this securely — it cannot be retrieved again
列出商家现有的 API 密钥
const keys = await coherent.merchants.listApiKeys('merchant-uuid')
// Returns: [{ uuid, createdTime }]
Webhook URL 管理器
获取当前商家的 Webhook URL
const webhook = await coherent.merchants.getWebhookUrl('merchant-uuid')
console.log(webhook.webhookUrl) // null if not set
更新商家的网络浏览 URL
await coherent.merchants.updateWebhookUrl('merchant-uuid', 'https://example.com/webhook')